Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Update comments for multi round
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbarrdahl committed Apr 15, 2024
1 parent e37fec6 commit 19f53bd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
10 changes: 9 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ model Ballot {
}

model Round {
id String @id @default(cuid())
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
creatorId String
name String
description String?
Expand All @@ -100,6 +103,8 @@ model Round {
calculationConfig Json?
poolId Int?
comments Comment[]
}

model Comment {
Expand All @@ -109,4 +114,7 @@ model Comment {
content String
creatorId String
projectId String
roundId String
round Round @relation(fields: [roundId], references: [id])
}
19 changes: 12 additions & 7 deletions src/server/api/routers/comments.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import {
createTRPCRouter,
protectedProcedure,
protectedRoundProcedure,
} from "~/server/api/trpc";
import { CommentSchema } from "~/features/comments/types";
import { fetchApprovedVoter } from "~/utils/fetchAttestations";
import { TRPCError } from "@trpc/server";
import { z } from "zod";

export const commentsRouter = createTRPCRouter({
create: protectedProcedure
create: protectedRoundProcedure
.input(CommentSchema)
.mutation(async ({ input: { content, projectId }, ctx }) => {
const creatorId = String(ctx.session?.user.name);

if (!(await fetchApprovedVoter(creatorId))) {
if (!(await fetchApprovedVoter(ctx.round, creatorId))) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Must be an approved voter to comment",
});
}
1;
const roundId = ctx.round.id;
return ctx.db.comment.create({
data: { content, creatorId, projectId },
data: { content, creatorId, projectId, roundId },
});
}),
delete: protectedProcedure
Expand All @@ -36,9 +40,10 @@ export const commentsRouter = createTRPCRouter({
data: { content },
});
}),
list: protectedProcedure
list: protectedRoundProcedure
.input(z.object({ projectId: z.string() }))
.query(async ({ input: { projectId }, ctx }) => {
return ctx.db.comment.findMany({ where: { projectId } });
const roundId = ctx.round.id;
return ctx.db.comment.findMany({ where: { projectId, roundId } });
}),
});
3 changes: 3 additions & 0 deletions src/server/api/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ const roundMiddleware = t.middleware(async ({ ctx, next }) => {
})
: null;

if (!round)
throw new TRPCError({ code: "NOT_FOUND", message: "Round not found" });

return next({ ctx: { ...ctx, round } });
});
const attestationMiddleware = t.middleware(async ({ ctx, next }) => {
Expand Down

0 comments on commit 19f53bd

Please sign in to comment.