2022-05-27 23:27:41 +00:00
|
|
|
import { SchedulingType } from "@prisma/client";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-08-03 15:35:08 +00:00
|
|
|
import { LocationType } from "@calcom/app-store/locations";
|
2022-05-27 23:27:41 +00:00
|
|
|
import EventManager from "@calcom/core/EventManager";
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
2022-06-06 17:49:56 +00:00
|
|
|
import { sendLocationChangeEmails } from "@calcom/emails";
|
2022-06-10 00:32:34 +00:00
|
|
|
import { parseRecurringEvent } from "@calcom/lib";
|
2022-05-27 23:27:41 +00:00
|
|
|
import logger from "@calcom/lib/logger";
|
|
|
|
import { getTranslation } from "@calcom/lib/server/i18n";
|
2022-06-06 19:49:00 +00:00
|
|
|
import type { AdditionalInformation, CalendarEvent } from "@calcom/types/Calendar";
|
2022-05-27 23:27:41 +00:00
|
|
|
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
2022-07-22 17:27:06 +00:00
|
|
|
import { createProtectedRouter } from "../../createRouter";
|
|
|
|
|
2022-05-27 23:27:41 +00:00
|
|
|
// Common data for all endpoints under webhook
|
|
|
|
const commonBookingSchema = z.object({
|
|
|
|
bookingId: z.number(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const bookingsRouter = createProtectedRouter()
|
|
|
|
.middleware(async ({ ctx, rawInput, next }) => {
|
|
|
|
// Endpoints that just read the logged in user's data - like 'list' don't necessary have any input
|
|
|
|
if (!rawInput) return next({ ctx: { ...ctx, booking: null } });
|
|
|
|
const webhookIdAndEventTypeId = commonBookingSchema.safeParse(rawInput);
|
|
|
|
if (!webhookIdAndEventTypeId.success) throw new TRPCError({ code: "PARSE_ERROR" });
|
|
|
|
|
|
|
|
const { bookingId } = webhookIdAndEventTypeId.data;
|
|
|
|
const booking = await ctx.prisma.booking.findFirst({
|
|
|
|
where: {
|
2022-08-08 19:38:02 +00:00
|
|
|
id: bookingId,
|
|
|
|
AND: [
|
2022-05-27 23:27:41 +00:00
|
|
|
{
|
2022-08-08 19:38:02 +00:00
|
|
|
OR: [
|
|
|
|
/* If user is organizer */
|
|
|
|
{ userId: ctx.user.id },
|
|
|
|
/* Or part of a collective booking */
|
|
|
|
{
|
|
|
|
eventType: {
|
|
|
|
schedulingType: SchedulingType.COLLECTIVE,
|
|
|
|
users: {
|
|
|
|
some: {
|
|
|
|
id: ctx.user.id,
|
|
|
|
},
|
|
|
|
},
|
2022-05-27 23:27:41 +00:00
|
|
|
},
|
|
|
|
},
|
2022-08-08 19:38:02 +00:00
|
|
|
],
|
2022-05-27 23:27:41 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
attendees: true,
|
|
|
|
eventType: true,
|
2022-08-08 19:38:02 +00:00
|
|
|
destinationCalendar: true,
|
|
|
|
references: true,
|
2022-05-27 23:27:41 +00:00
|
|
|
user: {
|
2022-08-08 19:38:02 +00:00
|
|
|
include: {
|
|
|
|
destinationCalendar: true,
|
|
|
|
credentials: true,
|
|
|
|
},
|
2022-05-27 23:27:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return next({ ctx: { ...ctx, booking } });
|
|
|
|
})
|
|
|
|
.middleware(async ({ ctx, next }) => {
|
|
|
|
// So TS doesn't compain in the previous middleware.
|
|
|
|
// This means the user doesn't have access to this booking
|
|
|
|
if (!ctx.booking) throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
// Booking here is non-nullable anymore
|
|
|
|
return next({ ctx: { ...ctx, booking: ctx.booking } });
|
|
|
|
})
|
|
|
|
.mutation("editLocation", {
|
|
|
|
input: commonBookingSchema.extend({
|
2022-08-03 15:35:08 +00:00
|
|
|
newLocation: z.string().transform((val) => val || LocationType.Daily),
|
2022-05-27 23:27:41 +00:00
|
|
|
}),
|
|
|
|
async resolve({ ctx, input }) {
|
|
|
|
const { bookingId, newLocation: location } = input;
|
|
|
|
const { booking } = ctx;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const organizer = await ctx.prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
id: booking.userId || 0,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
name: true,
|
|
|
|
email: true,
|
|
|
|
timeZone: true,
|
|
|
|
locale: true,
|
|
|
|
},
|
|
|
|
rejectOnNotFound: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const tOrganizer = await getTranslation(organizer.locale ?? "en", "common");
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
title: booking.title || "",
|
|
|
|
type: (booking.eventType?.title as string) || booking?.title || "",
|
|
|
|
description: booking.description || "",
|
|
|
|
startTime: booking.startTime ? dayjs(booking.startTime).format() : "",
|
|
|
|
endTime: booking.endTime ? dayjs(booking.endTime).format() : "",
|
|
|
|
organizer: {
|
|
|
|
email: organizer.email,
|
|
|
|
name: organizer.name ?? "Nameless",
|
|
|
|
timeZone: organizer.timeZone,
|
|
|
|
language: { translate: tOrganizer, locale: organizer.locale ?? "en" },
|
|
|
|
},
|
|
|
|
attendees: attendeesList,
|
|
|
|
uid: booking.uid,
|
2022-06-10 00:32:34 +00:00
|
|
|
recurringEvent: parseRecurringEvent(booking.eventType?.recurringEvent),
|
2022-05-27 23:27:41 +00:00
|
|
|
location,
|
|
|
|
destinationCalendar: booking?.destinationCalendar || booking?.user?.destinationCalendar,
|
|
|
|
};
|
|
|
|
|
|
|
|
const eventManager = new EventManager(ctx.user);
|
|
|
|
|
2022-08-08 19:38:02 +00:00
|
|
|
const updatedResult = await eventManager.updateLocation(evt, booking);
|
|
|
|
const results = updatedResult.results;
|
2022-05-27 23:27:41 +00:00
|
|
|
if (results.length > 0 && results.every((res) => !res.success)) {
|
|
|
|
const error = {
|
|
|
|
errorCode: "BookingUpdateLocationFailed",
|
|
|
|
message: "Updating location failed",
|
|
|
|
};
|
|
|
|
logger.error(`Booking ${ctx.user.username} failed`, error, results);
|
|
|
|
} else {
|
2022-08-08 19:38:02 +00:00
|
|
|
await ctx.prisma.booking.update({
|
|
|
|
where: {
|
|
|
|
id: bookingId,
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
location,
|
|
|
|
references: {
|
|
|
|
create: updatedResult.referencesToCreate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-06-06 19:49:00 +00:00
|
|
|
const metadata: AdditionalInformation = {};
|
2022-05-27 23:27:41 +00:00
|
|
|
if (results.length) {
|
2022-08-08 19:38:02 +00:00
|
|
|
metadata.hangoutLink = results[0].updatedEvent?.hangoutLink;
|
|
|
|
metadata.conferenceData = results[0].updatedEvent?.conferenceData;
|
|
|
|
metadata.entryPoints = results[0].updatedEvent?.entryPoints;
|
2022-05-27 23:27:41 +00:00
|
|
|
}
|
|
|
|
try {
|
2022-06-06 19:49:00 +00:00
|
|
|
await sendLocationChangeEmails({ ...evt, additionalInformation: metadata });
|
2022-05-27 23:27:41 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log("Error sending LocationChangeEmails");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
|
|
|
|
}
|
|
|
|
return { message: "Location updated" };
|
|
|
|
},
|
|
|
|
});
|