Skip to content

Commit

Permalink
Fix duplicated calendar events (#5209)
Browse files Browse the repository at this point in the history
Fix duplicated calendar events when two workspace members participate to
the same event.
  • Loading branch information
bosiraphael committed Apr 29, 2024
1 parent 9809298 commit 6cafd25
Show file tree
Hide file tree
Showing 3 changed files with 320 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ export class CalendarEventParticipantRepository {

public async updateCalendarEventParticipantsAndReturnNewOnes(
calendarEventParticipants: CalendarEventParticipant[],
iCalUIDCalendarEventIdMap: Map<string, string>,
workspaceId: string,
transactionManager?: EntityManager,
): Promise<CalendarEventParticipant[]> {
Expand All @@ -173,10 +172,10 @@ export class CalendarEventParticipantRepository {
const dataSourceSchema =
this.workspaceDataSourceService.getSchemaName(workspaceId);

const calendarEventIds = Array.from(iCalUIDCalendarEventIdMap.values());

const existingCalendarEventParticipants = await this.getByCalendarEventIds(
calendarEventIds,
calendarEventParticipants.map(
(calendarEventParticipant) => calendarEventParticipant.calendarEventId,
),
workspaceId,
transactionManager,
);
Expand Down Expand Up @@ -205,23 +204,17 @@ export class CalendarEventParticipantRepository {
transactionManager,
);

const values = calendarEventParticipants.map(
(calendarEventParticipant) => ({
...calendarEventParticipant,
calendarEventId: iCalUIDCalendarEventIdMap.get(
calendarEventParticipant.iCalUID,
),
}),
);

const { flattenedValues, valuesString } =
getFlattenedValuesAndValuesStringForBatchRawQuery(values, {
calendarEventId: 'uuid',
handle: 'text',
displayName: 'text',
isOrganizer: 'boolean',
responseStatus: `${dataSourceSchema}."calendarEventParticipant_responsestatus_enum"`,
});
getFlattenedValuesAndValuesStringForBatchRawQuery(
calendarEventParticipants,
{
calendarEventId: 'uuid',
handle: 'text',
displayName: 'text',
isOrganizer: 'boolean',
responseStatus: `${dataSourceSchema}."calendarEventParticipant_responsestatus_enum"`,
},
);

await this.workspaceDataSourceService.executeRawQuery(
`UPDATE ${dataSourceSchema}."calendarEventParticipant" AS "calendarEventParticipant"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ export class CalendarEventRepository {
);
}

public async getByICalUIDs(
iCalUIDs: string[],
workspaceId: string,
transactionManager?: EntityManager,
): Promise<ObjectRecord<CalendarEventObjectMetadata>[]> {
if (iCalUIDs.length === 0) {
return [];
}

const dataSourceSchema =
this.workspaceDataSourceService.getSchemaName(workspaceId);

return await this.workspaceDataSourceService.executeRawQuery(
`SELECT * FROM ${dataSourceSchema}."calendarEvent" WHERE "iCalUID" = ANY($1)`,
[iCalUIDs],
workspaceId,
transactionManager,
);
}

public async deleteByIds(
calendarEventIds: string[],
workspaceId: string,
Expand Down Expand Up @@ -80,11 +100,11 @@ export class CalendarEventRepository {
}

public async getICalUIDCalendarEventIdMap(
calendarEventIds: string[],
iCalUIDs: string[],
workspaceId: string,
transactionManager?: EntityManager,
): Promise<Map<string, string>> {
if (calendarEventIds.length === 0) {
if (iCalUIDs.length === 0) {
return new Map();
}

Expand All @@ -97,8 +117,8 @@ export class CalendarEventRepository {
iCalUID: string;
}[]
| undefined = await this.workspaceDataSourceService.executeRawQuery(
`SELECT id, "iCalUID" FROM ${dataSourceSchema}."calendarEvent" WHERE "id" = ANY($1)`,
[calendarEventIds],
`SELECT id, "iCalUID" FROM ${dataSourceSchema}."calendarEvent" WHERE "iCalUID" = ANY($1)`,
[iCalUIDs],
workspaceId,
transactionManager,
);
Expand Down
Loading

0 comments on commit 6cafd25

Please sign in to comment.