The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Reset workspaces to delete between executions (#3625)
martmullca6250286a02
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.tsindex 43167dfde4c..4b292f9a71d 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 CleanInactiveWorkspaceJobprivate 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<void> {- 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<void> {- 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<boolean> {const workspaceFeatureFlags = await this.featureFlagRepository.find({where: { workspaceId: dataSource.workspaceId },@@ -223,11 +178,23 @@ export class CleanInactiveWorkspaceJobreturn chunkedArray;}- async sendDeleteWorkspaceEmail(isDryRun: boolean): Promise<void> {- if (isDryRun || this.workspacesToDelete.length === 0) {+ async sendDeleteWorkspaceEmail(+ workspacesToDelete: WorkspaceToDeleteData[],+ isDryRun: boolean,+ ): Promise<void> {+ 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 CleanInactiveWorkspaceJobasync handle(data: CleanInactiveWorkspacesCommandOptions): Promise<void> {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 CleanInactiveWorkspaceJobdataSource.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!`);}