diff --git a/apps/portal/prisma/migrations/20221029180052_add_ecnounter_sort_support/migration.sql b/apps/portal/prisma/migrations/20221029180052_add_ecnounter_sort_support/migration.sql new file mode 100644 index 00000000..6744fc0c --- /dev/null +++ b/apps/portal/prisma/migrations/20221029180052_add_ecnounter_sort_support/migration.sql @@ -0,0 +1,5 @@ +-- AlterTable +ALTER TABLE "QuestionsQuestion" ADD COLUMN "numEncounters" INTEGER NOT NULL DEFAULT 0; + +-- CreateIndex +CREATE INDEX "QuestionsQuestion_numEncounters_id_idx" ON "QuestionsQuestion"("numEncounters", "id"); diff --git a/apps/portal/prisma/schema.prisma b/apps/portal/prisma/schema.prisma index 3a9277f0..b2820c3c 100644 --- a/apps/portal/prisma/schema.prisma +++ b/apps/portal/prisma/schema.prisma @@ -438,14 +438,15 @@ enum QuestionsQuestionType { } model QuestionsQuestion { - id String @id @default(cuid()) - userId String? - content String @db.Text - questionType QuestionsQuestionType - lastSeenAt DateTime? - upvotes Int @default(0) - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt + id String @id @default(cuid()) + userId String? + content String @db.Text + questionType QuestionsQuestionType + lastSeenAt DateTime? + upvotes Int @default(0) + numEncounters Int @default(0) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt user User? @relation(fields: [userId], references: [id], onDelete: SetNull) encounters QuestionsQuestionEncounter[] @@ -455,6 +456,7 @@ model QuestionsQuestion { questionsListQuestionEntries QuestionsListQuestionEntry[] @@index([lastSeenAt, id]) + @@index([numEncounters, id]) @@index([upvotes, id]) } diff --git a/apps/portal/src/server/router/questions/questions-question-encounter-user-router.ts b/apps/portal/src/server/router/questions/questions-question-encounter-user-router.ts index 54a34cfa..6a78dc14 100644 --- a/apps/portal/src/server/router/questions/questions-question-encounter-user-router.ts +++ b/apps/portal/src/server/router/questions/questions-question-encounter-user-router.ts @@ -41,19 +41,21 @@ export const questionsQuestionEncounterUserRouter = createProtectedRouter() }); } - if ( - questionToUpdate.lastSeenAt === null || - questionToUpdate.lastSeenAt < input.seenAt - ) { - await tx.questionsQuestion.update({ - data: { - lastSeenAt: input.seenAt, - }, - where: { - id: input.questionId, + + await tx.questionsQuestion.update({ + data: { + lastSeenAt: (questionToUpdate.lastSeenAt === null || + questionToUpdate.lastSeenAt < input.seenAt) + ? input.seenAt : undefined, + numEncounters: { + increment: 1, }, - }); - } + }, + where: { + id: input.questionId, + }, + }); + return questionEncounterCreated; }); }, @@ -160,6 +162,8 @@ export const questionsQuestionEncounterUserRouter = createProtectedRouter() }), ]); + let lastSeenVal = undefined; + if (questionToUpdate!.lastSeenAt === questionEncounterToDelete.seenAt) { const latestEncounter = await ctx.prisma.questionsQuestionEncounter.findFirst({ @@ -171,17 +175,20 @@ export const questionsQuestionEncounterUserRouter = createProtectedRouter() }, }); - const lastSeenVal = latestEncounter ? latestEncounter!.seenAt : null; + lastSeenVal = latestEncounter ? latestEncounter!.seenAt : null; + } - await tx.questionsQuestion.update({ + await tx.questionsQuestion.update({ data: { lastSeenAt: lastSeenVal, + numEncounters: { + increment: -1, + }, }, where: { id: questionToUpdate!.id, }, }); - } return questionEncounterDeleted; }); 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 0a2d58a4..a5c06f2d 100644 --- a/apps/portal/src/server/router/questions/questions-question-router.ts +++ b/apps/portal/src/server/router/questions/questions-question-router.ts @@ -27,24 +27,40 @@ export const questionsQuestionRouter = createRouter() async resolve({ ctx, input }) { 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, diff --git a/apps/portal/src/types/questions.d.ts b/apps/portal/src/types/questions.d.ts index 8391e6e1..0a4a9339 100644 --- a/apps/portal/src/types/questions.d.ts +++ b/apps/portal/src/types/questions.d.ts @@ -88,4 +88,5 @@ export enum SortOrder { export enum SortType { TOP, NEW, + ENCOUNTERS, }