89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
|
import { BookingStatus } from "@prisma/client";
|
||
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||
|
import queue from "queue";
|
||
|
import { z, ZodError } from "zod";
|
||
|
|
||
|
import prisma from "@calcom/prisma";
|
||
|
|
||
|
import { Reschedule } from "../lib";
|
||
|
|
||
|
const wipeMyCalendarBodySchema = z.object({
|
||
|
initialDate: z.string(),
|
||
|
endDate: z.string(),
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* /api/integrations/wipemycalother/wipe
|
||
|
* @param req
|
||
|
* @param res
|
||
|
*/
|
||
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||
|
if (!req.session?.user?.id) {
|
||
|
return res.status(401).json({ message: "You must be logged in to do this" });
|
||
|
}
|
||
|
let result = false;
|
||
|
try {
|
||
|
console.log("try");
|
||
|
const { initialDate, endDate } = req.body;
|
||
|
|
||
|
const todayBookings = await prisma.booking.findMany({
|
||
|
where: {
|
||
|
startTime: {
|
||
|
gte: initialDate,
|
||
|
},
|
||
|
endTime: {
|
||
|
lte: endDate,
|
||
|
},
|
||
|
status: {
|
||
|
in: [BookingStatus.ACCEPTED, BookingStatus.PENDING],
|
||
|
},
|
||
|
},
|
||
|
select: {
|
||
|
id: true,
|
||
|
uid: true,
|
||
|
status: true,
|
||
|
},
|
||
|
});
|
||
|
// const [booking] = todayBookings;
|
||
|
const q = queue({ results: [] });
|
||
|
if (todayBookings.length > 0) {
|
||
|
todayBookings.forEach((booking) =>
|
||
|
q.push(() => {
|
||
|
return Reschedule(booking.uid, "Can't do it");
|
||
|
})
|
||
|
);
|
||
|
}
|
||
|
const result = await q.start();
|
||
|
console.log({ result });
|
||
|
// result = !!(await Reschedule(booking.uid, "Can't do it"));
|
||
|
} catch (error: unknown) {
|
||
|
if (error instanceof Error) {
|
||
|
return res.status(500).json({ message: error.message });
|
||
|
}
|
||
|
return res.status(500);
|
||
|
}
|
||
|
return res.status(200).json({ success: result });
|
||
|
};
|
||
|
|
||
|
function validate(
|
||
|
handler: (req: NextApiRequest, res: NextApiResponse) => Promise<any | NextApiResponse | void>
|
||
|
) {
|
||
|
return async (req: NextApiRequest, res: NextApiResponse) => {
|
||
|
if (req.method === "POST") {
|
||
|
try {
|
||
|
wipeMyCalendarBodySchema.parse(req.body);
|
||
|
} catch (error) {
|
||
|
if (error instanceof ZodError && error?.name === "ZodError") {
|
||
|
return res.status(400).json(error?.issues);
|
||
|
}
|
||
|
return res.status(402);
|
||
|
}
|
||
|
} else {
|
||
|
return res.status(405);
|
||
|
}
|
||
|
await handler(req, res);
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export default validate(handler);
|