diff --git a/packages/twenty-server/src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace-cron-pattern.ts b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace-cron-pattern.ts new file mode 100644 index 0000000000..a8bac100d4 --- /dev/null +++ b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace-cron-pattern.ts @@ -0,0 +1 @@ +export const dataSeedDemoWorkspaceCronPattern = '0 22 * * *'; // Every day at 10pm diff --git a/packages/twenty-server/src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace.cron.command.ts b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace.cron.command.ts new file mode 100644 index 0000000000..51d8cb100d --- /dev/null +++ b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace.cron.command.ts @@ -0,0 +1,29 @@ +import { Inject } from '@nestjs/common'; + +import { Command, CommandRunner } from 'nest-commander'; + +import { dataSeedDemoWorkspaceCronPattern } from 'src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace-cron-pattern'; +import { DataSeedDemoWorkspaceJob } from 'src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.job'; +import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants'; +import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service'; + +@Command({ + name: 'workspace:cron:seed:demo', + description: 'Seed workspace with demo data.', +}) +export class DataSeedDemoWorkspaceCronCommand extends CommandRunner { + constructor( + @Inject(MessageQueue.cronQueue) + private readonly messageQueueService: MessageQueueService, + ) { + super(); + } + + async run(): Promise { + await this.messageQueueService.addCron( + DataSeedDemoWorkspaceJob.name, + undefined, + dataSeedDemoWorkspaceCronPattern, + ); + } +} diff --git a/packages/twenty-server/src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace-command.ts b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace-command.ts new file mode 100644 index 0000000000..611932a33b --- /dev/null +++ b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace-command.ts @@ -0,0 +1,19 @@ +import { Command, CommandRunner } from 'nest-commander'; + +import { DataSeedDemoWorkspaceService } from 'src/database/commands/data-seed-demo-workspace/services/data-seed-demo-workspace.service'; + +@Command({ + name: 'workspace:seed:demo', + description: 'Seed workspace with demo data. Use in development only.', +}) +export class DataSeedDemoWorkspaceCommand extends CommandRunner { + constructor( + private readonly dataSeedDemoWorkspaceService: DataSeedDemoWorkspaceService, + ) { + super(); + } + + async run(): Promise { + await this.dataSeedDemoWorkspaceService.seedDemo(); + } +} diff --git a/packages/twenty-server/src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module.ts b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module.ts new file mode 100644 index 0000000000..62d2a54774 --- /dev/null +++ b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; + +import { EnvironmentModule } from 'src/integrations/environment/environment.module'; +import { WorkspaceManagerModule } from 'src/workspace/workspace-manager/workspace-manager.module'; +import { DataSeedDemoWorkspaceService } from 'src/database/commands/data-seed-demo-workspace/services/data-seed-demo-workspace.service'; + +@Module({ + imports: [WorkspaceManagerModule, EnvironmentModule], + providers: [DataSeedDemoWorkspaceService], + exports: [DataSeedDemoWorkspaceService], +}) +export class DataSeedDemoWorkspaceModule {} diff --git a/packages/twenty-server/src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.job.ts b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.job.ts new file mode 100644 index 0000000000..f4e33fa20e --- /dev/null +++ b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.job.ts @@ -0,0 +1,16 @@ +import { Injectable } from '@nestjs/common'; + +import { MessageQueueJob } from 'src/integrations/message-queue/interfaces/message-queue-job.interface'; + +import { DataSeedDemoWorkspaceService } from 'src/database/commands/data-seed-demo-workspace/services/data-seed-demo-workspace.service'; + +@Injectable() +export class DataSeedDemoWorkspaceJob implements MessageQueueJob { + constructor( + private readonly dataSeedDemoWorkspaceService: DataSeedDemoWorkspaceService, + ) {} + + async handle(): Promise { + await this.dataSeedDemoWorkspaceService.seedDemo(); + } +} diff --git a/packages/twenty-server/src/database/commands/data-seed-demo-workspace.command.ts b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/services/data-seed-demo-workspace.service.ts similarity index 77% rename from packages/twenty-server/src/database/commands/data-seed-demo-workspace.command.ts rename to packages/twenty-server/src/database/commands/data-seed-demo-workspace/services/data-seed-demo-workspace.service.ts index c1c17612c3..3b635b6198 100644 --- a/packages/twenty-server/src/database/commands/data-seed-demo-workspace.command.ts +++ b/packages/twenty-server/src/database/commands/data-seed-demo-workspace/services/data-seed-demo-workspace.service.ts @@ -1,27 +1,22 @@ -import { Command, CommandRunner } from 'nest-commander'; +import { Injectable } from '@nestjs/common'; + import { DataSource } from 'typeorm'; import { deleteCoreSchema, seedCoreSchema, -} from 'src/database/typeorm-seeds/core/demo'; +} from 'src/database/typeorm-seeds/core'; import { EnvironmentService } from 'src/integrations/environment/environment.service'; import { WorkspaceManagerService } from 'src/workspace/workspace-manager/workspace-manager.service'; -@Command({ - name: 'workspace:seed:demo', - description: - 'Seed workspace with demo data. This command is intended for development only.', -}) -export class DataSeedDemoWorkspaceCommand extends CommandRunner { +@Injectable() +export class DataSeedDemoWorkspaceService { constructor( private readonly environmentService: EnvironmentService, private readonly workspaceManagerService: WorkspaceManagerService, - ) { - super(); - } + ) {} - async run(): Promise { + async seedDemo(): Promise { try { const dataSource = new DataSource({ url: this.environmentService.getPGDatabaseUrl(), diff --git a/packages/twenty-server/src/database/commands/database-command.module.ts b/packages/twenty-server/src/database/commands/database-command.module.ts index 20b8163f22..47b9794d16 100644 --- a/packages/twenty-server/src/database/commands/database-command.module.ts +++ b/packages/twenty-server/src/database/commands/database-command.module.ts @@ -6,11 +6,13 @@ import { DataSourceModule } from 'src/metadata/data-source/data-source.module'; import { TypeORMModule } from 'src/database/typeorm/typeorm.module'; import { WorkspaceModule } from 'src/core/workspace/workspace.module'; import { DataSeedWorkspaceCommand } from 'src/database/commands/data-seed-dev-workspace.command'; -import { DataSeedDemoWorkspaceCommand } from 'src/database/commands/data-seed-demo-workspace.command'; +import { DataSeedDemoWorkspaceCronCommand } from 'src/database/commands/data-seed-demo-workspace/crons/data-seed-demo-workspace.cron.command'; import { WorkspaceDataSourceModule } from 'src/workspace/workspace-datasource/workspace-datasource.module'; import { WorkspaceSyncMetadataModule } from 'src/workspace/workspace-sync-metadata/workspace-sync-metadata.module'; import { ObjectMetadataModule } from 'src/metadata/object-metadata/object-metadata.module'; import { WorkspaceAddTotalCountCommand } from 'src/database/commands/workspace-add-total-count.command'; +import { DataSeedDemoWorkspaceCommand } from 'src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace-command'; +import { DataSeedDemoWorkspaceModule } from 'src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module'; @Module({ imports: [ @@ -21,10 +23,12 @@ import { WorkspaceAddTotalCountCommand } from 'src/database/commands/workspace-a WorkspaceDataSourceModule, WorkspaceSyncMetadataModule, ObjectMetadataModule, + DataSeedDemoWorkspaceModule, ], providers: [ DataSeedWorkspaceCommand, DataSeedDemoWorkspaceCommand, + DataSeedDemoWorkspaceCronCommand, WorkspaceAddTotalCountCommand, ConfirmationQuestion, ], diff --git a/packages/twenty-server/src/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/integrations/message-queue/jobs.module.ts index adaba9f10f..0b03314c26 100644 --- a/packages/twenty-server/src/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/integrations/message-queue/jobs.module.ts @@ -24,6 +24,8 @@ import { CreateCompaniesAndContactsAfterSyncJob } from 'src/workspace/messaging/ import { CreateCompaniesAndContactsModule } from 'src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.module'; import { MessageChannelModule } from 'src/workspace/messaging/repositories/message-channel/message-channel.module'; import { MessageParticipantModule } from 'src/workspace/messaging/repositories/message-participant/message-participant.module'; +import { DataSeedDemoWorkspaceJob } from 'src/database/commands/data-seed-demo-workspace/jobs/data-seed-demo-workspace.job'; +import { DataSeedDemoWorkspaceModule } from 'src/database/commands/data-seed-demo-workspace/data-seed-demo-workspace.module'; @Module({ imports: [ @@ -41,6 +43,7 @@ import { MessageParticipantModule } from 'src/workspace/messaging/repositories/m MessageParticipantModule, CreateCompaniesAndContactsModule, MessageChannelModule, + DataSeedDemoWorkspaceModule, ], providers: [ { @@ -76,6 +79,10 @@ import { MessageParticipantModule } from 'src/workspace/messaging/repositories/m provide: CreateCompaniesAndContactsAfterSyncJob.name, useClass: CreateCompaniesAndContactsAfterSyncJob, }, + { + provide: DataSeedDemoWorkspaceJob.name, + useClass: DataSeedDemoWorkspaceJob, + }, ], }) export class JobsModule {