diff --git a/packages/twenty-server/src/workspace/cron/clean-inactive-workspaces/clean-inactive-workspace.job.ts b/packages/twenty-server/src/workspace/cron/clean-inactive-workspaces/clean-inactive-workspace.job.ts index 43167dfde..4b292f9a7 100644 --- a/packages/twenty-server/src/workspace/cron/clean-inactive-workspaces/clean-inactive-workspace.job.ts +++ b/packages/twenty-server/src/workspace/cron/clean-inactive-workspaces/clean-inactive-workspace.job.ts @@ -39,7 +39,6 @@ export class CleanInactiveWorkspaceJob private readonly logger = new Logger(CleanInactiveWorkspaceJob.name); private readonly inactiveDaysBeforeDelete; private readonly inactiveDaysBeforeEmail; - private workspacesToDelete: WorkspaceToDeleteData[] = []; constructor( private readonly dataSourceService: DataSourceService, @@ -149,50 +148,6 @@ export class CleanInactiveWorkspaceJob }); } - async deleteWorkspace( - dataSource: DataSourceEntity, - daysSinceInactive: number, - isDryRun: boolean, - ): Promise { - this.logger.log( - `${this.getDryRunLogHeader(isDryRun)}Sending email to delete workspace ${ - dataSource.workspaceId - } inactive since ${daysSinceInactive} days`, - ); - - if (isDryRun) { - return; - } - - const emailData = { - daysSinceInactive: daysSinceInactive, - workspaceId: `${dataSource.workspaceId}`, - }; - - this.workspacesToDelete.push(emailData); - } - - async processWorkspace( - dataSource: DataSourceEntity, - objectsMetadata: ObjectMetadataEntity[], - isDryRun: boolean, - ): Promise { - const mostRecentUpdatedAt = await this.getMostRecentUpdatedAt( - dataSource, - objectsMetadata, - ); - const daysSinceInactive = Math.floor( - (new Date().getTime() - mostRecentUpdatedAt.getTime()) / - MILLISECONDS_IN_ONE_DAY, - ); - - if (daysSinceInactive > this.inactiveDaysBeforeDelete) { - await this.deleteWorkspace(dataSource, daysSinceInactive, isDryRun); - } else if (daysSinceInactive > this.inactiveDaysBeforeEmail) { - await this.warnWorkspaceUsers(dataSource, daysSinceInactive, isDryRun); - } - } - async isWorkspaceCleanable(dataSource: DataSourceEntity): Promise { const workspaceFeatureFlags = await this.featureFlagRepository.find({ where: { workspaceId: dataSource.workspaceId }, @@ -223,11 +178,23 @@ export class CleanInactiveWorkspaceJob return chunkedArray; } - async sendDeleteWorkspaceEmail(isDryRun: boolean): Promise { - if (isDryRun || this.workspacesToDelete.length === 0) { + async sendDeleteWorkspaceEmail( + workspacesToDelete: WorkspaceToDeleteData[], + isDryRun: boolean, + ): Promise { + this.logger.log( + `${this.getDryRunLogHeader( + isDryRun, + )}Sending email to delete workspaces "${workspacesToDelete + .map((workspaceToDelete) => workspaceToDelete.workspaceId) + .join('", "')}"`, + ); + + if (isDryRun || workspacesToDelete.length === 0) { return; } - const emailTemplate = DeleteInactiveWorkspaceEmail(this.workspacesToDelete); + + const emailTemplate = DeleteInactiveWorkspaceEmail(workspacesToDelete); const html = render(emailTemplate, { pretty: true, }); @@ -247,6 +214,8 @@ export class CleanInactiveWorkspaceJob async handle(data: CleanInactiveWorkspacesCommandOptions): Promise { const isDryRun = data.dryRun || false; + const workspacesToDelete: WorkspaceToDeleteData[] = []; + this.logger.log(`${this.getDryRunLogHeader(isDryRun)}Job running...`); if (!this.inactiveDaysBeforeDelete && !this.inactiveDaysBeforeEmail) { this.logger.log( @@ -289,11 +258,32 @@ export class CleanInactiveWorkspaceJob dataSource.workspaceId }`, ); - await this.processWorkspace(dataSource, objectsMetadata, isDryRun); + + const mostRecentUpdatedAt = await this.getMostRecentUpdatedAt( + dataSource, + objectsMetadata, + ); + const daysSinceInactive = Math.floor( + (new Date().getTime() - mostRecentUpdatedAt.getTime()) / + MILLISECONDS_IN_ONE_DAY, + ); + + if (daysSinceInactive > this.inactiveDaysBeforeDelete) { + workspacesToDelete.push({ + daysSinceInactive: daysSinceInactive, + workspaceId: `${dataSource.workspaceId}`, + }); + } else if (daysSinceInactive > this.inactiveDaysBeforeEmail) { + await this.warnWorkspaceUsers( + dataSource, + daysSinceInactive, + isDryRun, + ); + } } } - await this.sendDeleteWorkspaceEmail(isDryRun); + await this.sendDeleteWorkspaceEmail(workspacesToDelete, isDryRun); this.logger.log(`${this.getDryRunLogHeader(isDryRun)}job done!`); }