Server Side business rules validation

The context: I need to confirm a booking: So the user just pressed a button on the browser to confirm the booking Then I just need to update the booking status. But I'd like to perform some validation before. is this the right way to achieve that?
export const confirmBooking = () => {
return protectedProcedure.input(z.string().uuid()).mutation(async ({ input, ctx }) => {
// fetch the booking object
// perform validation
// reject if any error

return await ctx.db.update(bookings)
.set({ status: 'CONFIRMED' })
.where(eq(bookings.id, input))
);
});
};
export const confirmBooking = () => {
return protectedProcedure.input(z.string().uuid()).mutation(async ({ input, ctx }) => {
// fetch the booking object
// perform validation
// reject if any error

return await ctx.db.update(bookings)
.set({ status: 'CONFIRMED' })
.where(eq(bookings.id, input))
);
});
};
1 Reply
Neto
Neto16mo ago
open a transaction for that and you probably want some versioning to avoid race conditions
Want results from more Discord servers?
Add your server