Skip to content

Commit

Permalink
Merge 6235477 into 7e36c22
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmahidalgo committed Feb 21, 2022
2 parents 7e36c22 + 6235477 commit 0ed77f0
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/Collection/Collection.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ describe('Collection router', () => {
;(thirdPartyAPI.fetchThirdPartiesByManager as jest.Mock).mockReturnValueOnce(
[{ id: dbTPCollection.third_party_id }]
)
;(ItemCuration.findLastCreatedByCollectionIdAndStatus as jest.Mock).mockReturnValueOnce(
;(ItemCuration.findLastByCollectionId as jest.Mock).mockReturnValueOnce(
itemCurationMock
)
mockThirdPartyCollectionIsPublished(dbTPCollection.id, false)
Expand Down
6 changes: 2 additions & 4 deletions src/Collection/Collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { FactoryCollection } from '../ethereum/FactoryCollection'
import { Bridge } from '../ethereum/api/Bridge'
import { isPublished } from '../utils/eth'
import { ItemCuration } from '../Curation/ItemCuration'
import { CurationStatus } from '../Curation'
import { Ownable } from '../Ownable'
import { Item } from '../Item'
import { decodeTPCollectionURN, isTPCollection, toDBCollection } from './utils'
Expand Down Expand Up @@ -260,9 +259,8 @@ export class CollectionService {
private async getTPCollection(
dbCollection: ThirdPartyCollectionAttributes
): Promise<CollectionAttributes> {
const lastItemCuration = await ItemCuration.findLastCreatedByCollectionIdAndStatus(
dbCollection.id,
CurationStatus.APPROVED
const lastItemCuration = await ItemCuration.findLastByCollectionId(
dbCollection.id
)
return lastItemCuration
? Bridge.mergeTPCollection(dbCollection, lastItemCuration)
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('getMergedCollection', () => {
jest.spyOn(Collection, 'findOne').mockResolvedValueOnce(collection)

jest
.spyOn(ItemCuration, 'findLastCreatedByCollectionIdAndStatus')
.spyOn(ItemCuration, 'findLastByCollectionId')
.mockResolvedValueOnce(undefined)
})

Expand All @@ -133,7 +133,7 @@ describe('getMergedCollection', () => {
jest.spyOn(Collection, 'findOne').mockResolvedValueOnce(collection)

jest
.spyOn(ItemCuration, 'findLastCreatedByCollectionIdAndStatus')
.spyOn(ItemCuration, 'findLastByCollectionId')
.mockResolvedValueOnce(itemCurationMock)
})

Expand Down
6 changes: 2 additions & 4 deletions src/Collection/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Bridge } from '../ethereum/api/Bridge'
import { collectionAPI } from '../ethereum/api/collection'
import { matchers } from '../common/matchers'
import { ItemCuration } from '../Curation/ItemCuration'
import { CurationStatus } from '../Curation'
import { Collection } from './Collection.model'
import {
CollectionAttributes,
Expand Down Expand Up @@ -136,9 +135,8 @@ export async function getMergedCollection(
let mergedCollection: CollectionAttributes

if (isTPCollection(dbCollection)) {
const lastItemCuration = await ItemCuration.findLastCreatedByCollectionIdAndStatus(
dbCollection.id,
CurationStatus.APPROVED
const lastItemCuration = await ItemCuration.findLastByCollectionId(
dbCollection.id
)

if (!lastItemCuration) {
Expand Down
25 changes: 8 additions & 17 deletions src/Curation/ItemCuration/ItemCuration.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Model, raw, SQL } from 'decentraland-server'
import { Collection } from '../../Collection'
import { Item } from '../../Item'
import { CurationStatus, CurationType } from '../Curation.types'
import { CurationType } from '../Curation.types'
import { ItemCurationAttributes } from './ItemCuration.types'

export class ItemCuration extends Model<ItemCurationAttributes> {
Expand All @@ -27,22 +27,6 @@ export class ItemCuration extends Model<ItemCurationAttributes> {
return counts[0].count > 0
}

static async findLastCreatedByCollectionIdAndStatus(
collectionId: string,
curationStatus: CurationStatus
): Promise<ItemCurationAttributes | undefined> {
const itemCurations = await this.query<ItemCurationAttributes>(SQL`
SELECT *
FROM ${raw(this.tableName)} item_curations
JOIN ${raw(Item.tableName)} items ON items.id = item_curations.item_id
WHERE items.collection_id = ${collectionId}
AND item_curations.status = ${curationStatus}
ORDER BY item_curations.created_at DESC
LIMIT 1`)

return itemCurations[0]
}

static async findByCollectionId(collectionId: string) {
return this.query<ItemCurationAttributes>(SQL`
SELECT DISTINCT on (item.id) item_curation.*
Expand All @@ -53,6 +37,13 @@ export class ItemCuration extends Model<ItemCurationAttributes> {
ORDER BY item.id, item_curation.created_at DESC`)
}

static async findLastByCollectionId(
collectionId: string
): Promise<ItemCurationAttributes | undefined> {
const itemCurations = await this.findByCollectionId(collectionId)
return itemCurations[0]
}

static async getItemCurationCountByThirdPartyId(thirdPartyId: string) {
const counts = await this.query<{ count: number }>(
SQL`SELECT COUNT(DISTINCT item_curations.id) AS Count
Expand Down
34 changes: 28 additions & 6 deletions src/Item/Item.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { tpWearableMock, wearableMock } from '../../spec/mocks/peer'
import { wallet } from '../../spec/mocks/wallet'
import { isCommitteeMember } from '../Committee'
import { app } from '../server'
import { ItemCuration } from '../Curation/ItemCuration'
import { ItemCuration, ItemCurationAttributes } from '../Curation/ItemCuration'
import { Collection } from '../Collection/Collection.model'
import { collectionAPI } from '../ethereum/api/collection'
import { peerAPI, Wearable } from '../ethereum/api/peer'
Expand Down Expand Up @@ -83,13 +83,16 @@ const server = supertest(app.getApp())

describe('Item router', () => {
let dbItem: ItemAttributes
let dbItemCuration: ItemCurationAttributes
let dbTPItem: ThirdPartyItemAttributes
let dbItemNotPublished: ItemAttributes
let dbTPItemNotPublished: ThirdPartyItemAttributes
let dbTPItemPublished: ThirdPartyItemAttributes
let resultingItem: ResultItem
let resultingTPItem: ResultItem
let resultItemNotPublished: ResultItem
let resultTPItemNotPublished: ResultItem
let resultTPItemPublished: ResultItem
let wearable: Wearable
let tpWearable: Wearable
let itemFragment: ItemFragment
Expand Down Expand Up @@ -118,10 +121,20 @@ describe('Item router', () => {
beneficiary: '',
urn_suffix: '',
}
dbTPItemPublished = {
...dbTPItem,
id: uuidv4(),
urn_suffix: '3',
}
dbItemCuration = { ...itemCurationMock, item_id: dbTPItemPublished.id }
resultingItem = toResultItem(dbItem, itemFragment, wearable)
resultingTPItem = toResultTPItem(dbTPItem, dbTPCollectionMock)
resultItemNotPublished = asResultItem(dbItemNotPublished)
resultTPItemNotPublished = asResultItem(dbTPItemNotPublished)
resultTPItemNotPublished = asResultItem(dbTPItemNotPublished) // no itemCuration & no catalyst, should be regular Item
resultTPItemPublished = {
...toResultTPItem(dbTPItemPublished, dbTPCollectionMock),
is_approved: false,
}
})

afterEach(() => {
Expand Down Expand Up @@ -233,7 +246,9 @@ describe('Item router', () => {
dbTPItemMock,
dbTPItemNotPublishedMock,
dbItemNotPublished,
dbTPItemPublished,
])
;(ItemCuration.find as jest.Mock).mockResolvedValueOnce([dbItemCuration])
;(collectionAPI.fetchItems as jest.Mock).mockResolvedValueOnce([
itemFragment,
])
Expand Down Expand Up @@ -271,6 +286,7 @@ describe('Item router', () => {
dbTPItemNotPublishedMock.urn_suffix!
),
},
resultTPItemPublished,
],
ok: true,
})
Expand All @@ -297,6 +313,7 @@ describe('Item router', () => {
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollectionMock,
]) // for third parties
;(ItemCuration.find as jest.Mock).mockResolvedValueOnce([dbItemCuration])
mockItemConsolidation([dbItem], [wearable])
;(peerAPI.fetchWearables as jest.Mock).mockResolvedValueOnce([tpWearable])
url = `/${wallet.address}/items`
Expand Down Expand Up @@ -373,6 +390,7 @@ describe('Item router', () => {
beforeEach(() => {
;(Item.find as jest.Mock).mockResolvedValueOnce([
dbTPItem,
dbTPItemPublished,
dbTPItemNotPublished,
])
;(Collection.findOne as jest.Mock).mockResolvedValueOnce(
Expand All @@ -381,9 +399,9 @@ describe('Item router', () => {
;(Collection.findByIds as jest.Mock).mockResolvedValueOnce([
dbTPCollectionMock,
])
;(ItemCuration.findLastCreatedByCollectionIdAndStatus as jest.Mock).mockResolvedValueOnce(
itemCurationMock
)
;(ItemCuration.findByCollectionId as jest.Mock).mockResolvedValueOnce([
dbItemCuration,
])
mockItemConsolidation([dbItemMock], [tpWearable])
url = `/collections/${dbCollectionMock.id}/items`
})
Expand All @@ -395,7 +413,11 @@ describe('Item router', () => {
.expect(200)
.then((response: any) => {
expect(response.body).toEqual({
data: [resultingTPItem, resultTPItemNotPublished],
data: [
resultingTPItem,
resultTPItemPublished,
resultTPItemNotPublished,
],
ok: true,
})
})
Expand Down
13 changes: 7 additions & 6 deletions src/Item/Item.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { server } from 'decentraland-server'
import { Router } from '../common/Router'
import { HTTPError, STATUS_CODES } from '../common/HTTPError'
import { collectionAPI } from '../ethereum/api/collection'
import { thirdPartyAPI } from '../ethereum/api/thirdParty'
import { Bridge } from '../ethereum/api/Bridge'
import {
withModelAuthorization,
Expand All @@ -20,6 +19,7 @@ import { Collection, CollectionService } from '../Collection'
import { hasPublicAccess as hasCollectionAccess } from '../Collection/access'
import { NonExistentCollectionError } from '../Collection/Collection.errors'
import { isCommitteeMember } from '../Committee'
import { ItemCuration, ItemCurationAttributes } from '../Curation/ItemCuration'
import { Item } from './Item.model'
import { ItemAttributes } from './Item.types'
import { areItemRepresentationsValid, upsertItemSchema } from './Item.schema'
Expand Down Expand Up @@ -153,17 +153,17 @@ export class ItemRouter extends Router {
}

// TODO: We need to paginate this. To do it, we'll have to fetch remote items via the paginated dbItemIds
const [allItems, remoteItems] = await Promise.all([
const [allItems, remoteItems, itemCurations] = await Promise.all([
Item.find<ItemAttributes>(),
collectionAPI.fetchItems(),
thirdPartyAPI.fetchItems(),
ItemCuration.find<ItemCurationAttributes>(),
])

const { items, tpItems } = this.itemService.splitItems(allItems)

const [fullItems, fullTPItems] = await Promise.all([
Bridge.consolidateItems(items, remoteItems),
Bridge.consolidateTPItems(tpItems),
Bridge.consolidateTPItems(tpItems, itemCurations),
])

// TODO: sorting (we're not breaking pagination)
Expand All @@ -182,15 +182,16 @@ export class ItemRouter extends Router {
)
}

const [dbItems, remoteItems, dbTPItems] = await Promise.all([
const [dbItems, remoteItems, dbTPItems, itemCurations] = await Promise.all([
Item.find<ItemAttributes>({ eth_address }),
collectionAPI.fetchItemsByAuthorizedUser(eth_address),
this.itemService.getTPItemsByManager(eth_address),
ItemCuration.find<ItemCurationAttributes>(),
])

const [items, tpItems] = await Promise.all([
Bridge.consolidateItems(dbItems, remoteItems),
Bridge.consolidateTPItems(dbTPItems),
Bridge.consolidateTPItems(dbTPItems, itemCurations),
])

// TODO: list.concat(list2) will not break pagination (when we add it), but it will break any order we have beforehand.
Expand Down
27 changes: 14 additions & 13 deletions src/Item/Item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
} from '../Collection'
import { CollectionService } from '../Collection/Collection.service'
import { isTPCollection } from '../Collection/utils'
import { CurationStatus } from '../Curation'
import { ItemCuration } from '../Curation/ItemCuration'
import { Bridge } from '../ethereum/api/Bridge'
import { collectionAPI } from '../ethereum/api/collection'
Expand Down Expand Up @@ -169,15 +168,18 @@ export class ItemService {
dbCollection: ThirdPartyCollectionAttributes,
dbItems: ItemAttributes[]
): Promise<{ collection: CollectionAttributes; items: FullItem[] }> {
const lastItemCuration = await ItemCuration.findLastCreatedByCollectionIdAndStatus(
dbCollection.id,
CurationStatus.APPROVED
const collectionItemCurations = await ItemCuration.findByCollectionId(
dbCollection.id
)
const collection =
collectionItemCurations.length > 0
? Bridge.mergeTPCollection(dbCollection, collectionItemCurations[0])
: dbCollection

const items = await Bridge.consolidateTPItems(
dbItems,
collectionItemCurations
)
const collection = lastItemCuration
? Bridge.mergeTPCollection(dbCollection, lastItemCuration)
: dbCollection

const items = await Bridge.consolidateTPItems(dbItems)
return { collection, items }
}

Expand Down Expand Up @@ -213,17 +215,16 @@ export class ItemService {
dbItem.urn_suffix!
)

const lastItemCuration = await ItemCuration.findLastCreatedByCollectionIdAndStatus(
collection.id,
CurationStatus.APPROVED
const lastItemCuration = await ItemCuration.findLastByCollectionId(
collection.id
)
collection = lastItemCuration
? Bridge.mergeTPCollection(collection, lastItemCuration)
: collection

const catalystItems = await peerAPI.fetchWearables([urn])
if (catalystItems.length > 0) {
item = Bridge.mergeTPItem(dbItem, catalystItems[0])
item = Bridge.mergeTPItem(dbItem, collection, catalystItems[0])
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Item/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export async function getMergedItem(id: string): Promise<FullItem> {
throw new UnpublishedItemError(id)
}

fullItem = Bridge.mergeTPItem(dbItem, wearable)
fullItem = Bridge.mergeTPItem(dbItem, dbCollection, wearable)
} else {
const {
collection: remoteCollection,
Expand Down
Loading

0 comments on commit 0ed77f0

Please sign in to comment.