fix: moves to pick and separates availabilities

pull/9078/head
Agusti Fernandez Pardo 2022-04-26 22:23:33 +02:00
parent 3759eccfca
commit 6997606a4c
7 changed files with 48 additions and 36 deletions

View File

@ -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,
});

View File

@ -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",
});

View File

@ -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<Att
case "GET":
await prisma.attendee
.findUnique({ where: { id: safeQuery.data.id } })
.then((data) => 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<Att
}
await prisma.attendee
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
.then((data) => schemaAttendeePublic.parse(data))
.then((data) => schemaAttendeeReadPublic.parse(data))
.then((attendee) => res.status(200).json({ attendee }))
.catch((error: Error) =>
res.status(404).json({

View File

@ -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({

View File

@ -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<AvailabilityResponse>) {
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);

View File

@ -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

View File

@ -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