fix: availability patch

pull/9078/head
Agusti Fernandez Pardo 2022-04-25 06:13:27 +02:00
parent b6bce18b29
commit ad04be9394
2 changed files with 6 additions and 5 deletions

View File

@ -14,8 +14,8 @@ export const schemaAvailabilityBaseBodyParams = Availability.pick({
export const schemaAvailabilityPublic = Availability.omit({}); export const schemaAvailabilityPublic = Availability.omit({});
const schemaAvailabilityRequiredParams = z.object({ const schemaAvailabilityRequiredParams = z.object({
startTime: z.date(), startTime: z.date().or(z.string()).optional(),
endTime: z.date(), endTime: z.date().or(z.string()).optional(),
days: z.array(z.number()).optional(), days: z.array(z.number()).optional(),
eventTypeId: z.number().optional(), eventTypeId: z.number().optional(),
}); });

View File

@ -93,6 +93,7 @@ import {
*/ */
export async function availabilityById(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) { export async function availabilityById(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) {
const { method, query, body } = req; const { method, query, body } = req;
const safeBody = schemaAvailabilityBodyParams.safeParse(body);
const safeQuery = schemaQueryIdParseInt.safeParse(query); const safeQuery = schemaQueryIdParseInt.safeParse(query);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = req.userId; const userId = req.userId;
@ -112,14 +113,14 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse
break; break;
case "PATCH": case "PATCH":
const safeBody = schemaAvailabilityBodyParams.safeParse(body);
if (!safeBody.success) throw new Error("Invalid request body"); if (!safeBody.success) throw new Error("Invalid request body");
const userEventTypes = await prisma.eventType.findMany({ where: { userId } }); const userEventTypes = await prisma.eventType.findMany({ where: { userId } });
const userEventTypesIds = userEventTypes.map((event) => event.id); const userEventTypesIds = userEventTypes.map((event) => event.id);
if (safeBody.data.eventTypeId && !userEventTypesIds.includes(safeBody.data.eventTypeId)) { if (safeBody.data.eventTypeId && !userEventTypesIds.includes(safeBody.data.eventTypeId)) {
res.status(401).json({ message: "Bad request. You're not the owner of eventTypeId" }); res.status(401).json({
// throw new Error("Bad request. You're not the owner of eventTypeId"); message: `Bad request. You're not the owner of eventTypeId: ${safeBody.data.eventTypeId}`,
});
} }
await prisma.availability await prisma.availability
.update({ .update({