
* patch applied * patch applied * We shouldn't pollute global css * Build fixes * Updates typings * WIP extracting zoom to package * Revert "Upgrades next to 12.1 (#1895)" (#1903) This reverts commitede0e98e1f
. * Tweak/gitignore prisma zod (#1905) * Extracts ignored createEventTypeBaseInput * Adds postinstall script * Revert "Tweak/gitignore prisma zod (#1905)" (#1906) This reverts commit15bfeb30d7
. * Eslint fixes (#1898) * Eslint fixes * Docs build fixes * Upgrade to next 12.1 (#1904) * Upgrades next to 12.1 * Fixes build * Updaters e2e test pipelines Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> * Fix URL by removing slash and backslash (#1733) * Fix URl by removing slash and backslash * Implement slugify * Add data type * Fixing folder structure * Solve zod-utils conflict * Build fixes (#1929) * Build fixes * Fixes type error * WIP * Conflict fixes * Removes unused file * TODO * WIP * Type fixes * Linting * WIP * Moved App definition to types * WIP * WIP * WIP * WIP WIP * Renamed zoomvideo app * Import fix * Daily.co app (#2022) * Daily.co app * Update packages/app-store/dailyvideo/lib/VideoApiAdapter.ts Co-authored-by: Omar López <zomars@me.com> * Update packages/app-store/dailyvideo/lib/VideoApiAdapter.ts Co-authored-by: Omar López <zomars@me.com> * Missing deps for newly added contants to lib Co-authored-by: Omar López <zomars@me.com> * WIP * WIP * WIP * Daily fixes * Updated type info * Slack Oauth integration - api route ideas * Adds getLocationOptions * Type fixes * Adds location option for daily video * Revert "Slack Oauth integration - api route ideas" This reverts commit 35ffa78e929339c4badb98cdab4e4b953ecc7cca. * Slack Oauth + verify sig * Revert "Slack Oauth + verify sig" This reverts commit ee95795e0f0ae6d06be4e0a423afb8c315d9af7d. * Huddle01 migration to app store (#2038) * Jitsi Video App migration * Removing uneeded dependencies * Missed unused reference * Missing dependency `@calcom/lib` is needed in the `locationOption.ts` file * Huddle01 migration to app store * Jitsi Video App migration (#2027) * Jitsi Video App migration * Removing uneeded dependencies * Missed unused reference * Missing dependency `@calcom/lib` is needed in the `locationOption.ts` file Co-authored-by: Omar López <zomars@me.com> * Monorepo/app store MS Teams Integration (#2080) * Create teamsvideo package * Remove zoom specific refrences * Add teams video files * Rename to office365_video * Add call back to add crednetial type office365_teams * Rename to office_video to match type * Add MS Teams as a location option * Rename files * Add teams reponse interface and create meeting * Comment out Daily imports * Add check for Teams integration * Add token checking functions * Change template to create event rather than meeting * Add comment to test between create link and event * Add teams URL to booking * Ask for just onlineMeeting permission * Add MS Teams logo * Add message to have an enterprise account * Remove comments * Comment back hasDailyIntegration * Comment back daily credentials * Update link to MS Graph section of README * Move API calls to package Co-authored-by: Omar López <zomars@me.com> * Re-adds missing module for transpiling * Adds email as required field for app store metadata * WIP: migrates tandem to app store * Cleanup * Migrates tandem api routes to app store * Fixes tandem api handlers * Big WIP WIP * Build fixes * WIP * Fixes annoying circular dependency bug I've spent a whole day on this.... * Location option cleanup * Type fixes * Update EventManager.ts * Update CalendarManager.ts * Moves CalendarService back to lib * Moves apple calendar to App Store * Cleanup * More cleanup * Migrates apple calendar * Returns all connected calendars credentials * No tsx needed in calcom/lib * Update auth.ts * Reordering * Update i18n.utils.ts * WIP: Google Meet * Type fixes * Type fixes * Cleanup * Update LinkIconButton.tsx * Update TrialBanner.tsx * Cleanup * Cleanup * Type fixes * Update _appRegistry.ts * Update fonts.css * Update CalEventParser.ts * Delete yarn.lock.rej * Update eslint-preset.js * Delete zoom.tsx * Type fixes * Migrates caldav to app store * Cleanup * Type fixes * Adds caldav to app store * Test fixes * Updates integration tests * Moar test fixes * Redirection fixes * Redirection fixes * Update timeFormat.ts * Update booking-pages.test.ts * Connect button fixes * Fix empty item * Cal fixes andrea (#2234) * Fixes #2178 * Fixes #2178 * Update apps/web/components/availability/Schedule.tsx * Update apps/web/components/availability/Schedule.tsx Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Peer Richelsen <peer@cal.com> * added meta viewport to disable zoom on input focus on mobile (#2238) * Update lint.yml (#2211) Co-authored-by: Peer Richelsen <peeroke@gmail.com> * Fix prisma client bundle makes app slow (#2237) Co-authored-by: Omar López <zomars@me.com> * Slider fixes * Removed unused code * Full Shell when unauthed * App sidebar responsive fixes * Adds dynamic install button * Fix for duplicate connected calendars * Various fixes * Display notification on app delete * Reuse connect button * Adds CalDav button * Deprecates ConnectIntegration * Simplify install button * Adds Google Calendar connect button * Adds Office 365 Install button * Migrates Stripe to App Store * Zoom Install Button (#2244) * Fix minor css, app image load from static path * Fix app logos remote img src (#2252) * Adds missing exports * Cleanup * Disables install button for globally enabled apps * Update EventManager.ts * Stripe fixes * Disables example app Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Juan Esteban Nieto Cifuentes <89233604+Jenietoc@users.noreply.github.com> Co-authored-by: Leo Giovanetti <hello@leog.me> Co-authored-by: Sean Brydon <seanbrydon.me@gmail.com> Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Bailey Pumfleet <pumfleet@hey.com> Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Co-authored-by: andreaestefania12 <andreaestefania12@hotmail.com> Co-authored-by: Peer Richelsen <peer@cal.com> Co-authored-by: Demian Caldelas <denik.works@protonmail.com> Co-authored-by: Alan <alannnc@gmail.com>
666 lines
20 KiB
TypeScript
666 lines
20 KiB
TypeScript
import { Credential, Prisma, SchedulingType, WebhookTriggerEvents } from "@prisma/client";
|
|
import async from "async";
|
|
import dayjs from "dayjs";
|
|
import dayjsBusinessTime from "dayjs-business-time";
|
|
import isBetween from "dayjs/plugin/isBetween";
|
|
import timezone from "dayjs/plugin/timezone";
|
|
import utc from "dayjs/plugin/utc";
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import short from "short-uuid";
|
|
import { v5 as uuidv5 } from "uuid";
|
|
|
|
import { getBusyCalendarTimes } from "@calcom/core/CalendarManager";
|
|
import EventManager from "@calcom/core/EventManager";
|
|
import { getBusyVideoTimes } from "@calcom/core/videoClient";
|
|
import { getErrorFromUnknown } from "@calcom/lib/errors";
|
|
import logger from "@calcom/lib/logger";
|
|
import notEmpty from "@calcom/lib/notEmpty";
|
|
import type { BufferedBusyTime } from "@calcom/types/BufferedBusyTime";
|
|
import type { AdditionInformation, CalendarEvent, EventBusyDate } from "@calcom/types/Calendar";
|
|
import type { EventResult, PartialReference } from "@calcom/types/EventManager";
|
|
import { handlePayment } from "@ee/lib/stripe/server";
|
|
|
|
import {
|
|
sendAttendeeRequestEmail,
|
|
sendOrganizerRequestEmail,
|
|
sendRescheduledEmails,
|
|
sendScheduledEmails,
|
|
} from "@lib/emails/email-manager";
|
|
import { ensureArray } from "@lib/ensureArray";
|
|
import { getEventName } from "@lib/event";
|
|
import prisma from "@lib/prisma";
|
|
import { BookingCreateBody } from "@lib/types/booking";
|
|
import sendPayload from "@lib/webhooks/sendPayload";
|
|
import getSubscribers from "@lib/webhooks/subscriptions";
|
|
|
|
import { getTranslation } from "@server/lib/i18n";
|
|
|
|
import verifyAccount from "../../../web3/utils/verifyAccount";
|
|
|
|
dayjs.extend(dayjsBusinessTime);
|
|
dayjs.extend(utc);
|
|
dayjs.extend(isBetween);
|
|
dayjs.extend(timezone);
|
|
|
|
const translator = short();
|
|
const log = logger.getChildLogger({ prefix: ["[api] book:user"] });
|
|
|
|
type BufferedBusyTimes = BufferedBusyTime[];
|
|
|
|
/**
|
|
* Refreshes a Credential with fresh data from the database.
|
|
*
|
|
* @param credential
|
|
*/
|
|
async function refreshCredential(credential: Credential): Promise<Credential> {
|
|
const newCredential = await prisma.credential.findUnique({
|
|
where: {
|
|
id: credential.id,
|
|
},
|
|
});
|
|
|
|
if (!newCredential) {
|
|
return credential;
|
|
} else {
|
|
return newCredential;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Refreshes the given set of credentials.
|
|
*
|
|
* @param credentials
|
|
*/
|
|
async function refreshCredentials(credentials: Array<Credential>): Promise<Array<Credential>> {
|
|
return await async.mapLimit(credentials, 5, refreshCredential);
|
|
}
|
|
|
|
function isAvailable(busyTimes: BufferedBusyTimes, time: string, length: number): boolean {
|
|
// Check for conflicts
|
|
let t = true;
|
|
|
|
if (Array.isArray(busyTimes) && busyTimes.length > 0) {
|
|
busyTimes.forEach((busyTime) => {
|
|
const startTime = dayjs(busyTime.start);
|
|
const endTime = dayjs(busyTime.end);
|
|
|
|
// Check if time is between start and end times
|
|
if (dayjs(time).isBetween(startTime, endTime, null, "[)")) {
|
|
t = false;
|
|
}
|
|
|
|
// Check if slot end time is between start and end time
|
|
if (dayjs(time).add(length, "minutes").isBetween(startTime, endTime)) {
|
|
t = false;
|
|
}
|
|
|
|
// Check if startTime is between slot
|
|
if (startTime.isBetween(dayjs(time), dayjs(time).add(length, "minutes"))) {
|
|
t = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
return t;
|
|
}
|
|
|
|
function isOutOfBounds(
|
|
time: dayjs.ConfigType,
|
|
{ periodType, periodDays, periodCountCalendarDays, periodStartDate, periodEndDate, timeZone }: any // FIXME types
|
|
): boolean {
|
|
const date = dayjs(time);
|
|
|
|
switch (periodType) {
|
|
case "rolling": {
|
|
const periodRollingEndDay = periodCountCalendarDays
|
|
? dayjs().tz(timeZone).add(periodDays, "days").endOf("day")
|
|
: dayjs().tz(timeZone).addBusinessTime(periodDays, "days").endOf("day");
|
|
return date.endOf("day").isAfter(periodRollingEndDay);
|
|
}
|
|
|
|
case "range": {
|
|
const periodRangeStartDay = dayjs(periodStartDate).tz(timeZone).endOf("day");
|
|
const periodRangeEndDay = dayjs(periodEndDate).tz(timeZone).endOf("day");
|
|
return date.endOf("day").isBefore(periodRangeStartDay) || date.endOf("day").isAfter(periodRangeEndDay);
|
|
}
|
|
|
|
case "unlimited":
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
const userSelect = Prisma.validator<Prisma.UserArgs>()({
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
name: true,
|
|
username: true,
|
|
timeZone: true,
|
|
credentials: true,
|
|
bufferTime: true,
|
|
destinationCalendar: true,
|
|
locale: true,
|
|
},
|
|
});
|
|
|
|
const getUserNameWithBookingCounts = async (eventTypeId: number, selectedUserNames: string[]) => {
|
|
const users = await prisma.user.findMany({
|
|
where: {
|
|
username: { in: selectedUserNames },
|
|
eventTypes: {
|
|
some: {
|
|
id: eventTypeId,
|
|
},
|
|
},
|
|
},
|
|
select: {
|
|
id: true,
|
|
username: true,
|
|
locale: true,
|
|
},
|
|
});
|
|
|
|
const userNamesWithBookingCounts = await Promise.all(
|
|
users.map(async (user) => ({
|
|
username: user.username,
|
|
bookingCount: await prisma.booking.count({
|
|
where: {
|
|
user: {
|
|
id: user.id,
|
|
},
|
|
startTime: {
|
|
gt: new Date(),
|
|
},
|
|
eventTypeId,
|
|
},
|
|
}),
|
|
}))
|
|
);
|
|
|
|
return userNamesWithBookingCounts;
|
|
};
|
|
|
|
type User = Prisma.UserGetPayload<typeof userSelect>;
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
const reqBody = req.body as BookingCreateBody;
|
|
const eventTypeId = reqBody.eventTypeId;
|
|
const tAttendees = await getTranslation(reqBody.language ?? "en", "common");
|
|
const tGuests = await getTranslation("en", "common");
|
|
log.debug(`Booking eventType ${eventTypeId} started`);
|
|
|
|
const isTimeInPast = (time: string): boolean => {
|
|
return dayjs(time).isBefore(new Date(), "day");
|
|
};
|
|
|
|
if (isTimeInPast(reqBody.start)) {
|
|
const error = {
|
|
errorCode: "BookingDateInPast",
|
|
message: "Attempting to create a meeting in the past.",
|
|
};
|
|
|
|
log.error(`Booking ${eventTypeId} failed`, error);
|
|
return res.status(400).json(error);
|
|
}
|
|
|
|
const eventType = await prisma.eventType.findUnique({
|
|
rejectOnNotFound: true,
|
|
where: {
|
|
id: eventTypeId,
|
|
},
|
|
select: {
|
|
users: userSelect,
|
|
team: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
},
|
|
},
|
|
title: true,
|
|
length: true,
|
|
eventName: true,
|
|
schedulingType: true,
|
|
periodType: true,
|
|
periodStartDate: true,
|
|
periodEndDate: true,
|
|
periodDays: true,
|
|
periodCountCalendarDays: true,
|
|
requiresConfirmation: true,
|
|
userId: true,
|
|
price: true,
|
|
currency: true,
|
|
metadata: true,
|
|
destinationCalendar: true,
|
|
},
|
|
});
|
|
|
|
if (!eventType) return res.status(404).json({ message: "eventType.notFound" });
|
|
|
|
let users = eventType.users;
|
|
|
|
/* If this event was pre-relationship migration */
|
|
if (!users.length && eventType.userId) {
|
|
const eventTypeUser = await prisma.user.findUnique({
|
|
where: {
|
|
id: eventType.userId,
|
|
},
|
|
...userSelect,
|
|
});
|
|
if (!eventTypeUser) return res.status(404).json({ message: "eventTypeUser.notFound" });
|
|
users.push(eventTypeUser);
|
|
}
|
|
|
|
const organizer = await prisma.user.findUnique({
|
|
where: {
|
|
id: users[0].id,
|
|
},
|
|
select: {
|
|
locale: true,
|
|
},
|
|
});
|
|
|
|
const tOrganizer = await getTranslation(organizer?.locale ?? "en", "common");
|
|
|
|
if (eventType.schedulingType === SchedulingType.ROUND_ROBIN) {
|
|
const bookingCounts = await getUserNameWithBookingCounts(
|
|
eventTypeId,
|
|
ensureArray(reqBody.user) || users.map((user) => user.username)
|
|
);
|
|
|
|
users = getLuckyUsers(users, bookingCounts);
|
|
}
|
|
|
|
const invitee = [
|
|
{
|
|
email: reqBody.email,
|
|
name: reqBody.name,
|
|
timeZone: reqBody.timeZone,
|
|
language: { translate: tAttendees, locale: reqBody.language ?? "en" },
|
|
},
|
|
];
|
|
const guests = (reqBody.guests || []).map((guest) => {
|
|
const g = {
|
|
email: guest,
|
|
name: "",
|
|
timeZone: reqBody.timeZone,
|
|
language: { translate: tGuests, locale: "en" },
|
|
};
|
|
return g;
|
|
});
|
|
|
|
const teamMemberPromises =
|
|
eventType.schedulingType === SchedulingType.COLLECTIVE
|
|
? users.slice(1).map(async function (user) {
|
|
return {
|
|
email: user.email || "",
|
|
name: user.name || "",
|
|
timeZone: user.timeZone,
|
|
language: {
|
|
translate: await getTranslation(user.locale ?? "en", "common"),
|
|
locale: user.locale ?? "en",
|
|
},
|
|
};
|
|
})
|
|
: [];
|
|
|
|
const teamMembers = await Promise.all(teamMemberPromises);
|
|
|
|
const attendeesList = [...invitee, ...guests, ...teamMembers];
|
|
|
|
const seed = `${users[0].username}:${dayjs(req.body.start).utc().format()}:${new Date().getTime()}`;
|
|
const uid = translator.fromUUID(uuidv5(seed, uuidv5.URL));
|
|
|
|
const eventNameObject = {
|
|
attendeeName: reqBody.name || "Nameless",
|
|
eventType: eventType.title,
|
|
eventName: eventType.eventName,
|
|
host: users[0].name || "Nameless",
|
|
t: tOrganizer,
|
|
};
|
|
|
|
const description =
|
|
reqBody.notes +
|
|
reqBody.customInputs.reduce(
|
|
(str, input) => str + "<br /><br />" + input.label + ":<br />" + input.value,
|
|
""
|
|
);
|
|
|
|
const evt: CalendarEvent = {
|
|
type: eventType.title,
|
|
title: getEventName(eventNameObject), //this needs to be either forced in english, or fetched for each attendee and organizer separately
|
|
description,
|
|
startTime: reqBody.start,
|
|
endTime: reqBody.end,
|
|
organizer: {
|
|
name: users[0].name || "Nameless",
|
|
email: users[0].email || "Email-less",
|
|
timeZone: users[0].timeZone,
|
|
language: { translate: tOrganizer, locale: organizer?.locale ?? "en" },
|
|
},
|
|
attendees: attendeesList,
|
|
location: reqBody.location, // Will be processed by the EventManager later.
|
|
/** For team events, we will need to handle each member destinationCalendar eventually */
|
|
destinationCalendar: eventType.destinationCalendar || users[0].destinationCalendar,
|
|
};
|
|
|
|
if (eventType.schedulingType === SchedulingType.COLLECTIVE) {
|
|
evt.team = {
|
|
members: users.map((user) => user.name || user.username || "Nameless"),
|
|
name: eventType.team?.name || "Nameless",
|
|
}; // used for invitee emails
|
|
}
|
|
|
|
// Initialize EventManager with credentials
|
|
const rescheduleUid = reqBody.rescheduleUid;
|
|
|
|
async function createBooking() {
|
|
// @TODO: check as metadata
|
|
if (req.body.web3Details) {
|
|
const { web3Details } = req.body;
|
|
await verifyAccount(web3Details.userSignature, web3Details.userWallet);
|
|
}
|
|
|
|
return prisma.booking.create({
|
|
include: {
|
|
user: {
|
|
select: { email: true, name: true, timeZone: true },
|
|
},
|
|
attendees: true,
|
|
},
|
|
data: {
|
|
uid,
|
|
title: evt.title,
|
|
startTime: dayjs(evt.startTime).toDate(),
|
|
endTime: dayjs(evt.endTime).toDate(),
|
|
description: evt.description,
|
|
confirmed: (!eventType.requiresConfirmation && !eventType.price) || !!rescheduleUid,
|
|
location: evt.location,
|
|
eventType: {
|
|
connect: {
|
|
id: eventTypeId,
|
|
},
|
|
},
|
|
attendees: {
|
|
createMany: {
|
|
data: evt.attendees.map((attendee) => {
|
|
//if attendee is team member, it should fetch their locale not booker's locale
|
|
//perhaps make email fetch request to see if his locale is stored, else
|
|
const retObj = {
|
|
name: attendee.name,
|
|
email: attendee.email,
|
|
timeZone: attendee.timeZone,
|
|
locale: attendee.language.locale,
|
|
};
|
|
return retObj;
|
|
}),
|
|
},
|
|
},
|
|
user: {
|
|
connect: {
|
|
id: users[0].id,
|
|
},
|
|
},
|
|
destinationCalendar: evt.destinationCalendar
|
|
? {
|
|
connect: { id: evt.destinationCalendar.id },
|
|
}
|
|
: undefined,
|
|
},
|
|
});
|
|
}
|
|
|
|
let results: EventResult[] = [];
|
|
let referencesToCreate: PartialReference[] = [];
|
|
let user: User | null = null;
|
|
|
|
/** Let's start checking for availability */
|
|
for (const currentUser of users) {
|
|
if (!currentUser) {
|
|
console.error(`currentUser not found`);
|
|
return;
|
|
}
|
|
if (!user) user = currentUser;
|
|
|
|
const selectedCalendars = await prisma.selectedCalendar.findMany({
|
|
where: {
|
|
userId: currentUser.id,
|
|
},
|
|
});
|
|
|
|
const credentials = currentUser.credentials;
|
|
const calendarBusyTimes: EventBusyDate[] = await prisma.booking
|
|
.findMany({
|
|
where: {
|
|
userId: currentUser.id,
|
|
eventTypeId: eventTypeId,
|
|
},
|
|
})
|
|
.then((bookings) => bookings.map((booking) => ({ end: booking.endTime, start: booking.startTime })));
|
|
|
|
if (credentials) {
|
|
await getBusyCalendarTimes(credentials, reqBody.start, reqBody.end, selectedCalendars).then(
|
|
(busyTimes) => calendarBusyTimes.push(...busyTimes)
|
|
);
|
|
|
|
const videoBusyTimes = (await getBusyVideoTimes(credentials)).filter(notEmpty);
|
|
calendarBusyTimes.push(...videoBusyTimes);
|
|
}
|
|
|
|
console.log("calendarBusyTimes==>>>", calendarBusyTimes);
|
|
|
|
const bufferedBusyTimes: BufferedBusyTimes = calendarBusyTimes.map((a) => ({
|
|
start: dayjs(a.start).subtract(currentUser.bufferTime, "minute").toString(),
|
|
end: dayjs(a.end).add(currentUser.bufferTime, "minute").toString(),
|
|
}));
|
|
|
|
let isAvailableToBeBooked = true;
|
|
try {
|
|
isAvailableToBeBooked = isAvailable(bufferedBusyTimes, reqBody.start, eventType.length);
|
|
} catch {
|
|
log.debug({
|
|
message: "Unable set isAvailableToBeBooked. Using true. ",
|
|
});
|
|
}
|
|
|
|
if (!isAvailableToBeBooked) {
|
|
const error = {
|
|
errorCode: "BookingUserUnAvailable",
|
|
message: `${currentUser.name} is unavailable at this time.`,
|
|
};
|
|
|
|
log.debug(`Booking ${currentUser.name} failed`, error);
|
|
res.status(409).json(error);
|
|
return;
|
|
}
|
|
|
|
let timeOutOfBounds = false;
|
|
|
|
try {
|
|
timeOutOfBounds = isOutOfBounds(reqBody.start, {
|
|
periodType: eventType.periodType,
|
|
periodDays: eventType.periodDays,
|
|
periodEndDate: eventType.periodEndDate,
|
|
periodStartDate: eventType.periodStartDate,
|
|
periodCountCalendarDays: eventType.periodCountCalendarDays,
|
|
timeZone: currentUser.timeZone,
|
|
});
|
|
} catch {
|
|
log.debug({
|
|
message: "Unable set timeOutOfBounds. Using false. ",
|
|
});
|
|
}
|
|
|
|
if (timeOutOfBounds) {
|
|
const error = {
|
|
errorCode: "BookingUserUnAvailable",
|
|
message: `${currentUser.name} is unavailable at this time.`,
|
|
};
|
|
|
|
log.debug(`Booking ${currentUser.name} failed`, error);
|
|
res.status(400).json(error);
|
|
return;
|
|
}
|
|
}
|
|
|
|
type Booking = Prisma.PromiseReturnType<typeof createBooking>;
|
|
let booking: Booking | null = null;
|
|
try {
|
|
booking = await createBooking();
|
|
evt.uid = booking.uid;
|
|
} catch (_err) {
|
|
const err = getErrorFromUnknown(_err);
|
|
log.error(`Booking ${eventTypeId} failed`, "Error when saving booking to db", err.message);
|
|
if (err.code === "P2002") {
|
|
res.status(409).json({ message: "booking.conflict" });
|
|
return;
|
|
}
|
|
res.status(500).end();
|
|
return;
|
|
}
|
|
|
|
if (!user) throw Error("Can't continue, user not found.");
|
|
|
|
// After polling videoBusyTimes, credentials might have been changed due to refreshment, so query them again.
|
|
const credentials = await refreshCredentials(user.credentials);
|
|
const eventManager = new EventManager({ ...user, credentials });
|
|
|
|
if (rescheduleUid) {
|
|
// Use EventManager to conditionally use all needed integrations.
|
|
const updateManager = await eventManager.update(evt, rescheduleUid);
|
|
|
|
results = updateManager.results;
|
|
referencesToCreate = updateManager.referencesToCreate;
|
|
|
|
if (results.length > 0 && results.every((res) => !res.success)) {
|
|
const error = {
|
|
errorCode: "BookingReschedulingMeetingFailed",
|
|
message: "Booking Rescheduling failed",
|
|
};
|
|
|
|
log.error(`Booking ${user.name} failed`, error, results);
|
|
} else {
|
|
const metadata: AdditionInformation = {};
|
|
|
|
if (results.length) {
|
|
// TODO: Handle created event metadata more elegantly
|
|
const [updatedEvent] = Array.isArray(results[0].updatedEvent)
|
|
? results[0].updatedEvent
|
|
: [results[0].updatedEvent];
|
|
if (updatedEvent) {
|
|
metadata.hangoutLink = updatedEvent.hangoutLink;
|
|
metadata.conferenceData = updatedEvent.conferenceData;
|
|
metadata.entryPoints = updatedEvent.entryPoints;
|
|
}
|
|
}
|
|
|
|
await sendRescheduledEmails({ ...evt, additionInformation: metadata });
|
|
}
|
|
// If it's not a reschedule, doesn't require confirmation and there's no price,
|
|
// Create a booking
|
|
} else if (!eventType.requiresConfirmation && !eventType.price) {
|
|
// Use EventManager to conditionally use all needed integrations.
|
|
const createManager = await eventManager.create(evt);
|
|
|
|
results = createManager.results;
|
|
referencesToCreate = createManager.referencesToCreate;
|
|
|
|
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: AdditionInformation = {};
|
|
|
|
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;
|
|
}
|
|
await sendScheduledEmails({ ...evt, additionInformation: metadata });
|
|
}
|
|
}
|
|
|
|
if (eventType.requiresConfirmation && !rescheduleUid) {
|
|
await sendOrganizerRequestEmail(evt);
|
|
await sendAttendeeRequestEmail(evt, attendeesList[0]);
|
|
}
|
|
|
|
if (typeof eventType.price === "number" && eventType.price > 0) {
|
|
try {
|
|
const [firstStripeCredential] = user.credentials.filter((cred) => cred.type == "stripe_payment");
|
|
if (!booking.user) booking.user = user;
|
|
const payment = await handlePayment(evt, eventType, firstStripeCredential, booking);
|
|
|
|
res.status(201).json({ ...booking, message: "Payment required", paymentUid: payment.uid });
|
|
return;
|
|
} catch (e) {
|
|
log.error(`Creating payment failed`, e);
|
|
res.status(500).json({ message: "Payment Failed" });
|
|
return;
|
|
}
|
|
}
|
|
|
|
log.debug(`Booking ${user.username} completed`);
|
|
|
|
const eventTrigger: WebhookTriggerEvents = rescheduleUid ? "BOOKING_RESCHEDULED" : "BOOKING_CREATED";
|
|
const subscriberOptions = {
|
|
userId: user.id,
|
|
eventTypeId,
|
|
triggerEvent: eventTrigger,
|
|
};
|
|
|
|
// Send Webhook call if hooked to BOOKING_CREATED & BOOKING_RESCHEDULED
|
|
const subscribers = await getSubscribers(subscriberOptions);
|
|
console.log("evt:", {
|
|
...evt,
|
|
metadata: reqBody.metadata,
|
|
});
|
|
const promises = subscribers.map((sub) =>
|
|
sendPayload(
|
|
eventTrigger,
|
|
new Date().toISOString(),
|
|
sub.subscriberUrl,
|
|
{
|
|
...evt,
|
|
rescheduleUid,
|
|
metadata: reqBody.metadata,
|
|
},
|
|
sub.payloadTemplate
|
|
).catch((e) => {
|
|
console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${sub.subscriberUrl}`, e);
|
|
})
|
|
);
|
|
await Promise.all(promises);
|
|
|
|
await prisma.booking.update({
|
|
where: {
|
|
uid: booking.uid,
|
|
},
|
|
data: {
|
|
references: {
|
|
createMany: {
|
|
data: referencesToCreate,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
// booking successful
|
|
return res.status(201).json(booking);
|
|
}
|
|
|
|
export function getLuckyUsers(
|
|
users: User[],
|
|
bookingCounts: Prisma.PromiseReturnType<typeof getUserNameWithBookingCounts>
|
|
) {
|
|
if (!bookingCounts.length) users.slice(0, 1);
|
|
|
|
const [firstMostAvailableUser] = bookingCounts.sort((a, b) => (a.bookingCount > b.bookingCount ? 1 : -1));
|
|
const luckyUser = users.find((user) => user.username === firstMostAvailableUser?.username);
|
|
return luckyUser ? [luckyUser] : users;
|
|
}
|