All files / libs/tenants/companies/core/src/lib/companiesIntegrations companiesIntegrations.service.ts

77.64% Statements 66/85
40% Branches 2/5
50% Functions 2/4
77.64% Lines 66/85

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 861x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 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 } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
 
import { Company, CompanyIntegration } from '@amalia/core/models';
import { CompanyIntegrationName } from '@amalia/tenants/companies/types';
 
import { type CompanyIntegrationDto } from '../dto/companyIntegration.dto';
 
@Injectable()
export class CompaniesIntegrationsService {
  public constructor(
    @InjectRepository(CompanyIntegration)
    private readonly companyIntegrationRepository: Repository<CompanyIntegration>,
    @InjectRepository(Company)
    private readonly companyRepository: Repository<Company>,
  ) {}
 
  /**
   *
   * @param companyId
   * @param integrationName
   * @returns
   */
  public async getCompanyIntegration(
    companyId: string,
    integrationName: CompanyIntegrationName,
  ): Promise<CompanyIntegration | null> {
    return this.companyIntegrationRepository.findOne({
      where: {
        companyId,
        name: integrationName,
      },
    });
  }
 
  public async getSlackAccessToken(companyId: string): Promise<string | null> {
    const integration = await this.getCompanyIntegration(companyId, CompanyIntegrationName.slack);

    return integration?.credentials.accessToken || null;
  }
 
  /**
   *
   * @param companyId
   * @param integrationDto
   */
  public async updateCompanyIntegration(companyId: string, integrationDto: CompanyIntegrationDto): Promise<void> {
    const companyIntegration = await this.companyIntegrationRepository.findOne({
      where: {
        companyId,
        name: integrationDto.name,
      },
    });
 
    if (companyIntegration) {
      await this.companyIntegrationRepository.save({
        ...companyIntegration,
        ...integrationDto,
        companyId,
      });
    } else {
      await this.companyIntegrationRepository.insert({
        companyId,
        ...integrationDto,
      });
    }
 
    const { integrationsStatus } = (await this.companyRepository.findOneBy({
      id: companyId,
    }))!;
 
    await this.companyRepository.update(
      { id: companyId },
      {
        integrationsStatus: {
          ...integrationsStatus,
          [integrationDto.name]: {
            status: integrationDto.credentials.status,
          },
        },
      },
    );
  }
}