2022-04-15 02:24:27 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import queue from "queue";
|
|
|
|
import { z, ZodError } from "zod";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { BookingStatus } from "@calcom/prisma/enums";
|
2022-04-15 02:24:27 +00:00
|
|
|
|
|
|
|
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" });
|
|
|
|
}
|
2022-06-10 19:16:10 +00:00
|
|
|
|
2022-04-15 02:24:27 +00:00
|
|
|
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],
|
|
|
|
},
|
2022-04-15 10:09:40 +00:00
|
|
|
userId: req.session.user.id,
|
2022-04-15 02:24:27 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
uid: true,
|
|
|
|
status: true,
|
|
|
|
},
|
|
|
|
});
|
2022-04-15 10:09:40 +00:00
|
|
|
|
2022-04-15 02:24:27 +00:00
|
|
|
const q = queue({ results: [] });
|
|
|
|
if (todayBookings.length > 0) {
|
|
|
|
todayBookings.forEach((booking) =>
|
|
|
|
q.push(() => {
|
2022-04-15 10:09:40 +00:00
|
|
|
return Reschedule(booking.uid, "");
|
2022-04-15 02:24:27 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2022-04-15 10:09:40 +00:00
|
|
|
await q.start();
|
2022-04-15 02:24:27 +00:00
|
|
|
} catch (error: unknown) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
return res.status(500).json({ message: error.message });
|
|
|
|
}
|
|
|
|
return res.status(500);
|
|
|
|
}
|
2022-04-15 10:09:40 +00:00
|
|
|
return res.status(200).json({ success: true });
|
2022-04-15 02:24:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|