Deprecate inject workspace repo (#6353)

This commit is contained in:
Charles Bochet
2024-07-20 00:43:29 +02:00
committed by GitHub
parent 2e38c3bbc1
commit d8cadad0fa
29 changed files with 441 additions and 359 deletions

View File

@ -1,19 +1,18 @@
import { Scope } from '@nestjs/common';
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
import { CalendarEventsImportService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service';
import { isThrottled } from 'src/modules/connected-account/utils/is-throttled';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { CalendarEventsImportService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service';
import {
CalendarChannelSyncStage,
CalendarChannelWorkspaceEntity,
} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { isThrottled } from 'src/modules/connected-account/utils/is-throttled';
export type CalendarEventsImportJobData = {
calendarChannelId: string;
@ -26,18 +25,22 @@ export type CalendarEventsImportJobData = {
})
export class CalendarEventListFetchJob {
constructor(
private readonly twentyORMManager: TwentyORMManager,
private readonly calendarEventsImportService: CalendarEventsImportService,
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
private readonly connectedAccountRepository: ConnectedAccountRepository,
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
) {}
@Process(CalendarEventListFetchJob.name)
async handle(data: CalendarEventsImportJobData): Promise<void> {
const { workspaceId, calendarChannelId } = data;
const calendarChannel = await this.calendarChannelRepository.findOne({
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
const calendarChannel = await calendarChannelRepository.findOne({
where: {
id: calendarChannelId,
isSyncEnabled: true,
@ -65,7 +68,7 @@ export class CalendarEventListFetchJob {
switch (calendarChannel.syncStage) {
case CalendarChannelSyncStage.FULL_CALENDAR_EVENT_LIST_FETCH_PENDING:
await this.calendarChannelRepository.update(calendarChannelId, {
await calendarChannelRepository.update(calendarChannelId, {
syncCursor: '',
syncStageStartedAt: null,
});

View File

@ -3,8 +3,7 @@ import { Injectable } from '@nestjs/common';
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service';
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator';
import { CacheStorageNamespace } from 'src/engine/integrations/cache-storage/types/cache-storage-namespace.enum';
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import {
CalendarChannelSyncStage,
CalendarChannelSyncStatus,
@ -14,14 +13,18 @@ import {
@Injectable()
export class CalendarChannelSyncStatusService {
constructor(
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
private readonly twentyORMManager: TwentyORMManager,
@InjectCacheStorage(CacheStorageNamespace.Calendar)
private readonly cacheStorage: CacheStorageService,
) {}
public async scheduleFullCalendarEventListFetch(calendarChannelId: string) {
await this.calendarChannelRepository.update(calendarChannelId, {
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await calendarChannelRepository.update(calendarChannelId, {
syncStage:
CalendarChannelSyncStage.FULL_CALENDAR_EVENT_LIST_FETCH_PENDING,
});
@ -30,14 +33,24 @@ export class CalendarChannelSyncStatusService {
public async schedulePartialCalendarEventListFetch(
calendarChannelId: string,
) {
await this.calendarChannelRepository.update(calendarChannelId, {
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await calendarChannelRepository.update(calendarChannelId, {
syncStage:
CalendarChannelSyncStage.PARTIAL_CALENDAR_EVENT_LIST_FETCH_PENDING,
});
}
public async markAsCalendarEventListFetchOngoing(calendarChannelId: string) {
await this.calendarChannelRepository.update(calendarChannelId, {
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await calendarChannelRepository.update(calendarChannelId, {
syncStage: CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_ONGOING,
syncStatus: CalendarChannelSyncStatus.ONGOING,
syncStageStartedAt: new Date().toISOString(),
@ -52,7 +65,12 @@ export class CalendarChannelSyncStatusService {
`calendar-events-to-import:${workspaceId}:google-calendar:${calendarChannelId}`,
);
await this.calendarChannelRepository.update(calendarChannelId, {
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await calendarChannelRepository.update(calendarChannelId, {
syncCursor: '',
syncStageStartedAt: null,
throttleFailureCount: 0,
@ -62,13 +80,23 @@ export class CalendarChannelSyncStatusService {
}
public async scheduleCalendarEventsImport(calendarChannelId: string) {
await this.calendarChannelRepository.update(calendarChannelId, {
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await calendarChannelRepository.update(calendarChannelId, {
syncStage: CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_PENDING,
});
}
public async markAsCalendarEventsImportOngoing(calendarChannelId: string) {
await this.calendarChannelRepository.update(calendarChannelId, {
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await calendarChannelRepository.update(calendarChannelId, {
syncStage: CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_ONGOING,
syncStatus: CalendarChannelSyncStatus.ONGOING,
});
@ -77,7 +105,12 @@ export class CalendarChannelSyncStatusService {
public async markAsCompletedAndSchedulePartialMessageListFetch(
calendarChannelId: string,
) {
await this.calendarChannelRepository.update(calendarChannelId, {
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await calendarChannelRepository.update(calendarChannelId, {
syncStage:
CalendarChannelSyncStage.PARTIAL_CALENDAR_EVENT_LIST_FETCH_PENDING,
syncStatus: CalendarChannelSyncStatus.ACTIVE,
@ -92,11 +125,16 @@ export class CalendarChannelSyncStatusService {
calendarChannelId: string,
workspaceId: string,
) {
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await this.cacheStorage.del(
`calendar-events-to-import:${workspaceId}:google-calendar:${calendarChannelId}`,
);
await this.calendarChannelRepository.update(calendarChannelId, {
await calendarChannelRepository.update(calendarChannelId, {
syncStatus: CalendarChannelSyncStatus.FAILED_UNKNOWN,
syncStage: CalendarChannelSyncStage.FAILED,
});
@ -106,11 +144,16 @@ export class CalendarChannelSyncStatusService {
calendarChannelId: string,
workspaceId: string,
) {
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await this.cacheStorage.del(
`calendar-events-to-import:${workspaceId}:google-calendar:${calendarChannelId}`,
);
await this.calendarChannelRepository.update(calendarChannelId, {
await calendarChannelRepository.update(calendarChannelId, {
syncStatus: CalendarChannelSyncStatus.FAILED_INSUFFICIENT_PERMISSIONS,
syncStage: CalendarChannelSyncStage.FAILED,
});

View File

@ -1,7 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { CALENDAR_THROTTLE_MAX_ATTEMPTS } from 'src/modules/calendar/calendar-event-import-manager/constants/calendar-throttle-max-attempts';
import { CalendarChannelSyncStatusService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-channel-sync-status.service';
import { CalendarEventError } from 'src/modules/calendar/calendar-event-import-manager/types/calendar-event-error.type';
@ -16,9 +15,8 @@ export enum CalendarEventImportSyncStep {
@Injectable()
export class CalendarEventImportErrorHandlerService {
constructor(
private readonly twentyORMManager: TwentyORMManager,
private readonly calendarChannelSyncStatusService: CalendarChannelSyncStatusService,
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
) {}
public async handleError(
@ -68,7 +66,12 @@ export class CalendarEventImportErrorHandlerService {
return;
}
await this.calendarChannelRepository.increment(
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
await calendarChannelRepository.increment(
{
id: calendarChannel.id,
},

View File

@ -3,8 +3,7 @@ import { Injectable } from '@nestjs/common';
import { Any } from 'typeorm';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { BlocklistRepository } from 'src/modules/blocklist/repositories/blocklist.repository';
import { BlocklistWorkspaceEntity } from 'src/modules/blocklist/standard-objects/blocklist.workspace-entity';
import { CalendarEventCleanerService } from 'src/modules/calendar/calendar-event-cleaner/services/calendar-event-cleaner.service';
@ -29,10 +28,7 @@ import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/s
@Injectable()
export class CalendarEventsImportService {
constructor(
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
@InjectWorkspaceRepository(CalendarChannelEventAssociationWorkspaceEntity)
private readonly calendarChannelEventAssociationRepository: WorkspaceRepository<CalendarChannelEventAssociationWorkspaceEntity>,
private readonly twentyORMManager: TwentyORMManager,
@InjectObjectMetadataRepository(BlocklistWorkspaceEntity)
private readonly blocklistRepository: BlocklistRepository,
private readonly calendarEventCleanerService: CalendarEventCleanerService,
@ -79,8 +75,13 @@ export class CalendarEventsImportService {
return;
}
const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
'calendarChannel',
);
if (!calendarEvents || calendarEvents?.length === 0) {
await this.calendarChannelRepository.update(
await calendarChannelRepository.update(
{
id: calendarChannel.id,
},
@ -117,7 +118,12 @@ export class CalendarEventsImportService {
workspaceId,
);
await this.calendarChannelEventAssociationRepository.delete({
const calendarChannelEventAssociationRepository =
await this.twentyORMManager.getRepository<CalendarChannelEventAssociationWorkspaceEntity>(
'calendarChannelEventAssociation',
);
await calendarChannelEventAssociationRepository.delete({
eventExternalId: Any(cancelledEventExternalIds),
calendarChannel: {
id: calendarChannel.id,
@ -128,7 +134,7 @@ export class CalendarEventsImportService {
workspaceId,
);
await this.calendarChannelRepository.update(
await calendarChannelRepository.update(
{
id: calendarChannel.id,
},

View File

@ -8,8 +8,7 @@ import { MessageQueue } from 'src/engine/integrations/message-queue/message-queu
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
import { InjectWorkspaceDatasource } from 'src/engine/twenty-orm/decorators/inject-workspace-datasource.decorator';
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { injectIdsInCalendarEvents } from 'src/modules/calendar/calendar-event-import-manager/utils/inject-ids-in-calendar-events.util';
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
@ -26,10 +25,7 @@ import {
@Injectable()
export class CalendarSaveEventsService {
constructor(
@InjectWorkspaceRepository(CalendarEventWorkspaceEntity)
private readonly calendarEventRepository: WorkspaceRepository<CalendarEventWorkspaceEntity>,
@InjectWorkspaceRepository(CalendarChannelEventAssociationWorkspaceEntity)
private readonly calendarChannelEventAssociationRepository: WorkspaceRepository<CalendarChannelEventAssociationWorkspaceEntity>,
private readonly twentyORMManager: TwentyORMManager,
@InjectWorkspaceDatasource()
private readonly workspaceDataSource: WorkspaceDataSource,
private readonly calendarEventParticipantService: CalendarEventParticipantService,
@ -44,7 +40,12 @@ export class CalendarSaveEventsService {
connectedAccount: ConnectedAccountWorkspaceEntity,
workspaceId: string,
): Promise<void> {
const existingCalendarEvents = await this.calendarEventRepository.find({
const calendarEventRepository =
await this.twentyORMManager.getRepository<CalendarEventWorkspaceEntity>(
'calendarEvent',
);
const existingCalendarEvents = await calendarEventRepository.find({
where: {
iCalUID: Any(filteredEvents.map((event) => event.iCalUID as string)),
},
@ -77,8 +78,13 @@ export class CalendarSaveEventsService {
existingEventsICalUIDs.includes(calendarEvent.iCalUID),
);
const calendarChannelEventAssociationRepository =
await this.twentyORMManager.getRepository<CalendarChannelEventAssociationWorkspaceEntity>(
'calendarChannelEventAssociation',
);
const existingCalendarChannelEventAssociations =
await this.calendarChannelEventAssociationRepository.find({
await calendarChannelEventAssociationRepository.find({
where: {
eventExternalId: Any(
calendarEventsWithIds.map((calendarEvent) => calendarEvent.id),
@ -114,19 +120,15 @@ export class CalendarSaveEventsService {
[];
await this.workspaceDataSource?.transaction(async (transactionManager) => {
await this.calendarEventRepository.save(
eventsToSave,
{},
transactionManager,
);
await calendarEventRepository.save(eventsToSave, {}, transactionManager);
await this.calendarEventRepository.save(
await calendarEventRepository.save(
eventsToUpdate,
{},
transactionManager,
);
await this.calendarChannelEventAssociationRepository.save(
await calendarChannelEventAssociationRepository.save(
calendarChannelEventAssociationsToSave,
{},
transactionManager,