chore: migrate api/cancel to App Router (#19074)
* Add app/api/cancel/route.ts * remove pages router api/cancel * refactors needed * refactor
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { cookies, headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import type { NextRequest } from "next/server";
|
||||
|
||||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
||||
import handleCancelBooking from "@calcom/features/bookings/lib/handleCancelBooking";
|
||||
|
||||
import { buildLegacyRequest } from "@lib/buildLegacyCtx";
|
||||
|
||||
async function handler(req: NextRequest) {
|
||||
let appDirRequestBody;
|
||||
try {
|
||||
appDirRequestBody = await req.json();
|
||||
} catch (error) {
|
||||
return NextResponse.json({ success: false, message: "Invalid JSON" }, { status: 400 });
|
||||
}
|
||||
const session = await getServerSession({ req: buildLegacyRequest(headers(), cookies()) });
|
||||
const result = await handleCancelBooking({
|
||||
appDirRequestBody,
|
||||
userId: session?.user?.id || -1,
|
||||
});
|
||||
|
||||
const statusCode = result.success ? 200 : 400;
|
||||
|
||||
return NextResponse.json(result, { status: statusCode });
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest) {
|
||||
return handler(req);
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
return handler(req);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
import type { NextApiRequest } from "next";
|
||||
|
||||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
||||
import handleCancelBooking from "@calcom/features/bookings/lib/handleCancelBooking";
|
||||
import { defaultResponder, defaultHandler } from "@calcom/lib/server";
|
||||
|
||||
async function handler(req: NextApiRequest & { userId?: number }) {
|
||||
const session = await getServerSession({ req });
|
||||
/* To mimic API behavior and comply with types */
|
||||
req.userId = session?.user?.id || -1;
|
||||
return await handleCancelBooking(req);
|
||||
}
|
||||
|
||||
export default defaultHandler({
|
||||
DELETE: Promise.resolve({ default: defaultResponder(handler) }),
|
||||
POST: Promise.resolve({ default: defaultResponder(handler) }),
|
||||
});
|
||||
@@ -140,7 +140,8 @@ async function getBookingToDelete(id: number | undefined, uid: string | undefine
|
||||
|
||||
export type BookingToDelete = Awaited<ReturnType<typeof getBookingToDelete>>;
|
||||
|
||||
export type CustomRequest = NextApiRequest & {
|
||||
export type AppRouterRequest = { appDirRequestBody: unknown };
|
||||
export type CustomRequest = (NextApiRequest | AppRouterRequest) & {
|
||||
userId?: number;
|
||||
bookingToDelete?: BookingToDelete;
|
||||
platformClientId?: string;
|
||||
@@ -159,6 +160,7 @@ export type HandleCancelBookingResponse = {
|
||||
};
|
||||
|
||||
async function handler(req: CustomRequest) {
|
||||
const body = (req as AppRouterRequest).appDirRequestBody ?? (req as NextApiRequest).body;
|
||||
const {
|
||||
id,
|
||||
uid,
|
||||
@@ -168,7 +170,7 @@ async function handler(req: CustomRequest) {
|
||||
cancelledBy,
|
||||
cancelSubsequentBookings,
|
||||
internalNote,
|
||||
} = bookingCancelInput.parse(req.body);
|
||||
} = bookingCancelInput.parse(body);
|
||||
req.bookingToDelete = await getBookingToDelete(id, uid);
|
||||
const {
|
||||
bookingToDelete,
|
||||
@@ -568,7 +570,7 @@ async function handler(req: CustomRequest) {
|
||||
await handleInternalNote({
|
||||
internalNote,
|
||||
booking: bookingToDelete,
|
||||
userId,
|
||||
userId: userId || -1,
|
||||
teamId: teamId,
|
||||
});
|
||||
}
|
||||
@@ -586,7 +588,7 @@ async function handler(req: CustomRequest) {
|
||||
} catch (error) {
|
||||
console.error("Error deleting event", error);
|
||||
}
|
||||
req.statusCode = 200;
|
||||
(req as NextApiRequest).statusCode = 200;
|
||||
return {
|
||||
success: true,
|
||||
message: "Booking successfully cancelled.",
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { NextApiRequest } from "next";
|
||||
|
||||
import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
|
||||
import { updateMeeting } from "@calcom/core/videoClient";
|
||||
import { sendCancelledSeatEmailsAndSMS } from "@calcom/emails";
|
||||
@@ -15,7 +17,7 @@ import { bookingCancelAttendeeSeatSchema } from "@calcom/prisma/zod-utils";
|
||||
import type { EventTypeMetadata } from "@calcom/prisma/zod-utils";
|
||||
import type { CalendarEvent } from "@calcom/types/Calendar";
|
||||
|
||||
import type { CustomRequest } from "../../handleCancelBooking";
|
||||
import type { AppRouterRequest, CustomRequest } from "../../handleCancelBooking";
|
||||
|
||||
async function cancelAttendeeSeat(
|
||||
req: CustomRequest,
|
||||
@@ -32,7 +34,8 @@ async function cancelAttendeeSeat(
|
||||
},
|
||||
eventTypeMetadata: EventTypeMetadata
|
||||
) {
|
||||
const input = bookingCancelAttendeeSeatSchema.safeParse(req.body);
|
||||
const body = (req as AppRouterRequest).appDirRequestBody ?? (req as NextApiRequest).body;
|
||||
const input = bookingCancelAttendeeSeatSchema.safeParse(body);
|
||||
const { webhooks, evt, eventTypeInfo } = dataForWebhooks;
|
||||
if (!input.success) return;
|
||||
const { seatReferenceUid } = input.data;
|
||||
@@ -61,7 +64,7 @@ async function cancelAttendeeSeat(
|
||||
},
|
||||
}),
|
||||
]);
|
||||
req.statusCode = 200;
|
||||
(req as NextApiRequest).statusCode = 200;
|
||||
|
||||
const attendee = bookingToDelete?.attendees.find((attendee) => attendee.id === seatReference.attendeeId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user