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 type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
|
||||||
|
import safeParseJSON from "@lib/helpers/safeParseJSON";
|
||||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||||
import type { ScheduleResponse } from "@lib/types";
|
import type { ScheduleResponse } from "@lib/types";
|
||||||
import { schemaSingleScheduleBodyParams, schemaSchedulePublic } from "@lib/validations/schedule";
|
import { schemaSingleScheduleBodyParams, schemaSchedulePublic } from "@lib/validations/schedule";
|
||||||
|
@ -12,13 +13,18 @@ export async function scheduleById(
|
||||||
{ method, query, body, userId, isAdmin, prisma }: NextApiRequest,
|
{ method, query, body, userId, isAdmin, prisma }: NextApiRequest,
|
||||||
res: NextApiResponse<ScheduleResponse>
|
res: NextApiResponse<ScheduleResponse>
|
||||||
) {
|
) {
|
||||||
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
body = safeParseJSON(body);
|
||||||
const safeBody = schemaSingleScheduleBodyParams.safeParse(body);
|
if (!body.success) {
|
||||||
|
res.status(400).json({ message: body.message });
|
||||||
|
}
|
||||||
|
|
||||||
|
const safe = schemaScheduleBodyParams.safeParse(body);
|
||||||
if (!safeBody.success) {
|
if (!safeBody.success) {
|
||||||
res.status(400).json({ message: "Bad request" });
|
res.status(400).json({ message: "Bad request" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
||||||
if (safeBody.data.userId && !isAdmin) {
|
if (safeBody.data.userId && !isAdmin) {
|
||||||
res.status(401).json({ message: "Unauthorized" });
|
res.status(401).json({ message: "Unauthorized" });
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
|
||||||
import { getAvailabilityFromSchedule, DEFAULT_SCHEDULE } from "@calcom/lib/availability";
|
import { getAvailabilityFromSchedule, DEFAULT_SCHEDULE } from "@calcom/lib/availability";
|
||||||
|
|
||||||
|
import safeParseJSON from "@lib/helpers/safeParseJSON";
|
||||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||||
import { ScheduleResponse, SchedulesResponse } from "@lib/types";
|
import { ScheduleResponse, SchedulesResponse } from "@lib/types";
|
||||||
import {
|
import {
|
||||||
|
@ -14,6 +15,11 @@ async function createOrlistAllSchedules(
|
||||||
{ method, body, userId, isAdmin, prisma }: NextApiRequest,
|
{ method, body, userId, isAdmin, prisma }: NextApiRequest,
|
||||||
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
|
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
|
||||||
) {
|
) {
|
||||||
|
body = safeParseJSON(body);
|
||||||
|
if (!body.success) {
|
||||||
|
res.status(400).json({ message: body.message });
|
||||||
|
}
|
||||||
|
|
||||||
const safe = schemaScheduleBodyParams.safeParse(body);
|
const safe = schemaScheduleBodyParams.safeParse(body);
|
||||||
|
|
||||||
if (!safe.success) {
|
if (!safe.success) {
|
||||||
|
|
Loading…
Reference in New Issue