perf: improve booking confirm authorization (#8304)
* perf: improve booking confirm authorization * removes unreachable code --------- Co-authored-by: Efraín Rochín <roae.85@gmail.com>pull/8339/head
parent
e5eb7c3906
commit
ad958f10bd
|
@ -781,30 +781,22 @@ export const bookingsRouter = router({
|
||||||
});
|
});
|
||||||
|
|
||||||
const authorized = async () => {
|
const authorized = async () => {
|
||||||
// if the organizer
|
const eventType = await prisma.eventType.findFirst({
|
||||||
if (booking.userId === user.id) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
const eventType = await prisma.eventType.findUnique({
|
|
||||||
where: {
|
where: {
|
||||||
id: booking.eventTypeId || undefined,
|
id: booking.eventTypeId || undefined,
|
||||||
|
schedulingType: SchedulingType.COLLECTIVE,
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
|
||||||
schedulingType: true,
|
|
||||||
users: true,
|
users: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (
|
|
||||||
eventType?.schedulingType === SchedulingType.COLLECTIVE &&
|
return eventType && eventType.users.find((user) => booking.userId === user.id);
|
||||||
eventType.users.find((user) => user.id === user.id)
|
|
||||||
) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!(await authorized())) throw new TRPCError({ code: "UNAUTHORIZED", message: "UNAUTHORIZED" });
|
if (booking.userId !== user.id && !(await authorized())) {
|
||||||
|
throw new TRPCError({ code: "UNAUTHORIZED", message: "UNAUTHORIZED" });
|
||||||
|
}
|
||||||
|
|
||||||
const isConfirmed = booking.status === BookingStatus.ACCEPTED;
|
const isConfirmed = booking.status === BookingStatus.ACCEPTED;
|
||||||
if (isConfirmed) throw new TRPCError({ code: "BAD_REQUEST", message: "Booking already confirmed" });
|
if (isConfirmed) throw new TRPCError({ code: "BAD_REQUEST", message: "Booking already confirmed" });
|
||||||
|
|
Loading…
Reference in New Issue