All files / apps/api/src/modules/sampleData sampleData.service.ts

100% Statements 96/96
30.76% Branches 4/13
100% Functions 4/4
100% Lines 96/96

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 971x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
 
import { Company as CompanyEntity } from '@amalia/core/models';
import { CompanyFeatureFlags, type Company } from '@amalia/tenants/companies/types';
import { type UserContract } from '@amalia/tenants/users/types';
 
import { SampleDataCalculationService } from './loaders/sampleDataCalculation.service';
import { SampleDataCurrenciesService } from './loaders/sampleDataCurrencies.service';
import { SampleDataCustomObjectsService } from './loaders/sampleDataCustomObjects.service';
import { SampleDataDesignerService } from './loaders/sampleDataDesigner.service';
import { SampleDataDownloadService } from './loaders/sampleDataDownload.service';
import { SampleDataReportingService } from './loaders/sampleDataReporting.service';
import { SampleDataUserManagementService } from './loaders/sampleDataUserManagement.service';
import { SampleDataWorkflowService } from './loaders/sampleDataWorkflow.service';
 
@Injectable()
export class SampleDataService {
  private readonly logger = new Logger(SampleDataService.name);
 
  public constructor(
    @InjectRepository(CompanyEntity)
    private readonly companyRepository: Repository<CompanyEntity>,
    private readonly sampleDataUserManagementService: SampleDataUserManagementService,
    private readonly sampleDataCustomObjectsService: SampleDataCustomObjectsService,
    private readonly sampleDataCalculationService: SampleDataCalculationService,
    private readonly sampleDataDesignerService: SampleDataDesignerService,
    private readonly sampleDataCurrenciesService: SampleDataCurrenciesService,
    private readonly sampleDataWorkflowService: SampleDataWorkflowService,
    private readonly sampleDataReportingService: SampleDataReportingService,
    private readonly sampleDataDownloadService: SampleDataDownloadService,
  ) {}
 
  /**
   * Loads sample datas for a given company.
   * @param company company to create
   * @param admin
   * @param runCalculation
   */
  public async loadSampleData(company: Company, admin: UserContract, runCalculation: boolean): Promise<void> {
    const emailPrefix = `idriss+${company.name}+`;
 
    await this.setCompanyFeatures(company);
    await this.sampleDataCurrenciesService.load(company, admin);
    const { users, teams } = await this.sampleDataUserManagementService.load(company, emailPrefix);
    const customObjectDefinitions = await this.sampleDataCustomObjectsService.load(company);
    await this.sampleDataDesignerService.load(company, customObjectDefinitions, users, teams, emailPrefix);
 
    if (runCalculation) {
      await this.sampleDataCalculationService.load(company);
    }
  }
 
  /**
   * Purge company datas.
   * @param company
   */
  public async purgeCompanyData(company: Company): Promise<void> {
    this.logger.log('Purge download objects');
    await this.sampleDataDownloadService.purge(company);
    this.logger.log('Purge calculation objects');
    await this.sampleDataCalculationService.purge(company);
    this.logger.log('Purge designer objects (variables, rules, plans, ...)');
    await this.sampleDataDesignerService.purge(company);
    this.logger.log('Purge workflow objects');
    await this.sampleDataWorkflowService.purge(company);
    this.logger.log('Purge custom objects (definitions, records, ...)');
    await this.sampleDataCustomObjectsService.purge(company);
    this.logger.log('Purge currencies objects');
    await this.sampleDataCurrenciesService.purge(company);
    this.logger.log('Purge custom report objects');
    await this.sampleDataReportingService.purge(company);
    this.logger.log('Purge user management objects (users, teams, ...)');
    await this.sampleDataUserManagementService.purge(company);
    this.logger.log('And finally delete company');
    await this.companyRepository.delete(company.id);
  }
 
  /**
   * Enable features for company.
   * @param company company to create
   */
  private async setCompanyFeatures(company: Company): Promise<void> {
    // Update company, set its currencies.
    await this.companyRepository.update(
      { id: company.id },
      {
        featureFlags: {
          [CompanyFeatureFlags.USER_CONNECTOR_BAMBOO]: true,
          [CompanyFeatureFlags.USER_CONNECTOR_PERSONIO]: true,
        },
      },
    );
  }
}