2020-06-30 15:24:30 +02:00
|
|
|
import {
|
2020-07-01 18:13:32 +02:00
|
|
|
SearchQuery,
|
|
|
|
QueryResult
|
2020-07-14 13:01:24 +02:00
|
|
|
} from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore'
|
2020-07-01 18:13:32 +02:00
|
|
|
import { priceQueryParamToWei } from '../../../utils'
|
2020-07-14 13:01:24 +02:00
|
|
|
import { MetadataStore, Logger } from '@oceanprotocol/lib'
|
2020-07-08 15:15:02 +02:00
|
|
|
import { oceanConfig } from '../../../../app.config'
|
2020-05-07 08:03:30 +02:00
|
|
|
|
2020-06-30 15:24:30 +02:00
|
|
|
export function getSearchQuery(
|
|
|
|
page?: string | string[],
|
|
|
|
offset?: string | string[],
|
|
|
|
text?: string | string[],
|
|
|
|
tag?: string | string[],
|
|
|
|
priceQuery?: [string | undefined, string | undefined]
|
|
|
|
): SearchQuery {
|
|
|
|
return {
|
|
|
|
page: Number(page) || 1,
|
|
|
|
offset: Number(offset) || 20,
|
|
|
|
query: {
|
|
|
|
text,
|
|
|
|
tags: tag ? [tag] : undefined,
|
|
|
|
price: priceQuery
|
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
created: -1
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:00:16 +02:00
|
|
|
// Something in squid-js is weird when using 'tags: [tag]'
|
2020-06-30 15:24:30 +02:00
|
|
|
// which is the only way the query actually returns desired results.
|
|
|
|
// But it doesn't follow 'SearchQuery' interface so we have to assign
|
|
|
|
// it here.
|
|
|
|
} as SearchQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getResults(params: any): Promise<QueryResult> {
|
|
|
|
const { text, tag, page, offset, minPrice, maxPrice } = params
|
|
|
|
|
|
|
|
const minPriceParsed = priceQueryParamToWei(
|
|
|
|
minPrice as string,
|
|
|
|
'Error parsing context.query.minPrice'
|
|
|
|
)
|
|
|
|
const maxPriceParsed = priceQueryParamToWei(
|
|
|
|
maxPrice as string,
|
|
|
|
'Error parsing context.query.maxPrice'
|
|
|
|
)
|
|
|
|
const priceQuery =
|
|
|
|
minPriceParsed || maxPriceParsed
|
|
|
|
? // sometimes TS gets a bit silly
|
|
|
|
([minPriceParsed, maxPriceParsed] as [
|
|
|
|
string | undefined,
|
|
|
|
string | undefined
|
|
|
|
])
|
|
|
|
: undefined
|
|
|
|
|
2020-07-14 13:01:24 +02:00
|
|
|
const metadataStore = new MetadataStore(oceanConfig.metadataStoreUri, Logger)
|
|
|
|
const queryResult = await metadataStore.queryMetadata(
|
2020-06-30 15:24:30 +02:00
|
|
|
getSearchQuery(page, offset, text, tag, priceQuery)
|
2020-05-07 08:03:30 +02:00
|
|
|
)
|
2020-06-30 15:24:30 +02:00
|
|
|
|
|
|
|
return queryResult
|
2020-05-07 08:03:30 +02:00
|
|
|
}
|