From 6926aa41cd841bb3a71a7b6fe5ce8825ffe5ca1f Mon Sep 17 00:00:00 2001 From: hpkoh <53825802+hpkoh@users.noreply.github.com> Date: Mon, 7 Nov 2022 14:08:44 +0800 Subject: [PATCH] [questions][fix] fix encounter sorting with text (#522) --- .../questions/questions-question-router.ts | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 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 e00c4ae8..0ad2457a 100644 --- a/apps/portal/src/server/router/questions/questions-question-router.ts +++ b/apps/portal/src/server/router/questions/questions-question-router.ts @@ -327,24 +327,40 @@ export const questionsQuestionRouter = createRouter() const { cursor } = input; - const sortCondition = - input.sortType === SortType.TOP - ? [ - { - upvotes: input.sortOrder, - }, - { - id: input.sortOrder, - }, - ] - : [ - { - lastSeenAt: input.sortOrder, - }, - { - id: input.sortOrder, - }, - ]; + let sortCondition = undefined; + + switch (input.sortType) { + case SortType.TOP: + sortCondition = [ + { + upvotes: input.sortOrder, + }, + { + id: input.sortOrder, + }, + ] + break; + case SortType.NEW: + sortCondition = [ + { + lastSeenAt: input.sortOrder, + }, + { + id: input.sortOrder, + }, + ]; + break; + case SortType.ENCOUNTERS: + sortCondition = [ + { + numEncounters: input.sortOrder, + }, + { + id: input.sortOrder, + }, + ]; + break; + } const questionsData = await ctx.prisma.questionsQuestion.findMany({ cursor: cursor ? { id: cursor } : undefined,