Merge pull request #173 from calcom/fix/create-schedule-availability

Fix/create schedule availability
pull/9078/head
Syed Ali Shahbaz 2022-10-07 09:46:07 +02:00 committed by GitHub
commit cc01e47d58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 3 deletions

View File

@ -0,0 +1,14 @@
export default function parseJSONSafely(str: string) {
try {
return JSON.parse(str);
} catch (e) {
console.error((e as Error).message);
if ((e as Error).message.includes("Unexpected token")) {
return {
success: false,
message: `Invalid JSON in the body: ${(e as Error).message}`,
};
}
return {};
}
}

View File

@ -1,5 +1,6 @@
import type { NextApiRequest, NextApiResponse } from "next";
import safeParseJSON from "@lib/helpers/safeParseJSON";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { ScheduleResponse } from "@lib/types";
import { schemaSingleScheduleBodyParams, schemaSchedulePublic } from "@lib/validations/schedule";
@ -12,13 +13,18 @@ export async function scheduleById(
{ method, query, body, userId, isAdmin, prisma }: NextApiRequest,
res: NextApiResponse<ScheduleResponse>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
const safeBody = schemaSingleScheduleBodyParams.safeParse(body);
body = safeParseJSON(body);
if (!body.success) {
res.status(400).json({ message: body.message });
}
const safe = schemaScheduleBodyParams.safeParse(body);
if (!safeBody.success) {
res.status(400).json({ message: "Bad request" });
return;
}
const safeQuery = schemaQueryIdParseInt.safeParse(query);
if (safeBody.data.userId && !isAdmin) {
res.status(401).json({ message: "Unauthorized" });
return;

View File

@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
import { getAvailabilityFromSchedule, DEFAULT_SCHEDULE } from "@calcom/lib/availability";
import safeParseJSON from "@lib/helpers/safeParseJSON";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { ScheduleResponse, SchedulesResponse } from "@lib/types";
import {
@ -14,6 +15,11 @@ async function createOrlistAllSchedules(
{ method, body, userId, isAdmin, prisma }: NextApiRequest,
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
) {
body = safeParseJSON(body);
if (!body.success) {
res.status(400).json({ message: body.message });
}
const safe = schemaScheduleBodyParams.safeParse(body);
if (!safe.success) {