cal.pub0.org/packages/app-store/wipemycalother/api/wipe.ts

89 lines
2.3 KiB
TypeScript
Raw Normal View History

feature/app wipe my cal (#2487) * WIP bookings page ui changes, created api endpoint * Ui changes mobile/desktop * Added translations * Fix lib import and common names * WIP reschedule * WIP * Save wip * [WIP] builder and class for CalendarEvent, email for attende * update rescheduled emails, booking view and availability page view * Working version reschedule * Fix for req.user as array * Added missing translation and refactor dialog to self component * Test for reschedule * update on types * Update lib no required * Update type on createBooking * fix types * remove preview stripe sub * remove unused file * remove unused import * Fix reschedule test * Refactor and cleaning up code * Email reschedule title fixes * Adding calendar delete and recreate placeholder of cancelled * Add translation * Removed logs, notes, fixed types * Fixes process.env types * Use strict compare * Fixes type inference * Type fixing is my middle name * Update apps/web/components/booking/BookingListItem.tsx * Update apps/web/components/dialog/RescheduleDialog.tsx * Update packages/core/builders/CalendarEvent/director.ts * Update apps/web/pages/success.tsx * Updates rescheduling labels * Update packages/core/builders/CalendarEvent/builder.ts * Type fixes * Update packages/core/builders/CalendarEvent/builder.ts * Only validating input blocked once * E2E fixes * Stripe tests fixes * Wipe my cal init commit * Fixes circular dependencies * Added conditional display for wipe my cal button * Added placeholder image for app category * Fix type string for conditional validation Co-authored-by: Peer Richelsen <peer@cal.com> Co-authored-by: zomars <zomars@me.com>
2022-04-15 02:24:27 +00:00
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);