Skip to content

Commit

Permalink
feat: Add collectionId query param to the items per address endpoint (#…
Browse files Browse the repository at this point in the history
…491)

* feat: Add collectionId query param to the items per address endpoint

* chore: rename method and update test

* fix: filter by blockchain_item_id instead of collectionId

* chore: add JSDoc comment to collection fragment id
  • Loading branch information
juanmahidalgo committed Apr 13, 2022
1 parent bb2edda commit 9107124
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 31 deletions.
33 changes: 23 additions & 10 deletions src/Item/Item.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,38 @@ export class Item extends Model<ItemAttributes> {

// PAGINATED QUERIES

static findAllItemsByAddress(
thirdPartyIds: string[],
static findItemsByAddress(
address: string,
limit: number = DEFAULT_LIMIT,
offset: number = 0
thirdPartyIds: string[],
parmas: {
collectionId?: string
limit?: number
offset?: number
}
) {
const { collectionId, limit, offset } = parmas
return this.query<ItemWithTotalCount>(SQL`
SELECT items.*, count(*) OVER() AS total_count
FROM ${raw(this.tableName)} items
JOIN ${raw(
LEFT JOIN ${raw(
Collection.tableName
)} collections ON collections.id = items.collection_id
WHERE
collections.third_party_id = ANY(${thirdPartyIds})
OR
(items.eth_address = ${address} AND items.urn_suffix IS NULL)
WHERE
(
collections.third_party_id = ANY(${thirdPartyIds})
OR
(items.eth_address = ${address} AND items.urn_suffix IS NULL)
)
${
collectionId
? SQL`AND items.collection_id ${
collectionId === 'null' ? SQL`is NULL` : SQL`= ${collectionId}`
}`
: SQL``
}
LIMIT ${limit}
OFFSET ${offset}
`)
`)
}

static async findByCollectionIds(
Expand Down
31 changes: 19 additions & 12 deletions src/Item/Item.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ describe('Item router', () => {
let allAddressItems: ItemAttributes[]
beforeEach(() => {
allAddressItems = [dbItem, dbItemNotPublished, dbTPItem]
;(Item.findAllItemsByAddress as jest.Mock).mockResolvedValueOnce(
;(Item.findItemsByAddress as jest.Mock).mockResolvedValueOnce(
allAddressItems.map((item) => ({
...item,
total_count: allAddressItems.length,
Expand Down Expand Up @@ -345,24 +345,28 @@ describe('Item router', () => {
],
ok: true,
})
expect(Item.findAllItemsByAddress).toHaveBeenCalledWith(
[thirdPartyFragmentMock.id],
expect(Item.findItemsByAddress).toHaveBeenCalledWith(
wallet.address,
undefined,
undefined
[thirdPartyFragmentMock.id],
{
page: undefined,
limit: undefined,
collecitonId: undefined,
}
)
})
})
})

describe('and pagination params are passed', () => {
describe('and pagination & collectionId params are passed', () => {
let baseUrl: string
let page: number
let limit: number
let collectionId: string
beforeEach(() => {
;(page = 1), (limit = 1)
;(page = 1), (limit = 1), (collectionId = 'null')
baseUrl = `/${wallet.address}/items`
url = `${baseUrl}?limit=${limit}&page=${page}`
url = `${baseUrl}?limit=${limit}&page=${page}&collectionId=${collectionId}`
})
it('should call the find method with the pagination params', () => {
return server
Expand Down Expand Up @@ -390,11 +394,14 @@ describe('Item router', () => {
},
ok: true,
})
expect(Item.findAllItemsByAddress).toHaveBeenCalledWith(
[thirdPartyFragmentMock.id],
expect(Item.findItemsByAddress).toHaveBeenCalledWith(
wallet.address,
limit,
page - 1 // it's the offset
[thirdPartyFragmentMock.id],
{
limit,
offset: page - 1, // it's the offset
collectionId,
}
)
})
})
Expand Down
20 changes: 15 additions & 5 deletions src/Item/Item.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ export class ItemRouter extends Router {
): Promise<PaginatedResponse<FullItem> | FullItem[]> => {
const { page, limit } = getPaginationParams(req)
const eth_address = server.extractFromReq(req, 'address')
let collectionId: string | undefined
try {
collectionId = server.extractFromReq(req, 'collectionId')
} catch (error) {}
const auth_address = req.auth.ethAddress

if (eth_address !== auth_address) {
Expand All @@ -194,11 +198,11 @@ export class ItemRouter extends Router {
}

const [allItemsWithCount, remoteItems, itemCurations] = await Promise.all([
this.itemService.findAllItemsForAddress(
eth_address,
this.itemService.findItemsForAddress(eth_address, {
collectionId,
limit,
page && limit ? getOffset(page, limit) : undefined
),
offset: page && limit ? getOffset(page, limit) : undefined,
}),
collectionAPI.fetchItemsByAuthorizedUser(eth_address),
ItemCuration.find<ItemCurationAttributes>(),
])
Expand All @@ -207,12 +211,18 @@ export class ItemRouter extends Router {
const allItems = allItemsWithCount.map((itemWithCount) =>
omit<ItemAttributes>(itemWithCount, ['total_count'])
)
const dbBlockchainItemIds = allItems.map((item) => item.blockchain_item_id)
const { items: dbItems, tpItems: dbTPItems } = this.itemService.splitItems(
allItems
)

const [items, tpItems] = await Promise.all([
Bridge.consolidateItems(dbItems, remoteItems),
Bridge.consolidateItems(
dbItems,
remoteItems.filter((remoteItem) =>
dbBlockchainItemIds.includes(remoteItem.blockchainId)
)
),
Bridge.consolidateTPItems(dbTPItems, itemCurations),
])

Expand Down
11 changes: 7 additions & 4 deletions src/Item/Item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,13 @@ export class ItemService {
}
}

public async findAllItemsForAddress(
public async findItemsForAddress(
address: string,
limit?: number,
offset?: number
params: {
collectionId?: string
limit?: number
offset?: number
}
): Promise<(ItemAttributes & { total_count: number })[]> {
const thirdParties = await thirdPartyAPI.fetchThirdPartiesByManager(address)
if (thirdParties.length <= 0) {
Expand All @@ -175,7 +178,7 @@ export class ItemService {

const thirdPartyIds = thirdParties.map((thirdParty) => thirdParty.id)

return Item.findAllItemsByAddress(thirdPartyIds, address, limit, offset)
return Item.findItemsByAddress(address, thirdPartyIds, params)
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/ethereum/api/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export type ItemFragment = {
}

export type CollectionFragment = {
/** Collection address */
id: string
creator: string
owner: string
Expand Down

0 comments on commit 9107124

Please sign in to comment.