From 861303016f0cf3a9c0326e9e09ca9d9df06eebc2 Mon Sep 17 00:00:00 2001 From: hpkoh <53825802+hpkoh@users.noreply.github.com> Date: Tue, 8 Nov 2022 01:58:19 +0800 Subject: [PATCH] [questions][fix] encounter last seen sorting (#530) Co-authored-by: Jeff Sieu --- .../forms/ContributeQuestionForm.tsx | 2 +- .../[questionId]/[questionSlug]/index.tsx | 2 +- apps/portal/src/pages/questions/browse.tsx | 4 +--- apps/portal/src/pages/questions/lists.tsx | 2 +- .../questions/questions-question-router.ts | 24 +++++++++---------- apps/portal/src/types/questions.d.ts | 3 +-- .../questions/server/aggregate-encounters.ts | 8 +------ 7 files changed, 18 insertions(+), 27 deletions(-) diff --git a/apps/portal/src/components/questions/forms/ContributeQuestionForm.tsx b/apps/portal/src/components/questions/forms/ContributeQuestionForm.tsx index 02ac6647..c1d4c111 100644 --- a/apps/portal/src/components/questions/forms/ContributeQuestionForm.tsx +++ b/apps/portal/src/components/questions/forms/ContributeQuestionForm.tsx @@ -223,7 +223,7 @@ export default function ContributeQuestionForm({ createEncounterButtonText="Yes, this is my question" questionId={question.id} roles={roleCounts} - timestamp={question.seenAt} + timestamp={question.lastSeenAt} type={question.type} onReceivedSubmit={async (data) => { await addEncounterAsync({ diff --git a/apps/portal/src/pages/questions/[questionId]/[questionSlug]/index.tsx b/apps/portal/src/pages/questions/[questionId]/[questionSlug]/index.tsx index 5baee8fe..0e981294 100644 --- a/apps/portal/src/pages/questions/[questionId]/[questionSlug]/index.tsx +++ b/apps/portal/src/pages/questions/[questionId]/[questionSlug]/index.tsx @@ -210,7 +210,7 @@ export default function QuestionPage() { questionId={question.id} receivedCount={undefined} roles={relabeledAggregatedEncounters?.roleCounts ?? {}} - timestamp={question.seenAt} + timestamp={question.lastSeenAt} upvoteCount={question.numVotes} onReceivedSubmit={async (data) => { await addEncounterAsync({ diff --git a/apps/portal/src/pages/questions/browse.tsx b/apps/portal/src/pages/questions/browse.tsx index 9a4b6d31..6943fbdb 100644 --- a/apps/portal/src/pages/questions/browse.tsx +++ b/apps/portal/src/pages/questions/browse.tsx @@ -558,9 +558,7 @@ export default function QuestionsBrowsePage() { questionId={question.id} receivedCount={question.receivedCount} roles={roleCounts} - timestamp={ - question.aggregatedQuestionEncounters.latestSeenAt - } + timestamp={question.lastSeenAt} type={question.type} upvoteCount={question.numVotes} /> diff --git a/apps/portal/src/pages/questions/lists.tsx b/apps/portal/src/pages/questions/lists.tsx index 99539de7..0e729f16 100644 --- a/apps/portal/src/pages/questions/lists.tsx +++ b/apps/portal/src/pages/questions/lists.tsx @@ -220,7 +220,7 @@ export default function ListPage() { questionId={question.id} receivedCount={question.receivedCount} roles={roleCounts} - timestamp={question.seenAt} + timestamp={question.lastSeenAt} type={question.type} onDelete={() => { deleteQuestionEntry({ id: entryId }); 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 0ad2457a..fb01861b 100644 --- a/apps/portal/src/server/router/questions/questions-question-router.ts +++ b/apps/portal/src/server/router/questions/questions-question-router.ts @@ -39,7 +39,7 @@ export const questionsQuestionRouter = createRouter() { id: input.sortOrder, }, - ] + ]; break; case SortType.NEW: sortCondition = [ @@ -61,7 +61,7 @@ export const questionsQuestionRouter = createRouter() }, ]; break; - } + } const questionsData = await ctx.prisma.questionsQuestion.findMany({ cursor: cursor ? { id: cursor } : undefined, @@ -309,9 +309,8 @@ export const questionsQuestionRouter = createRouter() let relatedQuestionsId: Array<{ id: string }> = []; - if (input.content !== "") { - relatedQuestionsId = await ctx.prisma - .$queryRaw` + if (input.content !== '') { + relatedQuestionsId = await ctx.prisma.$queryRaw` SELECT id FROM "QuestionsQuestion" WHERE ts_rank_cd(to_tsvector("content"), to_tsquery(${query}), 32) > 0.1 @@ -319,8 +318,6 @@ export const questionsQuestionRouter = createRouter() `; } - - const relatedQuestionsIdArray = relatedQuestionsId.map( (current) => current.id, ); @@ -338,7 +335,7 @@ export const questionsQuestionRouter = createRouter() { id: input.sortOrder, }, - ] + ]; break; case SortType.NEW: sortCondition = [ @@ -360,7 +357,7 @@ export const questionsQuestionRouter = createRouter() }, ]; break; - } + } const questionsData = await ctx.prisma.questionsQuestion.findMany({ cursor: cursor ? { id: cursor } : undefined, @@ -391,9 +388,12 @@ export const questionsQuestionRouter = createRouter() orderBy: sortCondition, take: input.limit + 1, where: { - id: input.content !== "" ? { - in: relatedQuestionsIdArray, - } : undefined, + id: + input.content !== '' + ? { + in: relatedQuestionsIdArray, + } + : undefined, ...(input.questionTypes.length > 0 ? { questionType: { diff --git a/apps/portal/src/types/questions.d.ts b/apps/portal/src/types/questions.d.ts index 0a4a9339..425d6962 100644 --- a/apps/portal/src/types/questions.d.ts +++ b/apps/portal/src/types/questions.d.ts @@ -4,11 +4,11 @@ export type Question = { aggregatedQuestionEncounters: AggregatedQuestionEncounter; content: string; id: string; + lastSeenAt: Date | null; numAnswers: number; numComments: number; numVotes: number; receivedCount: number; - seenAt: Date; type: QuestionsQuestionType; updatedAt: Date; user: string; @@ -47,7 +47,6 @@ export type Location = CityLocation | CountryLocation | StateLocation; export type AggregatedQuestionEncounter = { companyCounts: Record; countryCounts: Record; - latestSeenAt: Date; roleCounts: Record; }; diff --git a/apps/portal/src/utils/questions/server/aggregate-encounters.ts b/apps/portal/src/utils/questions/server/aggregate-encounters.ts index 189bcd1a..acee4f0b 100644 --- a/apps/portal/src/utils/questions/server/aggregate-encounters.ts +++ b/apps/portal/src/utils/questions/server/aggregate-encounters.ts @@ -61,11 +61,11 @@ export function createQuestionWithAggregateData( ), content: data.content, id: data.id, + lastSeenAt: data.lastSeenAt, numAnswers: data._count.answers, numComments: data._count.comments, numVotes: votes, receivedCount: data.encounters.length, - seenAt: data.encounters[0].seenAt, type: data.questionType, updatedAt: data.updatedAt, user: data.user?.name ?? '', @@ -80,12 +80,7 @@ export function createAggregatedQuestionEncounter( const companyCounts: Record = {}; const roleCounts: Record = {}; - let latestSeenAt = encounters[0].seenAt; - for (const encounter of encounters) { - latestSeenAt = - latestSeenAt < encounter.seenAt ? encounter.seenAt : latestSeenAt; - if (encounter.company !== null) { if (!(encounter.company.name in companyCounts)) { companyCounts[encounter.company!.name] = 0; @@ -137,7 +132,6 @@ export function createAggregatedQuestionEncounter( return { companyCounts, countryCounts, - latestSeenAt, roleCounts, }; }