Skip to content

Commit

Permalink
feat: Allow filtering items by multiple ids (#260)
Browse files Browse the repository at this point in the history
* feat: Allow filtering items by multiple ids

* fix: Check if the length of what getList returns is gt 0

* chore: Upgrade schemas
  • Loading branch information
kevinszuchet authored Apr 3, 2023
1 parent ce97c12 commit 9ba15df
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type Item = {
- `emoteCategory`: Filter results by `EmoteCategory`. Possible values: `dance`, `stunt`, `greetings`, `fun`, `poses`, `reactions`, `horror`, `miscellaneous`.
- `emoteGender`: Filter results by `GenderFilterOption`. It supports multiple values by adding the query param multiple times. Possible values: `male`, `female`, `unisex`.
- `emotePlayMode`: Filter results by `EmotePlayMode`. It supports multiple values by adding the query param multiple times. Possible values: `simple`, `loop`
- `id`: Filter results by id. It supports multiple values by adding the query param multiple times. Type: `contractAddress-itemId`.
- `contractAddress`: Filter results by contract address. It supports multiple values by adding the query param multiple times. Type: `address`.
- `itemId`: Filter results by `itemId`. Type: `string`.
- `minPrice`: Return only sales with a price higher than this. Type `number`.
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"printWidth": 80
},
"dependencies": {
"@dcl/schemas": "^6.13.2",
"@dcl/schemas": "^6.14.0",
"@well-known-components/env-config-provider": "^1.2.0",
"@well-known-components/http-requests-logger-component": "^2.1.0",
"@well-known-components/http-server": "^1.1.6",
Expand Down
20 changes: 16 additions & 4 deletions src/adapters/handlers/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { IHttpServerComponent } from '@well-known-components/interfaces'
import { AppComponents, Context } from '../../types'
import { Params } from '../../logic/http/params'
import { asJSON } from '../../logic/http/response'
import { asJSON, HttpError } from '../../logic/http/response'

export function createItemsHandler(
components: Pick<AppComponents, 'items'>
Expand Down Expand Up @@ -53,15 +53,26 @@ export function createItemsHandler(
'emotePlayMode',
EmotePlayMode
)
const ids = params.getList('id')
const contractAddresses = params.getList('contractAddress')
const itemId = params.getString('itemId')
const network = params.getValue<Network>('network', Network)
const maxPrice = params.getString('maxPrice')
const minPrice = params.getString('minPrice')
const urns = params.getList('urn')

return asJSON(() =>
items.fetchAndCount({
return asJSON(() => {
if (
ids?.length > 0 &&
(contractAddresses?.length > 0 || itemId || urns?.length > 0)
) {
throw new HttpError(
'Ids cannot be set with contractAddress, itemId, or urn.',
400
)
}

return items.fetchAndCount({
first,
skip,
sortBy,
Expand All @@ -78,6 +89,7 @@ export function createItemsHandler(
emoteCategory,
emoteGenders,
emotePlayMode,
ids,
contractAddresses,
itemId,
isWearableSmart,
Expand All @@ -90,6 +102,6 @@ export function createItemsHandler(
: undefined,
urns,
})
)
})
}
}
17 changes: 17 additions & 0 deletions src/ports/items/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ describe("#getItemsQuery", () => {
})
})

describe('when ids is defined', () => {
it('should check ids in the received list by params', () => {
expect(
getItemsQuery({
ids: [
'0x00a4b2e743c609256ade1cf99b8e69ecdf27ab8c-0',
'0x00a4b2e743c609256ade1cf99b8e69ecdf27ab8c-1',
],
})
).toEqual(
expect.stringContaining(
'id_in: ["0x00a4b2e743c609256ade1cf99b8e69ecdf27ab8c-0","0x00a4b2e743c609256ade1cf99b8e69ecdf27ab8c-1"]'
)
)
})
})

describe('when urns is defined', () => {
it('should check urns in the list of urns received by params', () => {
expect(
Expand Down
9 changes: 9 additions & 0 deletions src/ports/items/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export function getItemsQuery(filters: ItemFilters, isCount = false) {
wearableGenders,
emoteCategory,
emoteGenders,
ids,
contractAddresses,
itemId,
minPrice,
Expand Down Expand Up @@ -214,6 +215,14 @@ export function getItemsQuery(filters: ItemFilters, isCount = false) {
where.push(`price_gte: "${minPrice}"`)
}

if (ids && ids.length > 0) {
where.push(
`id_in: [${ids
.map((id) => `"${id}"`)
.join(',')}]`
)
}

if (contractAddresses && contractAddresses.length > 0) {
where.push(
`collection_in: [${contractAddresses
Expand Down

0 comments on commit 9ba15df

Please sign in to comment.