+3



![kodiakhq[bot]](/avatar/78f03f45def735066748285e1c83c542df49e7b0562b0a8da815d5b24845d135?size=40)



![depfu[bot]](/avatar/b6b3643b027ad170b21b7b0771709b70d947b77fa3e410625fee15e94fb3edad?size=40)
4b6c701c48
* move custom inputs from description to own json object * show custom inputs on success page * fix type error * add custom inputs to email and webhook * add custom inputs to all emails * add values for custom inputs when rescheduling * add custom input everywhere description is shown * fix bug with boolean value * fix issues with null values * disable custom inputs and add notes for organizer * don't show custom input with empty string * don't show custom inputs with empty string in calender event and email * add link to booking details page * redirect to success page to see booking details * add functionality to cancel and reschedule booking * fix bookings that require confirmation * clean code * fix infinite lopp in useEffect of success page * show web conference details message when integration as location * improve design of cancelling event * clean code * disable darkmode for organizer on booking details page * fix dark mode for cancelling booking * fix build error * Fixes infinite loop * Fixes infinite loop * Fixes infinite loop * Update all Yarn dependencies (2022-05-16) (#2769) * Update all Yarn dependencies (2022-05-16) * Upgrade dependencies * Removes deprecated packages * Upgrades deps * Updates submodules * Update yarn.lock * Linting * Linting * Update website * Build fixes * TODO: fix this * Module resolving * Type fixes * Intercom fixes on SSG * Fixes infinite loop * Upgrades to React 18 * Type fixes * Locks node version to 14 * Upgrades daily-js * Readds missing types * Upgrades playwright * Noop when intercom is not installed * Update website * Removed yarn.lock in favor of monorepo Co-authored-by: depfu[bot] <23717796+depfu[bot]@users.noreply.github.com> Co-authored-by: zomars <[email protected]> * Create ci.yml * Update ci.yml * Reintroduces typescript-eslint Buckle up! * Type fixes * Update ci.yml * Update api * Update admin * Reusable inferSSRProps * Linting * Linting * Prisma fixes * Update ci.yml * Cache testing * Update e2e.yml * Update DatePicker.tsx * Update e2e.yml * Revert "Linting" This reverts commitadf817766e. * Revert "Linting" This reverts commit1b59dacd64. * Linting * Update e2e.yml * Ci updates * Add team Id to hash url (#2803) * Fix missing tabs - Embed (#2804) * Fix missing tabs * Fix Eslint error * Fix Eslint errors * Add import statement (#2812) * Add import statement * Update apps/docs/next.config.js Co-authored-by: Omar López <[email protected]> * Show success page if booking was deleted on calendar (#2808) * Add exception to 410 * Fix type error * Add GoogelCalError type * only show invite link for app.cal.dev (#2807) Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: Omar López <[email protected]> * fix: update eslint config to test .ts and .js separately (#2805) * fix: update eslint config * fix: update ts ignore * fix: update eslint config * Update TeamAvailabilityScreen.tsx * Type fixes * Update useIntercom.ts Co-authored-by: Omar López <[email protected]> * fix: sync api to latest commit (#2810) Co-authored-by: Agusti Fernandez Pardo <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> * Embed React improvements (#2782) * Add off support. Add getApi export. * Add publish command * Add embed-snippet in prod deps * Update README * Update package.json Co-authored-by: Bailey Pumfleet <[email protected]> Co-authored-by: zomars <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> * Consolidates test-results * Type fixes * Abstracts minimal booking select * Type fixes * Update listBookings.ts * Update common.json * Update bookingReminder.ts * Consolidates isOutOfBounds * Update webhookResponse-chromium.txt * Update TableActions.tsx * Type fixes * Update BookingPage.tsx * Update webhookResponse-chromium.txt Co-authored-by: CarinaWolli <[email protected]> Co-authored-by: Alex van Andel <[email protected]> Co-authored-by: Bailey Pumfleet <[email protected]> Co-authored-by: zomars <[email protected]> Co-authored-by: depfu[bot] <23717796+depfu[bot]@users.noreply.github.com> Co-authored-by: sean-brydon <[email protected]> Co-authored-by: Hariom Balhara <[email protected]> Co-authored-by: Joe Au-Yeung <[email protected]> Co-authored-by: iamkun <[email protected]> Co-authored-by: Agusti Fernandez Pardo <[email protected]> Co-authored-by: Agusti Fernandez Pardo <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
304 lines
9.4 KiB
TypeScript
304 lines
9.4 KiB
TypeScript
import { Booking, BookingStatus, Prisma, SchedulingType, User } from "@prisma/client";
|
||
import type { NextApiRequest, NextApiResponse } from "next";
|
||
|
||
import EventManager from "@calcom/core/EventManager";
|
||
import { isPrismaObjOrUndefined } from "@calcom/lib";
|
||
import logger from "@calcom/lib/logger";
|
||
import type { AdditionInformation, CalendarEvent, RecurringEvent } from "@calcom/types/Calendar";
|
||
import { refund } from "@ee/lib/stripe/server";
|
||
|
||
import { asStringOrNull } from "@lib/asStringOrNull";
|
||
import { getSession } from "@lib/auth";
|
||
import { sendDeclinedEmails, sendScheduledEmails } from "@lib/emails/email-manager";
|
||
import prisma from "@lib/prisma";
|
||
import { BookingConfirmBody } from "@lib/types/booking";
|
||
|
||
import { getTranslation } from "@server/lib/i18n";
|
||
|
||
const authorized = async (
|
||
currentUser: Pick<User, "id">,
|
||
booking: Pick<Booking, "eventTypeId" | "userId">
|
||
) => {
|
||
// if the organizer
|
||
if (booking.userId === currentUser.id) {
|
||
return true;
|
||
}
|
||
const eventType = await prisma.eventType.findUnique({
|
||
where: {
|
||
id: booking.eventTypeId || undefined,
|
||
},
|
||
select: {
|
||
schedulingType: true,
|
||
users: true,
|
||
},
|
||
});
|
||
if (
|
||
eventType?.schedulingType === SchedulingType.COLLECTIVE &&
|
||
eventType.users.find((user) => user.id === currentUser.id)
|
||
) {
|
||
return true;
|
||
}
|
||
return false;
|
||
};
|
||
|
||
const log = logger.getChildLogger({ prefix: ["[api] book:user"] });
|
||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||
const session = await getSession({ req: req });
|
||
if (!session?.user?.id) {
|
||
return res.status(401).json({ message: "Not authenticated" });
|
||
}
|
||
|
||
const reqBody = req.body as BookingConfirmBody;
|
||
const bookingId = reqBody.id;
|
||
|
||
if (!bookingId) {
|
||
return res.status(400).json({ message: "bookingId missing" });
|
||
}
|
||
|
||
const currentUser = await prisma.user.findFirst({
|
||
where: {
|
||
id: session.user.id,
|
||
},
|
||
select: {
|
||
id: true,
|
||
credentials: {
|
||
orderBy: { id: "desc" as Prisma.SortOrder },
|
||
},
|
||
timeZone: true,
|
||
email: true,
|
||
name: true,
|
||
username: true,
|
||
destinationCalendar: true,
|
||
locale: true,
|
||
},
|
||
});
|
||
|
||
if (!currentUser) {
|
||
return res.status(404).json({ message: "User not found" });
|
||
}
|
||
|
||
const tOrganizer = await getTranslation(currentUser.locale ?? "en", "common");
|
||
|
||
if (req.method === "PATCH") {
|
||
const booking = await prisma.booking.findFirst({
|
||
where: {
|
||
id: bookingId,
|
||
},
|
||
select: {
|
||
title: true,
|
||
description: true,
|
||
customInputs: true,
|
||
startTime: true,
|
||
endTime: true,
|
||
confirmed: true,
|
||
attendees: true,
|
||
eventTypeId: true,
|
||
eventType: {
|
||
select: {
|
||
recurringEvent: true,
|
||
},
|
||
},
|
||
location: true,
|
||
userId: true,
|
||
id: true,
|
||
uid: true,
|
||
payment: true,
|
||
destinationCalendar: true,
|
||
paid: true,
|
||
recurringEventId: true,
|
||
},
|
||
});
|
||
|
||
if (!booking) {
|
||
return res.status(404).json({ message: "booking not found" });
|
||
}
|
||
|
||
if (!(await authorized(currentUser, booking))) {
|
||
return res.status(401).end();
|
||
}
|
||
|
||
if (booking.confirmed) {
|
||
return res.status(400).json({ message: "booking already confirmed" });
|
||
}
|
||
|
||
/** When a booking that requires payment its being confirmed but doesn't have any payment,
|
||
* we shouldn’t save it on DestinationCalendars
|
||
*/
|
||
if (booking.payment.length > 0 && !booking.paid) {
|
||
await prisma.booking.update({
|
||
where: {
|
||
id: bookingId,
|
||
},
|
||
data: {
|
||
confirmed: true,
|
||
},
|
||
});
|
||
|
||
return res.status(204).end();
|
||
}
|
||
|
||
const attendeesListPromises = booking.attendees.map(async (attendee) => {
|
||
return {
|
||
name: attendee.name,
|
||
email: attendee.email,
|
||
timeZone: attendee.timeZone,
|
||
language: {
|
||
translate: await getTranslation(attendee.locale ?? "en", "common"),
|
||
locale: attendee.locale ?? "en",
|
||
},
|
||
};
|
||
});
|
||
|
||
const attendeesList = await Promise.all(attendeesListPromises);
|
||
|
||
const evt: CalendarEvent = {
|
||
type: booking.title,
|
||
title: booking.title,
|
||
description: booking.description,
|
||
customInputs: isPrismaObjOrUndefined(booking.customInputs),
|
||
startTime: booking.startTime.toISOString(),
|
||
endTime: booking.endTime.toISOString(),
|
||
organizer: {
|
||
email: currentUser.email,
|
||
name: currentUser.name || "Unnamed",
|
||
timeZone: currentUser.timeZone,
|
||
language: { translate: tOrganizer, locale: currentUser.locale ?? "en" },
|
||
},
|
||
attendees: attendeesList,
|
||
location: booking.location ?? "",
|
||
uid: booking.uid,
|
||
destinationCalendar: booking?.destinationCalendar || currentUser.destinationCalendar,
|
||
};
|
||
|
||
const recurringEvent = booking.eventType?.recurringEvent as RecurringEvent;
|
||
|
||
if (req.body.recurringEventId && recurringEvent) {
|
||
const groupedRecurringBookings = await prisma.booking.groupBy({
|
||
where: {
|
||
recurringEventId: booking.recurringEventId,
|
||
},
|
||
by: [Prisma.BookingScalarFieldEnum.recurringEventId],
|
||
_count: true,
|
||
});
|
||
// Overriding the recurring event configuration count to be the actual number of events booked for
|
||
// the recurring event (equal or less than recurring event configuration count)
|
||
recurringEvent.count = groupedRecurringBookings[0]._count;
|
||
}
|
||
|
||
if (reqBody.confirmed) {
|
||
const eventManager = new EventManager(currentUser);
|
||
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 ${currentUser.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;
|
||
}
|
||
try {
|
||
await sendScheduledEmails(
|
||
{ ...evt, additionInformation: metadata },
|
||
req.body.recurringEventId ? recurringEvent : {} // Send email with recurring event info only on recurring event context
|
||
);
|
||
} catch (error) {
|
||
log.error(error);
|
||
}
|
||
}
|
||
|
||
if (req.body.recurringEventId) {
|
||
// The booking to confirm is a recurring event and comes from /booking/upcoming, 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: req.body.recurringEventId,
|
||
confirmed: false,
|
||
},
|
||
});
|
||
unconfirmedRecurringBookings.map(async (recurringBooking) => {
|
||
await prisma.booking.update({
|
||
where: {
|
||
id: recurringBooking.id,
|
||
},
|
||
data: {
|
||
confirmed: true,
|
||
references: {
|
||
create: scheduleResult.referencesToCreate,
|
||
},
|
||
},
|
||
});
|
||
});
|
||
} 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
|
||
await prisma.booking.update({
|
||
where: {
|
||
id: bookingId,
|
||
},
|
||
data: {
|
||
confirmed: true,
|
||
references: {
|
||
create: scheduleResult.referencesToCreate,
|
||
},
|
||
},
|
||
});
|
||
}
|
||
|
||
res.status(204).end();
|
||
} else {
|
||
const rejectionReason = asStringOrNull(req.body.reason) || "";
|
||
evt.rejectionReason = rejectionReason;
|
||
if (req.body.recurringEventId) {
|
||
// The booking to reject is a recurring event and comes from /booking/upcoming, proceeding to mark all related
|
||
// bookings as rejected. Prisma updateMany does not support relations, so doing this in two steps for now.
|
||
const unconfirmedRecurringBookings = await prisma.booking.findMany({
|
||
where: {
|
||
recurringEventId: req.body.recurringEventId,
|
||
confirmed: false,
|
||
},
|
||
});
|
||
unconfirmedRecurringBookings.map(async (recurringBooking) => {
|
||
await prisma.booking.update({
|
||
where: {
|
||
id: recurringBooking.id,
|
||
},
|
||
data: {
|
||
rejected: true,
|
||
status: BookingStatus.REJECTED,
|
||
rejectionReason: rejectionReason,
|
||
},
|
||
});
|
||
});
|
||
} else {
|
||
await refund(booking, evt); // No payment integration for recurring events for v1
|
||
await prisma.booking.update({
|
||
where: {
|
||
id: bookingId,
|
||
},
|
||
data: {
|
||
rejected: true,
|
||
status: BookingStatus.REJECTED,
|
||
rejectionReason: rejectionReason,
|
||
},
|
||
});
|
||
}
|
||
|
||
await sendDeclinedEmails(evt, req.body.recurringEventId ? recurringEvent : {}); // Send email with recurring event info only on recurring event context
|
||
|
||
res.status(204).end();
|
||
}
|
||
}
|
||
}
|