* fix: break circular dependency in messageDispatcher via dependency injection Break the 4-file circular dependency chain: credit-service → reminderScheduler → smsReminderManager → messageDispatcher → credit-service Solution: - Add optional creditCheckFn parameter to messageDispatcher functions - Thread creditCheckFn through the call chain: scheduleWorkflowReminders → scheduleSMSReminder/scheduleWhatsappReminder → messageDispatcher - When creditCheckFn is provided, use it; otherwise fall back to dynamic CreditService import for backward compatibility - This breaks the workflows → billing import while preserving immediate fallback behavior Changes: - messageDispatcher: Accept optional creditCheckFn parameter, use it if provided - smsReminderManager: Thread creditCheckFn through scheduleSMSReminder - whatsappReminderManager: Thread creditCheckFn through scheduleWhatsappReminder - reminderScheduler: Add creditCheckFn to ScheduleWorkflowRemindersArgs and pass through processWorkflowStep All type checks, lint checks, and unit tests pass. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * feat: wire creditCheckFn from all callers to complete circular dependency fix - Add creditCheckFn parameter to WorkflowService.scheduleFormWorkflows - Wire creditCheckFn from all 10 entry points that call workflow scheduling: * formSubmissionUtils.ts (form submissions) * roundRobinManualReassignment.ts (round-robin reassignment) * triggerFormSubmittedNoEventWorkflow.ts (form workflow trigger) * handleBookingRequested.ts (booking requests) * RegularBookingService.ts (2 calls - payment initiated & new bookings) * handleSeats.ts (seated bookings) * handleConfirmation.ts (2 calls - confirmation & payment) * handleMarkNoShow.ts (no-show updates) * confirm.handler.ts (booking rejection) - Update test expectations to use expect.objectContaining() - Fix pre-existing lint warning in handleMarkNoShow.ts (any type) - This completes the messageDispatcher circular dependency fix by ensuring creditCheckFn is actually passed through the call chain, breaking the 4-file circular dependency at runtime Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * fix: use generic type with type guard in logFailedResults to fix type check error - Replace constrained type with generic type parameter - Add proper type guard for rejected promises - Fixes CI type check failure in handleMarkNoShow.ts:385 - Avoids 'any' type while accepting any fulfilled value shape Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * wip * wip * wip * revert * revert * feat: wire creditCheckFn from all remaining callers to eliminate fallbacks - Wire creditCheckFn in packages/sms/sms-manager.ts (can safely import CreditService) - Create makeHandler factory pattern for CRON endpoints (scheduleSMSReminders.ts, scheduleWhatsappReminders.ts) - Wire creditCheckFn from apps/web CRON routes to factories - Add warning log in messageDispatcher when fallback is used - Complete creditCheckFn wiring from all direct callers (activateEventType.handler.ts, util.ts) This eliminates all fallbacks to dynamic import except as a safety net for unforeseen call sites. The circular dependency (workflows ↔ billing) remains acceptable as discussed with user (Option C). Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * test: update formSubmissionUtils tests to expect creditCheckFn parameter The scheduleFormWorkflows function now receives creditCheckFn as a parameter. Updated test assertions to use expect.objectContaining() with creditCheckFn: expect.any(Function) to account for the new dependency injection parameter. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * test: update sms-manager test to expect creditCheckFn parameter The sendSmsOrFallbackEmail function now receives creditCheckFn as a parameter. Updated test assertion to use expect.objectContaining() with creditCheckFn: expect.any(Function) to account for the new dependency injection parameter. Also removed teamId: undefined assertion as the key may be omitted entirely from the actual call. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * feat: make creditCheckFn required to fully break circular dependency This commit completes the circular dependency fix by making creditCheckFn required throughout the call chain, eliminating the dynamic import fallback entirely. Changes: - Make creditCheckFn required in messageDispatcher functions (sendSmsOrFallbackEmail, scheduleSmsOrFallbackEmail) - Remove dynamic import fallback and warning log from messageDispatcher - Make creditCheckFn required in ScheduleTextReminderArgs (smsReminderManager) - Make creditCheckFn required in processWorkflowStep and ScheduleWorkflowRemindersArgs (reminderScheduler) - Add creditCheckFn to SendCancelledRemindersArgs and wire from handleCancelBooking The circular dependency is now fully broken - no more dynamic imports of CreditService from within the workflows package. All callers must explicitly provide creditCheckFn via dependency injection. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * fix: make creditCheckFn required in WorkflowService.scheduleFormWorkflows This commit fixes the CI type check error by making creditCheckFn required in WorkflowService.scheduleFormWorkflows. Previously, creditCheckFn was optional in scheduleFormWorkflows but required in scheduleWorkflowReminders, causing a type mismatch. Changes: - Make creditCheckFn required in scheduleFormWorkflows signature - Update WorkflowService.test.ts to pass mock creditCheckFn in all test cases - Add responseId and routedEventTypeId to test calls for completeness All callers of scheduleFormWorkflows already pass creditCheckFn, so this change is safe and completes the circular dependency fix. Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * remove * fix * refactor * refactor * refactor * wip * fix * fix * rm --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
597 lines
19 KiB
TypeScript
597 lines
19 KiB
TypeScript
import { eventTypeAppMetadataOptionalSchema } from "@calcom/app-store/zod-utils";
|
|
import { scheduleMandatoryReminder } from "@calcom/ee/workflows/lib/reminders/scheduleMandatoryReminder";
|
|
import { sendScheduledEmailsAndSMS } from "@calcom/emails/email-manager";
|
|
import type { EventManagerUser } from "@calcom/features/bookings/lib/EventManager";
|
|
import EventManager, { placeholderCreatedEvent } from "@calcom/features/bookings/lib/EventManager";
|
|
import { CreditService } from "@calcom/features/ee/billing/credit-service";
|
|
import { getBookerBaseUrl } from "@calcom/features/ee/organizations/lib/getBookerUrlServer";
|
|
import {
|
|
allowDisablingAttendeeConfirmationEmails,
|
|
allowDisablingHostConfirmationEmails,
|
|
} from "@calcom/features/ee/workflows/lib/allowDisablingStandardEmails";
|
|
import { WorkflowService } from "@calcom/features/ee/workflows/lib/service/WorkflowService";
|
|
import type { Workflow } from "@calcom/features/ee/workflows/lib/types";
|
|
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
|
|
import { scheduleTrigger } from "@calcom/features/webhooks/lib/scheduleTrigger";
|
|
import sendPayload from "@calcom/features/webhooks/lib/sendOrSchedulePayload";
|
|
import type { EventPayloadType, EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload";
|
|
import { getVideoCallUrlFromCalEvent } from "@calcom/lib/CalEventParser";
|
|
import getOrgIdFromMemberOrTeamId from "@calcom/lib/getOrgIdFromMemberOrTeamId";
|
|
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
|
|
import logger from "@calcom/lib/logger";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import type { PrismaClient } from "@calcom/prisma";
|
|
import type { Prisma } from "@calcom/prisma/client";
|
|
import type { SchedulingType } from "@calcom/prisma/enums";
|
|
import { BookingStatus, WebhookTriggerEvents, WorkflowTriggerEvents } from "@calcom/prisma/enums";
|
|
import type { PlatformClientParams } from "@calcom/prisma/zod-utils";
|
|
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
|
|
import { getAllWorkflowsFromEventType } from "@calcom/trpc/server/routers/viewer/workflows/util";
|
|
import type { AdditionalInformation, CalendarEvent } from "@calcom/types/Calendar";
|
|
|
|
import { getCalEventResponses } from "./getCalEventResponses";
|
|
import { scheduleNoShowTriggers } from "./handleNewBooking/scheduleNoShowTriggers";
|
|
|
|
const log = logger.getSubLogger({ prefix: ["[handleConfirmation] book:user"] });
|
|
|
|
export async function handleConfirmation(args: {
|
|
user: EventManagerUser & { username: string | null };
|
|
evt: CalendarEvent;
|
|
recurringEventId?: string;
|
|
prisma: PrismaClient;
|
|
bookingId: number;
|
|
booking: {
|
|
startTime: Date;
|
|
id: number;
|
|
uid: string;
|
|
eventType: {
|
|
currency: string;
|
|
description: string | null;
|
|
id: number;
|
|
length: number;
|
|
price: number;
|
|
requiresConfirmation: boolean;
|
|
metadata?: Prisma.JsonValue;
|
|
title: string;
|
|
team?: {
|
|
parentId: number | null;
|
|
} | null;
|
|
teamId?: number | null;
|
|
parentId?: number | null;
|
|
parent?: {
|
|
teamId: number | null;
|
|
} | null;
|
|
workflows?: {
|
|
workflow: Workflow;
|
|
}[];
|
|
} | null;
|
|
metadata?: Prisma.JsonValue;
|
|
eventTypeId: number | null;
|
|
smsReminderNumber: string | null;
|
|
userId: number | null;
|
|
location: string | null;
|
|
};
|
|
paid?: boolean;
|
|
emailsEnabled?: boolean;
|
|
platformClientParams?: PlatformClientParams;
|
|
}) {
|
|
const {
|
|
user,
|
|
evt,
|
|
recurringEventId,
|
|
prisma,
|
|
bookingId,
|
|
booking,
|
|
paid,
|
|
emailsEnabled = true,
|
|
platformClientParams,
|
|
} = args;
|
|
const eventType = booking.eventType;
|
|
const eventTypeMetadata = EventTypeMetaDataSchema.parse(eventType?.metadata || {});
|
|
const apps = eventTypeAppMetadataOptionalSchema.parse(eventTypeMetadata?.apps);
|
|
const eventManager = new EventManager(user, apps);
|
|
const areCalendarEventsEnabled = platformClientParams?.areCalendarEventsEnabled ?? true;
|
|
const scheduleResult = areCalendarEventsEnabled ? await eventManager.create(evt) : placeholderCreatedEvent;
|
|
const results = scheduleResult.results;
|
|
const metadata: AdditionalInformation = {};
|
|
const workflows = await getAllWorkflowsFromEventType(eventType, booking.userId);
|
|
|
|
if (results.length > 0 && results.every((res) => !res.success)) {
|
|
const error = {
|
|
errorCode: "BookingCreatingMeetingFailed",
|
|
message: "Booking failed",
|
|
};
|
|
|
|
log.error(`Booking ${user.username} failed`, safeStringify({ error, results }));
|
|
} else {
|
|
if (results.length) {
|
|
// TODO: Handle created event metadata more elegantly
|
|
metadata.hangoutLink = results[0].createdEvent?.hangoutLink;
|
|
metadata.conferenceData = results[0].createdEvent?.conferenceData;
|
|
metadata.entryPoints = results[0].createdEvent?.entryPoints;
|
|
}
|
|
try {
|
|
let isHostConfirmationEmailsDisabled = false;
|
|
let isAttendeeConfirmationEmailDisabled = false;
|
|
|
|
if (workflows) {
|
|
isHostConfirmationEmailsDisabled =
|
|
eventTypeMetadata?.disableStandardEmails?.confirmation?.host || false;
|
|
isAttendeeConfirmationEmailDisabled =
|
|
eventTypeMetadata?.disableStandardEmails?.confirmation?.attendee || false;
|
|
|
|
if (isHostConfirmationEmailsDisabled) {
|
|
isHostConfirmationEmailsDisabled = allowDisablingHostConfirmationEmails(workflows);
|
|
}
|
|
|
|
if (isAttendeeConfirmationEmailDisabled) {
|
|
isAttendeeConfirmationEmailDisabled = allowDisablingAttendeeConfirmationEmails(workflows);
|
|
}
|
|
}
|
|
|
|
if (emailsEnabled) {
|
|
await sendScheduledEmailsAndSMS(
|
|
{ ...evt, additionalInformation: metadata },
|
|
undefined,
|
|
isHostConfirmationEmailsDisabled,
|
|
isAttendeeConfirmationEmailDisabled,
|
|
eventTypeMetadata
|
|
);
|
|
}
|
|
} catch (error) {
|
|
log.error(error);
|
|
}
|
|
}
|
|
let updatedBookings: {
|
|
id: number;
|
|
description: string | null;
|
|
location: string | null;
|
|
attendees: {
|
|
name: string;
|
|
email: string;
|
|
phoneNumber?: string | null;
|
|
}[];
|
|
startTime: Date;
|
|
endTime: Date;
|
|
uid: string;
|
|
smsReminderNumber: string | null;
|
|
cancellationReason?: string | null;
|
|
metadata: Prisma.JsonValue | null;
|
|
customInputs: Prisma.JsonValue;
|
|
title: string;
|
|
responses: Prisma.JsonValue;
|
|
eventType: {
|
|
bookingFields: Prisma.JsonValue | null;
|
|
slug: string;
|
|
schedulingType: SchedulingType | null;
|
|
hosts: {
|
|
user: {
|
|
email: string;
|
|
destinationCalendar?: {
|
|
primaryEmail: string | null;
|
|
} | null;
|
|
};
|
|
}[];
|
|
owner: {
|
|
hideBranding?: boolean | null;
|
|
} | null;
|
|
} | null;
|
|
}[] = [];
|
|
|
|
const videoCallUrl = metadata.hangoutLink ? metadata.hangoutLink : evt.videoCallData?.url || "";
|
|
const meetingUrl = getVideoCallUrlFromCalEvent(evt) || videoCallUrl;
|
|
|
|
if (recurringEventId) {
|
|
// The booking to confirm is a recurring event and comes from /booking/recurring, proceeding to mark all related
|
|
// bookings as confirmed. Prisma updateMany does not support relations, so doing this in two steps for now.
|
|
const unconfirmedRecurringBookings = await prisma.booking.findMany({
|
|
where: {
|
|
recurringEventId,
|
|
status: BookingStatus.PENDING,
|
|
},
|
|
});
|
|
|
|
const updateBookingsPromise = unconfirmedRecurringBookings.map((recurringBooking) =>
|
|
prisma.booking.update({
|
|
where: {
|
|
id: recurringBooking.id,
|
|
},
|
|
data: {
|
|
status: BookingStatus.ACCEPTED,
|
|
references: {
|
|
create: scheduleResult.referencesToCreate,
|
|
},
|
|
paid,
|
|
metadata: {
|
|
...(typeof recurringBooking.metadata === "object" ? recurringBooking.metadata : {}),
|
|
videoCallUrl: meetingUrl,
|
|
},
|
|
},
|
|
select: {
|
|
eventType: {
|
|
select: {
|
|
slug: true,
|
|
bookingFields: true,
|
|
schedulingType: true,
|
|
hosts: {
|
|
select: {
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
destinationCalendar: {
|
|
select: {
|
|
primaryEmail: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
owner: {
|
|
select: {
|
|
hideBranding: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
description: true,
|
|
cancellationReason: true,
|
|
attendees: true,
|
|
responses: true,
|
|
location: true,
|
|
title: true,
|
|
uid: true,
|
|
startTime: true,
|
|
metadata: true,
|
|
endTime: true,
|
|
smsReminderNumber: true,
|
|
customInputs: true,
|
|
id: true,
|
|
},
|
|
})
|
|
);
|
|
|
|
const updatedBookingsResult = await Promise.all(updateBookingsPromise);
|
|
updatedBookings = updatedBookings.concat(updatedBookingsResult);
|
|
} else {
|
|
// @NOTE: be careful with this as if any error occurs before this booking doesn't get confirmed
|
|
// Should perform update on booking (confirm) -> then trigger the rest handlers
|
|
const updatedBooking = await prisma.booking.update({
|
|
where: {
|
|
id: bookingId,
|
|
},
|
|
data: {
|
|
status: BookingStatus.ACCEPTED,
|
|
references: {
|
|
create: scheduleResult.referencesToCreate,
|
|
},
|
|
metadata: {
|
|
...(typeof booking.metadata === "object" ? booking.metadata : {}),
|
|
videoCallUrl: meetingUrl,
|
|
},
|
|
},
|
|
select: {
|
|
eventType: {
|
|
select: {
|
|
slug: true,
|
|
bookingFields: true,
|
|
schedulingType: true,
|
|
owner: {
|
|
select: {
|
|
hideBranding: true,
|
|
},
|
|
},
|
|
hosts: {
|
|
select: {
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
destinationCalendar: {
|
|
select: {
|
|
primaryEmail: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
uid: true,
|
|
startTime: true,
|
|
responses: true,
|
|
title: true,
|
|
metadata: true,
|
|
cancellationReason: true,
|
|
endTime: true,
|
|
smsReminderNumber: true,
|
|
description: true,
|
|
attendees: true,
|
|
location: true,
|
|
customInputs: true,
|
|
id: true,
|
|
},
|
|
});
|
|
updatedBookings.push(updatedBooking);
|
|
}
|
|
|
|
const teamId = await getTeamIdFromEventType({
|
|
eventType: {
|
|
team: { id: eventType?.teamId ?? null },
|
|
parentId: eventType?.parentId ?? null,
|
|
},
|
|
});
|
|
|
|
const triggerForUser = !teamId || (teamId && eventType?.parentId);
|
|
|
|
const userId = triggerForUser ? booking.userId : null;
|
|
|
|
const orgId = await getOrgIdFromMemberOrTeamId({ memberId: userId, teamId });
|
|
|
|
const bookerUrl = await getBookerBaseUrl(orgId ?? null);
|
|
|
|
//Workflows - set reminders for confirmed events
|
|
try {
|
|
for (let index = 0; index < updatedBookings.length; index++) {
|
|
const eventTypeSlug = updatedBookings[index].eventType?.slug || "";
|
|
const evtOfBooking = {
|
|
...evt,
|
|
rescheduleReason: updatedBookings[index].cancellationReason || null,
|
|
metadata: { videoCallUrl: meetingUrl },
|
|
eventType: {
|
|
slug: eventTypeSlug,
|
|
schedulingType: updatedBookings[index].eventType?.schedulingType,
|
|
hosts: updatedBookings[index].eventType?.hosts,
|
|
},
|
|
bookerUrl,
|
|
};
|
|
evtOfBooking.startTime = updatedBookings[index].startTime.toISOString();
|
|
evtOfBooking.endTime = updatedBookings[index].endTime.toISOString();
|
|
evtOfBooking.uid = updatedBookings[index].uid;
|
|
const isFirstBooking = index === 0;
|
|
|
|
if (!eventTypeMetadata?.disableStandardEmails?.all?.attendee) {
|
|
await scheduleMandatoryReminder({
|
|
evt: evtOfBooking,
|
|
workflows,
|
|
requiresConfirmation: false,
|
|
hideBranding: !!updatedBookings[index].eventType?.owner?.hideBranding,
|
|
seatReferenceUid: evt.attendeeSeatId,
|
|
isPlatformNoEmail: !emailsEnabled && Boolean(platformClientParams?.platformClientId),
|
|
});
|
|
}
|
|
|
|
const creditService = new CreditService();
|
|
|
|
await WorkflowService.scheduleWorkflowsForNewBooking({
|
|
workflows,
|
|
smsReminderNumber: updatedBookings[index].smsReminderNumber,
|
|
calendarEvent: evtOfBooking,
|
|
hideBranding: !!updatedBookings[index].eventType?.owner?.hideBranding,
|
|
isConfirmedByDefault: true,
|
|
isNormalBookingOrFirstRecurringSlot: isFirstBooking,
|
|
isRescheduleEvent: false,
|
|
creditCheckFn: creditService.hasAvailableCredits.bind(creditService),
|
|
});
|
|
}
|
|
} catch (error) {
|
|
// Silently fail
|
|
console.error(error);
|
|
}
|
|
|
|
try {
|
|
const subscribersBookingCreated = await getWebhooks({
|
|
userId,
|
|
eventTypeId: booking.eventTypeId,
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_CREATED,
|
|
teamId,
|
|
orgId,
|
|
oAuthClientId: platformClientParams?.platformClientId,
|
|
});
|
|
const subscribersMeetingStarted = await getWebhooks({
|
|
userId,
|
|
eventTypeId: booking.eventTypeId,
|
|
triggerEvent: WebhookTriggerEvents.MEETING_STARTED,
|
|
teamId: eventType?.teamId,
|
|
orgId,
|
|
oAuthClientId: platformClientParams?.platformClientId,
|
|
});
|
|
const subscribersMeetingEnded = await getWebhooks({
|
|
userId,
|
|
eventTypeId: booking.eventTypeId,
|
|
triggerEvent: WebhookTriggerEvents.MEETING_ENDED,
|
|
teamId: eventType?.teamId,
|
|
orgId,
|
|
oAuthClientId: platformClientParams?.platformClientId,
|
|
});
|
|
|
|
const scheduleTriggerPromises: Promise<unknown>[] = [];
|
|
|
|
const updatedBookingsWithCalEventResponses = updatedBookings.map((booking) => {
|
|
return {
|
|
...booking,
|
|
...getCalEventResponses({
|
|
bookingFields: booking.eventType?.bookingFields ?? null,
|
|
booking,
|
|
}),
|
|
};
|
|
});
|
|
|
|
subscribersMeetingStarted.forEach((subscriber) => {
|
|
updatedBookingsWithCalEventResponses.forEach((booking) => {
|
|
scheduleTriggerPromises.push(
|
|
scheduleTrigger({
|
|
booking,
|
|
subscriberUrl: subscriber.subscriberUrl,
|
|
subscriber,
|
|
triggerEvent: WebhookTriggerEvents.MEETING_STARTED,
|
|
})
|
|
);
|
|
});
|
|
});
|
|
subscribersMeetingEnded.forEach((subscriber) => {
|
|
updatedBookingsWithCalEventResponses.forEach((booking) => {
|
|
scheduleTriggerPromises.push(
|
|
scheduleTrigger({
|
|
booking,
|
|
subscriberUrl: subscriber.subscriberUrl,
|
|
subscriber,
|
|
triggerEvent: WebhookTriggerEvents.MEETING_ENDED,
|
|
})
|
|
);
|
|
});
|
|
});
|
|
|
|
await Promise.all(scheduleTriggerPromises);
|
|
|
|
await scheduleNoShowTriggers({
|
|
booking: {
|
|
startTime: booking.startTime,
|
|
id: booking.id,
|
|
location: booking.location,
|
|
uid: booking.uid,
|
|
},
|
|
triggerForUser,
|
|
organizerUser: { id: booking.userId },
|
|
eventTypeId: booking.eventTypeId,
|
|
teamId,
|
|
orgId,
|
|
oAuthClientId: platformClientParams?.platformClientId,
|
|
});
|
|
|
|
const eventTypeInfo: EventTypeInfo = {
|
|
eventTitle: eventType?.title,
|
|
eventDescription: eventType?.description,
|
|
requiresConfirmation: eventType?.requiresConfirmation || null,
|
|
price: eventType?.price,
|
|
currency: eventType?.currency,
|
|
length: eventType?.length,
|
|
};
|
|
|
|
const payload: EventPayloadType = {
|
|
...evt,
|
|
...eventTypeInfo,
|
|
bookingId,
|
|
eventTypeId: eventType?.id,
|
|
status: "ACCEPTED",
|
|
smsReminderNumber: booking.smsReminderNumber || undefined,
|
|
metadata: meetingUrl ? { videoCallUrl: meetingUrl } : undefined,
|
|
...(platformClientParams ? platformClientParams : {}),
|
|
};
|
|
|
|
const promises = subscribersBookingCreated.map((sub) =>
|
|
sendPayload(
|
|
sub.secret,
|
|
WebhookTriggerEvents.BOOKING_CREATED,
|
|
new Date().toISOString(),
|
|
sub,
|
|
payload
|
|
).catch((e) => {
|
|
log.error(
|
|
`Error executing webhook for event: ${WebhookTriggerEvents.BOOKING_CREATED}, URL: ${sub.subscriberUrl}, bookingId: ${evt.bookingId}, bookingUid: ${evt.uid}, platformClientId: ${platformClientParams?.platformClientId}`,
|
|
safeStringify(e)
|
|
);
|
|
})
|
|
);
|
|
|
|
await Promise.all(promises);
|
|
|
|
if (paid) {
|
|
let paymentExternalId: string | undefined;
|
|
const subscriberMeetingPaid = await getWebhooks({
|
|
userId,
|
|
eventTypeId: booking.eventTypeId,
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_PAID,
|
|
teamId: eventType?.teamId,
|
|
orgId,
|
|
oAuthClientId: platformClientParams?.platformClientId,
|
|
});
|
|
const bookingWithPayment = await prisma.booking.findUnique({
|
|
where: {
|
|
id: bookingId,
|
|
},
|
|
select: {
|
|
payment: {
|
|
select: {
|
|
id: true,
|
|
success: true,
|
|
externalId: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
const successPayment = bookingWithPayment?.payment?.find((item) => item.success);
|
|
if (successPayment) {
|
|
paymentExternalId = successPayment.externalId;
|
|
}
|
|
|
|
const paymentMetadata = {
|
|
identifier: "cal.com",
|
|
bookingId,
|
|
eventTypeId: eventType?.id,
|
|
bookerEmail: evt.attendees[0].email,
|
|
eventTitle: eventType?.title,
|
|
externalId: paymentExternalId,
|
|
};
|
|
|
|
payload.paymentId = bookingWithPayment?.payment?.[0].id;
|
|
payload.metadata = {
|
|
...(paid ? paymentMetadata : {}),
|
|
};
|
|
|
|
const bookingPaidSubscribers = subscriberMeetingPaid.map((sub) =>
|
|
sendPayload(
|
|
sub.secret,
|
|
WebhookTriggerEvents.BOOKING_PAID,
|
|
new Date().toISOString(),
|
|
sub,
|
|
payload
|
|
).catch((e) => {
|
|
log.error(
|
|
`Error executing webhook for event: ${WebhookTriggerEvents.BOOKING_PAID}, URL: ${sub.subscriberUrl}, bookingId: ${evt.bookingId}, bookingUid: ${evt.uid}`,
|
|
safeStringify(e)
|
|
);
|
|
})
|
|
);
|
|
|
|
// I don't need to await for this
|
|
Promise.all(bookingPaidSubscribers);
|
|
|
|
try {
|
|
const calendarEventForWorkflow = {
|
|
...evt,
|
|
eventType: {
|
|
slug: updatedBookings[0].eventType?.slug || "",
|
|
schedulingType: updatedBookings[0].eventType?.schedulingType,
|
|
hosts:
|
|
updatedBookings[0].eventType?.hosts?.map((host) => ({
|
|
user: {
|
|
email: host.user.email,
|
|
destinationCalendar: host.user.destinationCalendar,
|
|
},
|
|
})) || [],
|
|
},
|
|
bookerUrl: bookerUrl,
|
|
metadata: { videoCallUrl: meetingUrl },
|
|
};
|
|
|
|
const creditService = new CreditService();
|
|
|
|
await WorkflowService.scheduleWorkflowsFilteredByTriggerEvent({
|
|
workflows,
|
|
smsReminderNumber: booking.smsReminderNumber,
|
|
calendarEvent: calendarEventForWorkflow,
|
|
hideBranding: !!updatedBookings[0].eventType?.owner?.hideBranding,
|
|
triggers: [WorkflowTriggerEvents.BOOKING_PAID],
|
|
creditCheckFn: creditService.hasAvailableCredits.bind(creditService),
|
|
});
|
|
} catch (error) {
|
|
log.error("Error while scheduling workflow reminders for booking paid", safeStringify(error));
|
|
}
|
|
}
|
|
} catch (error) {
|
|
// Silently fail
|
|
console.error(error);
|
|
}
|
|
}
|