2023-04-18 10:08:09 +00:00
|
|
|
import type { Prisma, PrismaClient, Workflow, WorkflowsOnEventTypes, WorkflowStep } from "@prisma/client";
|
2023-02-25 03:57:49 +00:00
|
|
|
|
|
|
|
import { scheduleTrigger } from "@calcom/app-store/zapier/lib/nodeScheduler";
|
|
|
|
import type { EventManagerUser } from "@calcom/core/EventManager";
|
|
|
|
import EventManager from "@calcom/core/EventManager";
|
|
|
|
import { sendScheduledEmails } from "@calcom/emails";
|
2023-08-10 18:52:36 +00:00
|
|
|
import { isEventTypeOwnerKYCVerified } from "@calcom/features/ee/workflows/lib/isEventTypeOwnerKYCVerified";
|
2023-02-25 03:57:49 +00:00
|
|
|
import { scheduleWorkflowReminders } from "@calcom/features/ee/workflows/lib/reminders/reminderScheduler";
|
|
|
|
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
|
|
|
|
import type { EventTypeInfo } from "@calcom/features/webhooks/lib/sendPayload";
|
|
|
|
import sendPayload from "@calcom/features/webhooks/lib/sendPayload";
|
2023-07-25 17:05:02 +00:00
|
|
|
import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType";
|
2023-02-25 03:57:49 +00:00
|
|
|
import logger from "@calcom/lib/logger";
|
2023-05-02 11:44:05 +00:00
|
|
|
import { BookingStatus, WebhookTriggerEvents } from "@calcom/prisma/enums";
|
2023-04-18 10:08:09 +00:00
|
|
|
import { bookingMetadataSchema } from "@calcom/prisma/zod-utils";
|
2023-02-25 03:57:49 +00:00
|
|
|
import type { AdditionalInformation, CalendarEvent } from "@calcom/types/Calendar";
|
|
|
|
|
|
|
|
const log = logger.getChildLogger({ prefix: ["[handleConfirmation] book:user"] });
|
|
|
|
|
|
|
|
export async function handleConfirmation(args: {
|
|
|
|
user: EventManagerUser & { username: string | null };
|
|
|
|
evt: CalendarEvent;
|
|
|
|
recurringEventId?: string;
|
|
|
|
prisma: PrismaClient;
|
|
|
|
bookingId: number;
|
|
|
|
booking: {
|
|
|
|
eventType: {
|
|
|
|
currency: string;
|
|
|
|
description: string | null;
|
|
|
|
id: number;
|
|
|
|
length: number;
|
|
|
|
price: number;
|
|
|
|
requiresConfirmation: boolean;
|
|
|
|
title: string;
|
2023-05-23 01:15:29 +00:00
|
|
|
teamId?: number | null;
|
2023-07-25 17:05:02 +00:00
|
|
|
parentId?: number | null;
|
2023-02-25 03:57:49 +00:00
|
|
|
} | null;
|
|
|
|
eventTypeId: number | null;
|
|
|
|
smsReminderNumber: string | null;
|
|
|
|
userId: number | null;
|
|
|
|
};
|
|
|
|
paid?: boolean;
|
|
|
|
}) {
|
|
|
|
const { user, evt, recurringEventId, prisma, bookingId, booking, paid } = args;
|
|
|
|
const eventManager = new EventManager(user);
|
|
|
|
const scheduleResult = await eventManager.create(evt);
|
|
|
|
const results = scheduleResult.results;
|
|
|
|
|
|
|
|
if (results.length > 0 && results.every((res) => !res.success)) {
|
|
|
|
const error = {
|
|
|
|
errorCode: "BookingCreatingMeetingFailed",
|
|
|
|
message: "Booking failed",
|
|
|
|
};
|
|
|
|
|
|
|
|
log.error(`Booking ${user.username} failed`, error, results);
|
|
|
|
} else {
|
|
|
|
const metadata: AdditionalInformation = {};
|
|
|
|
|
|
|
|
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 {
|
|
|
|
await sendScheduledEmails({ ...evt, additionalInformation: metadata });
|
|
|
|
} catch (error) {
|
|
|
|
log.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let updatedBookings: {
|
|
|
|
scheduledJobs: string[];
|
|
|
|
id: number;
|
2023-04-18 10:08:09 +00:00
|
|
|
description: string | null;
|
|
|
|
location: string | null;
|
|
|
|
attendees: {
|
|
|
|
name: string;
|
|
|
|
email: string;
|
|
|
|
}[];
|
2023-02-25 03:57:49 +00:00
|
|
|
startTime: Date;
|
|
|
|
endTime: Date;
|
|
|
|
uid: string;
|
|
|
|
smsReminderNumber: string | null;
|
2023-04-18 10:08:09 +00:00
|
|
|
metadata: Prisma.JsonValue | null;
|
|
|
|
customInputs: Prisma.JsonValue;
|
2023-02-25 03:57:49 +00:00
|
|
|
eventType: {
|
2023-04-18 10:08:09 +00:00
|
|
|
bookingFields: Prisma.JsonValue | null;
|
|
|
|
slug: string;
|
2023-08-10 18:52:36 +00:00
|
|
|
team: {
|
|
|
|
metadata: Prisma.JsonValue;
|
|
|
|
} | null;
|
2023-04-18 10:08:09 +00:00
|
|
|
owner: {
|
|
|
|
hideBranding?: boolean | null;
|
2023-08-10 18:52:36 +00:00
|
|
|
metadata: Prisma.JsonValue;
|
|
|
|
teams: {
|
|
|
|
accepted: boolean;
|
|
|
|
team: {
|
|
|
|
metadata: Prisma.JsonValue;
|
|
|
|
};
|
|
|
|
}[];
|
2023-04-18 10:08:09 +00:00
|
|
|
} | null;
|
2023-02-25 03:57:49 +00:00
|
|
|
workflows: (WorkflowsOnEventTypes & {
|
|
|
|
workflow: Workflow & {
|
|
|
|
steps: WorkflowStep[];
|
|
|
|
};
|
|
|
|
})[];
|
|
|
|
} | null;
|
|
|
|
}[] = [];
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
eventType: {
|
|
|
|
select: {
|
2023-04-18 10:08:09 +00:00
|
|
|
slug: true,
|
|
|
|
bookingFields: true,
|
2023-08-10 18:52:36 +00:00
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
metadata: true,
|
|
|
|
},
|
|
|
|
},
|
2023-04-18 10:08:09 +00:00
|
|
|
owner: {
|
|
|
|
select: {
|
|
|
|
hideBranding: true,
|
2023-08-10 18:52:36 +00:00
|
|
|
metadata: true,
|
|
|
|
teams: {
|
|
|
|
select: {
|
|
|
|
accepted: true,
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
metadata: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-04-18 10:08:09 +00:00
|
|
|
},
|
|
|
|
},
|
2023-02-25 03:57:49 +00:00
|
|
|
workflows: {
|
|
|
|
include: {
|
|
|
|
workflow: {
|
|
|
|
include: {
|
|
|
|
steps: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-04-18 10:08:09 +00:00
|
|
|
description: true,
|
|
|
|
attendees: true,
|
|
|
|
location: true,
|
2023-02-25 03:57:49 +00:00
|
|
|
uid: true,
|
|
|
|
startTime: true,
|
2023-04-18 10:08:09 +00:00
|
|
|
metadata: true,
|
2023-02-25 03:57:49 +00:00
|
|
|
endTime: true,
|
|
|
|
smsReminderNumber: true,
|
2023-04-18 10:08:09 +00:00
|
|
|
customInputs: true,
|
2023-02-25 03:57:49 +00:00
|
|
|
id: true,
|
|
|
|
scheduledJobs: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2023-04-18 10:08:09 +00:00
|
|
|
|
2023-02-25 03:57:49 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
eventType: {
|
|
|
|
select: {
|
2023-04-18 10:08:09 +00:00
|
|
|
slug: true,
|
|
|
|
bookingFields: true,
|
2023-08-10 18:52:36 +00:00
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
metadata: true,
|
|
|
|
},
|
|
|
|
},
|
2023-04-18 10:08:09 +00:00
|
|
|
owner: {
|
|
|
|
select: {
|
|
|
|
hideBranding: true,
|
2023-08-10 18:52:36 +00:00
|
|
|
metadata: true,
|
|
|
|
teams: {
|
|
|
|
select: {
|
|
|
|
accepted: true,
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
metadata: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-04-18 10:08:09 +00:00
|
|
|
},
|
|
|
|
},
|
2023-02-25 03:57:49 +00:00
|
|
|
workflows: {
|
|
|
|
include: {
|
|
|
|
workflow: {
|
|
|
|
include: {
|
|
|
|
steps: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
uid: true,
|
|
|
|
startTime: true,
|
2023-04-18 10:08:09 +00:00
|
|
|
metadata: true,
|
2023-02-25 03:57:49 +00:00
|
|
|
endTime: true,
|
|
|
|
smsReminderNumber: true,
|
2023-04-18 10:08:09 +00:00
|
|
|
description: true,
|
|
|
|
attendees: true,
|
|
|
|
location: true,
|
|
|
|
customInputs: true,
|
2023-02-25 03:57:49 +00:00
|
|
|
id: true,
|
|
|
|
scheduledJobs: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
updatedBookings.push(updatedBooking);
|
|
|
|
}
|
|
|
|
|
2023-08-10 18:52:36 +00:00
|
|
|
const isKYCVerified = isEventTypeOwnerKYCVerified(updatedBookings[0].eventType);
|
|
|
|
|
2023-02-25 03:57:49 +00:00
|
|
|
//Workflows - set reminders for confirmed events
|
|
|
|
try {
|
|
|
|
for (let index = 0; index < updatedBookings.length; index++) {
|
|
|
|
if (updatedBookings[index].eventType?.workflows) {
|
|
|
|
const evtOfBooking = evt;
|
|
|
|
evtOfBooking.startTime = updatedBookings[index].startTime.toISOString();
|
|
|
|
evtOfBooking.endTime = updatedBookings[index].endTime.toISOString();
|
|
|
|
evtOfBooking.uid = updatedBookings[index].uid;
|
2023-04-18 10:08:09 +00:00
|
|
|
const eventTypeSlug = updatedBookings[index].eventType?.slug || "";
|
2023-02-25 03:57:49 +00:00
|
|
|
|
|
|
|
const isFirstBooking = index === 0;
|
2023-04-18 10:08:09 +00:00
|
|
|
const videoCallUrl =
|
|
|
|
bookingMetadataSchema.parse(updatedBookings[index].metadata || {})?.videoCallUrl || "";
|
2023-02-25 03:57:49 +00:00
|
|
|
|
2023-04-13 19:03:08 +00:00
|
|
|
await scheduleWorkflowReminders({
|
|
|
|
workflows: updatedBookings[index]?.eventType?.workflows || [],
|
|
|
|
smsReminderNumber: updatedBookings[index].smsReminderNumber,
|
2023-04-18 10:08:09 +00:00
|
|
|
calendarEvent: {
|
|
|
|
...evtOfBooking,
|
|
|
|
...{ metadata: { videoCallUrl }, eventType: { slug: eventTypeSlug } },
|
|
|
|
},
|
2023-04-13 19:03:08 +00:00
|
|
|
isFirstRecurringEvent: isFirstBooking,
|
2023-04-18 10:08:09 +00:00
|
|
|
hideBranding: !!updatedBookings[index].eventType?.owner?.hideBranding,
|
2023-08-10 18:52:36 +00:00
|
|
|
eventTypeRequiresConfirmation: true,
|
|
|
|
isKYCVerified,
|
2023-04-13 19:03:08 +00:00
|
|
|
});
|
2023-02-25 03:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Silently fail
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-07-25 17:05:02 +00:00
|
|
|
const teamId = await getTeamIdFromEventType({
|
|
|
|
eventType: {
|
|
|
|
team: { id: booking.eventType?.teamId ?? null },
|
|
|
|
parentId: booking?.eventType?.parentId ?? null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-02-25 03:57:49 +00:00
|
|
|
const subscribersBookingCreated = await getWebhooks({
|
2023-05-23 01:15:29 +00:00
|
|
|
userId: booking.userId,
|
|
|
|
eventTypeId: booking.eventTypeId,
|
2023-02-25 03:57:49 +00:00
|
|
|
triggerEvent: WebhookTriggerEvents.BOOKING_CREATED,
|
2023-07-25 17:05:02 +00:00
|
|
|
teamId,
|
2023-02-25 03:57:49 +00:00
|
|
|
});
|
|
|
|
const subscribersMeetingEnded = await getWebhooks({
|
2023-05-23 01:15:29 +00:00
|
|
|
userId: booking.userId,
|
|
|
|
eventTypeId: booking.eventTypeId,
|
2023-02-25 03:57:49 +00:00
|
|
|
triggerEvent: WebhookTriggerEvents.MEETING_ENDED,
|
2023-05-23 01:15:29 +00:00
|
|
|
teamId: booking.eventType?.teamId,
|
2023-02-25 03:57:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
subscribersMeetingEnded.forEach((subscriber) => {
|
|
|
|
updatedBookings.forEach((booking) => {
|
|
|
|
scheduleTrigger(booking, subscriber.subscriberUrl, subscriber);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const eventTypeInfo: EventTypeInfo = {
|
|
|
|
eventTitle: booking.eventType?.title,
|
|
|
|
eventDescription: booking.eventType?.description,
|
|
|
|
requiresConfirmation: booking.eventType?.requiresConfirmation || null,
|
|
|
|
price: booking.eventType?.price,
|
|
|
|
currency: booking.eventType?.currency,
|
|
|
|
length: booking.eventType?.length,
|
|
|
|
};
|
|
|
|
|
|
|
|
const promises = subscribersBookingCreated.map((sub) =>
|
|
|
|
sendPayload(sub.secret, WebhookTriggerEvents.BOOKING_CREATED, new Date().toISOString(), sub, {
|
|
|
|
...evt,
|
|
|
|
...eventTypeInfo,
|
|
|
|
bookingId,
|
|
|
|
eventTypeId: booking.eventType?.id,
|
|
|
|
status: "ACCEPTED",
|
|
|
|
smsReminderNumber: booking.smsReminderNumber || undefined,
|
|
|
|
}).catch((e) => {
|
|
|
|
console.error(
|
|
|
|
`Error executing webhook for event: ${WebhookTriggerEvents.BOOKING_CREATED}, URL: ${sub.subscriberUrl}`,
|
|
|
|
e
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
await Promise.all(promises);
|
|
|
|
} catch (error) {
|
|
|
|
// Silently fail
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|