Skip to content

Commit 81fa3f0

Browse files
6256 refactor messaging module to remove all provider specific code and put it inside the drivers folders (#6721)
Closes #6256 Closes #6257 + Create custom exceptions --------- Co-authored-by: Charles Bochet <[email protected]>
1 parent eb49cb2 commit 81fa3f0

File tree

62 files changed

+1521
-1341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1521
-1341
lines changed

packages/twenty-server/src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { CalendarEventListFetchCronJob } from 'src/modules/calendar/calendar-eve
1414
import { GoogleCalendarDriverModule } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/google-calendar-driver.module';
1515
import { CalendarEventListFetchJob } from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-event-list-fetch.job';
1616
import { CalendarChannelSyncStatusService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-channel-sync-status.service';
17-
import { CalendarEventImportErrorHandlerService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-event-import-error-handling.service';
17+
import { CalendarEventImportErrorHandlerService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-event-import-exception-handler.service';
1818
import { CalendarEventsImportService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service';
1919
import { CalendarGetCalendarEventsService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-get-events.service';
2020
import { CalendarSaveEventsService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-save-events.service';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { CustomException } from 'src/utils/custom-exception';
2+
3+
export class CalendarEventImportDriverException extends CustomException {
4+
code: CalendarEventImportDriverExceptionCode;
5+
constructor(message: string, code: CalendarEventImportDriverExceptionCode) {
6+
super(message, code);
7+
}
8+
}
9+
10+
export enum CalendarEventImportDriverExceptionCode {
11+
NOT_FOUND = 'NOT_FOUND',
12+
TEMPORARY_ERROR = 'TEMPORARY_ERROR',
13+
INSUFFICIENT_PERMISSIONS = 'INSUFFICIENT_PERMISSIONS',
14+
UNKNOWN = 'UNKNOWN',
15+
UNKNOWN_NETWORK_ERROR = 'UNKNOWN_NETWORK_ERROR',
16+
}

packages/twenty-server/src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/services/google-calendar-get-events.service.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { GaxiosError } from 'gaxios';
44
import { calendar_v3 as calendarV3 } from 'googleapis';
55

66
import { GoogleCalendarClientProvider } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/providers/google-calendar.provider';
7-
import { GoogleCalendarError } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/types/google-calendar-error.type';
87
import { formatGoogleCalendarEvents } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/utils/format-google-calendar-event.util';
98
import { parseGaxiosError } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/utils/parse-gaxios-error.util';
109
import { parseGoogleCalendarError } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/utils/parse-google-calendar-error.util';
@@ -92,7 +91,7 @@ export class GoogleCalendarGetEventsService {
9291
throw parseGaxiosError(error);
9392
}
9493
if (error.response?.status !== 410) {
95-
const googleCalendarError: GoogleCalendarError = {
94+
const googleCalendarError = {
9695
code: error.response?.status,
9796
reason:
9897
error.response?.data?.error?.errors?.[0].reason ||

packages/twenty-server/src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/types/google-calendar-error.type.ts

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { GaxiosError } from 'gaxios';
22

33
import {
4-
CalendarEventError,
5-
CalendarEventErrorCode,
6-
} from 'src/modules/calendar/calendar-event-import-manager/types/calendar-event-error.type';
4+
CalendarEventImportDriverException,
5+
CalendarEventImportDriverExceptionCode,
6+
} from 'src/modules/calendar/calendar-event-import-manager/drivers/exceptions/calendar-event-import-driver.exception';
77

8-
export const parseGaxiosError = (error: GaxiosError): CalendarEventError => {
8+
export const parseGaxiosError = (
9+
error: GaxiosError,
10+
): CalendarEventImportDriverException => {
911
const { code } = error;
1012

1113
switch (code) {
@@ -14,15 +16,15 @@ export const parseGaxiosError = (error: GaxiosError): CalendarEventError => {
1416
case 'ECONNABORTED':
1517
case 'ETIMEDOUT':
1618
case 'ERR_NETWORK':
17-
return {
18-
code: CalendarEventErrorCode.TEMPORARY_ERROR,
19-
message: error.message,
20-
};
19+
return new CalendarEventImportDriverException(
20+
error.message,
21+
CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR,
22+
);
2123

2224
default:
23-
return {
24-
code: CalendarEventErrorCode.UNKNOWN,
25-
message: error.message,
26-
};
25+
return new CalendarEventImportDriverException(
26+
error.message,
27+
CalendarEventImportDriverExceptionCode.UNKNOWN_NETWORK_ERROR,
28+
);
2729
}
2830
};
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,87 @@
1-
import { GoogleCalendarError } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/types/google-calendar-error.type';
21
import {
3-
CalendarEventError,
4-
CalendarEventErrorCode,
5-
} from 'src/modules/calendar/calendar-event-import-manager/types/calendar-event-error.type';
2+
CalendarEventImportDriverException,
3+
CalendarEventImportDriverExceptionCode,
4+
} from 'src/modules/calendar/calendar-event-import-manager/drivers/exceptions/calendar-event-import-driver.exception';
65

7-
export const parseGoogleCalendarError = (
8-
error: GoogleCalendarError,
9-
): CalendarEventError => {
6+
export const parseGoogleCalendarError = (error: {
7+
code?: number;
8+
reason: string;
9+
message: string;
10+
}): CalendarEventImportDriverException => {
1011
const { code, reason, message } = error;
1112

1213
switch (code) {
1314
case 400:
1415
if (reason === 'invalid_grant') {
15-
return {
16-
code: CalendarEventErrorCode.INSUFFICIENT_PERMISSIONS,
16+
return new CalendarEventImportDriverException(
1717
message,
18-
};
18+
CalendarEventImportDriverExceptionCode.INSUFFICIENT_PERMISSIONS,
19+
);
1920
}
2021
if (reason === 'failedPrecondition') {
21-
return {
22-
code: CalendarEventErrorCode.TEMPORARY_ERROR,
22+
return new CalendarEventImportDriverException(
2323
message,
24-
};
24+
CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR,
25+
);
2526
}
2627

27-
return {
28-
code: CalendarEventErrorCode.UNKNOWN,
28+
return new CalendarEventImportDriverException(
2929
message,
30-
};
30+
CalendarEventImportDriverExceptionCode.UNKNOWN,
31+
);
3132

3233
case 404:
33-
return {
34-
code: CalendarEventErrorCode.NOT_FOUND,
34+
return new CalendarEventImportDriverException(
3535
message,
36-
};
36+
CalendarEventImportDriverExceptionCode.NOT_FOUND,
37+
);
3738

3839
case 429:
39-
return {
40-
code: CalendarEventErrorCode.TEMPORARY_ERROR,
40+
return new CalendarEventImportDriverException(
4141
message,
42-
};
42+
CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR,
43+
);
4344

4445
case 403:
4546
if (
4647
reason === 'rateLimitExceeded' ||
4748
reason === 'userRateLimitExceeded'
4849
) {
49-
return {
50-
code: CalendarEventErrorCode.TEMPORARY_ERROR,
50+
return new CalendarEventImportDriverException(
5151
message,
52-
};
52+
CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR,
53+
);
5354
} else {
54-
return {
55-
code: CalendarEventErrorCode.INSUFFICIENT_PERMISSIONS,
55+
return new CalendarEventImportDriverException(
5656
message,
57-
};
57+
CalendarEventImportDriverExceptionCode.INSUFFICIENT_PERMISSIONS,
58+
);
5859
}
5960

6061
case 401:
61-
return {
62-
code: CalendarEventErrorCode.INSUFFICIENT_PERMISSIONS,
62+
return new CalendarEventImportDriverException(
6363
message,
64-
};
64+
CalendarEventImportDriverExceptionCode.INSUFFICIENT_PERMISSIONS,
65+
);
6566
case 500:
6667
if (reason === 'backendError') {
67-
return {
68-
code: CalendarEventErrorCode.TEMPORARY_ERROR,
68+
return new CalendarEventImportDriverException(
6969
message,
70-
};
70+
CalendarEventImportDriverExceptionCode.TEMPORARY_ERROR,
71+
);
7172
} else {
72-
return {
73-
code: CalendarEventErrorCode.UNKNOWN,
73+
return new CalendarEventImportDriverException(
7474
message,
75-
};
75+
CalendarEventImportDriverExceptionCode.UNKNOWN,
76+
);
7677
}
7778

7879
default:
7980
break;
8081
}
8182

82-
return {
83-
code: CalendarEventErrorCode.UNKNOWN,
83+
return new CalendarEventImportDriverException(
8484
message,
85-
};
85+
CalendarEventImportDriverExceptionCode.UNKNOWN,
86+
);
8687
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { CustomException } from 'src/utils/custom-exception';
2+
3+
export class CalendarEventImportException extends CustomException {
4+
code: CalendarEventImportExceptionCode;
5+
constructor(message: string, code: CalendarEventImportExceptionCode) {
6+
super(message, code);
7+
}
8+
}
9+
10+
export enum CalendarEventImportExceptionCode {
11+
PROVIDER_NOT_SUPPORTED = 'PROVIDER_NOT_SUPPORTED',
12+
UNKNOWN = 'UNKNOWN',
13+
CALENDAR_CHANNEL_NOT_FOUND = 'CALENDAR_CHANNEL_NOT_FOUND',
14+
}

packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-channel-sync-status.service.ts

+30
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache
44
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator';
55
import { CacheStorageNamespace } from 'src/engine/integrations/cache-storage/types/cache-storage-namespace.enum';
66
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
7+
import {
8+
CalendarEventImportException,
9+
CalendarEventImportExceptionCode,
10+
} from 'src/modules/calendar/calendar-event-import-manager/exceptions/calendar-event-import.exception';
711
import {
812
CalendarChannelSyncStage,
913
CalendarChannelSyncStatus,
1014
CalendarChannelWorkspaceEntity,
1115
} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
1216
import { AccountsToReconnectService } from 'src/modules/connected-account/services/accounts-to-reconnect.service';
17+
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
1318
import { AccountsToReconnectKeys } from 'src/modules/connected-account/types/accounts-to-reconnect-key-value.type';
1419

1520
@Injectable()
@@ -161,6 +166,31 @@ export class CalendarChannelSyncStatusService {
161166
syncStage: CalendarChannelSyncStage.FAILED,
162167
});
163168

169+
const connectedAccountRepository =
170+
await this.twentyORMManager.getRepository<ConnectedAccountWorkspaceEntity>(
171+
'connectedAccount',
172+
);
173+
174+
const calendarChannel = await calendarChannelRepository.findOne({
175+
where: { id: calendarChannelId },
176+
});
177+
178+
if (!calendarChannel) {
179+
throw new CalendarEventImportException(
180+
`Calendar channel ${calendarChannelId} not found in workspace ${workspaceId}`,
181+
CalendarEventImportExceptionCode.CALENDAR_CHANNEL_NOT_FOUND,
182+
);
183+
}
184+
185+
const connectedAccountId = calendarChannel.connectedAccountId;
186+
187+
await connectedAccountRepository.update(
188+
{ id: connectedAccountId },
189+
{
190+
authFailedAt: new Date(),
191+
},
192+
);
193+
164194
await this.addToAccountsToReconnect(calendarChannelId, workspaceId);
165195
}
166196

0 commit comments

Comments
 (0)