diff --git a/apps/portal/prisma/migrations/20221021150358_add_vote_count_and_last_seen/migration.sql b/apps/portal/prisma/migrations/20221021150358_add_vote_count_and_last_seen/migration.sql new file mode 100644 index 00000000..a6319a17 --- /dev/null +++ b/apps/portal/prisma/migrations/20221021150358_add_vote_count_and_last_seen/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - Added the required column `upvotes` to the `QuestionsQuestion` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "QuestionsQuestion" ADD COLUMN "lastSeenAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, +ADD COLUMN "upvotes" INTEGER NOT NULL; + +-- AlterTable +ALTER TABLE "QuestionsQuestionEncounter" ADD COLUMN "netVotes" INTEGER NOT NULL DEFAULT 0; diff --git a/apps/portal/prisma/migrations/20221021151424_delete_extra_encounter_fields/migration.sql b/apps/portal/prisma/migrations/20221021151424_delete_extra_encounter_fields/migration.sql new file mode 100644 index 00000000..ef9e4229 --- /dev/null +++ b/apps/portal/prisma/migrations/20221021151424_delete_extra_encounter_fields/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - You are about to drop the column `netVotes` on the `QuestionsQuestionEncounter` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "QuestionsQuestion" ALTER COLUMN "lastSeenAt" DROP DEFAULT, +ALTER COLUMN "upvotes" SET DEFAULT 0; + +-- AlterTable +ALTER TABLE "QuestionsQuestionEncounter" DROP COLUMN "netVotes"; diff --git a/apps/portal/prisma/schema.prisma b/apps/portal/prisma/schema.prisma index eec5bb5b..7be7942c 100644 --- a/apps/portal/prisma/schema.prisma +++ b/apps/portal/prisma/schema.prisma @@ -394,8 +394,8 @@ model QuestionsQuestion { userId String? content String @db.Text questionType QuestionsQuestionType - lastSeenAt DateTime @default(now()) - upvotes Int + lastSeenAt DateTime + upvotes Int @default(0) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@ -414,7 +414,6 @@ model QuestionsQuestionEncounter { companyId String location String @db.Text role String @db.Text - netVotes Int @default(0) seenAt DateTime createdAt DateTime @default(now()) updatedAt DateTime @updatedAt diff --git a/apps/portal/src/server/router/questions-question-router.ts b/apps/portal/src/server/router/questions-question-router.ts index bf5a3241..3548e975 100644 --- a/apps/portal/src/server/router/questions-question-router.ts +++ b/apps/portal/src/server/router/questions-question-router.ts @@ -24,6 +24,21 @@ export const questionsQuestionRouter = createProtectedRouter() startDate: z.date().default(new Date(Date.now() - TWO_WEEK_IN_MS)), }), async resolve({ ctx, input }) { + let sortCondition; + + switch(input.sortType) { + case SortType.TOP: + sortCondition = { + upvotes: input.sortOrder, + } + break; + case SortType.NEW: + sortCondition = { + lastSeenAt: input.sortOrder, + } + break; + } + const questionsData = await ctx.prisma.questionsQuestion.findMany({ include: { _count: { @@ -48,7 +63,7 @@ export const questionsQuestionRouter = createProtectedRouter() votes: true, }, orderBy: { - createdAt: 'desc', + ...sortCondition, }, where: { ...(input.questionTypes.length > 0 @@ -214,7 +229,6 @@ export const questionsQuestionRouter = createProtectedRouter() return await ctx.prisma.questionsQuestion.create({ data: { content: input.content, - lastSeenAt: input.seenAt, encounters: { create: { company: { @@ -232,6 +246,7 @@ export const questionsQuestionRouter = createProtectedRouter() }, }, }, + lastSeenAt: input.seenAt, questionType: input.questionType, userId, }, @@ -326,18 +341,28 @@ export const questionsQuestionRouter = createProtectedRouter() const userId = ctx.session?.user?.id; const { questionId, vote } = input; - return await ctx.prisma.questionsQuestionVote.create({ - question: { - update :{ + const incrementValue = vote === Vote.UPVOTE ? 1 : -1; - } - } - data: { - questionId, - userId, - vote, - }, - }); + const [questionVote, question] = await ctx.prisma.$transaction([ + ctx.prisma.questionsQuestionVote.create({ + data: { + questionId, + userId, + vote, + }, + }), + ctx.prisma.questionsQuestion.update({ + data: { + upvotes : { + increment: incrementValue, + }, + }, + where: { + id: questionId, + }, + }) + ]); + return questionVote; }, }) .mutation('updateVote', { @@ -362,14 +387,30 @@ export const questionsQuestionRouter = createProtectedRouter() }); } - return await ctx.prisma.questionsQuestionVote.update({ - data: { - vote, - }, - where: { - id, - }, - }); + const incrementValue = vote === Vote.UPVOTE ? 2 : -2; + + const [questionVote, question] = await ctx.prisma.$transaction([ + ctx.prisma.questionsQuestionVote.update({ + data: { + vote, + }, + where: { + id, + }, + }), + ctx.prisma.questionsQuestion.update({ + data: { + upvotes : { + increment: incrementValue, + }, + }, + where: { + id: voteToUpdate.questionId, + }, + }) + ]); + + return questionVote; }, }) .mutation('deleteVote', { @@ -392,10 +433,25 @@ export const questionsQuestionRouter = createProtectedRouter() }); } - return await ctx.prisma.questionsQuestionVote.delete({ - where: { - id: input.id, - }, - }); + const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1; + + const [questionVote, question] = await ctx.prisma.$transaction([ + ctx.prisma.questionsQuestionVote.delete({ + where: { + id: input.id, + }, + }), + ctx.prisma.questionsQuestion.update({ + data: { + upvotes : { + increment: incrementValue, + }, + }, + where: { + id: voteToDelete.questionId, + }, + }) + ]); + return questionVote; }, });