From e6f3b76269967d436fdf7f140d56e4f1dd7e7969 Mon Sep 17 00:00:00 2001 From: hpkoh Date: Mon, 31 Oct 2022 18:31:18 +0800 Subject: [PATCH] [questions][fix] update filter --- .../questions/questions-question-router.ts | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/apps/portal/src/server/router/questions/questions-question-router.ts b/apps/portal/src/server/router/questions/questions-question-router.ts index 70d39bff..475e3e5b 100644 --- a/apps/portal/src/server/router/questions/questions-question-router.ts +++ b/apps/portal/src/server/router/questions/questions-question-router.ts @@ -291,14 +291,20 @@ export const questionsQuestionRouter = createRouter() .split(/\s+/) .join(' | '); - const relatedQuestionsId: Array<{ id: string }> = await ctx.prisma - .$queryRaw` - SELECT id FROM "QuestionsQuestion" - WHERE - to_tsvector("content") @@ to_tsquery('english', ${query}) - AND ts_rank_cd(to_tsvector("content"), to_tsquery('english', ${query}), 32) > 0.8 - ORDER BY ts_rank_cd(to_tsvector("content"), to_tsquery('english', ${query}), 32) DESC; - `; + let relatedQuestionsId: Array<{ id: string }> = []; + + if (input.content !== "") { + relatedQuestionsId = await ctx.prisma + .$queryRaw` + SELECT id FROM "QuestionsQuestion" + WHERE + to_tsvector("content") @@ to_tsquery('english', ${query}) + AND ts_rank_cd(to_tsvector("content"), to_tsquery('english', ${query}), 32) > 0.8 + ORDER BY ts_rank_cd(to_tsvector("content"), to_tsquery('english', ${query}), 32) DESC; + `; + } + + const relatedQuestionsIdArray = relatedQuestionsId.map( (current) => current.id, @@ -354,9 +360,9 @@ export const questionsQuestionRouter = createRouter() orderBy: sortCondition, take: input.limit + 1, where: { - id: { + id: input.content !== "" ? { in: relatedQuestionsIdArray, - }, + } : undefined, ...(input.questionTypes.length > 0 ? { questionType: {