Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feat/add-sound-geom…
Browse files Browse the repository at this point in the history
…etry-values
  • Loading branch information
meelrossi committed Sep 5, 2023
2 parents e94c6b4 + 94d2214 commit 8665b3a
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/logic/nfts/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export function fromCollectionsFragment(
updatedAt: +fragment.updatedAt * 1000,
//@ts-ignore
soldAt: +fragment.soldAt * 1000,
urn: fragment.urn,
},
order:
fragment.activeOrder &&
Expand Down
36 changes: 10 additions & 26 deletions src/ports/catalog/component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { IPgComponent } from '@well-known-components/pg-component'
import { Item, Network } from '@dcl/schemas'
import {
getCollectionsChainId,
getMarketplaceChainId,
} from '../../logic/chainIds'
import { Item } from '@dcl/schemas'
import { HttpError } from '../../logic/http/response'
import { getLatestSubgraphSchema } from '../../subgraphUtils'
import { enhanceItemsWithPicksStats } from '../../logic/favorites/utils'
import { IFavoritesComponent } from '../favorites/types'
import {
Expand All @@ -15,9 +12,9 @@ import {
import {
getCatalogQuery,
fromCollectionsItemDbResultToCatalogItem,
getQuerySources,
} from './utils'
import { getItemIdsBySearchTextQuery } from './queries'
import { getLatestSubgraphSchema, getSubgraphNameForNetwork } from '../../subgraphUtils'

export function createCatalogComponent(options: {
database: IPgComponent
Expand All @@ -26,26 +23,12 @@ export function createCatalogComponent(options: {
const { database, favoritesComponent } = options

async function fetch(filters: CatalogOptions) {
const { network, creator } = filters
const marketplaceChainId = getMarketplaceChainId()
const collectionsChainId = getCollectionsChainId()
const { network } = filters
let catalogItems: Item[] = []
let total = 0
const client = await database.getPool().connect()
try {
const sources = (
network
? [network]
: creator
? [Network.MATIC]
: [Network.ETHEREUM, Network.MATIC]
).reduce((acc, curr) => {
acc[curr] = getSubgraphNameForNetwork(
curr,
curr === Network.ETHEREUM ? marketplaceChainId : collectionsChainId
)
return acc
}, {} as Record<string, string>)
const sources = getQuerySources(filters)

const latestSchemasPromises: Promise<Record<string, string>>[] =
Object.entries(sources).map(async ([network, subgraphName]) => {
Expand All @@ -67,15 +50,16 @@ export function createCatalogComponent(options: {
const filteredItemsById = await client.query<CollectionsItemDBResult>(
getItemIdsBySearchTextQuery(schema, filters.search)
)
if (filteredItemsById.rowCount === 0) {
// if no items matched the search text, return empty result
return { data: [], total: 0 }
}
filters.ids = [
...(filters.ids ?? []),
...filteredItemsById.rows.map(({ id }) => id),
]
}

if (filters.ids?.length === 0) {
// if no items matched the search text, return empty result
return { data: [], total: 0 }
}
}
const results = await client.query<CollectionsItemDBResult>(
getCatalogQuery(reducedSchemas, filters)
Expand Down
5 changes: 5 additions & 0 deletions src/ports/catalog/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ export const getHasGeometryWhere = () => {
return SQL`items.search_emote_has_geometry = true`
}

export const getUrnsWhere = (filters: CatalogFilters) => {
return SQL`items.urn = ANY(${filters.urns})`
}

export const getCollectionsQueryWhere = (filters: CatalogFilters) => {
const conditions = [
filters.category ? getCategoryWhere(filters) : undefined,
Expand All @@ -234,6 +238,7 @@ export const getCollectionsQueryWhere = (filters: CatalogFilters) => {
filters.ids?.length ? getIdsWhere(filters) : undefined,
filters.emoteHasSound ? getHasSoundWhere() : undefined,
filters.emoteHasGeometry ? getHasGeometryWhere() : undefined,
filters.urns?.length ? getUrnsWhere(filters) : undefined,
].filter(Boolean)

const result = SQL`WHERE items.search_is_collection_approved = true`
Expand Down
28 changes: 27 additions & 1 deletion src/ports/catalog/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ import {
getCollectionsChainId,
getMarketplaceChainId,
} from '../../logic/chainIds'
import { getSubgraphNameForNetwork } from '../../subgraphUtils'
import { FragmentItemType } from '../items/types'
import { CatalogQueryFilters, CollectionsItemDBResult } from './types'
import {
CatalogOptions,
CatalogQueryFilters,
CollectionsItemDBResult,
} from './types'
import { addQuerySort, getCollectionsItemsCatalogQuery } from './queries'

const getMultiNetworkQuery = (
Expand Down Expand Up @@ -152,3 +157,24 @@ export function fromCollectionsItemDbResultToCatalogItem(
urn: dbItem.urn,
}
}

export const getQuerySources = (filters: CatalogOptions) => {
const { network, creator } = filters
const sources = (
network
? [network]
: creator?.length
? [Network.MATIC]
: [Network.ETHEREUM, Network.MATIC]
).reduce((acc, curr) => {
acc[curr] = getSubgraphNameForNetwork(
curr,
curr === Network.ETHEREUM
? getMarketplaceChainId()
: getCollectionsChainId()
)
return acc
}, {} as Record<string, string>)

return sources
}
16 changes: 16 additions & 0 deletions src/tests/ports/catalog-queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@ test('catalog utils', () => {
})
})

describe('and passing the "urns" filter', () => {
beforeEach(() => {
filters = {
urns: ['anUrn'],
}
})
it('should add the urns definition to the WHERE', () => {
expect(getCollectionsQueryWhere(filters).text).toContain(
`items.urn = ANY($1)`
)
expect(getCollectionsQueryWhere(filters).values).toStrictEqual([
filters.urns,
])
})
})

describe('and passing the wearable related filters', () => {
let isWearableHead: boolean, isWearableAccessory: boolean
beforeEach(() => {
Expand Down
64 changes: 64 additions & 0 deletions src/tests/ports/catalog-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { CatalogFilters, ChainId, Network } from '@dcl/schemas'
import { getQuerySources } from '../../ports/catalog/utils'
import { getSubgraphNameForNetwork } from '../../subgraphUtils'

const marketplaceChainId = 1
const collectionsChainId = 137

jest.mock('../../logic/chainIds', () => ({
getCollectionsChainId: () => collectionsChainId,
getMarketplaceChainId: () => marketplaceChainId,
}))

describe('getQuerySources', () => {
let filters: CatalogFilters
let sources: Record<string, string>
describe('when filtering by network', () => {
beforeEach(() => {
filters = { network: Network.ETHEREUM }
sources = getQuerySources(filters)
})
it('should return sources for the provided network', () => {
expect(sources).toEqual({
[Network.ETHEREUM]: getSubgraphNameForNetwork(
Network.ETHEREUM,
marketplaceChainId as ChainId
),
})
})
})

describe('when filtering by creator', () => {
beforeEach(() => {
filters = { creator: ['someCreator'] }
sources = getQuerySources(filters)
})
it('should return sources for the creator provided with default networks', () => {
expect(sources).toEqual({
[Network.MATIC]: getSubgraphNameForNetwork(
Network.MATIC,
collectionsChainId as ChainId
),
})
})
})

describe('when no filter is provided', () => {
beforeEach(() => {
filters = {}
sources = getQuerySources(filters)
})
it('should return sources for default networks', () => {
expect(sources).toEqual({
[Network.ETHEREUM]: getSubgraphNameForNetwork(
Network.ETHEREUM,
marketplaceChainId as ChainId
),
[Network.MATIC]: getSubgraphNameForNetwork(
Network.MATIC,
collectionsChainId as ChainId
),
})
})
})
})

0 comments on commit 8665b3a

Please sign in to comment.