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

100% Statements 114/114
66.66% Branches 10/15
100% Functions 6/6
100% Lines 114/114

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 1151x 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 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3318x 3318x 3318x 3318x 3318x 3318x 3318x 3318x 3318x 3318x 1x 1x 1x 1x  
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, type FindOptionsWhere } from 'typeorm';
 
import {
  CustomObject,
  CustomObjectDefinition,
  DataConnectionName,
  DataConnector,
  DataRefreshment,
  Relationship,
} from '@amalia/core/models';
import { assert } from '@amalia/ext/typescript';
import { type Company } from '@amalia/tenants/companies/types';
 
import { sampleCustomObjectDefinitions } from '../../../assets/sampleData/customObjectDefinitions';
import sampleCustomObjects from '../../../assets/sampleData/customObjects.json';
import { sampleRelationships } from '../../../assets/sampleData/relationships';
 
@Injectable()
export class SampleDataCustomObjectsService {
  public constructor(
    @InjectRepository(CustomObject, DataConnectionName)
    private readonly customObjectRepository: Repository<CustomObject>,
    @InjectRepository(CustomObjectDefinition)
    private readonly customObjectDefinitionRepository: Repository<CustomObjectDefinition>,
    @InjectRepository(DataRefreshment)
    private readonly dataRefreshmentRepository: Repository<DataRefreshment>,
    @InjectRepository(Relationship)
    private readonly relationshipRepository: Repository<Relationship>,
    @InjectRepository(DataConnector)
    private readonly dataConnectorRepository: Repository<DataConnector>,
  ) {}
 
  /**
   * Loads sample data for a given company.
   * @param company company to create
   */
  public async load(company: Company): Promise<CustomObjectDefinition[]> {
    const customObjectDefinitions = await this.loadCustomObjectDefinitions(company);
    await this.loadRelationships(company);
 
    await this.loadCustomObjects(company, customObjectDefinitions);
 
    return customObjectDefinitions;
  }
 
  public async purge(company: Company): Promise<void> {
    const companyConditions: FindOptionsWhere<{ company: { id: string } }> = {
      company: { id: company.id },
    };
 
    await this.customObjectRepository.delete({ companyId: company.id });
    await this.dataRefreshmentRepository.delete(companyConditions);
    await this.relationshipRepository.delete(companyConditions);
    await this.customObjectDefinitionRepository.delete(companyConditions);
    await this.dataConnectorRepository.delete(companyConditions);
  }
 
  /**
   * Load customObject definitions for company.
   * @param company
   * @private
   */
  private async loadCustomObjectDefinitions(company: Company): Promise<CustomObjectDefinition[]> {
    return Promise.all(
      sampleCustomObjectDefinitions.map((customObjectDefinition) =>
        this.customObjectDefinitionRepository.save({ ...customObjectDefinition, company }),
      ),
    );
  }
 
  /**
   * Load relationships for company.
   * @param company
   * @private
   */
  private async loadRelationships(company: Company): Promise<void> {
    await Promise.all(
      sampleRelationships.map((relation) =>
        this.relationshipRepository.save({
          ...relation,
          company,
        }),
      ),
    );
  }
 
  /**
   * Load custom objects.
   * @param company
   * @param customObjectDefinitions
   * @private
   */
  private loadCustomObjects(
    company: Company,
    customObjectDefinitions: CustomObjectDefinition[],
  ): Promise<CustomObject[]> {
    return Promise.all(
      sampleCustomObjects.map(({ definition, ...sampleCustomObject }) => {
        const objectDefinition = customObjectDefinitions.find(({ machineName }) => machineName === definition);
 
        assert(objectDefinition, `missing definition${definition}`);
 
        return this.customObjectRepository.save({
          ...sampleCustomObject,
          content: sampleCustomObject.content as unknown as CustomObject['content'],
          definitionId: objectDefinition.id,
          companyId: company.id,
        });
      }),
    );
  }
}