From f2a0025bdf2bcd517b9f1c2c04293cc6ecbd99ef Mon Sep 17 00:00:00 2001 From: Mikael Araya Date: Sat, 28 Dec 2024 21:30:26 +0300 Subject: [PATCH 1/3] Add Query.tokensCount --- packages/api/src/resolvers/queries/index.ts | 2 ++ .../api/src/resolvers/queries/warehousing/tokensCount.ts | 8 ++++++++ packages/api/src/schema/query.ts | 5 +++++ .../src/module/configureWarehousingModule.ts | 5 +++++ 4 files changed, 20 insertions(+) create mode 100644 packages/api/src/resolvers/queries/warehousing/tokensCount.ts diff --git a/packages/api/src/resolvers/queries/index.ts b/packages/api/src/resolvers/queries/index.ts index 90a5132b39..8ad83281d7 100755 --- a/packages/api/src/resolvers/queries/index.ts +++ b/packages/api/src/resolvers/queries/index.ts @@ -63,6 +63,7 @@ import warehousingProviders from './warehousing/warehousingProviders.js'; import warehousingProvidersCount from './warehousing/warehousingProvidersCount.js'; import token from './warehousing/token.js'; import tokens from './warehousing/tokens.js'; +import tokensCount from './warehousing/tokensCount.js'; import work from './worker/work.js'; import workQueue from './worker/workQueue.js'; import workStatistics from './worker/workStatistics.js'; @@ -104,6 +105,7 @@ export default { warehousingInterfaces: acl(actions.viewWarehousingInterfaces)(warehousingInterfaces), token: acl(actions.viewToken)(token), tokens: acl(actions.viewTokens)(tokens), + tokensCount: acl(actions.viewTokens)(tokensCount), translatedProductTexts: acl(actions.viewTranslations)(translatedProductTexts), translatedProductMediaTexts: acl(actions.viewTranslations)(translatedProductMediaTexts), translatedProductVariationTexts: acl(actions.viewTranslations)(translatedProductVariationTexts), diff --git a/packages/api/src/resolvers/queries/warehousing/tokensCount.ts b/packages/api/src/resolvers/queries/warehousing/tokensCount.ts new file mode 100644 index 0000000000..a8c85ac9a2 --- /dev/null +++ b/packages/api/src/resolvers/queries/warehousing/tokensCount.ts @@ -0,0 +1,8 @@ +import { log } from '@unchainedshop/logger'; +import { Context } from '../../../context.js'; + +export default async function tokensCount(root: never, _, { modules, userId }: Context) { + log(`query tokensCount`, { userId }); + + return modules.warehousing.tokensCount(); +} diff --git a/packages/api/src/schema/query.ts b/packages/api/src/schema/query.ts index aa10c7042f..d69b054c71 100644 --- a/packages/api/src/schema/query.ts +++ b/packages/api/src/schema/query.ts @@ -213,6 +213,11 @@ export default [ """ tokens(limit: Int = 10, offset: Int = 0): [Token!]! + """ + Returns total tokens + """ + tokensCount: Int! + """ Returns total number of payment providers, optionally filtered by type """ diff --git a/packages/core-warehousing/src/module/configureWarehousingModule.ts b/packages/core-warehousing/src/module/configureWarehousingModule.ts index 8f5b5fbef6..6a6a29db0c 100644 --- a/packages/core-warehousing/src/module/configureWarehousingModule.ts +++ b/packages/core-warehousing/src/module/configureWarehousingModule.ts @@ -59,6 +59,11 @@ export const configureWarehousingModule = async ({ db }: ModuleInput => { + const tokenCount = await TokenSurrogates.countDocuments(); + return tokenCount; + }, + findTokensForUser: async ( params: { userId: string } | { walletAddresses: string[] }, options?: mongodb.FindOptions, From b3c4967505349bbda2b7add61e2860e9ac0533f8 Mon Sep 17 00:00:00 2001 From: Mikael Araya Date: Sat, 28 Dec 2024 21:34:01 +0300 Subject: [PATCH 2/3] Adjust counter --- .../core-warehousing/src/module/configureWarehousingModule.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-warehousing/src/module/configureWarehousingModule.ts b/packages/core-warehousing/src/module/configureWarehousingModule.ts index 6a6a29db0c..d2c1140bdd 100644 --- a/packages/core-warehousing/src/module/configureWarehousingModule.ts +++ b/packages/core-warehousing/src/module/configureWarehousingModule.ts @@ -60,7 +60,7 @@ export const configureWarehousingModule = async ({ db }: ModuleInput => { - const tokenCount = await TokenSurrogates.countDocuments(); + const tokenCount = await TokenSurrogates.countDocuments({}); return tokenCount; }, From ecd06030f4f87d4327ff371a4fe76d88f0b487fe Mon Sep 17 00:00:00 2001 From: Mikael Araya Date: Mon, 30 Dec 2024 21:33:36 +0300 Subject: [PATCH 3/3] Add basic text search index for tokens and extend query --- .../resolvers/queries/warehousing/tokens.ts | 4 +++- .../queries/warehousing/tokensCount.ts | 8 +++++-- packages/api/src/schema/query.ts | 4 ++-- .../src/db/TokenSurrogateCollection.ts | 22 +++++++++++++++++++ .../src/module/configureWarehousingModule.ts | 19 +++++++++++++--- 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/packages/api/src/resolvers/queries/warehousing/tokens.ts b/packages/api/src/resolvers/queries/warehousing/tokens.ts index 93a04774f1..faac5efacb 100644 --- a/packages/api/src/resolvers/queries/warehousing/tokens.ts +++ b/packages/api/src/resolvers/queries/warehousing/tokens.ts @@ -4,15 +4,17 @@ import { Context } from '../../../context.js'; export default async function tokens( root: never, { + queryString = null, limit = 10, offset = 0, }: { limit: number; offset: number; + queryString?: string; }, { modules, userId }: Context, ) { log(`query tokens`, { userId }); - return modules.warehousing.findTokens({}, { limit, skip: offset }); + return modules.warehousing.findTokens({ queryString }, { limit, skip: offset }); } diff --git a/packages/api/src/resolvers/queries/warehousing/tokensCount.ts b/packages/api/src/resolvers/queries/warehousing/tokensCount.ts index a8c85ac9a2..8814882030 100644 --- a/packages/api/src/resolvers/queries/warehousing/tokensCount.ts +++ b/packages/api/src/resolvers/queries/warehousing/tokensCount.ts @@ -1,8 +1,12 @@ import { log } from '@unchainedshop/logger'; import { Context } from '../../../context.js'; -export default async function tokensCount(root: never, _, { modules, userId }: Context) { +export default async function tokensCount( + root: never, + { queryString = null }, + { modules, userId }: Context, +) { log(`query tokensCount`, { userId }); - return modules.warehousing.tokensCount(); + return modules.warehousing.tokensCount({ queryString }); } diff --git a/packages/api/src/schema/query.ts b/packages/api/src/schema/query.ts index d69b054c71..a442e162d1 100644 --- a/packages/api/src/schema/query.ts +++ b/packages/api/src/schema/query.ts @@ -211,12 +211,12 @@ export default [ """ Get all tokens """ - tokens(limit: Int = 10, offset: Int = 0): [Token!]! + tokens(queryString: String, limit: Int = 10, offset: Int = 0): [Token!]! """ Returns total tokens """ - tokensCount: Int! + tokensCount(queryString: String): Int! """ Returns total number of payment providers, optionally filtered by type diff --git a/packages/core-warehousing/src/db/TokenSurrogateCollection.ts b/packages/core-warehousing/src/db/TokenSurrogateCollection.ts index f5d7185e37..6e51213acf 100644 --- a/packages/core-warehousing/src/db/TokenSurrogateCollection.ts +++ b/packages/core-warehousing/src/db/TokenSurrogateCollection.ts @@ -45,6 +45,28 @@ export const TokenSurrogateCollection = async (db: mongodb.Db) => { orderPositionId: 1, }, }, + { + index: { + chainTokenId: 'text', + userId: 'text', + productId: 'text', + _id: 'text', + walletAddress: 'text', + contractAddress: 'text', + } as any, + options: { + weights: { + _id: 9, + chainTokenId: 8, + userId: 3, + productId: 6, + contractAddress: 5, + walletAddress: 4, + status: 1, + }, + name: 'token_fulltext_search', + }, + }, ]); return TokenSurrogates; diff --git a/packages/core-warehousing/src/module/configureWarehousingModule.ts b/packages/core-warehousing/src/module/configureWarehousingModule.ts index d2c1140bdd..b16318046d 100644 --- a/packages/core-warehousing/src/module/configureWarehousingModule.ts +++ b/packages/core-warehousing/src/module/configureWarehousingModule.ts @@ -11,6 +11,11 @@ type WarehousingProviderQuery = { type?: WarehousingProviderType; }; +type TokenQuery = { + queryString?: string; + userId?: string; +}; + const WAREHOUSING_PROVIDER_EVENTS: string[] = [ 'WAREHOUSING_PROVIDER_CREATE', 'WAREHOUSING_PROVIDER_UPDATE', @@ -23,6 +28,14 @@ export const buildFindSelector = ({ type }: WarehousingProviderQuery = {}) => { const query = type ? { type, deleted: null } : { deleted: null }; return query; }; +export const buildTokenFindSelector = ({ queryString, ...rest }: TokenQuery) => { + const selector: mongodb.Filter = { ...(rest || {}) }; + if (queryString) { + (selector as any).$text = { $search: queryString }; + } + + return selector; +}; export const configureWarehousingModule = async ({ db }: ModuleInput>) => { registerEvents(WAREHOUSING_PROVIDER_EVENTS); @@ -56,11 +69,11 @@ export const configureWarehousingModule = async ({ db }: ModuleInput> => { - return TokenSurrogates.find(selector, options).toArray(); + return TokenSurrogates.find(buildTokenFindSelector(selector), options).toArray(); }, - tokensCount: async (): Promise => { - const tokenCount = await TokenSurrogates.countDocuments({}); + tokensCount: async (selector: any = {}): Promise => { + const tokenCount = await TokenSurrogates.countDocuments(buildTokenFindSelector(selector)); return tokenCount; },