Skip to content

Commit f81bb92

Browse files
fix: increment iCalSequence when changing booking location (calcom#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>
1 parent 1d1a242 commit f81bb92

4 files changed

Lines changed: 29 additions & 8 deletions

File tree

packages/lib/__tests__/buildCalEventFromBooking.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const createBooking = (overrides = {}) => ({
5252
},
5353
destinationCalendar: null,
5454
user: null,
55+
iCalSequence: 0,
56+
iCalUID: "icaluid",
5557
...overrides,
5658
});
5759

@@ -121,6 +123,10 @@ describe("buildCalEventFromBooking", () => {
121123
destinationCalendar: [],
122124
seatsPerTimeSlot: booking.eventType?.seatsPerTimeSlot,
123125
seatsShowAttendees: true,
126+
customReplyToEmail: undefined,
127+
hideOrganizerEmail: undefined,
128+
iCalSequence: 0,
129+
iCalUID: booking.iCalUID,
124130
});
125131

126132
expect(parseRecurringEvent).toHaveBeenCalledWith(booking.eventType?.recurringEvent);
@@ -135,6 +141,8 @@ describe("buildCalEventFromBooking", () => {
135141
userPrimaryEmail: null,
136142
attendees: [],
137143
eventType: null,
144+
iCalUID: "icaluid",
145+
iCalSequence: 0,
138146
});
139147

140148
const organizer = createOrganizer({ name: null, locale: null });
@@ -170,6 +178,10 @@ describe("buildCalEventFromBooking", () => {
170178
destinationCalendar: [],
171179
seatsPerTimeSlot: undefined,
172180
seatsShowAttendees: undefined,
181+
customReplyToEmail: undefined,
182+
hideOrganizerEmail: undefined,
183+
iCalSequence: 0,
184+
iCalUID: "icaluid",
173185
});
174186

175187
// @ts-expect-error - locale is set in mock
@@ -192,6 +204,8 @@ describe("buildCalEventFromBooking", () => {
192204
credentialId: 1,
193205
},
194206
},
207+
iCalUID: "icaluid",
208+
iCalSequence: 0,
195209
});
196210

197211
const organizer = createOrganizer();

packages/lib/buildCalEventFromBooking.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type Booking = {
5555
} | null;
5656
attendees: Attendee[];
5757
eventType: EventType | null;
58+
iCalUID: string | null;
59+
iCalSequence: number;
5860
};
5961

6062
export const buildCalEventFromBooking = async ({
@@ -110,5 +112,7 @@ export const buildCalEventFromBooking = async ({
110112
seatsPerTimeSlot: booking.eventType?.seatsPerTimeSlot,
111113
seatsShowAttendees: booking.eventType?.seatsShowAttendees,
112114
customReplyToEmail: booking.eventType?.customReplyToEmail,
115+
iCalUID: booking.iCalUID ?? booking.uid,
116+
iCalSequence: booking.iCalSequence ?? 0,
113117
};
114118
};

packages/lib/server/repository/booking.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,15 @@ export class BookingRepository {
522522

523523
async updateLocationById({
524524
where: { id },
525-
data: { location, metadata, referencesToCreate },
525+
data: { location, metadata, referencesToCreate, responses, iCalSequence },
526526
}: {
527527
where: { id: number };
528528
data: {
529529
location: string;
530530
metadata: Record<string, unknown>;
531531
referencesToCreate: Prisma.BookingReferenceCreateInput[];
532+
responses?: Record<string, unknown>;
533+
iCalSequence?: number;
532534
};
533535
}) {
534536
await this.prismaClient.booking.update({
@@ -538,6 +540,8 @@ export class BookingRepository {
538540
data: {
539541
location,
540542
metadata,
543+
...(responses && { responses }),
544+
...(iCalSequence !== undefined && { iCalSequence }),
541545
references: {
542546
create: referencesToCreate,
543547
},

packages/trpc/server/routers/viewer/bookings/editLocation.handler.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import logger from "@calcom/lib/logger";
1010
import { safeStringify } from "@calcom/lib/safeStringify";
1111
import { getUsersCredentialsIncludeServiceAccountKey } from "@calcom/lib/server/getUsersCredentials";
1212
import { getTranslation } from "@calcom/lib/server/i18n";
13+
import { BookingRepository } from "@calcom/lib/server/repository/booking";
1314
import { CredentialRepository } from "@calcom/lib/server/repository/credential";
1415
import { UserRepository } from "@calcom/lib/server/repository/user";
1516
import { prisma } from "@calcom/prisma";
@@ -97,26 +98,24 @@ async function updateBookingLocationInDb({
9798
};
9899
});
99100

100-
await prisma.booking.update({
101-
where: {
102-
id: booking.id,
103-
},
101+
const bookingRepository = new BookingRepository(prisma);
102+
await bookingRepository.updateLocationById({
103+
where: { id: booking.id },
104104
data: {
105105
location: evt.location,
106106
metadata: {
107107
...(typeof booking.metadata === "object" && booking.metadata),
108108
...bookingMetadataUpdate,
109109
},
110-
references: {
111-
create: referencesToCreate,
112-
},
110+
referencesToCreate,
113111
responses: {
114112
...(typeof booking.responses === "object" && booking.responses),
115113
location: {
116114
value: evt.location,
117115
optionValue: "",
118116
},
119117
},
118+
iCalSequence: (evt.iCalSequence || 0) + 1,
120119
},
121120
});
122121
}

0 commit comments

Comments
 (0)