Skip to content

Commit

Permalink
chore: migrate api/cancel to App Router (#19074)
Browse files Browse the repository at this point in the history
* Add app/api/cancel/route.ts

* remove pages router api/cancel

* refactors needed

* refactor
  • Loading branch information
hbjORbj authored Feb 6, 2025
1 parent c9bfd97 commit e889015
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
34 changes: 34 additions & 0 deletions apps/web/app/api/cancel/route.ts
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 0 additions & 17 deletions apps/web/pages/api/cancel.ts

This file was deleted.

10 changes: 6 additions & 4 deletions packages/features/bookings/lib/handleCancelBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -568,7 +570,7 @@ async function handler(req: CustomRequest) {
await handleInternalNote({
internalNote,
booking: bookingToDelete,
userId,
userId: userId || -1,
teamId: teamId,
});
}
Expand All @@ -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.",
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit e889015

Please sign in to comment.