Files
calendar/packages/lib/buildCalEventFromBooking.ts
T
Joe Au-YeungGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
f81bb92beb fix: increment iCalSequence when changing booking location (#22847)
* fix: increment iCalSequence when changing booking location

- Add iCalSequence parameter to buildCalEventFromBooking function
- Update editLocation handler to increment sequence by 1 in CalendarEvent
- Update database with incremented iCalSequence when location changes
- Update buildCalEventFromBooking test to include new fields
- Ensures ICS files properly update existing calendar events instead of creating duplicates

Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com>

* `BookingRepository.updateLocationById` to accept responses and iCalSequence

* `editLocation.handler` use `BookingRepository`

* Update iCalSequence

* Use iCalSequence from booking param

* Add iCalUID to evt object

* Clean up

* Fix test

* Fix test

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-07-31 20:58:48 +01:00

119 lines
3.3 KiB
TypeScript

import type { Prisma } from "@prisma/client";
import dayjs from "@calcom/dayjs";
import { parseRecurringEvent } from "./isRecurringEvent";
import { getTranslation } from "./server/i18n";
type DestinationCalendar = {
id: number;
integration: string;
externalId: string;
primaryEmail: string | null;
userId: number | null;
eventTypeId: number | null;
credentialId: number | null;
delegationCredentialId: string | null;
domainWideDelegationCredentialId: string | null;
createdAt: Date | null;
updatedAt: Date | null;
} | null;
type Attendee = {
email: string;
name: string;
timeZone: string;
locale: string | null;
};
type Organizer = {
email: string;
name: string | null;
timeZone: string;
locale: string | null;
};
type EventType = {
title: string;
recurringEvent: Prisma.JsonValue | null;
seatsPerTimeSlot: number | null;
seatsShowAttendees: boolean | null;
hideOrganizerEmail: boolean;
customReplyToEmail: string | null;
};
type Booking = {
title: string;
description: string | null;
startTime: Date | null;
endTime: Date | null;
userPrimaryEmail: string | null;
uid: string;
destinationCalendar: DestinationCalendar;
user: {
destinationCalendar: DestinationCalendar;
} | null;
attendees: Attendee[];
eventType: EventType | null;
iCalUID: string | null;
iCalSequence: number;
};
export const buildCalEventFromBooking = async ({
booking,
organizer,
location,
conferenceCredentialId,
}: {
booking: Booking;
organizer: Organizer;
location: string;
conferenceCredentialId: number | null;
}) => {
const attendeesList = await Promise.all(
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 tOrganizer = await getTranslation(organizer.locale ?? "en", "common");
return {
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: booking.userPrimaryEmail ?? organizer.email,
name: organizer.name ?? "Nameless",
timeZone: organizer.timeZone,
language: { translate: tOrganizer, locale: organizer.locale ?? "en" },
},
attendees: attendeesList,
hideOrganizerEmail: booking.eventType?.hideOrganizerEmail,
uid: booking.uid,
recurringEvent: parseRecurringEvent(booking.eventType?.recurringEvent),
location,
conferenceCredentialId: conferenceCredentialId ?? undefined,
destinationCalendar: booking.destinationCalendar
? [booking.destinationCalendar]
: booking.user?.destinationCalendar
? [booking.user?.destinationCalendar]
: [],
seatsPerTimeSlot: booking.eventType?.seatsPerTimeSlot,
seatsShowAttendees: booking.eventType?.seatsShowAttendees,
customReplyToEmail: booking.eventType?.customReplyToEmail,
iCalUID: booking.iCalUID ?? booking.uid,
iCalSequence: booking.iCalSequence ?? 0,
};
};