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