From 70b9969fe3907352554a4b06e51ddf4fe7257b5a Mon Sep 17 00:00:00 2001 From: Leo Giovanetti Date: Thu, 6 Oct 2022 18:27:49 -0300 Subject: [PATCH] Fixing en error case for invalid eventTypeId --- pages/api/bookings/_post.ts | 5 +++-- test/lib/bookings/_post.test.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pages/api/bookings/_post.ts b/pages/api/bookings/_post.ts index 2509c24540..da7d473d39 100644 --- a/pages/api/bookings/_post.ts +++ b/pages/api/bookings/_post.ts @@ -64,14 +64,15 @@ async function handler( const eventTypeDb = await prisma.eventType.findUnique({ where: { id: booking.eventTypeId }, }); + if (!eventTypeDb) throw new HttpError({ statusCode: 400, message: "Invalid eventTypeId." }); const eventType = schemaEventTypeReadPublic.parse(eventTypeDb); let bookings: z.infer[]; if (!eventType) throw new HttpError({ statusCode: 400, message: "Could not create new booking" }); if (eventType.recurringEvent) { console.log("Event type has recurring configuration"); - if (!booking.recurringCount) throw new HttpError({ statusCode: 400, message: "Missing recurringCount" }); + if (!booking.recurringCount) throw new HttpError({ statusCode: 400, message: "Missing recurringCount." }); if (eventType.recurringEvent.count && booking.recurringCount > eventType?.recurringEvent.count) { - throw new HttpError({ statusCode: 400, message: "Invalid recurringCount" }); + throw new HttpError({ statusCode: 400, message: "Invalid recurringCount." }); } // Event type is recurring, ceating each booking const recurringEventId = uuidv4(); diff --git a/test/lib/bookings/_post.test.ts b/test/lib/bookings/_post.test.ts index 5fc57e5c9d..1fc3d0d108 100644 --- a/test/lib/bookings/_post.test.ts +++ b/test/lib/bookings/_post.test.ts @@ -28,7 +28,8 @@ describe("POST /api/bookings", () => { expect(res._getStatusCode()).toBe(400); expect(JSON.parse(res._getData())).toEqual( expect.objectContaining({ - message: "Booking body is invalid.", + message: + "'invalid_type' in 'eventTypeId': Required; 'invalid_type' in 'title': Required; 'invalid_type' in 'startTime': Required; 'invalid_type' in 'startTime': Required; 'invalid_type' in 'endTime': Required; 'invalid_type' in 'endTime': Required", }) ); }); @@ -45,7 +46,7 @@ describe("POST /api/bookings", () => { prisma, }); - //prismaMock.eventType.findUnique.mockResolvedValue(null); + prismaMock.eventType.findUnique.mockResolvedValue(null); await handler(req, res);