All files / libs/data-capture/imports/core/src/lib refreshments.service.ts

70.83% Statements 68/96
77.77% Branches 7/9
66.66% Functions 4/6
70.83% Lines 68/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 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x                             1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x                             1x 1x 8x 8x 8x 8x 8x 1x  
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
 
import { DataRefreshment, type Company, type DataConnector } from '@amalia/core/models';
import { CustomObjectImportStatus, REFRESHMENTS_ONGOING_STATUSES } from '@amalia/core/types';
import { DataConnectorObject, getConnectorObjectName } from '@amalia/data-capture/connectors/types';
import { assert } from '@amalia/ext/typescript';
 
@Injectable()
export class DataRefreshmentsService {
  public constructor(
    @InjectRepository(DataRefreshment)
    private readonly dataRefreshmentRepository: Repository<DataRefreshment>,
  ) {}
 
  /**
   * Get last connector refreshment for a given object if specified.
   * @param company
   * @param connectorId
   * @param object
   * @param status
   */
  public async getLastRefreshment(
    company: Company,
    connectorId: string | null,
    object: string,
    status?: CustomObjectImportStatus,
  ): Promise<DataRefreshment | null> {
    return this.dataRefreshmentRepository.findOne({
      where: {
        company: { id: company.id },
        object,
        ...(connectorId ? { connectorId } : {}),
        ...(status ? { status } : {}),
      },
      order: {
        updatedAt: 'DESC',
      },
    });
  }
 
  public async getLastRefreshmentsOfEachObject(company: Company): Promise<DataRefreshment[]> {
    const subQuery = this.dataRefreshmentRepository
      .createQueryBuilder('dr')
      .select('dr.object', 'object')
      .addSelect('MAX(dr.updatedAt)', 'maxUpdatedAt')
      .where('dr.companyId = :companyId', { companyId: company.id })
      .groupBy('dr.object');

    return this.dataRefreshmentRepository
      .createQueryBuilder('dr')
      .innerJoin(`(${subQuery.getQuery()})`, 'sub', 'dr.object = sub.object AND dr.updatedAt = sub."maxUpdatedAt"')
      .setParameters(subQuery.getParameters())
      .where('dr.companyId = :companyId', { companyId: company.id })
      .getMany();
  }
 
  /**
   * Returns true if at least a refreshment is ongoing for the company.
   * @param company
   */
  public async hasRefreshmentsOngoing(company: Company): Promise<boolean> {
    const refreshment = await this.dataRefreshmentRepository.findOne({
      where: {
        company: { id: company.id },
        status: In(REFRESHMENTS_ONGOING_STATUSES),
      },
    });
 
    return !!refreshment;
  }
 
  public async getRefreshmentsByTriggerCalculationId(
    company: Company,
    triggerCalculationId: DataRefreshment['triggerCalculationId'],
  ) {
    if (!triggerCalculationId) {
      return [];
    }

    return this.dataRefreshmentRepository.find({
      where: {
        company: { id: company.id },
        triggerCalculationId,
      },
    });
  }
 
  public static getConnectorObjectDefinition(connector: DataConnector, name: string): DataConnectorObject | undefined {
    assert(connector.source?.objects, `Objects not available on connector ${connector.id}`);
    return connector.source.objects.find(
      (dataConnectorObjectFromSource) => getConnectorObjectName(dataConnectorObjectFromSource) === name,
    );
  }
}