Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max 5 Reviews Per User #110

Merged
merged 3 commits into from
Jan 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading