Skip to content

Commit

Permalink
fix: main catalog query sort when filtering by text (#355)
Browse files Browse the repository at this point in the history
* fix: main catalog query sort when filtering by text

* fix: updates logic to remove default sorting from the route handler

* test: update test comments
  • Loading branch information
juanmahidalgo committed Oct 4, 2023
1 parent e075ae0 commit 2d90377
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
4 changes: 1 addition & 3 deletions src/adapters/handlers/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export function createCatalogHandler(
const params = new Params(context.url.searchParams)
const onlyListing = params.getBoolean('onlyListing')
const onlyMinting = params.getBoolean('onlyMinting')
const sortBy =
params.getValue<CatalogSortBy>('sortBy', CatalogSortBy) ||
CatalogSortBy.CHEAPEST
const sortBy = params.getValue<CatalogSortBy>('sortBy', CatalogSortBy)
const sortDirection =
params.getValue<CatalogSortDirection>(
'sortDirection',
Expand Down
7 changes: 4 additions & 3 deletions src/ports/catalog/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const WEARABLE_ITEM_TYPES = [
const MAX_ORDER_TIMESTAMP = 253378408747000 // some orders have a timestmap that can't be cast by Postgres, this is the max possible value

export function getOrderBy(filters: CatalogFilters) {
const { sortBy, sortDirection, isOnSale, search } = filters
const { sortBy, sortDirection, isOnSale, search, ids } = filters
const sortByParam = sortBy ?? CatalogSortBy.NEWEST
const sortDirectionParam = sortDirection ?? CatalogSortDirection.DESC

Expand All @@ -30,8 +30,9 @@ export function getOrderBy(filters: CatalogFilters) {
return ''
}

if (search) {
// If the filters have a search term, we need to order by the position of the item in the search results that is pre-computed and passed in the ids filter.
if (search && !sortBy && ids?.length) {
// If the filters have a search term, there's no other Sort applied and ids matching the search were returned, then
// we need to order by the position of the item in the search results that is pre-computed and passed in the ids filter.
return SQL`ORDER BY array_position(${filters.ids}::text[], id) `
}

Expand Down
8 changes: 4 additions & 4 deletions src/ports/catalog/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
CatalogQueryFilters,
CollectionsItemDBResult,
} from './types'
import { addQuerySort, getCollectionsItemsCatalogQuery } from './queries'
import { getCollectionsItemsCatalogQuery, getOrderBy } from './queries'

const getMultiNetworkQuery = (
schemas: Record<string, string>,
Expand All @@ -43,7 +43,7 @@ const getMultiNetworkQuery = (
}
})
unionQuery.append(SQL`\n) as temp \n`)
addQuerySort(unionQuery, filters)
unionQuery.append(getOrderBy(filters))
if (limit !== undefined && offset !== undefined) {
unionQuery.append(SQL`LIMIT ${limit} OFFSET ${offset}`)
}
Expand Down Expand Up @@ -101,7 +101,7 @@ export function fromCollectionsItemDbResultToCatalogItem(
loop,
category: emoteCategory,
has_sound,
has_geometry
has_geometry,
} = dbItem.metadata
;(name = emoteName), (category = NFTCategory.EMOTE)
data = {
Expand All @@ -112,7 +112,7 @@ export function fromCollectionsItemDbResultToCatalogItem(
rarity: rarity as Rarity,
loop: !!loop,
hasSound: !!has_sound,
hasGeometry: !!has_geometry
hasGeometry: !!has_geometry,
},
}
break
Expand Down
18 changes: 10 additions & 8 deletions src/tests/ports/catalog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ test('catalog component', function () {
filters = {
network,
search,
// ids: ['id1', 'id2'],
}

latestSubgraphSchemaResponse = {
Expand Down Expand Up @@ -325,12 +324,13 @@ test('catalog component', function () {
getSubgraphNameForNetwork(network, ChainId.ETHEREUM_SEPOLIA)
)
)
const itemIdsBySearchTextQuery = getItemIdsBySearchTextQuery(
latestSchema,
filters.search,
filters.category
)
expect(dbClientQueryMock.mock.calls[1][0]).toEqual(
getItemIdsBySearchTextQuery(
latestSchema,
filters.search,
filters.category
)
itemIdsBySearchTextQuery
)
// It's repeated 4 times due to this WHERE statement: `WHERE word_wearable % $1 OR word_emote % $2 ORDER BY GREATEST(similarity(word_wearable, $3), similarity(word_emote, $4)) DESC;`
expect(dbClientQueryMock.mock.calls[1][0].values).toEqual(
Expand Down Expand Up @@ -389,9 +389,11 @@ test('catalog component', function () {
expect(dbClientQueryMock.mock.calls[1][0].values).toEqual(
Array(4).fill(search)
)
expect(dbClientQueryMock.mock.calls[2][0]).toEqual(
getCatalogQuery({ [network]: latestSchema }, filters)
const mainCatalogQuery = getCatalogQuery(
{ [network]: latestSchema },
{ ...filters, ids: [mockedDBItemResponse.id] } // the main query should have the ids returned by the search query
)
expect(dbClientQueryMock.mock.calls[2][0]).toEqual(mainCatalogQuery)
})
})
})
Expand Down

0 comments on commit 2d90377

Please sign in to comment.