Skip to content

Commit

Permalink
Max 5 Reviews Per User (#110)
Browse files Browse the repository at this point in the history
* prevents creation of new review if already 5

* max 2 reviews per cycle

* resolved changes
  • Loading branch information
mattrwang authored Jan 26, 2025
1 parent 01d3375 commit 88a1f5a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/api/src/router/review.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TRPCRouterRecord } from "@trpc/server";
import { TRPCError } from "@trpc/server";
import Fuse from "fuse.js";
import { z } from "zod";

Expand Down Expand Up @@ -72,7 +73,33 @@ export const reviewRouter = {

create: protectedProcedure
.input(CreateReviewSchema)
.mutation(({ ctx, input }) => {
.mutation(async ({ ctx, input }) => {
if (!input.profileId) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You must be logged in to leave a review",
});
}
const reviews = await ctx.db.query.Review.findMany({
where: eq(Review.profileId, input.profileId),
});
if (reviews.length >= 5) {
throw new TRPCError({
code: "PRECONDITION_FAILED",
message: "You can only leave 5 reviews",
});
}
const reviewsInSameCycle = reviews.filter(
(review) =>
review.workTerm === input.workTerm &&
review.workYear === input.workYear,
);
if (reviewsInSameCycle.length >= 2) {
throw new TRPCError({
code: "PRECONDITION_FAILED",
message: "You can only leave 2 reviews per cycle",
});
}
return ctx.db.insert(Review).values(input);
}),

Expand Down

0 comments on commit 88a1f5a

Please sign in to comment.