Fix type errors
parent
00ccb4ffd1
commit
da88beb1f5
|
@ -11,6 +11,14 @@ const schemaScheduleRequiredParams = z.object({
|
|||
|
||||
export const schemaScheduleBodyParams = schemaScheduleBaseBodyParams.merge(schemaScheduleRequiredParams);
|
||||
|
||||
export const schemaSingleScheduleBodyParams = schemaScheduleBaseBodyParams.merge(
|
||||
z.object({ userId: z.number().optional() })
|
||||
);
|
||||
|
||||
export const schemaCreateScheduleBodyParams = schemaScheduleBaseBodyParams.merge(
|
||||
z.object({ userId: z.number().optional(), name: z.string() })
|
||||
);
|
||||
|
||||
export const schemaSchedulePublic = z
|
||||
.object({ id: z.number() })
|
||||
.merge(Schedule)
|
||||
|
|
|
@ -2,7 +2,7 @@ import type { NextApiRequest, NextApiResponse } from "next";
|
|||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import type { ScheduleResponse } from "@lib/types";
|
||||
import { schemaScheduleBodyParams, schemaSchedulePublic } from "@lib/validations/schedule";
|
||||
import { schemaSingleScheduleBodyParams, schemaSchedulePublic } from "@lib/validations/schedule";
|
||||
import {
|
||||
schemaQueryIdParseInt,
|
||||
withValidQueryIdTransformParseInt,
|
||||
|
@ -13,7 +13,11 @@ export async function scheduleById(
|
|||
res: NextApiResponse<ScheduleResponse>
|
||||
) {
|
||||
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
||||
const safeBody = schemaScheduleBodyParams.safeParse(body);
|
||||
const safeBody = schemaSingleScheduleBodyParams.safeParse(body);
|
||||
if (!safeBody.success) {
|
||||
res.status(400).json({ message: "Bad request" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (safeBody.data.userId && !isAdmin) {
|
||||
res.status(401).json({ message: "Unauthorized" });
|
||||
|
|
|
@ -4,15 +4,26 @@ import { getAvailabilityFromSchedule, DEFAULT_SCHEDULE } from "@calcom/lib/avail
|
|||
|
||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||
import { ScheduleResponse, SchedulesResponse } from "@lib/types";
|
||||
import { schemaScheduleBodyParams, schemaSchedulePublic } from "@lib/validations/schedule";
|
||||
import {
|
||||
schemaScheduleBodyParams,
|
||||
schemaSchedulePublic,
|
||||
schemaCreateScheduleBodyParams,
|
||||
} from "@lib/validations/schedule";
|
||||
|
||||
async function createOrlistAllSchedules(
|
||||
{ method, body, userId, isAdmin, prisma }: NextApiRequest,
|
||||
res: NextApiResponse<SchedulesResponse | ScheduleResponse>
|
||||
) {
|
||||
const safeBody = schemaScheduleBodyParams.safeParse(body);
|
||||
const safe = schemaScheduleBodyParams.safeParse(body);
|
||||
|
||||
if (safeBody.data.userId && !isAdmin) {
|
||||
if (!safe.success) {
|
||||
res.status(400).json({ message: "Bad request" });
|
||||
return;
|
||||
}
|
||||
|
||||
const safeBody = safe.data;
|
||||
|
||||
if (safeBody.userId && !isAdmin) {
|
||||
res.status(401).json({ message: "Unauthorized" });
|
||||
return;
|
||||
} else {
|
||||
|
@ -34,14 +45,13 @@ async function createOrlistAllSchedules(
|
|||
* description: No schedules were found
|
||||
*/
|
||||
|
||||
const userIds = Array.isArray(safeBody.data.userId)
|
||||
? safeBody.data.userId
|
||||
: [safeBody.data.userId || userId];
|
||||
const userIds = Array.isArray(safeBody.userId) ? safeBody.userId : [safeBody.userId || userId];
|
||||
|
||||
const data = await prisma.schedule.findMany({
|
||||
where: {
|
||||
userId: { in: userIds },
|
||||
},
|
||||
include: { availability: true },
|
||||
...(Array.isArray(body.userId) && { orderBy: { userId: "asc" } }),
|
||||
});
|
||||
const schedules = data.map((schedule) => schemaSchedulePublic.parse(schedule));
|
||||
|
@ -69,7 +79,7 @@ async function createOrlistAllSchedules(
|
|||
* 401:
|
||||
* description: Authorization information is missing or invalid.
|
||||
*/
|
||||
const safe = schemaScheduleBodyParams.safeParse(body);
|
||||
const safe = schemaCreateScheduleBodyParams.safeParse(body);
|
||||
if (body.userId && !isAdmin) {
|
||||
res.status(401).json({ message: "Unauthorized" });
|
||||
return;
|
||||
|
@ -79,10 +89,11 @@ async function createOrlistAllSchedules(
|
|||
res.status(400).json({ message: "Invalid request body" });
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await prisma.schedule.create({
|
||||
data: {
|
||||
...safe.data,
|
||||
userId: body.userId && isAdmin ? body.userId : userId,
|
||||
userId: safe.data.userId || userId,
|
||||
availability: {
|
||||
createMany: {
|
||||
data: getAvailabilityFromSchedule(DEFAULT_SCHEDULE).map((schedule) => ({
|
||||
|
|
Loading…
Reference in New Issue