The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Keep only the writes inside the calendar and messaging import transactions (#24681)
neo773bb933f2122fa
diff --git a/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/constants/postgres-error-messages.constants.ts b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/constants/postgres-error-messages.constants.tsindex a7021f6f7c9..d379fb46e5e 100644--- a/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/constants/postgres-error-messages.constants.ts+++ b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/constants/postgres-error-messages.constants.ts@@ -5,11 +5,15 @@ import { POSTGRESQL_ERROR_CODES } from 'src/engine/api/graphql/workspace-query-rexport const QUERY_READ_TIMEOUT_MESSAGE = 'Query read timeout';+export const CONNECTION_TERMINATED_MESSAGE = 'Connection terminated';+export const DUPLICATE_ENTRY_DETECTED_MESSAGE ='A duplicate entry was detected';export const QUERY_READ_TIMEOUT_USER_FRIENDLY_MESSAGE = msg`We are experiencing a temporary issue with our database. Please try again later.`;+export const TRANSIENT_DATABASE_ERROR_USER_FRIENDLY_MESSAGE = msg`We are experiencing a temporary issue with our database. Please try again later.`;+export const DUPLICATE_ENTRY_USER_FRIENDLY_MESSAGE = msg`This record already exists. Please check your data and try again.`;export const INVALID_INPUT_USER_FRIENDLY_MESSAGE = msg`Invalid input provided.`;diff --git a/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/constants/transient-postgres-error-codes.constants.ts b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/constants/transient-postgres-error-codes.constants.tsnew file mode 100644index 00000000000..3920bf6970f--- /dev/null+++ b/packages/twenty-server/src/engine/api/graphql/workspace-query-runner/constants/transient-postgres-error-codes.constants.ts@@ -0,0 +1,20 @@+import { POSTGRESQL_ERROR_CODES } from 'src/engine/api/graphql/workspace-query-runner/constants/postgres-error-codes.constants';++export const TRANSIENT_POSTGRESQL_ERROR_CODES: string[] = [+ POSTGRESQL_ERROR_CODES.CONNECTION_EXCEPTION,+ POSTGRESQL_ERROR_CODES.CONNECTION_DOES_NOT_EXIST,+ POSTGRESQL_ERROR_CODES.CONNECTION_FAILURE,+ POSTGRESQL_ERROR_CODES.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION,+ POSTGRESQL_ERROR_CODES.SQLSERVER_REJECTED_ESTABLISHMENT_OF_SQLCONNECTION,+ POSTGRESQL_ERROR_CODES.TRANSACTION_RESOLUTION_UNKNOWN,+ POSTGRESQL_ERROR_CODES.IDLE_IN_TRANSACTION_SESSION_TIMEOUT,+ POSTGRESQL_ERROR_CODES.TRANSACTION_TIMEOUT,+ POSTGRESQL_ERROR_CODES.SERIALIZATION_FAILURE,+ POSTGRESQL_ERROR_CODES.DEADLOCK_DETECTED,+ POSTGRESQL_ERROR_CODES.TOO_MANY_CONNECTIONS,+ POSTGRESQL_ERROR_CODES.LOCK_NOT_AVAILABLE,+ POSTGRESQL_ERROR_CODES.ADMIN_SHUTDOWN,+ POSTGRESQL_ERROR_CODES.CRASH_SHUTDOWN,+ POSTGRESQL_ERROR_CODES.CANNOT_CONNECT_NOW,+ POSTGRESQL_ERROR_CODES.IDLE_SESSION_TIMEOUT,+];diff --git a/packages/twenty-server/src/engine/twenty-orm/datasource/utils/__tests__/run-in-rollback-safe-transaction.util.spec.ts b/packages/twenty-server/src/engine/twenty-orm/datasource/utils/__tests__/run-in-rollback-safe-transaction.util.spec.tsindex aa6f26bde1a..8d5876439c5 100644--- a/packages/twenty-server/src/engine/twenty-orm/datasource/utils/__tests__/run-in-rollback-safe-transaction.util.spec.ts+++ b/packages/twenty-server/src/engine/twenty-orm/datasource/utils/__tests__/run-in-rollback-safe-transaction.util.spec.ts@@ -1,6 +1,11 @@import { type Pool, type PoolClient } from 'pg';+import { POSTGRESQL_ERROR_CODES } from 'src/engine/api/graphql/workspace-query-runner/constants/postgres-error-codes.constants';import { runInRollbackSafeTransaction } from 'src/engine/twenty-orm/datasource/utils/run-in-rollback-safe-transaction.util';+import {+ TwentyOrmException,+ TwentyOrmExceptionCode,+} from 'src/engine/twenty-orm/exceptions/twenty-orm.exception';const buildClient = () => ({query: jest.fn().mockResolvedValue({ rows: [] }),@@ -13,6 +18,26 @@ const buildPool = (client: ReturnType<typeof buildClient>): Pool =>}) as unknown as Pool;describe('runInRollbackSafeTransaction', () => {+ it('should map a transient failure raised while acquiring the connection', async () => {+ const connectionError = Object.assign(+ new Error('too many clients already'),+ { code: POSTGRESQL_ERROR_CODES.TOO_MANY_CONNECTIONS },+ );+ const pool = {+ connect: jest.fn().mockRejectedValue(connectionError),+ } as unknown as Pool;++ const error = await runInRollbackSafeTransaction({+ pool,+ work: async () => 'never runs',+ }).catch((thrownError: Error) => thrownError);++ expect(error).toBeInstanceOf(TwentyOrmException);+ expect((error as TwentyOrmException).code).toBe(+ TwentyOrmExceptionCode.TRANSIENT_DATABASE_ERROR,+ );+ });+it('should wrap the work in BEGIN/COMMIT and release the client reusable', async () => {const client = buildClient();@@ -29,7 +54,7 @@ describe('runInRollbackSafeTransaction', () => {it('should roll back and rethrow the work error', async () => {const client = buildClient();- const workError = new Error('Query read timeout');+ const workError = new Error('work failed');await expect(runInRollbackSafeTransaction({@@ -85,6 +110,64 @@ describe('runInRollbackSafeTransaction', () => {expect(client.release).toHaveBeenCalledWith(true);});+ it.each([+ POSTGRESQL_ERROR_CODES.IDLE_IN_TRANSACTION_SESSION_TIMEOUT,+ POSTGRESQL_ERROR_CODES.CONNECTION_FAILURE,+ POSTGRESQL_ERROR_CODES.DEADLOCK_DETECTED,+ ])(+ 'should surface the transient postgres failure %s raised at COMMIT as TRANSIENT_DATABASE_ERROR',+ async (code) => {+ const client = buildClient();+ const commitError = Object.assign(+ new Error('terminating connection due to idle-in-transaction timeout'),+ { code },+ );++ client.query.mockImplementation((statement: string) =>+ statement === 'COMMIT'+ ? Promise.reject(commitError)+ : Promise.resolve({ rows: [] }),+ );++ const error = await runInRollbackSafeTransaction({+ pool: buildPool(client),+ work: async () => 'done',+ }).catch((thrownError: Error) => thrownError);++ expect(error).toBeInstanceOf(TwentyOrmException);+ expect((error as TwentyOrmException).code).toBe(+ TwentyOrmExceptionCode.TRANSIENT_DATABASE_ERROR,+ );+ expect((error as Error & { cause?: Error }).cause).toBe(commitError);+ },+ );++ it('should still surface the original transient failure as TRANSIENT_DATABASE_ERROR when ROLLBACK also fails', async () => {+ const client = buildClient();+ const workError = Object.assign(+ new Error('terminating connection due to administrator command'),+ { code: POSTGRESQL_ERROR_CODES.ADMIN_SHUTDOWN },+ );++ client.query.mockImplementation((statement: string) =>+ statement === 'ROLLBACK'+ ? Promise.reject(new Error('Connection terminated unexpectedly'))+ : Promise.resolve({ rows: [] }),+ );++ const error = await runInRollbackSafeTransaction({+ pool: buildPool(client),+ work: async () => {+ throw workError;+ },+ }).catch((thrownError: Error) => thrownError);++ expect((error as TwentyOrmException).code).toBe(+ TwentyOrmExceptionCode.TRANSIENT_DATABASE_ERROR,+ );+ expect((error as Error & { cause?: Error }).cause).toBe(workError);+ });+it('should rethrow a BEGIN failure and release the client reusable when ROLLBACK succeeds', async () => {const client = buildClient();const beginError = new Error('BEGIN failed');diff --git a/packages/twenty-server/src/engine/twenty-orm/datasource/utils/run-in-rollback-safe-transaction.util.ts b/packages/twenty-server/src/engine/twenty-orm/datasource/utils/run-in-rollback-safe-transaction.util.tsindex 3025257ebc6..cf77826ea05 100644--- a/packages/twenty-server/src/engine/twenty-orm/datasource/utils/run-in-rollback-safe-transaction.util.ts+++ b/packages/twenty-server/src/engine/twenty-orm/datasource/utils/run-in-rollback-safe-transaction.util.ts@@ -2,6 +2,8 @@ import { Logger } from '@nestjs/common';import { type Pool, type PoolClient } from 'pg';+import { computeTwentyOrmException } from 'src/engine/twenty-orm/error-handling/compute-twenty-orm-exception.util';+const logger = new Logger('runInRollbackSafeTransaction');export const runInRollbackSafeTransaction = async <T>({@@ -11,7 +13,9 @@ export const runInRollbackSafeTransaction = async <T>({pool: Pool;work: (client: PoolClient) => Promise<T>;}): Promise<T> => {- const client = await pool.connect();+ const client = await pool.connect().catch((error: unknown) => {+ throw computeTwentyOrmException(error);+ });let shouldDestroyConnection = false;try {@@ -37,7 +41,7 @@ export const runInRollbackSafeTransaction = async <T>({);}- throw error;+ throw computeTwentyOrmException(error);} finally {client.release(shouldDestroyConnection);}diff --git a/packages/twenty-server/src/engine/twenty-orm/error-handling/__tests__/compute-twenty-orm-exception.util.spec.ts b/packages/twenty-server/src/engine/twenty-orm/error-handling/__tests__/compute-twenty-orm-exception.util.spec.tsindex 573ac6db861..a437a1842cf 100644--- a/packages/twenty-server/src/engine/twenty-orm/error-handling/__tests__/compute-twenty-orm-exception.util.spec.ts+++ b/packages/twenty-server/src/engine/twenty-orm/error-handling/__tests__/compute-twenty-orm-exception.util.spec.ts@@ -68,6 +68,27 @@ describe('computeTwentyOrmException', () => {);});+ it.each([+ POSTGRESQL_ERROR_CODES.IDLE_IN_TRANSACTION_SESSION_TIMEOUT,+ POSTGRESQL_ERROR_CODES.CONNECTION_FAILURE,+ POSTGRESQL_ERROR_CODES.DEADLOCK_DETECTED,+ ])(+ 'should map the transient failure %s to TRANSIENT_DATABASE_ERROR',+ (code) => {+ const result = computeTwentyOrmException(+ buildPostgresError(+ 'terminating connection due to idle-in-transaction timeout',+ code,+ ),+ );++ expect(result).toBeInstanceOf(TwentyOrmException);+ expect((result as TwentyOrmException).code).toBe(+ TwentyOrmExceptionCode.TRANSIENT_DATABASE_ERROR,+ );+ },+ );+it('should map any other known postgres code to a PostgresException carrying that code', () => {const error = buildPostgresError('cannot execute UPDATE in a read-only transaction',@@ -83,9 +104,20 @@ describe('computeTwentyOrmException', () => {expect(result.message).toBe('Data validation error.');});- it('should return an unrecognised driver error untouched', () => {+ it('should return a TRANSIENT_DATABASE_ERROR exception when the socket dies before postgres reports a code', () => {const error = new Error('Connection terminated unexpectedly');+ const result = computeTwentyOrmException(error);++ expect(result).toBeInstanceOf(TwentyOrmException);+ expect((result as TwentyOrmException).code).toBe(+ TwentyOrmExceptionCode.TRANSIENT_DATABASE_ERROR,+ );+ });++ it('should return an unrecognised driver error untouched', () => {+ const error = new Error('some driver failure nobody maps');+expect(computeTwentyOrmException(error)).toBe(error);});diff --git a/packages/twenty-server/src/engine/twenty-orm/error-handling/compute-twenty-orm-exception.util.ts b/packages/twenty-server/src/engine/twenty-orm/error-handling/compute-twenty-orm-exception.util.tsindex 3c59af5d966..7e5c980edf4 100644--- a/packages/twenty-server/src/engine/twenty-orm/error-handling/compute-twenty-orm-exception.util.ts+++ b/packages/twenty-server/src/engine/twenty-orm/error-handling/compute-twenty-orm-exception.util.ts@@ -1,13 +1,16 @@import { isDefined } from 'twenty-shared/utils';import { POSTGRESQL_ERROR_CODES } from 'src/engine/api/graphql/workspace-query-runner/constants/postgres-error-codes.constants';+import { TRANSIENT_POSTGRESQL_ERROR_CODES } from 'src/engine/api/graphql/workspace-query-runner/constants/transient-postgres-error-codes.constants';import {+ CONNECTION_TERMINATED_MESSAGE,CONSTRAINT_VIOLATION_USER_FRIENDLY_MESSAGES,DUPLICATE_ENTRY_DETECTED_MESSAGE,DUPLICATE_ENTRY_USER_FRIENDLY_MESSAGE,INVALID_INPUT_USER_FRIENDLY_MESSAGE,QUERY_READ_TIMEOUT_MESSAGE,QUERY_READ_TIMEOUT_USER_FRIENDLY_MESSAGE,+ TRANSIENT_DATABASE_ERROR_USER_FRIENDLY_MESSAGE,} from 'src/engine/api/graphql/workspace-query-runner/constants/postgres-error-messages.constants';import { PostgresException } from 'src/engine/api/graphql/workspace-query-runner/utils/postgres-exception';import {@@ -20,12 +23,6 @@ const KNOWN_POSTGRES_ERROR_CODES: string[] = Object.values(POSTGRESQL_ERROR_CODES,);-const getPostgresErrorCode = (error: Error): string | undefined => {- const { code } = error as Error & { code?: unknown };-- return typeof code === 'string' ? code : undefined;-};-// The pg error carries the failing statement's detail; the sentry driver reads `cause`const withCause = <TError extends Error>(exception: TError,@@ -52,7 +49,23 @@ export const computeTwentyOrmException = (error: unknown): Error => {);}- const errorCode = getPostgresErrorCode(error);+ const errorCode =+ 'code' in error && typeof error.code === 'string' ? error.code : undefined;++ if (+ error.message.includes(CONNECTION_TERMINATED_MESSAGE) ||+ (isDefined(errorCode) &&+ TRANSIENT_POSTGRESQL_ERROR_CODES.includes(errorCode))+ ) {+ return withCause(+ new TwentyOrmException(+ error.message,+ TwentyOrmExceptionCode.TRANSIENT_DATABASE_ERROR,+ { userFriendlyMessage: TRANSIENT_DATABASE_ERROR_USER_FRIENDLY_MESSAGE },+ ),+ error,+ );+ }if (!isDefined(errorCode)) {return error;diff --git a/packages/twenty-server/src/engine/twenty-orm/exceptions/twenty-orm.exception.ts b/packages/twenty-server/src/engine/twenty-orm/exceptions/twenty-orm.exception.tsindex d382f24826c..a7bd7e61312 100644--- a/packages/twenty-server/src/engine/twenty-orm/exceptions/twenty-orm.exception.ts+++ b/packages/twenty-server/src/engine/twenty-orm/exceptions/twenty-orm.exception.ts@@ -3,7 +3,10 @@ import { msg } from '@lingui/core/macro';import { assertUnreachable } from 'twenty-shared/utils';import { STANDARD_ERROR_MESSAGE } from 'src/engine/api/common/common-query-runners/errors/standard-error-message.constant';-import { INVALID_INPUT_USER_FRIENDLY_MESSAGE } from 'src/engine/api/graphql/workspace-query-runner/constants/postgres-error-messages.constants';+import {+ INVALID_INPUT_USER_FRIENDLY_MESSAGE,+ TRANSIENT_DATABASE_ERROR_USER_FRIENDLY_MESSAGE,+} from 'src/engine/api/graphql/workspace-query-runner/constants/postgres-error-messages.constants';import { CustomException } from 'src/utils/custom-exception';export enum TwentyOrmExceptionCode {@@ -21,6 +24,7 @@ export enum TwentyOrmExceptionCode {METHOD_NOT_ALLOWED = 'METHOD_NOT_ALLOWED',ENUM_TYPE_NAME_NOT_FOUND = 'ENUM_TYPE_NAME_NOT_FOUND',QUERY_READ_TIMEOUT = 'QUERY_READ_TIMEOUT',+ TRANSIENT_DATABASE_ERROR = 'TRANSIENT_DATABASE_ERROR',DUPLICATE_ENTRY_DETECTED = 'DUPLICATE_ENTRY_DETECTED',TOO_MANY_RECORDS_TO_UPDATE = 'TOO_MANY_RECORDS_TO_UPDATE',INVALID_INPUT = 'INVALID_INPUT',@@ -69,6 +73,8 @@ const getTwentyOrmExceptionUserFriendlyMessage = (return msg`This operation is not allowed.`;case TwentyOrmExceptionCode.QUERY_READ_TIMEOUT:return msg`Query timed out. Please try again.`;+ case TwentyOrmExceptionCode.TRANSIENT_DATABASE_ERROR:+ return TRANSIENT_DATABASE_ERROR_USER_FRIENDLY_MESSAGE;case TwentyOrmExceptionCode.DUPLICATE_ENTRY_DETECTED:return msg`A duplicate entry was detected.`;case TwentyOrmExceptionCode.TOO_MANY_RECORDS_TO_UPDATE:diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-event-import-exception-handler.service.ts b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-event-import-exception-handler.service.tsindex 24347b5df97..34da0549a40 100644--- a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-event-import-exception-handler.service.ts+++ b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-event-import-exception-handler.service.ts@@ -57,6 +57,7 @@ export class CalendarEventImportErrorHandlerService {);break;case TwentyOrmExceptionCode.QUERY_READ_TIMEOUT:+ case TwentyOrmExceptionCode.TRANSIENT_DATABASE_ERROR:case CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR:case ConnectedAccountRefreshAccessTokenExceptionCode.TEMPORARY_NETWORK_ERROR:await this.handleTemporaryException(diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-save-events.service.ts b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-save-events.service.tsindex 3174265daf6..8cc21f4a69e 100644--- a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-save-events.service.ts+++ b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-save-events.service.ts@@ -1,23 +1,17 @@import { Injectable } from '@nestjs/common';-import { isDefined } from 'twenty-shared/utils';import { Any } from 'typeorm';-import { v4 as uuid } from 'uuid';import { type CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';+import { type ConnectedAccountEntity } from 'src/engine/metadata-modules/connected-account/entities/connected-account.entity';import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';+import { buildCalendarEventSaveOperations } from 'src/modules/calendar/calendar-event-import-manager/utils/build-calendar-event-save-operations.util';import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';+import { buildCalendarEventParticipantSaveOperations } from 'src/modules/calendar/calendar-event-participant-manager/utils/build-calendar-event-participant-save-operations.util';import { type CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';import { type CalendarEventWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event.workspace-entity';import { type FetchedCalendarEvent } from 'src/modules/calendar/common/types/fetched-calendar-event';-import { type ConnectedAccountEntity } from 'src/engine/metadata-modules/connected-account/entities/connected-account.entity';--type FetchedCalendarEventWithDBEvent = {- fetchedCalendarEvent: FetchedCalendarEvent;- existingCalendarEvent: CalendarEventWorkspaceEntity | null;- newlyCreatedCalendarEvent: CalendarEventWorkspaceEntity | null;-};@Injectable()export class CalendarSaveEventsService {@@ -34,283 +28,123 @@ export class CalendarSaveEventsService {): Promise<void> {const authContext = buildSystemAuthContext(workspaceId);- await this.workspaceOrmManager.executeInWorkspaceContext(- async () => {- await this.workspaceOrmManager.runInWorkspaceTransaction(- async (transactionScope) => {- const calendarEventRepository =- transactionScope.getRepository<CalendarEventWorkspaceEntity>(- 'calendarEvent',- );-- const calendarChannelEventAssociationRepository =- transactionScope.getRepository<CalendarChannelEventAssociationWorkspaceEntity>(- 'calendarChannelEventAssociation',- );-- const existingAssociations =- await calendarChannelEventAssociationRepository.find({- where: {- eventExternalId: Any(- fetchedCalendarEvents.map((event) => event.id),- ),- calendarChannelId: calendarChannel.id,- },- });-- const existingCalendarEventIdByExternalId = new Map(- existingAssociations.map((association) => [- association.eventExternalId,- association.calendarEventId,- ]),+ const { savedParticipantIds, calendarEventIds } =+ await this.workspaceOrmManager.executeInWorkspaceContext(+ async () => {+ const calendarChannelEventAssociationRepository =+ this.workspaceOrmManager.getRepository<CalendarChannelEventAssociationWorkspaceEntity>(+ 'calendarChannelEventAssociation',);- const existingAssociationIdByExternalId = new Map(- existingAssociations.map((association) => [- association.eventExternalId,- association.id,- ]),+ const existingAssociations =+ await calendarChannelEventAssociationRepository.find({+ where: {+ eventExternalId: Any(+ fetchedCalendarEvents.map((event) => event.id),+ ),+ calendarChannelId: calendarChannel.id,+ },+ });++ const {+ saveOperations,+ participantsOfNewEvents,+ participantsOfExistingEvents,+ } = buildCalendarEventSaveOperations({+ fetchedCalendarEvents,+ existingAssociations,+ calendarChannelId: calendarChannel.id,+ });++ const existingParticipants =+ await this.calendarEventParticipantService.findCalendarEventParticipantsByCalendarEventIds(+ {+ calendarEventIds: participantsOfExistingEvents.map(+ (participant) => participant.calendarEventId,+ ),+ },);- const fetchedCalendarEventsWithDBEvents: FetchedCalendarEventWithDBEvent[] =- fetchedCalendarEvents.map(- (event): FetchedCalendarEventWithDBEvent => {- const existingCalendarEventId =- existingCalendarEventIdByExternalId.get(event.id);-- return {- fetchedCalendarEvent: event,- existingCalendarEvent: existingCalendarEventId- ? ({- id: existingCalendarEventId,- } as CalendarEventWorkspaceEntity)- : null,- newlyCreatedCalendarEvent: null,- };- },- );-- const newCalendarEventIdByExternalId = new Map<string, string>();+ const participantOperations =+ buildCalendarEventParticipantSaveOperations({+ fetchedParticipants: [+ ...participantsOfNewEvents,+ ...participantsOfExistingEvents,+ ],+ existingParticipants,+ });++ await this.workspaceOrmManager.runInWorkspaceTransaction(+ async (transactionScope) => {+ const calendarEventRepository =+ transactionScope.getRepository<CalendarEventWorkspaceEntity>(+ 'calendarEvent',+ );- const newCalendarEventsToInsert = fetchedCalendarEventsWithDBEvents- .filter(- ({ existingCalendarEvent }) => existingCalendarEvent === null,- )- .map(({ fetchedCalendarEvent }) => {- const calendarEventId = uuid();+ const associationRepository =+ transactionScope.getRepository<CalendarChannelEventAssociationWorkspaceEntity>(+ 'calendarChannelEventAssociation',+ );- newCalendarEventIdByExternalId.set(- fetchedCalendarEvent.id,- calendarEventId,+ if (saveOperations.calendarEventsToInsert.length > 0) {+ await calendarEventRepository.insert(+ saveOperations.calendarEventsToInsert,);+ }- return {- id: calendarEventId,- iCalUid: fetchedCalendarEvent.iCalUid,- title: fetchedCalendarEvent.title,- description: fetchedCalendarEvent.description,- startsAt: fetchedCalendarEvent.startsAt,- endsAt: fetchedCalendarEvent.endsAt,- location: fetchedCalendarEvent.location,- isFullDay: fetchedCalendarEvent.isFullDay,- isCanceled: fetchedCalendarEvent.isCanceled,- conferenceSolution: fetchedCalendarEvent.conferenceSolution,- conferenceLink: {- primaryLinkLabel: fetchedCalendarEvent.conferenceLinkLabel,- primaryLinkUrl: fetchedCalendarEvent.conferenceLinkUrl,- secondaryLinks: [],- },- externalCreatedAt: fetchedCalendarEvent.externalCreatedAt,- externalUpdatedAt: fetchedCalendarEvent.externalUpdatedAt,- };- });+ if (saveOperations.calendarEventsToUpdate.length > 0) {+ await calendarEventRepository.updateMany(+ saveOperations.calendarEventsToUpdate,+ );+ }- if (newCalendarEventsToInsert.length > 0) {- await calendarEventRepository.insert(newCalendarEventsToInsert);- }+ if (saveOperations.associationsToInsert.length > 0) {+ await associationRepository.insert(+ saveOperations.associationsToInsert,+ );+ }- const fetchedCalendarEventsWithDBEventsEnrichedWithSavedEvents: FetchedCalendarEventWithDBEvent[] =- fetchedCalendarEventsWithDBEvents.map(- ({ fetchedCalendarEvent, existingCalendarEvent }) => {- const savedCalendarEventId =- newCalendarEventIdByExternalId.get(fetchedCalendarEvent.id);+ if (saveOperations.associationsToUpdate.length > 0) {+ await associationRepository.updateMany(+ saveOperations.associationsToUpdate,+ );+ }- return {- fetchedCalendarEvent,- existingCalendarEvent,- newlyCreatedCalendarEvent: savedCalendarEventId- ? ({- id: savedCalendarEventId,- } as CalendarEventWorkspaceEntity)- : null,- };+ await this.calendarEventParticipantService.writeCalendarEventParticipants(+ {+ operations: participantOperations,+ transactionScope,},);-- const existingEventsToUpdate =- fetchedCalendarEventsWithDBEventsEnrichedWithSavedEvents- .filter(- ({ existingCalendarEvent }) => existingCalendarEvent !== null,- )- .map(({ fetchedCalendarEvent, existingCalendarEvent }) => {- if (!existingCalendarEvent) {- throw new Error(- `Existing calendar event with iCalUid ${fetchedCalendarEvent.iCalUid} not found - should never happen`,- );- }-- return {- criteria: existingCalendarEvent.id,- partialEntity: {- iCalUid: fetchedCalendarEvent.iCalUid,- title: fetchedCalendarEvent.title,- description: fetchedCalendarEvent.description,- startsAt: fetchedCalendarEvent.startsAt,- endsAt: fetchedCalendarEvent.endsAt,- location: fetchedCalendarEvent.location,- isFullDay: fetchedCalendarEvent.isFullDay,- isCanceled: fetchedCalendarEvent.isCanceled,- conferenceSolution:- fetchedCalendarEvent.conferenceSolution,- conferenceLink: {- primaryLinkLabel:- fetchedCalendarEvent.conferenceLinkLabel,- primaryLinkUrl: fetchedCalendarEvent.conferenceLinkUrl,- secondaryLinks: [],- },- externalCreatedAt: fetchedCalendarEvent.externalCreatedAt,- externalUpdatedAt: fetchedCalendarEvent.externalUpdatedAt,- },- };- });-- if (existingEventsToUpdate.length > 0) {- await calendarEventRepository.updateMany(existingEventsToUpdate);- }-- const calendarChannelEventAssociationsToSave: Pick<- CalendarChannelEventAssociationWorkspaceEntity,- | 'calendarEventId'- | 'eventExternalId'- | 'calendarChannelId'- | 'recurringEventExternalId'- >[] = fetchedCalendarEventsWithDBEventsEnrichedWithSavedEvents- .filter(- ({ newlyCreatedCalendarEvent }) =>- newlyCreatedCalendarEvent !== null,- )- .map(({ fetchedCalendarEvent, newlyCreatedCalendarEvent }) => {- if (!newlyCreatedCalendarEvent?.id) {- throw new Error(- `Calendar event id not found for event with iCalUid ${fetchedCalendarEvent.iCalUid} - should never happen`,- );- }-- return {- calendarEventId: newlyCreatedCalendarEvent.id,- eventExternalId: fetchedCalendarEvent.id,- calendarChannelId: calendarChannel.id,- recurringEventExternalId:- fetchedCalendarEvent.recurringEventExternalId ?? '',- };- });-- if (calendarChannelEventAssociationsToSave.length > 0) {- await calendarChannelEventAssociationRepository.insert(- calendarChannelEventAssociationsToSave,- );- }-- const existingAssociationsToUpdate =- fetchedCalendarEventsWithDBEventsEnrichedWithSavedEvents- .filter(- ({ existingCalendarEvent }) => existingCalendarEvent !== null,- )- .map(({ fetchedCalendarEvent }) => ({- criteria: existingAssociationIdByExternalId.get(- fetchedCalendarEvent.id,- )!,- partialEntity: {- recurringEventExternalId:- fetchedCalendarEvent.recurringEventExternalId ?? '',- },- }));-- if (existingAssociationsToUpdate.length > 0) {- await calendarChannelEventAssociationRepository.updateMany(- existingAssociationsToUpdate,- );- }-- const participantsToCreate =- fetchedCalendarEventsWithDBEventsEnrichedWithSavedEvents- .filter(- ({ newlyCreatedCalendarEvent }) =>- newlyCreatedCalendarEvent !== null,- )- .flatMap(- ({ newlyCreatedCalendarEvent, fetchedCalendarEvent }) => {- if (!newlyCreatedCalendarEvent?.id) {- throw new Error(- `Newly created calendar event with iCalUid ${fetchedCalendarEvent.iCalUid} not found - should never happen`,- );- }-- return fetchedCalendarEvent.participants.map(- (participant) => ({- ...participant,- calendarEventId: newlyCreatedCalendarEvent.id,- }),- );- },- );-- // todo: we should prevent duplicate rows on calendarEventAssociation by creating- // an index on calendarChannelId and calendarEventId- const participantsToUpdate =- fetchedCalendarEventsWithDBEventsEnrichedWithSavedEvents- .filter(- ({ existingCalendarEvent }) => existingCalendarEvent !== null,- )- .flatMap(({ fetchedCalendarEvent, existingCalendarEvent }) => {- if (!existingCalendarEvent?.id) {- throw new Error(- `Existing calendar event with iCalUid ${fetchedCalendarEvent.iCalUid} not found - should never happen`,- );- }-- return fetchedCalendarEvent.participants.map(- (participant) => ({- ...participant,- calendarEventId: existingCalendarEvent.id,- }),- );- });-- const calendarEventIds =- fetchedCalendarEventsWithDBEventsEnrichedWithSavedEvents- .map(- ({ newlyCreatedCalendarEvent, existingCalendarEvent }) =>- newlyCreatedCalendarEvent?.id ?? existingCalendarEvent?.id,- )- .filter(isDefined);-- await this.calendarEventParticipantService.upsertAndDeleteCalendarEventParticipants(- {- participantsToCreate,- participantsToUpdate,- transactionScope,- calendarChannel,- connectedAccount,- workspaceId,- calendarEventIds,- },- );- },- );+ },+ );++ return {+ savedParticipantIds: participantOperations.participantsToInsert.map(+ (participant) => participant.id,+ ),+ calendarEventIds: [+ ...saveOperations.associationsToInsert.map(+ ({ calendarEventId }) => calendarEventId,+ ),+ ...saveOperations.calendarEventsToUpdate.map(+ ({ criteria }) => criteria,+ ),+ ],+ };+ },+ authContext,+ { lite: true },+ );++ await this.calendarEventParticipantService.matchParticipantsAndEnqueueContactCreationJob(+ {+ savedParticipantIds,+ calendarEventIds,+ calendarChannel,+ connectedAccount,+ workspaceId,},- authContext,- { lite: true },);}}diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/types/calendar-event-save-operations.type.ts b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/types/calendar-event-save-operations.type.tsnew file mode 100644index 00000000000..de66caa88fd--- /dev/null+++ b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/types/calendar-event-save-operations.type.ts@@ -0,0 +1,23 @@+import { type QueryDeepPartialEntityWithNestedRelationFields } from 'src/engine/twenty-orm/entity-manager/types/query-deep-partial-entity-with-nested-relation-fields.type';++import { type CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';+import { type CalendarEventWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event.workspace-entity';++export type CalendarEventSaveOperations = {+ calendarEventsToInsert: QueryDeepPartialEntityWithNestedRelationFields<CalendarEventWorkspaceEntity>[];+ calendarEventsToUpdate: {+ criteria: string;+ partialEntity: Partial<CalendarEventWorkspaceEntity>;+ }[];+ associationsToInsert: Pick<+ CalendarChannelEventAssociationWorkspaceEntity,+ | 'calendarEventId'+ | 'eventExternalId'+ | 'calendarChannelId'+ | 'recurringEventExternalId'+ >[];+ associationsToUpdate: {+ criteria: string;+ partialEntity: Partial<CalendarChannelEventAssociationWorkspaceEntity>;+ }[];+};diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/types/calendar-event-save-plan.type.ts b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/types/calendar-event-save-plan.type.tsnew file mode 100644index 00000000000..eaf2764587b--- /dev/null+++ b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/types/calendar-event-save-plan.type.ts@@ -0,0 +1,8 @@+import { type CalendarEventSaveOperations } from 'src/modules/calendar/calendar-event-import-manager/types/calendar-event-save-operations.type';+import { type FetchedParticipantWithCalendarEventId } from 'src/modules/calendar/common/types/fetched-participant-with-calendar-event-id.type';++export type CalendarEventSavePlan = {+ saveOperations: CalendarEventSaveOperations;+ participantsOfNewEvents: FetchedParticipantWithCalendarEventId[];+ participantsOfExistingEvents: FetchedParticipantWithCalendarEventId[];+};diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/utils/build-calendar-event-save-operations.util.ts b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/utils/build-calendar-event-save-operations.util.tsnew file mode 100644index 00000000000..52327206635--- /dev/null+++ b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/utils/build-calendar-event-save-operations.util.ts@@ -0,0 +1,111 @@+import { isDefined } from 'twenty-shared/utils';+import { v4 as uuid } from 'uuid';++import { type CalendarEventSaveOperations } from 'src/modules/calendar/calendar-event-import-manager/types/calendar-event-save-operations.type';+import { type CalendarEventSavePlan } from 'src/modules/calendar/calendar-event-import-manager/types/calendar-event-save-plan.type';+import { type CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';+import { type FetchedCalendarEvent } from 'src/modules/calendar/common/types/fetched-calendar-event';+import { type FetchedParticipantWithCalendarEventId } from 'src/modules/calendar/common/types/fetched-participant-with-calendar-event-id.type';++export const buildCalendarEventSaveOperations = ({+ fetchedCalendarEvents,+ existingAssociations,+ calendarChannelId,+}: {+ fetchedCalendarEvents: FetchedCalendarEvent[];+ existingAssociations: CalendarChannelEventAssociationWorkspaceEntity[];+ calendarChannelId: string;+}): CalendarEventSavePlan => {+ const existingAssociationByEventExternalId = new Map(+ existingAssociations.map((association) => [+ association.eventExternalId,+ association,+ ]),+ );++ const saveOperations: CalendarEventSaveOperations = {+ calendarEventsToInsert: [],+ calendarEventsToUpdate: [],+ associationsToInsert: [],+ associationsToUpdate: [],+ };+ const participantsOfNewEvents: FetchedParticipantWithCalendarEventId[] = [];+ const participantsOfExistingEvents: FetchedParticipantWithCalendarEventId[] =+ [];++ for (const fetchedCalendarEvent of fetchedCalendarEvents) {+ const {+ id: eventExternalId,+ participants,+ recurringEventExternalId = '',+ } = fetchedCalendarEvent;++ const calendarEvent = {+ iCalUid: fetchedCalendarEvent.iCalUid,+ title: fetchedCalendarEvent.title,+ description: fetchedCalendarEvent.description,+ startsAt: fetchedCalendarEvent.startsAt,+ endsAt: fetchedCalendarEvent.endsAt,+ location: fetchedCalendarEvent.location,+ isFullDay: fetchedCalendarEvent.isFullDay,+ isCanceled: fetchedCalendarEvent.isCanceled,+ conferenceSolution: fetchedCalendarEvent.conferenceSolution,+ conferenceLink: {+ primaryLinkLabel: fetchedCalendarEvent.conferenceLinkLabel,+ primaryLinkUrl: fetchedCalendarEvent.conferenceLinkUrl,+ secondaryLinks: [],+ },+ externalCreatedAt: fetchedCalendarEvent.externalCreatedAt,+ externalUpdatedAt: fetchedCalendarEvent.externalUpdatedAt,+ };++ const existingAssociation =+ existingAssociationByEventExternalId.get(eventExternalId);++ if (isDefined(existingAssociation)) {+ const { calendarEventId } = existingAssociation;++ saveOperations.calendarEventsToUpdate.push({+ criteria: calendarEventId,+ partialEntity: calendarEvent,+ });+ saveOperations.associationsToUpdate.push({+ criteria: existingAssociation.id,+ partialEntity: { recurringEventExternalId },+ });+ participantsOfExistingEvents.push(+ ...participants.map((participant) => ({+ ...participant,+ calendarEventId,+ })),+ );++ continue;+ }++ const calendarEventId = uuid();++ saveOperations.calendarEventsToInsert.push({+ id: calendarEventId,+ ...calendarEvent,+ });+ saveOperations.associationsToInsert.push({+ calendarEventId,+ eventExternalId,+ calendarChannelId,+ recurringEventExternalId,+ });+ participantsOfNewEvents.push(+ ...participants.map((participant) => ({+ ...participant,+ calendarEventId,+ })),+ );+ }++ return {+ saveOperations,+ participantsOfNewEvents,+ participantsOfExistingEvents,+ };+};diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/constants/calendar-event-participant-chunk-size.ts b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/constants/calendar-event-participant-chunk-size.tsnew file mode 100644index 00000000000..d6bd8dc3d05--- /dev/null+++ b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/constants/calendar-event-participant-chunk-size.ts@@ -0,0 +1 @@+export const CALENDAR_EVENT_PARTICIPANT_CHUNK_SIZE = 200;diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service.ts b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service.tsindex 2dd7f2dee72..b600c9ff457 100644--- a/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service.ts+++ b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service.ts@@ -1,37 +1,26 @@import { Injectable } from '@nestjs/common';-import { isDefined } from 'class-validator';import chunk from 'lodash.chunk';-import differenceWith from 'lodash.differencewith';import { FieldActorSource } from 'twenty-shared/types';import { Any, In } from 'typeorm';-import { type CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';-import { type WorkspaceTransactionScope } from 'src/engine/twenty-orm/types/workspace-transaction-scope.type';+import { type CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';+import { type ConnectedAccountEntity } from 'src/engine/metadata-modules/connected-account/entities/connected-account.entity';import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';+import { type WorkspaceTransactionScope } from 'src/engine/twenty-orm/types/workspace-transaction-scope.type';import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';+import { CALENDAR_EVENT_PARTICIPANT_CHUNK_SIZE } from 'src/modules/calendar/calendar-event-participant-manager/constants/calendar-event-participant-chunk-size';+import { type CalendarEventParticipantSaveOperations } from 'src/modules/calendar/calendar-event-participant-manager/types/calendar-event-participant-save-operations.type';import { type CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';-import { type FetchedCalendarEventParticipant } from 'src/modules/calendar/common/types/fetched-calendar-event';-import { type ConnectedAccountEntity } from 'src/engine/metadata-modules/connected-account/entities/connected-account.entity';import {CreateCompanyAndContactJob,type CreateCompanyAndContactJobData,} from 'src/modules/contact-creation-manager/jobs/create-company-and-contact.job';import { MatchParticipantService } from 'src/modules/match-participant/match-participant.service';-type FetchedCalendarEventParticipantWithCalendarEventId =- FetchedCalendarEventParticipant & {- calendarEventId: string;- };--type FetchedCalendarEventParticipantWithCalendarEventIdAndExistingId =- FetchedCalendarEventParticipantWithCalendarEventId & {- id: string;- };-@Injectable()export class CalendarEventParticipantService {constructor(@@ -41,18 +30,61 @@ export class CalendarEventParticipantService {private readonly messageQueueService: MessageQueueService,) {}- public async upsertAndDeleteCalendarEventParticipants({- participantsToCreate,- participantsToUpdate,+ public async findCalendarEventParticipantsByCalendarEventIds({+ calendarEventIds,+ }: {+ calendarEventIds: string[];+ }): Promise<CalendarEventParticipantWorkspaceEntity[]> {+ const calendarEventParticipantRepository =+ this.workspaceOrmManager.getRepository<CalendarEventParticipantWorkspaceEntity>(+ 'calendarEventParticipant',+ );++ return calendarEventParticipantRepository.find({+ where: { calendarEventId: Any([...new Set(calendarEventIds)]) },+ });+ }++ public async writeCalendarEventParticipants({+ operations,transactionScope,+ }: {+ operations: CalendarEventParticipantSaveOperations;+ transactionScope: WorkspaceTransactionScope;+ }): Promise<void> {+ const calendarEventParticipantRepository =+ transactionScope.getRepository<CalendarEventParticipantWorkspaceEntity>(+ 'calendarEventParticipant',+ );++ if (operations.participantIdsToDelete.length > 0) {+ await calendarEventParticipantRepository.delete({+ id: Any(operations.participantIdsToDelete),+ });+ }++ if (operations.participantsToUpdate.length > 0) {+ await calendarEventParticipantRepository.updateMany(+ operations.participantsToUpdate,+ );+ }++ for (const participantsChunk of chunk(+ operations.participantsToInsert,+ CALENDAR_EVENT_PARTICIPANT_CHUNK_SIZE,+ )) {+ await calendarEventParticipantRepository.insert(participantsChunk);+ }+ }++ public async matchParticipantsAndEnqueueContactCreationJob({+ savedParticipantIds,calendarChannel,connectedAccount,workspaceId,calendarEventIds,}: {- participantsToCreate: FetchedCalendarEventParticipantWithCalendarEventId[];- participantsToUpdate: FetchedCalendarEventParticipantWithCalendarEventId[];- transactionScope: WorkspaceTransactionScope;+ savedParticipantIds: string[];calendarChannel: CalendarChannelEntity;connectedAccount: ConnectedAccountEntity;workspaceId: string;@@ -62,101 +94,23 @@ export class CalendarEventParticipantService {await this.workspaceOrmManager.executeInWorkspaceContext(async () => {- const chunkedParticipantsToUpdate = chunk(participantsToUpdate, 200);-const calendarEventParticipantRepository =- transactionScope.getRepository<CalendarEventParticipantWorkspaceEntity>(+ this.workspaceOrmManager.getRepository<CalendarEventParticipantWorkspaceEntity>('calendarEventParticipant',);- for (const participantsToUpdateChunk of chunkedParticipantsToUpdate) {- const existingCalendarEventParticipants =- await calendarEventParticipantRepository.find({- where: {- calendarEventId: Any(- participantsToUpdateChunk- .map((participant) => participant.calendarEventId)- .filter(isDefined),- ),- },- });-- const {- calendarEventParticipantsToUpdate,- newCalendarEventParticipants,- } = participantsToUpdateChunk.reduce<{- calendarEventParticipantsToUpdate: FetchedCalendarEventParticipantWithCalendarEventIdAndExistingId[];- newCalendarEventParticipants: FetchedCalendarEventParticipantWithCalendarEventId[];- }>(- (acc, calendarEventParticipant) => {- const existingCalendarEventParticipant =- existingCalendarEventParticipants.find(- (existingCalendarEventParticipant) =>- existingCalendarEventParticipant.handle ===- calendarEventParticipant.handle &&- existingCalendarEventParticipant.calendarEventId ===- calendarEventParticipant.calendarEventId,- );-- if (existingCalendarEventParticipant) {- acc.calendarEventParticipantsToUpdate.push({- ...calendarEventParticipant,- id: existingCalendarEventParticipant.id,- });- } else {- acc.newCalendarEventParticipants.push(calendarEventParticipant);- }-- return acc;- },- {- calendarEventParticipantsToUpdate: [],- newCalendarEventParticipants: [],- },- );-- const calendarEventParticipantsToDelete = differenceWith(- existingCalendarEventParticipants,- participantsToUpdateChunk,- (existingCalendarEventParticipant, participantToUpdate) =>- existingCalendarEventParticipant.handle ===- participantToUpdate.handle &&- existingCalendarEventParticipant.calendarEventId ===- participantToUpdate.calendarEventId,- );-- await calendarEventParticipantRepository.delete({- id: Any(- calendarEventParticipantsToDelete.map(- (calendarEventParticipant) => calendarEventParticipant.id,- ),- ),- });-- await calendarEventParticipantRepository.updateMany(- calendarEventParticipantsToUpdate.map((participant) => ({- criteria: participant.id,- partialEntity: participant,- })),- );- participantsToCreate.push(...newCalendarEventParticipants);- }-- const chunkedParticipantsToCreate = chunk(participantsToCreate, 200);const savedParticipants: CalendarEventParticipantWorkspaceEntity[] = [];- for (const participantsToCreateChunk of chunkedParticipantsToCreate) {- const { identifiers } =- await calendarEventParticipantRepository.insert(- participantsToCreateChunk,- );-- const insertedParticipants =+ for (const savedParticipantIdsChunk of chunk(+ savedParticipantIds,+ CALENDAR_EVENT_PARTICIPANT_CHUNK_SIZE,+ )) {+ const participantsChunk =await calendarEventParticipantRepository.find({- where: { id: In(identifiers.map(({ id }) => id)) },+ where: { id: In(savedParticipantIdsChunk) },});- savedParticipants.push(...insertedParticipants);+ savedParticipants.push(...participantsChunk);}if (calendarChannel.isContactAutoCreationEnabled) {@@ -180,7 +134,6 @@ export class CalendarEventParticipantService {sourceRecordIds: calendarEventIds,objectMetadataName: 'calendarEventParticipant',matchWith: 'workspaceMemberAndPerson',- transactionScope,});},authContext,diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/types/calendar-event-participant-save-operations.type.ts b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/types/calendar-event-participant-save-operations.type.tsnew file mode 100644index 00000000000..319be19085f--- /dev/null+++ b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/types/calendar-event-participant-save-operations.type.ts@@ -0,0 +1,14 @@+import { type CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';+import { type FetchedCalendarEventParticipant } from 'src/modules/calendar/common/types/fetched-calendar-event';++export type CalendarEventParticipantSaveOperations = {+ participantsToInsert: (FetchedCalendarEventParticipant & {+ calendarEventId: string;+ id: string;+ })[];+ participantsToUpdate: {+ criteria: string;+ partialEntity: Partial<CalendarEventParticipantWorkspaceEntity>;+ }[];+ participantIdsToDelete: string[];+};diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/utils/build-calendar-event-participant-save-operations.util.ts b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/utils/build-calendar-event-participant-save-operations.util.tsnew file mode 100644index 00000000000..08eb6bcbdb2--- /dev/null+++ b/packages/twenty-server/src/modules/calendar/calendar-event-participant-manager/utils/build-calendar-event-participant-save-operations.util.ts@@ -0,0 +1,77 @@+import { isDefined } from 'twenty-shared/utils';+import { v4 as uuid } from 'uuid';++import { type CalendarEventParticipantSaveOperations } from 'src/modules/calendar/calendar-event-participant-manager/types/calendar-event-participant-save-operations.type';+import { type CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';+import { type FetchedParticipantWithCalendarEventId } from 'src/modules/calendar/common/types/fetched-participant-with-calendar-event-id.type';++const buildParticipantKey = ({+ calendarEventId,+ handle,+}: {+ calendarEventId: string | null;+ handle: string | null;+}): string => `${calendarEventId}:${handle}`;++export const buildCalendarEventParticipantSaveOperations = ({+ fetchedParticipants,+ existingParticipants,+}: {+ fetchedParticipants: FetchedParticipantWithCalendarEventId[];+ existingParticipants: CalendarEventParticipantWorkspaceEntity[];+}): CalendarEventParticipantSaveOperations => {+ const existingParticipantByKey = new Map<+ string,+ CalendarEventParticipantWorkspaceEntity+ >();++ for (const existingParticipant of existingParticipants) {+ const participantKey = buildParticipantKey(existingParticipant);++ if (existingParticipantByKey.has(participantKey)) {+ continue;+ }++ existingParticipantByKey.set(participantKey, existingParticipant);+ }++ const operations: CalendarEventParticipantSaveOperations = {+ participantsToInsert: [],+ participantsToUpdate: [],+ participantIdsToDelete: [],+ };++ for (const fetchedParticipant of fetchedParticipants) {+ const existingParticipant = existingParticipantByKey.get(+ buildParticipantKey(fetchedParticipant),+ );++ if (!isDefined(existingParticipant)) {+ operations.participantsToInsert.push({+ ...fetchedParticipant,+ id: uuid(),+ });++ continue;+ }++ operations.participantsToUpdate.push({+ criteria: existingParticipant.id,+ partialEntity: fetchedParticipant,+ });+ }++ const fetchedParticipantKeys = new Set(+ fetchedParticipants.map(buildParticipantKey),+ );++ for (const existingParticipant of existingParticipants) {+ if (fetchedParticipantKeys.has(buildParticipantKey(existingParticipant))) {+ continue;+ }++ operations.participantIdsToDelete.push(existingParticipant.id);+ }++ return operations;+};diff --git a/packages/twenty-server/src/modules/calendar/common/types/fetched-participant-with-calendar-event-id.type.ts b/packages/twenty-server/src/modules/calendar/common/types/fetched-participant-with-calendar-event-id.type.tsnew file mode 100644index 00000000000..1afca293631--- /dev/null+++ b/packages/twenty-server/src/modules/calendar/common/types/fetched-participant-with-calendar-event-id.type.ts@@ -0,0 +1,6 @@+import { type FetchedCalendarEventParticipant } from 'src/modules/calendar/common/types/fetched-calendar-event';++export type FetchedParticipantWithCalendarEventId =+ FetchedCalendarEventParticipant & {+ calendarEventId: string;+ };diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-import-exception-handler.service.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-import-exception-handler.service.tsindex fed6378b95d..34f107fc9a7 100644--- a/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-import-exception-handler.service.ts+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-import-exception-handler.service.ts@@ -72,6 +72,7 @@ export class MessageImportExceptionHandlerService {);break;case TwentyOrmExceptionCode.QUERY_READ_TIMEOUT:+ case TwentyOrmExceptionCode.TRANSIENT_DATABASE_ERROR:case MessageImportDriverExceptionCode.TEMPORARY_ERROR:case ConnectedAccountRefreshAccessTokenExceptionCode.TEMPORARY_NETWORK_ERROR:case MessageNetworkExceptionCode.ECONNABORTED:diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-save-messages-and-enqueue-contact-creation.service.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-save-messages-and-enqueue-contact-creation.service.tsindex 24dcd86eec7..505c0732350 100644--- a/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-save-messages-and-enqueue-contact-creation.service.ts+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/services/messaging-save-messages-and-enqueue-contact-creation.service.ts@@ -125,11 +125,12 @@ export class MessagingSaveMessagesAndEnqueueContactCreationService {: [];});- await this.messageParticipantService.saveMessageParticipants(- participantsWithMessageId,- workspaceId,- transactionScope,- );+ const savedMessageParticipants =+ await this.messageParticipantService.saveMessageParticipants(+ participantsWithMessageId,+ workspaceId,+ transactionScope,+ );const folderAssociations: MessageChannelMessageAssociationFolderAssociation[] =messagesToSave.flatMap((message) => {@@ -164,6 +165,7 @@ export class MessagingSaveMessagesAndEnqueueContactCreationService {return {participantsWithMessageId,+ savedMessageParticipants,messageExternalIdsAndIdsMap,messageExternalIdToMessageThreadIdMap,};@@ -195,6 +197,18 @@ export class MessagingSaveMessagesAndEnqueueContactCreationService {return undefined;}+ await this.messageParticipantService.matchMessageParticipants({+ participants: savedMessagesResult.savedMessageParticipants,+ messageIds: [+ ...new Set(+ savedMessagesResult.participantsWithMessageId.map(+ ({ messageId }) => messageId,+ ),+ ),+ ],+ workspaceId,+ });+return {messageExternalIdsAndIdsMap:savedMessagesResult.messageExternalIdsAndIdsMap,diff --git a/packages/twenty-server/src/modules/messaging/message-participant-manager/services/messaging-message-participant.service.ts b/packages/twenty-server/src/modules/messaging/message-participant-manager/services/messaging-message-participant.service.tsindex 1217657d835..f8d1847333a 100644--- a/packages/twenty-server/src/modules/messaging/message-participant-manager/services/messaging-message-participant.service.ts+++ b/packages/twenty-server/src/modules/messaging/message-participant-manager/services/messaging-message-participant.service.ts@@ -20,10 +20,10 @@ export class MessagingMessageParticipantService {participants: ParticipantWithMessageId[],workspaceId: string,transactionScope: WorkspaceTransactionScope,- ): Promise<void> {+ ): Promise<MessageParticipantWorkspaceEntity[]> {const authContext = buildSystemAuthContext(workspaceId);- await this.workspaceOrmManager.executeInWorkspaceContext(+ return this.workspaceOrmManager.executeInWorkspaceContext(async () => {const messageParticipantRepository =transactionScope.getRepository<MessageParticipantWorkspaceEntity>(@@ -65,18 +65,33 @@ export class MessagingMessageParticipantService {const { identifiers } =await messageParticipantRepository.insert(participantsToCreate);- const createdParticipants = await messageParticipantRepository.find({+ return messageParticipantRepository.find({where: { id: In(identifiers.map(({ id }) => id)) },});+ },+ authContext,+ { lite: true },+ );+ }++ public async matchMessageParticipants({+ participants,+ messageIds,+ workspaceId,+ }: {+ participants: MessageParticipantWorkspaceEntity[];+ messageIds: string[];+ workspaceId: string;+ }): Promise<void> {+ const authContext = buildSystemAuthContext(workspaceId);+ await this.workspaceOrmManager.executeInWorkspaceContext(+ async () => {await this.matchParticipantService.matchParticipants({- participants: createdParticipants,- sourceRecordIds: [- ...new Set(participants.map(({ messageId }) => messageId)),- ],+ participants,+ sourceRecordIds: messageIds,objectMetadataName: 'messageParticipant',matchWith: 'workspaceMemberAndPerson',- transactionScope,});},authContext,diff --git a/packages/twenty-server/test/integration/google/calendar/event-save-operations.integration-spec.ts b/packages/twenty-server/test/integration/google/calendar/event-save-operations.integration-spec.tsnew file mode 100644index 00000000000..2f7f547b406--- /dev/null+++ b/packages/twenty-server/test/integration/google/calendar/event-save-operations.integration-spec.ts@@ -0,0 +1,378 @@+import { randomUUID } from 'node:crypto';++import { type calendar_v3 } from 'googleapis';+import { ConnectedAccountProvider } from 'twenty-shared/types';++import { CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';+import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';+import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';++import { createOneOperationFactory } from 'test/integration/graphql/utils/create-one-operation-factory.util';+import { makeGraphqlAPIRequest } from 'test/integration/graphql/utils/make-graphql-api-request.util';+import { googleCalendarEvent } from 'test/integration/google/mocks/google-calendar-event.util';+import { setupGoogleMock } from 'test/integration/google/mocks/setup-google-mock.util';+import { connectMessagingAccount } from 'test/integration/utils/connect-messaging-account.util';+import { findRecordNodesByFilter } from 'test/integration/utils/find-records-by-filter.util';+import { getCoreRepository } from 'test/integration/utils/get-core-repository.util';+import { resetCalendarChannelSyncState } from 'test/integration/utils/reset-channel-sync-state.util';+import { runCalendarChannelEventsImport } from 'test/integration/utils/run-calendar-channel-events-import.util';+import { runCalendarChannelListFetch } from 'test/integration/utils/run-calendar-channel-list-fetch.util';++const HANDLE = '[email protected]';++type CalendarEventNode = {+ id: string;+ title: string;+ location: string;+ isFullDay: boolean;+};++type AssociationNode = {+ id: string;+ eventExternalId: string;+ recurringEventExternalId: string;+ calendarEventId: string;+};++type ParticipantNode = {+ id: string;+ handle: string;+ displayName: string;+ isOrganizer: boolean;+ responseStatus: string;+ calendarEventId: string;+};++describe('Calendar event save operations (integration)', () => {+ const google = setupGoogleMock({ handle: HANDLE });++ let channel: Awaited<ReturnType<typeof connectMessagingAccount>>;++ const importEvents = async (+ events: calendar_v3.Schema$Event[],+ ): Promise<void> => {+ google.serveCalendarEvents(events, {+ nextSyncToken: `sync-token-${randomUUID()}`,+ });++ await resetCalendarChannelSyncState(channel.calendarChannelId, '');+ await runCalendarChannelListFetch(channel.calendarChannelId);+ await runCalendarChannelEventsImport(channel.calendarChannelId);+ };++ const importEvent = (event: calendar_v3.Schema$Event): Promise<void> =>+ importEvents([event]);++ const findEventsByTitle = (title: string): Promise<CalendarEventNode[]> =>+ findRecordNodesByFilter<CalendarEventNode>(+ 'calendarEvent',+ 'calendarEvents',+ 'id title location isFullDay',+ { title: { eq: title } },+ );++ const findAssociationsByExternalId = (+ eventExternalId: string,+ ): Promise<AssociationNode[]> =>+ findRecordNodesByFilter<AssociationNode>(+ 'calendarChannelEventAssociation',+ 'calendarChannelEventAssociations',+ 'id eventExternalId recurringEventExternalId calendarEventId',+ { eventExternalId: { eq: eventExternalId } },+ );++ const findParticipantsByCalendarEventId = async (+ calendarEventId: string,+ ): Promise<ParticipantNode[]> => {+ const participants = await findRecordNodesByFilter<ParticipantNode>(+ 'calendarEventParticipant',+ 'calendarEventParticipants',+ 'id handle displayName isOrganizer responseStatus calendarEventId',+ { calendarEventId: { eq: calendarEventId } },+ );++ return participants.sort((a, b) => a.handle.localeCompare(b.handle));+ };++ const countParticipantsByHandleSuffix = async (+ handleSuffix: string,+ ): Promise<number> => {+ const rows: { count: string }[] =+ await getCoreRepository<CalendarChannelEntity>(+ CalendarChannelEntity,+ ).manager.query(+ `SELECT count(*) AS count FROM "${getWorkspaceSchemaName(+ SEED_APPLE_WORKSPACE_ID,+ )}"."calendarEventParticipant" WHERE handle LIKE $1`,+ [`%${handleSuffix}`],+ );++ return Number(rows[0].count);+ };++ beforeAll(async () => {+ channel = await connectMessagingAccount({+ provider: ConnectedAccountProvider.GOOGLE,+ handle: HANDLE,+ });+ }, 60000);++ afterAll(async () => {+ await channel?.cleanup().catch(() => undefined);+ });++ it('should insert the event, its channel association and its participants on a first import', async () => {+ const eventExternalId = `google-calendar-event-${randomUUID()}`;+ const title = `Calendar event ${randomUUID()}`;+ const attendee = `attendee-${randomUUID()}@acme.com`;++ await importEvent(+ googleCalendarEvent({+ id: eventExternalId,+ summary: title,+ location: 'Room 1',+ attendees: [+ {+ email: attendee,+ displayName: 'Ada Lovelace',+ organizer: true,+ responseStatus: 'accepted',+ },+ ],+ }),+ );++ const events = await findEventsByTitle(title);++ expect(events).toHaveLength(1);+ expect(events[0].location).toBe('Room 1');+ expect(events[0].isFullDay).toBe(false);++ const associations = await findAssociationsByExternalId(eventExternalId);++ expect(associations).toHaveLength(1);+ expect(associations[0].calendarEventId).toBe(events[0].id);+ expect(associations[0].recurringEventExternalId).toBe('');++ const participants = await findParticipantsByCalendarEventId(events[0].id);++ expect(participants).toHaveLength(1);+ expect(participants[0].handle).toBe(attendee);+ expect(participants[0].displayName).toBe('Ada Lovelace');+ expect(participants[0].isOrganizer).toBe(true);+ expect(participants[0].responseStatus).toBe('ACCEPTED');+ }, 120000);++ it('should update the existing event and association rather than insert a second one when the same external event is imported again', async () => {+ const eventExternalId = `google-calendar-event-${randomUUID()}`;+ const initialTitle = `Calendar event ${randomUUID()}`;+ const updatedTitle = `Calendar event ${randomUUID()}`;+ const recurringEventExternalId = `recurring-${randomUUID()}`;+ const attendee = `attendee-${randomUUID()}@acme.com`;++ await importEvent(+ googleCalendarEvent({+ id: eventExternalId,+ summary: initialTitle,+ location: 'Room 1',+ attendees: [{ email: attendee }],+ }),+ );++ const [initialEvent] = await findEventsByTitle(initialTitle);++ await importEvent(+ googleCalendarEvent({+ id: eventExternalId,+ summary: updatedTitle,+ location: 'Room 2',+ recurringEventId: recurringEventExternalId,+ attendees: [{ email: attendee }],+ }),+ );++ expect(await findEventsByTitle(initialTitle)).toHaveLength(0);++ const updatedEvents = await findEventsByTitle(updatedTitle);++ expect(updatedEvents).toHaveLength(1);+ expect(updatedEvents[0].id).toBe(initialEvent.id);+ expect(updatedEvents[0].location).toBe('Room 2');++ const associations = await findAssociationsByExternalId(eventExternalId);++ expect(associations).toHaveLength(1);+ expect(associations[0].recurringEventExternalId).toBe(+ recurringEventExternalId,+ );+ }, 180000);++ it('should update an existing participant in place when its attributes change', async () => {+ const eventExternalId = `google-calendar-event-${randomUUID()}`;+ const title = `Calendar event ${randomUUID()}`;+ const attendee = `attendee-${randomUUID()}@acme.com`;++ await importEvent(+ googleCalendarEvent({+ id: eventExternalId,+ summary: title,+ attendees: [+ {+ email: attendee,+ displayName: 'Before',+ responseStatus: 'needsAction',+ },+ ],+ }),+ );++ const [event] = await findEventsByTitle(title);+ const [initialParticipant] = await findParticipantsByCalendarEventId(+ event.id,+ );++ expect(initialParticipant.displayName).toBe('Before');+ expect(initialParticipant.responseStatus).toBe('NEEDS_ACTION');++ await importEvent(+ googleCalendarEvent({+ id: eventExternalId,+ summary: title,+ attendees: [+ {+ email: attendee,+ displayName: 'After',+ organizer: true,+ responseStatus: 'declined',+ },+ ],+ }),+ );++ const participants = await findParticipantsByCalendarEventId(event.id);++ expect(participants).toHaveLength(1);+ expect(participants[0].id).toBe(initialParticipant.id);+ expect(participants[0].displayName).toBe('After');+ expect(participants[0].isOrganizer).toBe(true);+ expect(participants[0].responseStatus).toBe('DECLINED');+ }, 180000);++ it('should insert an added attendee and delete a removed one while leaving the unchanged attendee alone', async () => {+ const eventExternalId = `google-calendar-event-${randomUUID()}`;+ const title = `Calendar event ${randomUUID()}`;+ const keptAttendee = `attendee-kept-${randomUUID()}@acme.com`;+ const removedAttendee = `attendee-removed-${randomUUID()}@acme.com`;+ const addedAttendee = `attendee-added-${randomUUID()}@acme.com`;++ await importEvent(+ googleCalendarEvent({+ id: eventExternalId,+ summary: title,+ attendees: [{ email: keptAttendee }, { email: removedAttendee }],+ }),+ );++ const [event] = await findEventsByTitle(title);+ const initialParticipants = await findParticipantsByCalendarEventId(+ event.id,+ );++ expect(+ initialParticipants.map((participant) => participant.handle),+ ).toEqual([keptAttendee, removedAttendee].sort());++ const initialKeptParticipant = initialParticipants.find(+ (participant) => participant.handle === keptAttendee,+ );++ await importEvent(+ googleCalendarEvent({+ id: eventExternalId,+ summary: title,+ attendees: [{ email: keptAttendee }, { email: addedAttendee }],+ }),+ );++ const participants = await findParticipantsByCalendarEventId(event.id);++ expect(participants.map((participant) => participant.handle)).toEqual(+ [keptAttendee, addedAttendee].sort(),+ );++ const keptParticipant = participants.find(+ (participant) => participant.handle === keptAttendee,+ );++ expect(keptParticipant?.id).toBe(initialKeptParticipant?.id);+ }, 180000);++ it('should insert every participant when a single import spans more than one insert chunk', async () => {+ const titlePrefix = `Calendar event ${randomUUID()}`;+ const attendeeSuffix = `chunked-${randomUUID()}@acme.com`;++ const events = Array.from({ length: 100 }, (_unused, eventIndex) =>+ googleCalendarEvent({+ id: `google-calendar-event-${eventIndex}-${randomUUID()}`,+ summary: `${titlePrefix} ${eventIndex}`,+ attendees: [+ { email: `a-${eventIndex}-${attendeeSuffix}` },+ { email: `b-${eventIndex}-${attendeeSuffix}` },+ { email: `c-${eventIndex}-${attendeeSuffix}` },+ ],+ }),+ );++ await importEvents(events);++ expect(await countParticipantsByHandleSuffix(attendeeSuffix)).toBe(300);+ }, 300000);++ it('should update exactly one row and leave the other untouched when two participants share a handle on the same event', async () => {+ const eventExternalId = `google-calendar-event-${randomUUID()}`;+ const title = `Calendar event ${randomUUID()}`;+ const attendee = `attendee-${randomUUID()}@acme.com`;++ await importEvent(+ googleCalendarEvent({+ id: eventExternalId,+ summary: title,+ attendees: [{ email: attendee, displayName: 'Original' }],+ }),+ );++ const [event] = await findEventsByTitle(title);++ const duplicateResponse = await makeGraphqlAPIRequest(+ createOneOperationFactory({+ objectMetadataSingularName: 'calendarEventParticipant',+ gqlFields: 'id',+ data: {+ calendarEventId: event.id,+ handle: attendee,+ displayName: 'Duplicate',+ },+ }),+ );++ expect(duplicateResponse.body.errors).toBeUndefined();+ expect(await findParticipantsByCalendarEventId(event.id)).toHaveLength(2);++ await importEvent(+ googleCalendarEvent({+ id: eventExternalId,+ summary: title,+ attendees: [{ email: attendee, displayName: 'Reconciled' }],+ }),+ );++ const participants = await findParticipantsByCalendarEventId(event.id);++ expect(participants).toHaveLength(2);+ expect(+ participants.filter(+ (participant) => participant.displayName === 'Reconciled',+ ),+ ).toHaveLength(1);+ }, 240000);+});diff --git a/packages/twenty-server/test/integration/google/calendar/transient-database-error.integration-spec.ts b/packages/twenty-server/test/integration/google/calendar/transient-database-error.integration-spec.tsnew file mode 100644index 00000000000..1556fe16bcd--- /dev/null+++ b/packages/twenty-server/test/integration/google/calendar/transient-database-error.integration-spec.ts@@ -0,0 +1,270 @@+import { randomUUID } from 'node:crypto';++import {+ CalendarChannelSyncStage,+ CalendarChannelSyncStatus,+ ConnectedAccountProvider,+} from 'twenty-shared/types';++import { POSTGRESQL_ERROR_CODES } from 'src/engine/api/graphql/workspace-query-runner/constants/postgres-error-codes.constants';+import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';++import { googleCalendarEvent } from 'test/integration/google/mocks/google-calendar-event.util';+import { setupGoogleMock } from 'test/integration/google/mocks/setup-google-mock.util';+import { connectMessagingAccount } from 'test/integration/utils/connect-messaging-account.util';+import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';+import { raiseSqlState } from 'test/integration/utils/raise-sql-state.util';+import { readBackendState } from 'test/integration/utils/read-backend-state.util';+import { queryCalendarChannel } from 'test/integration/utils/query-messaging.util';+import { resetCalendarChannelSyncState } from 'test/integration/utils/reset-channel-sync-state.util';+import { runCalendarChannelEventsImport } from 'test/integration/utils/run-calendar-channel-events-import.util';+import { runCalendarChannelListFetch } from 'test/integration/utils/run-calendar-channel-list-fetch.util';++const HANDLE = '[email protected]';++// The error has to be raised by Postgres itself: an Error built in the jest+// realm is not an `instanceof Error` for the application realm the app runs in.+describe('Calendar import transient database errors (integration)', () => {+ const google = setupGoogleMock({ handle: HANDLE });++ let channel: Awaited<ReturnType<typeof connectMessagingAccount>>;+ let calendarEventParticipantService: CalendarEventParticipantService;++ const fetchEvent = async ({+ eventId,+ title,+ attendees,+ }: {+ eventId: string;+ title: string;+ attendees: string[];+ }): Promise<void> => {+ google.serveCalendarEvents(+ [+ googleCalendarEvent({+ id: eventId,+ summary: title,+ attendees: attendees.map((email) => ({ email })),+ }),+ ],+ { nextSyncToken: `sync-token-${randomUUID()}` },+ );++ await resetCalendarChannelSyncState(channel.calendarChannelId, '');+ await runCalendarChannelListFetch(channel.calendarChannelId);+ };++ const fetchOneEvent = async (): Promise<void> => {+ await fetchEvent({+ eventId: `google-calendar-event-${randomUUID()}`,+ title: `Calendar event ${randomUUID()}`,+ attendees: [`attendee-${randomUUID()}@acme.com`],+ });+ };++ beforeAll(async () => {+ channel = await connectMessagingAccount({+ provider: ConnectedAccountProvider.GOOGLE,+ handle: HANDLE,+ });++ calendarEventParticipantService =+ getAppProviderByClassName<CalendarEventParticipantService>(+ 'CalendarEventParticipantService',+ );+ }, 60000);++ afterAll(async () => {+ await channel?.cleanup().catch(() => undefined);+ });++ afterEach(() => {+ jest.restoreAllMocks();+ });++ it('should fail the calendar channel as unknown when a unique violation aborts the import transaction', async () => {+ await fetchOneEvent();++ jest+ .spyOn(calendarEventParticipantService, 'writeCalendarEventParticipants')+ .mockImplementation(async ({ transactionScope }) => {+ await transactionScope.executeRawQuery(+ raiseSqlState(POSTGRESQL_ERROR_CODES.UNIQUE_VIOLATION),+ );+ });++ await runCalendarChannelEventsImport(channel.calendarChannelId);++ const channelState = await queryCalendarChannel(channel);++ expect(channelState.syncStatus).toBe(+ CalendarChannelSyncStatus.FAILED_UNKNOWN,+ );+ expect(channelState.syncStage).toBe(CalendarChannelSyncStage.FAILED);+ expect(channelState.throttleFailureCount).toBe(0);+ }, 120000);++ it('should reschedule the calendar channel when the import transaction is killed by the idle-in-transaction timeout', async () => {+ await fetchOneEvent();++ jest+ .spyOn(calendarEventParticipantService, 'writeCalendarEventParticipants')+ .mockImplementation(async ({ transactionScope }) => {+ await transactionScope.executeRawQuery(+ raiseSqlState(+ POSTGRESQL_ERROR_CODES.IDLE_IN_TRANSACTION_SESSION_TIMEOUT,+ ),+ );+ });++ await runCalendarChannelEventsImport(channel.calendarChannelId);++ const channelState = await queryCalendarChannel(channel);++ expect(channelState.throttleFailureCount).toBe(1);+ expect(channelState.syncStage).toBe(+ CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_PENDING,+ );+ expect(channelState.syncStatus).toBe(CalendarChannelSyncStatus.ONGOING);+ }, 120000);++ it('should have ended the import transaction before the contact creation job is enqueued and the participants are matched', async () => {+ await fetchOneEvent();++ const writeCalendarEventParticipants =+ calendarEventParticipantService.writeCalendarEventParticipants.bind(+ calendarEventParticipantService,+ );+ const matchParticipantsAndEnqueueContactCreationJob =+ calendarEventParticipantService.matchParticipantsAndEnqueueContactCreationJob.bind(+ calendarEventParticipantService,+ );++ let importBackendPid: number | undefined;+ let importBackendStateWhileMatching: string | undefined;++ jest+ .spyOn(calendarEventParticipantService, 'writeCalendarEventParticipants')+ .mockImplementation(async (args) => {+ const backends = await args.transactionScope.executeRawQuery(+ 'SELECT pg_backend_pid() AS pid',+ );++ importBackendPid = Number(backends[0].pid);++ return writeCalendarEventParticipants(args);+ });++ jest+ .spyOn(+ calendarEventParticipantService,+ 'matchParticipantsAndEnqueueContactCreationJob',+ )+ .mockImplementation(async (args) => {+ importBackendStateWhileMatching = await readBackendState(+ importBackendPid ?? 0,+ );++ return matchParticipantsAndEnqueueContactCreationJob(args);+ });++ await runCalendarChannelEventsImport(channel.calendarChannelId);++ expect(importBackendPid).toEqual(expect.any(Number));+ expect(importBackendStateWhileMatching).toBeDefined();+ expect(importBackendStateWhileMatching).not.toBe('idle in transaction');+ }, 120000);++ it('should return only the newly saved participants without appending to the caller participant list when an existing event gains an attendee', async () => {+ const eventId = `google-calendar-event-${randomUUID()}`;+ const title = `Calendar event ${randomUUID()}`;+ const knownAttendee = `attendee-known-${randomUUID()}@acme.com`;+ const newAttendee = `attendee-new-${randomUUID()}@acme.com`;++ await fetchEvent({ eventId, title, attendees: [knownAttendee] });+ await runCalendarChannelEventsImport(channel.calendarChannelId);++ await fetchEvent({+ eventId,+ title,+ attendees: [knownAttendee, newAttendee],+ });++ const writeCalendarEventParticipants =+ calendarEventParticipantService.writeCalendarEventParticipants.bind(+ calendarEventParticipantService,+ );++ let insertedHandles: string[] = [];+ let updatedParticipantCount = -1;++ jest+ .spyOn(calendarEventParticipantService, 'writeCalendarEventParticipants')+ .mockImplementation(async (args) => {+ insertedHandles = args.operations.participantsToInsert.map(+ (participant) => participant.handle,+ );+ updatedParticipantCount = args.operations.participantsToUpdate.length;++ return writeCalendarEventParticipants(args);+ });++ await runCalendarChannelEventsImport(channel.calendarChannelId);++ expect(insertedHandles).toEqual([newAttendee]);+ expect(updatedParticipantCount).toBe(1);+ }, 180000);++ it('should read and compute outside the transaction and only write inside it', async () => {+ await fetchOneEvent();++ const calls: string[] = [];++ const findCalendarEventParticipantsByCalendarEventIds =+ calendarEventParticipantService.findCalendarEventParticipantsByCalendarEventIds.bind(+ calendarEventParticipantService,+ );+ const writeCalendarEventParticipants =+ calendarEventParticipantService.writeCalendarEventParticipants.bind(+ calendarEventParticipantService,+ );+ const matchParticipantsAndEnqueueContactCreationJob =+ calendarEventParticipantService.matchParticipantsAndEnqueueContactCreationJob.bind(+ calendarEventParticipantService,+ );++ jest+ .spyOn(+ calendarEventParticipantService,+ 'findCalendarEventParticipantsByCalendarEventIds',+ )+ .mockImplementation(async (args) => {+ calls.push('read');++ return findCalendarEventParticipantsByCalendarEventIds(args);+ });++ jest+ .spyOn(calendarEventParticipantService, 'writeCalendarEventParticipants')+ .mockImplementation(async (args) => {+ calls.push('write');++ return writeCalendarEventParticipants(args);+ });++ jest+ .spyOn(+ calendarEventParticipantService,+ 'matchParticipantsAndEnqueueContactCreationJob',+ )+ .mockImplementation(async (args) => {+ calls.push('match');++ return matchParticipantsAndEnqueueContactCreationJob(args);+ });++ await runCalendarChannelEventsImport(channel.calendarChannelId);++ expect(calls).toEqual(['read', 'write', 'match']);+ }, 120000);+});diff --git a/packages/twenty-server/test/integration/google/messaging/transient-database-error.integration-spec.ts b/packages/twenty-server/test/integration/google/messaging/transient-database-error.integration-spec.tsnew file mode 100644index 00000000000..13624f5985c--- /dev/null+++ b/packages/twenty-server/test/integration/google/messaging/transient-database-error.integration-spec.ts@@ -0,0 +1,141 @@+import { randomUUID } from 'node:crypto';++import {+ ConnectedAccountProvider,+ MessageChannelSyncStage,+ MessageChannelSyncStatus,+} from 'twenty-shared/types';++import { POSTGRESQL_ERROR_CODES } from 'src/engine/api/graphql/workspace-query-runner/constants/postgres-error-codes.constants';+import { MessagingMessageParticipantService } from 'src/modules/messaging/message-participant-manager/services/messaging-message-participant.service';++import { gmailMessage } from 'test/integration/google/mocks/gmail-message.util';+import { setupGoogleMock } from 'test/integration/google/mocks/setup-google-mock.util';+import { connectMessagingAccount } from 'test/integration/utils/connect-messaging-account.util';+import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';+import { raiseSqlState } from 'test/integration/utils/raise-sql-state.util';+import { readBackendState } from 'test/integration/utils/read-backend-state.util';+import { queryMessageChannel } from 'test/integration/utils/query-messaging.util';+import { resetMessageChannelSyncState } from 'test/integration/utils/reset-channel-sync-state.util';+import { runMessageChannelSync } from 'test/integration/utils/run-message-channel-sync.util';++const HANDLE = '[email protected]';++// The error has to be raised by Postgres itself: an Error built in the jest+// realm is not an `instanceof Error` for the application realm the app runs in.+// The mock inbox is fixed at setup time, so every test needs its own message+// served out of it rather than a freshly built one.+const INBOX_MESSAGES = Array.from({ length: 2 }, () =>+ gmailMessage({ from: `sender-${randomUUID()}@acme.com`, to: HANDLE }),+);++describe('Messaging import transient database errors (integration)', () => {+ const google = setupGoogleMock({ handle: HANDLE, inbox: INBOX_MESSAGES });++ let channel: Awaited<ReturnType<typeof connectMessagingAccount>>;+ let messageParticipantService: MessagingMessageParticipantService;++ const serveInboundMessage = async (messageIndex: number): Promise<void> => {+ google.serveMessageList([INBOX_MESSAGES[messageIndex]]);++ await resetMessageChannelSyncState(channel.channelId, '');+ };++ beforeAll(async () => {+ channel = await connectMessagingAccount({+ provider: ConnectedAccountProvider.GOOGLE,+ handle: HANDLE,+ });++ messageParticipantService =+ getAppProviderByClassName<MessagingMessageParticipantService>(+ 'MessagingMessageParticipantService',+ );+ }, 60000);++ afterAll(async () => {+ await channel?.cleanup().catch(() => undefined);+ });++ afterEach(() => {+ jest.restoreAllMocks();+ });++ it('should reschedule the message channel instead of failing it when the connection is killed by the idle-in-transaction timeout', async () => {+ await serveInboundMessage(0);++ jest+ .spyOn(messageParticipantService, 'saveMessageParticipants')+ .mockImplementation(+ async (_participants, _workspaceId, transactionScope) => {+ await transactionScope.executeRawQuery(+ raiseSqlState(+ POSTGRESQL_ERROR_CODES.IDLE_IN_TRANSACTION_SESSION_TIMEOUT,+ ),+ );++ return [];+ },+ );++ await runMessageChannelSync(channel.channelId);++ const channelState = await queryMessageChannel(channel);++ expect(channelState.throttleFailureCount).toBe(1);+ expect(channelState.syncStage).toBe(+ MessageChannelSyncStage.MESSAGES_IMPORT_PENDING,+ );+ expect(channelState.syncStatus).toBe(MessageChannelSyncStatus.ONGOING);+ }, 120000);++ it('should have ended the import transaction before the message participants are matched', async () => {+ await serveInboundMessage(1);++ const saveMessageParticipants =+ messageParticipantService.saveMessageParticipants.bind(+ messageParticipantService,+ );+ const matchMessageParticipants =+ messageParticipantService.matchMessageParticipants.bind(+ messageParticipantService,+ );++ let importBackendPid: number | undefined;+ let importBackendStateWhileMatching: string | undefined;++ jest+ .spyOn(messageParticipantService, 'saveMessageParticipants')+ .mockImplementation(+ async (participants, workspaceId, transactionScope) => {+ const backends = await transactionScope.executeRawQuery(+ 'SELECT pg_backend_pid() AS pid',+ );++ importBackendPid = Number(backends[0].pid);++ return saveMessageParticipants(+ participants,+ workspaceId,+ transactionScope,+ );+ },+ );++ jest+ .spyOn(messageParticipantService, 'matchMessageParticipants')+ .mockImplementation(async (args) => {+ importBackendStateWhileMatching = await readBackendState(+ importBackendPid ?? 0,+ );++ return matchMessageParticipants(args);+ });++ await runMessageChannelSync(channel.channelId);++ expect(importBackendPid).toEqual(expect.any(Number));+ expect(importBackendStateWhileMatching).toBeDefined();+ expect(importBackendStateWhileMatching).not.toBe('idle in transaction');+ }, 120000);+});diff --git a/packages/twenty-server/test/integration/twenty-orm/transient-database-error-mapping.integration-spec.ts b/packages/twenty-server/test/integration/twenty-orm/transient-database-error-mapping.integration-spec.tsnew file mode 100644index 00000000000..2a3bb8d95ed--- /dev/null+++ b/packages/twenty-server/test/integration/twenty-orm/transient-database-error-mapping.integration-spec.ts@@ -0,0 +1,46 @@+import { POSTGRESQL_ERROR_CODES } from 'src/engine/api/graphql/workspace-query-runner/constants/postgres-error-codes.constants';+import { type WorkspaceTransactionScope } from 'src/engine/twenty-orm/types/workspace-transaction-scope.type';+import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';+import { WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';+import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';++import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';+import { raiseSqlState } from 'test/integration/utils/raise-sql-state.util';++const TRANSIENT_DATABASE_ERROR = 'TRANSIENT_DATABASE_ERROR';++const authContext = buildSystemAuthContext(SEED_APPLE_WORKSPACE_ID);++const runInTransaction = (+ work: (transactionScope: WorkspaceTransactionScope) => Promise<unknown>,+): Promise<unknown> => {+ const workspaceOrmManager =+ getAppProviderByClassName<WorkspaceOrmManager>('WorkspaceOrmManager');++ return workspaceOrmManager.executeInWorkspaceContext(+ async () => workspaceOrmManager.runInWorkspaceTransaction(work),+ authContext,+ { lite: true },+ );+};++const runInTransactionRaising = (sqlState: string): Promise<unknown> =>+ runInTransaction(async (transactionScope) => {+ await transactionScope.executeRawQuery(raiseSqlState(sqlState));+ });++describe('Transient database error mapping', () => {+ it('should mark the idle-in-transaction timeout that kills long imports as transient', async () => {+ await expect(+ runInTransactionRaising(+ POSTGRESQL_ERROR_CODES.IDLE_IN_TRANSACTION_SESSION_TIMEOUT,+ ),+ ).rejects.toMatchObject({ code: TRANSIENT_DATABASE_ERROR });+ }, 60000);++ it('should leave a unique violation alone so a real conflict is not retried', async () => {+ await expect(+ runInTransactionRaising(POSTGRESQL_ERROR_CODES.UNIQUE_VIOLATION),+ ).rejects.not.toMatchObject({ code: TRANSIENT_DATABASE_ERROR });+ }, 60000);+});diff --git a/packages/twenty-server/test/integration/utils/raise-sql-state.util.ts b/packages/twenty-server/test/integration/utils/raise-sql-state.util.tsnew file mode 100644index 00000000000..1aab73df47d--- /dev/null+++ b/packages/twenty-server/test/integration/utils/raise-sql-state.util.ts@@ -0,0 +1,2 @@+export const raiseSqlState = (sqlState: string): string =>+ `DO $$ BEGIN RAISE EXCEPTION 'simulated database failure' USING ERRCODE = '${sqlState}'; END $$;`;diff --git a/packages/twenty-server/test/integration/utils/read-backend-state.util.ts b/packages/twenty-server/test/integration/utils/read-backend-state.util.tsnew file mode 100644index 00000000000..380cd0c514b--- /dev/null+++ b/packages/twenty-server/test/integration/utils/read-backend-state.util.ts@@ -0,0 +1,15 @@+import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';++import { getCoreRepository } from 'test/integration/utils/get-core-repository.util';++export const readBackendState = async (+ backendPid: number,+): Promise<string | undefined> => {+ const activities: { state: string }[] = await getCoreRepository<WorkspaceEntity>(+ WorkspaceEntity,+ ).manager.query('SELECT state FROM pg_stat_activity WHERE pid = $1', [+ backendPid,+ ]);++ return activities[0]?.state;+};