Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Query.tokensCount #620

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/api/src/resolvers/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/resolvers/queries/warehousing/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
12 changes: 12 additions & 0 deletions packages/api/src/resolvers/queries/warehousing/tokensCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { log } from '@unchainedshop/logger';
import { Context } from '../../../context.js';

export default async function tokensCount(
root: never,
{ queryString = null },
{ modules, userId }: Context,
) {
log(`query tokensCount`, { userId });

return modules.warehousing.tokensCount({ queryString });
}
7 changes: 6 additions & 1 deletion packages/api/src/schema/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +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(queryString: String): Int!

"""
Returns total number of payment providers, optionally filtered by type
Expand Down
22 changes: 22 additions & 0 deletions packages/core-warehousing/src/db/TokenSurrogateCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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<TokenSurrogate> = { ...(rest || {}) };
if (queryString) {
(selector as any).$text = { $search: queryString };
}

return selector;
};

export const configureWarehousingModule = async ({ db }: ModuleInput<Record<string, never>>) => {
registerEvents(WAREHOUSING_PROVIDER_EVENTS);
Expand Down Expand Up @@ -56,7 +69,12 @@ export const configureWarehousingModule = async ({ db }: ModuleInput<Record<stri
},

findTokens: async (selector: any, options?: mongodb.FindOptions): Promise<Array<TokenSurrogate>> => {
return TokenSurrogates.find(selector, options).toArray();
return TokenSurrogates.find(buildTokenFindSelector(selector), options).toArray();
},

tokensCount: async (selector: any = {}): Promise<number> => {
const tokenCount = await TokenSurrogates.countDocuments(buildTokenFindSelector(selector));
return tokenCount;
},

findTokensForUser: async (
Expand Down
Loading