1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-29 00:57:50 +02:00
market/src/components/templates/Search/utils.ts

201 lines
5.4 KiB
TypeScript
Raw Normal View History

2020-06-30 15:24:30 +02:00
import {
2020-07-01 18:13:32 +02:00
SearchQuery,
QueryResult
} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
import { MetadataCache, Logger } from '@oceanprotocol/lib'
import queryString from 'query-string'
export const SortTermOptions = {
Liquidity: 'liquidity',
Price: 'price',
Created: 'created'
} as const
type SortTermOptions = typeof SortTermOptions[keyof typeof SortTermOptions]
export const SortElasticTerm = {
Liquidity: 'price.ocean',
Price: 'price.value',
Created: 'created'
} as const
type SortElasticTerm = typeof SortElasticTerm[keyof typeof SortElasticTerm]
export const SortValueOptions = {
Ascending: 'asc',
Descending: 'desc'
} as const
type SortValueOptions = typeof SortValueOptions[keyof typeof SortValueOptions]
export const FilterByPriceOptions = {
Fixed: 'exchange',
Dynamic: 'pool',
All: 'all'
} as const
type FilterByPriceOptions = typeof FilterByPriceOptions[keyof typeof FilterByPriceOptions]
export const FilterByTypeOptions = {
Data: 'dataset',
Algorithm: 'algorithm'
} as const
type FilterByTypeOptions = typeof FilterByTypeOptions[keyof typeof FilterByTypeOptions]
2021-03-01 18:08:18 +01:00
function addPriceFilterToQuery(sortTerm: string, priceFilter: string): string {
if (priceFilter === FilterByPriceOptions.All) {
sortTerm = priceFilter
? sortTerm === ''
? `(price.type:${FilterByPriceOptions.Fixed} OR price.type:${FilterByPriceOptions.Dynamic})`
: `${sortTerm} AND (price.type:${FilterByPriceOptions.Dynamic} OR price.type:${FilterByPriceOptions.Fixed})`
: sortTerm
} else {
sortTerm = priceFilter
? sortTerm === ''
? `price.type:${priceFilter}`
: `${sortTerm} AND price.type:${priceFilter}`
: sortTerm
}
return sortTerm
}
function addTypeFilterToQuery(sortTerm: string, typeFilter: string): string {
sortTerm = typeFilter
? sortTerm === ''
? `service.attributes.main.type:${typeFilter}`
: `${sortTerm} AND service.attributes.main.type:${typeFilter}`
: sortTerm
return sortTerm
}
function getSortType(sortParam: string): string {
const sortTerm =
sortParam === SortTermOptions.Liquidity
? SortElasticTerm.Liquidity
: sortParam === SortTermOptions.Price
? SortElasticTerm.Price
: SortTermOptions.Created
return sortTerm
}
2020-05-07 08:03:30 +02:00
2020-06-30 15:24:30 +02:00
export function getSearchQuery(
text?: string,
owner?: string,
tags?: string,
categories?: string,
page?: string,
offset?: string,
sort?: string,
sortOrder?: string,
priceType?: string,
serviceType?: string
2020-06-30 15:24:30 +02:00
): SearchQuery {
const sortTerm = getSortType(sort)
const sortValue = sortOrder === SortValueOptions.Ascending ? 1 : -1
let searchTerm = owner
? `(publicKey.owner:${owner})`
: tags
? // eslint-disable-next-line no-useless-escape
`(service.attributes.additionalInformation.tags:\"${tags}\")`
: categories
? // eslint-disable-next-line no-useless-escape
`(service.attributes.additionalInformation.categories:\"${categories}\")`
: text || ''
searchTerm = addTypeFilterToQuery(searchTerm, serviceType)
2021-03-01 18:08:18 +01:00
searchTerm = addPriceFilterToQuery(searchTerm, priceType)
2020-06-30 15:24:30 +02:00
return {
page: Number(page) || 1,
offset: Number(offset) || 21,
2020-06-30 15:24:30 +02:00
query: {
query_string: {
query: `${searchTerm} -isInPurgatory:true`
}
// ...(owner && { 'publicKey.owner': [owner] }),
// ...(tags && { tags: [tags] }),
// ...(categories && { categories: [categories] })
2020-06-30 15:24:30 +02:00
},
sort: {
[sortTerm]: sortValue
2020-06-30 15:24:30 +02:00
}
2020-10-24 17:39:51 +02:00
// Something in ocean.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
// And the next hack,
// nativeSearch is not implmeneted on ocean.js typings
}
2020-06-30 15:24:30 +02:00
}
export async function getResults(
2020-10-24 17:39:51 +02:00
params: {
text?: string
owner?: string
2020-10-24 17:39:51 +02:00
tags?: string
categories?: string
page?: string
offset?: string
sort?: string
sortOrder?: string
priceType?: string
serviceType?: string
2020-10-24 17:39:51 +02:00
},
metadataCacheUri: string
): Promise<QueryResult> {
const {
text,
owner,
tags,
page,
offset,
categories,
sort,
sortOrder,
priceType,
serviceType
} = params
const metadataCache = new MetadataCache(metadataCacheUri, Logger)
const searchQuery = getSearchQuery(
text,
owner,
tags,
categories,
page,
offset,
sort,
sortOrder,
priceType,
serviceType
2020-05-07 08:03:30 +02:00
)
const queryResult = await metadataCache.queryMetadata(searchQuery)
2020-06-30 15:24:30 +02:00
return queryResult
2020-05-07 08:03:30 +02:00
}
export async function addExistingParamsToUrl(
location: Location,
excludedParam: string,
secondExcludedParam?: string
): Promise<string> {
const parsed = queryString.parse(location.search)
let urlLocation = '/search?'
if (Object.keys(parsed).length > 0) {
for (const querryParam in parsed) {
if (
querryParam !== excludedParam &&
querryParam !== secondExcludedParam
) {
if (querryParam === 'page' && excludedParam === 'text') {
Logger.log('remove page when starting a new search')
} else {
const value = parsed[querryParam]
urlLocation = `${urlLocation}${querryParam}=${value}&`
}
}
}
} else {
urlLocation = `${urlLocation}sort=${SortTermOptions.Created}&sortOrder=${SortValueOptions.Descending}&`
}
urlLocation = urlLocation.slice(0, -1)
return urlLocation
}