Files
calendar/packages/lib/payment/handlePaymentSuccess.ts
T
Lauris SkraucisandGitHub 4a53ec0b74 feat: platform toggle calendar event creation (#21788)
* feat: add areCalendarEventsEnabled to PlatformOAuthClient table

* feat: areCalendarEvents enabled in handleNewBooking and v2

* feat: v2 handle areCalendarEventsEnabled

* feat: toggle areCalendarEventsEnabled on frontend

* feat: enable areCalendarEventsEnabled for recurring,confirm,paid

* refactor: use placeholder calendar event

* remove merge conflicts

* chore: bump libraries

* chore: bump libraries
2025-06-13 14:15:47 +00:00

109 lines
4.0 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import { sendScheduledEmailsAndSMS } from "@calcom/emails";
import { doesBookingRequireConfirmation } from "@calcom/features/bookings/lib/doesBookingRequireConfirmation";
import { getAllCredentialsIncludeServiceAccountKey } from "@calcom/features/bookings/lib/getAllCredentialsForUsersOnEvent/getAllCredentials";
import { handleBookingRequested } from "@calcom/features/bookings/lib/handleBookingRequested";
import { handleConfirmation } from "@calcom/features/bookings/lib/handleConfirmation";
import { getPlatformParams } from "@calcom/features/platform-oauth-client/get-platform-params";
import { PlatformOAuthClientRepository } from "@calcom/features/platform-oauth-client/platform-oauth-client.repository";
import EventManager, { placeholderCreatedEvent } from "@calcom/lib/EventManager";
import { HttpError as HttpCode } from "@calcom/lib/http-error";
import { getBooking } from "@calcom/lib/payment/getBooking";
import prisma from "@calcom/prisma";
import { BookingStatus } from "@calcom/prisma/enums";
import type { EventTypeMetadata } from "@calcom/prisma/zod-utils";
import { eventTypeAppMetadataOptionalSchema } from "@calcom/prisma/zod-utils";
import logger from "../logger";
const log = logger.getSubLogger({ prefix: ["[handlePaymentSuccess]"] });
export async function handlePaymentSuccess(paymentId: number, bookingId: number) {
log.debug(`handling payment success for bookingId ${bookingId}`);
const { booking, user: userWithCredentials, evt, eventType } = await getBooking(bookingId);
if (booking.location) evt.location = booking.location;
const bookingData: Prisma.BookingUpdateInput = {
paid: true,
status: BookingStatus.ACCEPTED,
};
const allCredentials = await getAllCredentialsIncludeServiceAccountKey(userWithCredentials, {
...booking.eventType,
metadata: booking.eventType?.metadata as EventTypeMetadata,
});
const isConfirmed = booking.status === BookingStatus.ACCEPTED;
const platformOAuthClientRepository = new PlatformOAuthClientRepository();
const platformOAuthClient = userWithCredentials.isPlatformManaged
? await platformOAuthClientRepository.getByUserId(userWithCredentials.id)
: null;
const areCalendarEventsEnabled = platformOAuthClient?.areCalendarEventsEnabled ?? true;
const areEmailsEnabled = platformOAuthClient?.areEmailsEnabled ?? true;
if (isConfirmed) {
const apps = eventTypeAppMetadataOptionalSchema.parse(eventType?.metadata?.apps);
const eventManager = new EventManager({ ...userWithCredentials, credentials: allCredentials }, apps);
const scheduleResult = areCalendarEventsEnabled
? await eventManager.create(evt)
: placeholderCreatedEvent;
bookingData.references = { create: scheduleResult.referencesToCreate };
}
const requiresConfirmation = doesBookingRequireConfirmation({
booking: {
...booking,
eventType,
},
});
if (requiresConfirmation) {
delete bookingData.status;
}
const paymentUpdate = prisma.payment.update({
where: {
id: paymentId,
},
data: {
success: true,
},
});
const bookingUpdate = prisma.booking.update({
where: {
id: booking.id,
},
data: bookingData,
});
await prisma.$transaction([paymentUpdate, bookingUpdate]);
if (!isConfirmed) {
if (!requiresConfirmation) {
await handleConfirmation({
user: { ...userWithCredentials, credentials: allCredentials },
evt,
prisma,
bookingId: booking.id,
booking,
paid: true,
platformClientParams: platformOAuthClient ? getPlatformParams(platformOAuthClient) : undefined,
});
} else {
await handleBookingRequested({
evt,
booking,
});
log.debug(`handling booking request for eventId ${eventType.id}`);
}
} else if (areEmailsEnabled) {
await sendScheduledEmailsAndSMS({ ...evt }, undefined, undefined, undefined, eventType.metadata);
}
throw new HttpCode({
statusCode: 200,
message: `Booking with id '${booking.id}' was paid and confirmed.`,
});
}