Skip to content

Commit

Permalink
Merge f32e7a8 into 1247c29
Browse files Browse the repository at this point in the history
  • Loading branch information
LautaroPetaccio committed Mar 21, 2022
2 parents 1247c29 + f32e7a8 commit f9aff92
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/Item/Item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Bridge } from '../ethereum/api/Bridge'
import { collectionAPI } from '../ethereum/api/collection'
import { peerAPI } from '../ethereum/api/peer'
import { thirdPartyAPI } from '../ethereum/api/thirdParty'
import { isStandardItemPublished } from '../ItemAndCollection/utils'
import { Ownable } from '../Ownable'
import { buildModelDates } from '../utils/dates'
import {
Expand Down Expand Up @@ -360,7 +361,6 @@ export class ItemService {
dbCollection: CollectionAttributes | undefined,
eth_address: string
): Promise<FullItem> {
let contentHash = null
const canUpsert = await new Ownable(Item).canUpsert(item.id, eth_address)
if (!canUpsert) {
throw new UnauthorizedToUpsertError(item.id, eth_address)
Expand Down Expand Up @@ -420,9 +420,6 @@ export class ItemService {
ItemAction.RARITY_UPDATE
)
}

// Compute the content hash of the item to later store it in the DB
contentHash = await calculateItemContentHash(dbItem, dbCollection!)
} else if (this.collectionService.isLockActive(dbCollection.lock)) {
throw new CollectionForItemLockedError(item.id, ItemAction.UPSERT)
}
Expand All @@ -433,7 +430,13 @@ export class ItemService {
eth_address,
})

attributes.local_content_hash = contentHash
attributes.blockchain_item_id = dbItem ? dbItem.blockchain_item_id : null

// Compute the content hash of the item to later store it in the DB
attributes.local_content_hash =
dbCollection && isStandardItemPublished(attributes, dbCollection)
? await calculateItemContentHash(attributes, dbCollection)
: null

const upsertedItem: ItemAttributes = await Item.upsert(attributes)
return Bridge.toFullItem(upsertedItem, dbCollection)
Expand Down
3 changes: 2 additions & 1 deletion src/Item/hashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
keccak256Hash,
} from '@dcl/hashing'
import { CollectionAttributes } from '../Collection'
import { isStandardItemPublished } from '../ItemAndCollection/utils'
import { getDecentralandItemURN, isTPCollection } from '../utils/urn'
import {
StandardWearableEntityMetadata,
Expand All @@ -18,7 +19,7 @@ export function buildStandardWearableEntityMetadata(
item: ItemAttributes,
collection: CollectionAttributes
): StandardWearableEntityMetadata {
if (!collection.contract_address || !item.blockchain_item_id!) {
if (!isStandardItemPublished(item, collection)) {
throw new Error(
"The item's collection must be published to build its metadata"
)
Expand Down
46 changes: 46 additions & 0 deletions src/ItemAndCollection/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { dbCollectionMock } from '../../spec/mocks/collections'
import { dbItemMock } from '../../spec/mocks/items'
import { CollectionAttributes } from '../Collection/Collection.types'
import { ItemAttributes } from '../Item/Item.types'
import { isStandardItemPublished } from './utils'

describe('when checking if an item is published', () => {
let item: ItemAttributes
let collection: CollectionAttributes

beforeEach(() => {
collection = { ...dbCollectionMock }
item = { ...dbItemMock, collection_id: dbCollectionMock.id }
})

describe("and the item doesn't have a blockchain item id", () => {
beforeEach(() => {
item.blockchain_item_id = null
})

it('should return false', () => {
expect(isStandardItemPublished(item, collection)).toBe(false)
})
})

describe("and the item's collection doesn't have a contract address", () => {
beforeEach(() => {
collection.contract_address = null
})

it('should return false', () => {
expect(isStandardItemPublished(item, collection)).toBe(false)
})
})

describe('and the item has a blockchain item id and the collection has a contract address', () => {
beforeEach(() => {
item.blockchain_item_id = '20'
collection.contract_address = '0x123'
})

it('should return true', () => {
expect(isStandardItemPublished(item, collection)).toBe(true)
})
})
})
9 changes: 9 additions & 0 deletions src/ItemAndCollection/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CollectionAttributes } from '../Collection/Collection.types'
import { ItemAttributes } from '../Item/Item.types'

export function isStandardItemPublished(
item: ItemAttributes,
collection: CollectionAttributes
): boolean {
return !!item.blockchain_item_id && !!collection.contract_address
}

0 comments on commit f9aff92

Please sign in to comment.