Skip to content

Commit

Permalink
feat: Add support for deleting TPW items (#355)
Browse files Browse the repository at this point in the history
* feat: Add support for deleting TPW items

* chore: Add functions to check if a db collection or a db items are TP collections or TP items

* chore: Renamed item exceptions to errors. Re-used the collection and item utils.

* fix: Code & tests
  • Loading branch information
LautaroPetaccio committed Nov 16, 2021
1 parent 6925c3f commit d6d69cc
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 99 deletions.
12 changes: 5 additions & 7 deletions src/Collection/Collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Item } from '../Item/Item.model'
import {
decodeTPCollectionURN,
getThirdPartyCollectionURN,
isTPCollection,
toDBCollection,
} from './utils'
import { CollectionAttributes, FullCollection } from './Collection.types'
Expand Down Expand Up @@ -134,10 +135,7 @@ export class CollectionService {
const collection = await Collection.findOne<CollectionAttributes>(id)

if (collection) {
if (
collection.third_party_id === null ||
collection.urn_suffix === null
) {
if (isTPCollection(collection)) {
throw new WrongCollectionException(
"The collection can't be converted into a third party collection.",
{ id }
Expand All @@ -155,8 +153,8 @@ export class CollectionService {
// We can't change the TPW collection's URN if there are already published items
await this.checkIfThirdPartyCollectionHasPublishedItems(
id,
collection.third_party_id,
collection.urn_suffix
collection.third_party_id!,
collection.urn_suffix!
)

// Check if the new URN for the collection already exists
Expand Down Expand Up @@ -295,7 +293,7 @@ export class CollectionService {
}))
}

private async getDBCollection(
public async getDBCollection(
collectionId: string
): Promise<CollectionAttributes> {
const collection = await Collection.findOne(collectionId)
Expand Down
4 changes: 4 additions & 0 deletions src/Collection/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export function getThirdPartyCollectionURN(
return `${third_party_id}:${urn_suffix}`
}

export function isTPCollection(collection: CollectionAttributes): boolean {
return collection.third_party_id === null || collection.urn_suffix === null
}

/**
* Converts a collection retrieved from the DB into a "FullCollection".
*
Expand Down
45 changes: 45 additions & 0 deletions src/Item/Item.errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export enum ItemAction {
DELETE = 'deleted',
}

export enum ItemType {
DCL,
THIRD_PARTY,
}

export class NonExistentItemError extends Error {
constructor(public id: string) {
super("The item doesn't exist.")
}
}

export class ThirdPartyItemAlreadyPublishedError extends Error {
constructor(public id: string, public urn: string, action: ItemAction) {
super(`The third party item is already published. It can't be ${action}.`)
}
}

export class DCLItemAlreadyPublishedError extends Error {
constructor(
public id: string,
public blockchainItemId: string,
public contractAddress: string,
action: ItemAction
) {
super(
`The collection that contains this item has been already published. The item can't be ${action}.`
)
}
}

export class CollectionForItemLockedError extends Error {
constructor(public id: string, action: ItemAction) {
super(`The collection for the item is locked. The item can't be ${action}.`)
}
}

export class InconsistentItemError extends Error {
constructor(public id: string, message: string) {
super(message)
}
}
Loading

0 comments on commit d6d69cc

Please sign in to comment.