Merge pull request #173 from calcom/fix/create-schedule-availability
Fix/create schedule availabilitypull/9078/head
commit
cc01e47d58
|
@ -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 {};
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue