fix availabilites validations

This commit is contained in:
Agusti Fernandez Pardo committed 2022-04-25 03:12:33 +02:00
1 parent b38f78bbad
commit 804fe27458
3 files changed
+23 -11

No files matched your search

+9 -2
View File
@@ -94,7 +94,6 @@ import {
export async function availabilityById(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) {
const { method, query, body } = req;
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaAvailabilityBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
const userId = req.userId;
const data = await prisma.availability.findMany({ where: { userId } });
@@ -113,8 +112,16 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse
break;
case "PATCH":
const safeBody = schemaAvailabilityBodyParams.safeParse(body);
if (!safeBody.success) throw new Error("Invalid request body");
const edited = await prisma.availability
const userEventTypes = await prisma.eventType.findMany({ where: { userId } });
const userEventTypesIds = userEventTypes.map((event) => event.id);
if (safeBody.data.eventTypeId && !userEventTypesIds.includes(safeBody.data.eventTypeId)) {
res.status(401).json({ message: "Bad request. You're not the owner of eventTypeId" });
// throw new Error("Bad request. You're not the owner of eventTypeId");
}
await prisma.availability
.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
+3 -5
View File
@@ -39,12 +39,10 @@ import { schemaUserEditBodyParams, schemaUserReadPublic, withValidUser } from "@
* - application/json
* parameters:
* - in: body
* name: user
* description: The user to edit
* name: name
* description: The users full name
* schema:
* type: object
* $ref: '#/components/schemas/User'
* required: true
* type: string
* - in: path
* name: id
* schema: