Skip to content

Commit

Permalink
allow admin editing
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyro292 committed Aug 12, 2024
1 parent 90f2364 commit a70edae
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export default async function Review({ params }: ReviewProps) {
});

if (!review) redirect("/404");
if (review.userId !== session.user.id) redirect("/404");

const opportunityTitle = db.opportunity.findUnique({
where: {
Expand Down
49 changes: 49 additions & 0 deletions server/api/routers/review.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ export const reviewRouter = createTRPCRouter({
)
.mutation(async ({ input, ctx }) => {
const { id, content, status } = input;

const review = await ctx.db.review.findUnique({
where: { id },
include: { application: true },
});

if (!review) {
throw new Error("Review not found");
}

const opportunity = await ctx.db.opportunity.findUnique({
where: { id: review.application.opportunityId },
include: { admins: true },
});

if (
!opportunity?.admins.some(
(admin) => admin.id === ctx.session.user.id,
) &&
review.userId !== ctx.session.user.id
) {
throw new Error("Unauthorized");
}

await ctx.db.review.update({
data: { content, status },
where: { id },
Expand All @@ -22,6 +46,31 @@ export const reviewRouter = createTRPCRouter({
deleteById: protectedProcedure
.input(z.object({ id: z.number() }))
.mutation(async ({ input, ctx }) => {
const { id } = input;

const review = await ctx.db.review.findUnique({
where: { id },
include: { application: true },
});

if (!review) {
throw new Error("Review not found");
}

const opportunity = await ctx.db.opportunity.findUnique({
where: { id: review.application.opportunityId },
include: { admins: true },
});

if (
!opportunity?.admins.some(
(admin) => admin.id === ctx.session.user.id,
) &&
review.userId !== ctx.session.user.id
) {
throw new Error("Unauthorized");
}

return await ctx.db.review.delete({
where: {
id: input.id,
Expand Down

0 comments on commit a70edae

Please sign in to comment.