From 5355b76be41924360c77498065a1745d7fb9613b Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 29 Aug 2023 20:22:40 +0300 Subject: [PATCH] fix(EstimateFilter): simple year filter support --- trpc/queries/goals.ts | 64 +++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/trpc/queries/goals.ts b/trpc/queries/goals.ts index 93c301e91..53eca7834 100644 --- a/trpc/queries/goals.ts +++ b/trpc/queries/goals.ts @@ -1,6 +1,7 @@ import { Estimate, EstimateToGoal, Goal, GoalAchieveCriteria, Prisma, Role, State, StateType } from '@prisma/client'; import { QueryWithFilters } from '../../src/schema/common'; +import { quarters } from '../../src/utils/dateTime'; const defaultOrderBy = { updatedAt: 'desc', @@ -35,6 +36,50 @@ const getStateFilter = (data: QueryWithFilters): Prisma.GoalFindManyArgs['where' return state; }; +const getEstimateFilter = (data: QueryWithFilters): Prisma.GoalFindManyArgs['where'] => { + const state: Prisma.GoalFindManyArgs['where'] = {}; + + if (data.estimate?.length) { + return { + estimate: { + some: { + estimate: { + OR: data.estimate.reduce((acum, e) => { + const match = e.match( + new RegExp(`((?${Object.keys(quarters).join('|')})/)?(?[0-9]+$)`), + ); + + if (match) { + const { groups: { q, y } = {} } = match; + + if (q) { + acum.push({ + q, + y, + }); + + acum.push({ + q: null, + y, + }); + } else { + acum.push({ + y, + }); + } + } + + return acum; + }, [] as { q?: string | null; y: string }[]), + }, + }, + }, + }; + } + + return state; +}; + export const goalsFilter = ( data: QueryWithFilters, activityId: string, @@ -59,24 +104,7 @@ export const goalsFilter = ( } : {}; - const estimateFilter: Prisma.GoalFindManyArgs['where'] = data.estimate?.length - ? { - estimate: { - some: { - estimate: { - OR: data.estimate.map((e) => { - const [q, y] = e.split('/'); - - return { - q, - y, - }; - }), - }, - }, - }, - } - : {}; + const estimateFilter: Prisma.GoalFindManyArgs['where'] = getEstimateFilter(data); const issuerFilter: Prisma.GoalFindManyArgs['where'] = data.issuer?.length ? {