From 6997606a4c926f1efdf7c7183a8843bd2344aa34 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Tue, 26 Apr 2022 22:23:33 +0200 Subject: [PATCH] fix: moves to pick and separates availabilities --- lib/validations/attendee.ts | 21 +++++++++++--------- lib/validations/availability.ts | 33 ++++++++++++++++++++----------- pages/api/attendees/[id].ts | 6 +++--- pages/api/attendees/index.ts | 4 ++-- pages/api/availabilities/[id].ts | 9 ++++----- pages/api/availabilities/index.ts | 9 ++++----- pages/api/users/[id].ts | 2 +- 7 files changed, 48 insertions(+), 36 deletions(-) diff --git a/lib/validations/attendee.ts b/lib/validations/attendee.ts index 5b7fee4e7f..e0b91fb309 100644 --- a/lib/validations/attendee.ts +++ b/lib/validations/attendee.ts @@ -10,25 +10,28 @@ export const schemaAttendeeBaseBodyParams = Attendee.pick({ timeZone: true, }).partial(); -export const schemaAttendeePublic = Attendee.omit({}); - -const schemaAttendeeCreateRequiredParams = z.object({ +const schemaAttendeeCreateParams = z.object({ bookingId: z.any(), email: z.string().email(), name: z.string(), timeZone: z.string(), }); -const schemaAttendeeEditeRequiredParams = z.object({ - // bookingId: z.any(), - // @note: disallowing email changes in attendee via API for now. - // email: z.string().email(), +const schemaAttendeeEditParams = z.object({ + // @note: disallowing email/bookingId changes in attendee via API for now as it would introduce side effects name: z.string(), timeZone: z.string(), }); export const schemaAttendeeEditBodyParams = schemaAttendeeBaseBodyParams.merge( - schemaAttendeeEditeRequiredParams + schemaAttendeeEditParams ); export const schemaAttendeeCreateBodyParams = schemaAttendeeBaseBodyParams.merge( - schemaAttendeeCreateRequiredParams + schemaAttendeeCreateParams ); + +export const schemaAttendeeReadPublic = Attendee.pick({ + bookingId: true, + name: true, + email: true, + timeZone: true, +}); diff --git a/lib/validations/availability.ts b/lib/validations/availability.ts index 20e3c61c94..be4691cf0e 100644 --- a/lib/validations/availability.ts +++ b/lib/validations/availability.ts @@ -11,21 +11,32 @@ export const schemaAvailabilityBaseBodyParams = Availability.pick({ days: true, }).partial(); -export const schemaAvailabilityPublic = Availability.omit({}); +export const schemaAvailabilityPublic = Availability.pick({ + startTime: true, + endTime: true, + date: true, + scheduleId: true, + days: true, + userId: true, + eventTypeId: true, +}); -const schemaAvailabilityRequiredParams = z.object({ +const schemaAvailabilityCreateParams = z.object({ + startTime: z.date().or(z.string()), + endTime: z.date().or(z.string()), + days: z.array(z.number()).optional(), + eventTypeId: z.number().optional(), +}); + +const schemaAvailabilityEditParams = z.object({ startTime: z.date().or(z.string()).optional(), endTime: z.date().or(z.string()).optional(), days: z.array(z.number()).optional(), eventTypeId: z.number().optional(), }); - -export const schemaAvailabilityBodyParams = schemaAvailabilityBaseBodyParams.merge( - schemaAvailabilityRequiredParams +export const schemaAvailabilityEditBodyParams = schemaAvailabilityBaseBodyParams.merge( + schemaAvailabilityEditParams +); +export const schemaAvailabilityCreateBodyParams = schemaAvailabilityBaseBodyParams.merge( + schemaAvailabilityCreateParams ); - -export const withValidAvailability = withValidation({ - schema: schemaAvailabilityBodyParams, - type: "Zod", - mode: "body", -}); diff --git a/pages/api/attendees/[id].ts b/pages/api/attendees/[id].ts index 23712e58b6..4d6aecd7f1 100644 --- a/pages/api/attendees/[id].ts +++ b/pages/api/attendees/[id].ts @@ -4,7 +4,7 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { AttendeeResponse } from "@lib/types"; -import { schemaAttendeeEditBodyParams, schemaAttendeePublic } from "@lib/validations/attendee"; +import { schemaAttendeeEditBodyParams, schemaAttendeeReadPublic } from "@lib/validations/attendee"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, @@ -105,7 +105,7 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse schemaAttendeePublic.parse(data)) + .then((data) => schemaAttendeeReadPublic.parse(data)) .then((attendee) => res.status(200).json({ attendee })) .catch((error: Error) => res.status(404).json({ @@ -123,7 +123,7 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse schemaAttendeePublic.parse(data)) + .then((data) => schemaAttendeeReadPublic.parse(data)) .then((attendee) => res.status(200).json({ attendee })) .catch((error: Error) => res.status(404).json({ diff --git a/pages/api/attendees/index.ts b/pages/api/attendees/index.ts index ffab82e919..80afde914a 100644 --- a/pages/api/attendees/index.ts +++ b/pages/api/attendees/index.ts @@ -4,7 +4,7 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import { AttendeeResponse, AttendeesResponse } from "@lib/types"; -import { schemaAttendeeCreateBodyParams, schemaAttendeePublic } from "@lib/validations/attendee"; +import { schemaAttendeeCreateBodyParams, schemaAttendeeReadPublic } from "@lib/validations/attendee"; /** * @swagger @@ -87,7 +87,7 @@ async function createOrlistAllAttendees( booking: { connect: { id: parseInt(bookingId) } }, }, }); - const attendee = schemaAttendeePublic.parse(data); + const attendee = schemaAttendeeReadPublic.parse(data); if (attendee) { res.status(201).json({ diff --git a/pages/api/availabilities/[id].ts b/pages/api/availabilities/[id].ts index cd277b058e..c77574692a 100644 --- a/pages/api/availabilities/[id].ts +++ b/pages/api/availabilities/[id].ts @@ -4,7 +4,7 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { AvailabilityResponse } from "@lib/types"; -import { schemaAvailabilityBodyParams, schemaAvailabilityPublic } from "@lib/validations/availability"; +import { schemaAvailabilityEditBodyParams, schemaAvailabilityReadPublic } from "@lib/validations/availability"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, @@ -93,7 +93,6 @@ import { */ export async function availabilityById(req: NextApiRequest, res: NextApiResponse) { const { method, query, body } = req; - const safeBody = schemaAvailabilityBodyParams.safeParse(body); const safeQuery = schemaQueryIdParseInt.safeParse(query); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); const userId = req.userId; @@ -105,7 +104,7 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse case "GET": await prisma.availability .findUnique({ where: { id: safeQuery.data.id } }) - .then((data) => schemaAvailabilityPublic.parse(data)) + .then((data) => schemaAvailabilityReadPublic.parse(data)) .then((availability) => res.status(200).json({ availability })) .catch((error: Error) => res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error }) @@ -113,7 +112,7 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse break; case "PATCH": - + const safeBody = schemaAvailabilityEditBodyParams.safeParse(body); if (!safeBody.success) throw new Error("Invalid request body"); const userEventTypes = await prisma.eventType.findMany({ where: { userId } }); const userEventTypesIds = userEventTypes.map((event) => event.id); @@ -127,7 +126,7 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse where: { id: safeQuery.data.id }, data: safeBody.data, }) - .then((data) => schemaAvailabilityPublic.parse(data)) + .then((data) => schemaAvailabilityReadPublic.parse(data)) .then((availability) => res.status(200).json({ availability })) .catch((error: Error) => { console.log(error); diff --git a/pages/api/availabilities/index.ts b/pages/api/availabilities/index.ts index ab7aa3b388..999639dc37 100644 --- a/pages/api/availabilities/index.ts +++ b/pages/api/availabilities/index.ts @@ -4,7 +4,7 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import { AvailabilityResponse, AvailabilitiesResponse } from "@lib/types"; -import { schemaAvailabilityBodyParams, schemaAvailabilityPublic } from "@lib/validations/availability"; +import { schemaAvailabilityCreateBodyParams, schemaAvailabilityReadPublic } from "@lib/validations/availability"; /** * @swagger @@ -50,7 +50,7 @@ async function createOrlistAllAvailabilities( if (method === "GET") { const data = await prisma.availability.findMany({ where: { userId } }); - const availabilities = data.map((availability) => schemaAvailabilityPublic.parse(availability)); + const availabilities = data.map((availability) => schemaAvailabilityReadPublic.parse(availability)); if (availabilities) res.status(200).json({ availabilities }); else (error: Error) => @@ -59,13 +59,12 @@ async function createOrlistAllAvailabilities( error, }); } else if (method === "POST") { - console.log(req.body); - const safe = schemaAvailabilityBodyParams.safeParse(req.body); + const safe = schemaAvailabilityCreateBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); const data = await prisma.availability.create({ data: { ...safe.data, userId } }); console.log(data); - const availability = schemaAvailabilityPublic.parse(data); + const availability = schemaAvailabilityReadPublic.parse(data); if (availability) res.status(201).json({ availability, message: "Availability created successfully" }); else diff --git a/pages/api/users/[id].ts b/pages/api/users/[id].ts index 24bf1075ed..ee9279da0a 100644 --- a/pages/api/users/[id].ts +++ b/pages/api/users/[id].ts @@ -8,7 +8,7 @@ import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -import { schemaUserEditBodyParams, schemaUserReadPublic, withValidUser } from "@lib/validations/user"; +import { schemaUserEditBodyParams, schemaUserReadPublic } from "@lib/validations/user"; /** * @swagger