From 6aec06ff00ebf80602b306af44f7bac28de85458 Mon Sep 17 00:00:00 2001 From: hpkoh Date: Mon, 17 Oct 2022 15:02:21 +0800 Subject: [PATCH] [questions][chore] refactor question queries --- .../router/questions-question-router.ts | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/apps/portal/src/server/router/questions-question-router.ts b/apps/portal/src/server/router/questions-question-router.ts index 2cb564d3..0776845e 100644 --- a/apps/portal/src/server/router/questions-question-router.ts +++ b/apps/portal/src/server/router/questions-question-router.ts @@ -51,29 +51,34 @@ export const questionsQuestionRouter = createProtectedRouter() }, } : {}), + encounters : { + some: { + ...(input.companies.length > 0 + ? { + company : { + in : input.companies + } + } + : {}), + ...(input.locations.length > 0 + ? { + location: { + in: input.locations + }, + } + : {}), + ...(input.roles.length > 0 + ? { + role : { + in: input.roles + } + } + : {}), + } + } }, }); return questionsData - .filter((data) => { - for (let i = 0; i < data.encounters.length; i++) { - const encounter = data.encounters[i]; - const matchCompany = - input.companies.length === 0 || - input.companies.includes(encounter.company); - const matchLocation = - input.locations.length === 0 || - input.locations.includes(encounter.location); - const matchRole = - input.roles.length === 0 || input.roles.includes(encounter.role); - const matchDate = - (!input.startDate || encounter.seenAt >= input.startDate) && - encounter.seenAt <= input.endDate; - if (matchCompany && matchLocation && matchRole && matchDate) { - return true; - } - } - return false; - }) .map((data) => { const votes: number = data.votes.reduce( (previousValue: number, currentValue) => {