All files / apps/public-api/src/app/public-api-module PublicApi.service.ts

100% Statements 72/72
76.47% Branches 13/17
100% Functions 2/2
100% Lines 72/72

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 731x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 7x 1x 1x 5x 7x 1x 1x 4x 7x 1x 1x 3x 3x 3x 3x 3x 3x 7x 1x 1x 2x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 1x  
import { BadRequestException, ForbiddenException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { validate as isUuid } from 'uuid';
 
import { CompanyApiKey, CustomReport } from '@amalia/core/models';
import { GetReportContentWithoutAuthenticationUseCase } from '@amalia/reporting/custom-reports/core';
import { buildCustomReportColumns } from '@amalia/reporting/custom-reports/shared';
 
@Injectable()
export class PublicApiService {
  public constructor(
    @InjectRepository(CompanyApiKey)
    private readonly apiKeyRepository: Repository<CompanyApiKey>,
    @InjectRepository(CustomReport)
    private readonly customReportRepository: Repository<CustomReport>,
    private readonly getReportContentWithoutAuthenticationUseCase: GetReportContentWithoutAuthenticationUseCase,
  ) {}
 
  public async getData(apiKey: string, customReportId: string, page: number = 1, limit: number = 100) {
    if (!apiKey) {
      throw new BadRequestException(
        'Token not found. You have to add the header X-API-KEY with your token value in it.',
      );
    }
 
    const companyApiKeys = await this.apiKeyRepository.findOne({
      where: { token: apiKey },
      relations: ['company'],
    });
 
    if (!companyApiKeys) {
      throw new ForbiddenException('Invalid token');
    }
 
    if (companyApiKeys.deactivatedAt) {
      throw new ForbiddenException('Token deactivated');
    }
 
    if (!isUuid(customReportId)) {
      throw new BadRequestException('Custom report id invalid');
    }
 
    const customReport = await this.customReportRepository.findOne({
      where: { company: { id: companyApiKeys.company.id }, id: customReportId },
      relations: ['company'],
    });
 
    if (!customReport) {
      throw new NotFoundException('Custom report not found');
    }
 
    if (!customReport.isAccessibleFromApi) {
      throw new NotFoundException('Custom Report is not accessible via API.');
    }
 
    const { records, manifestsMap } = await this.getReportContentWithoutAuthenticationUseCase.execute(
      companyApiKeys.company,
      customReport,
      { pagination: { limit, page } },
    );
 
    return {
      records,
      columns: buildCustomReportColumns(customReport, manifestsMap),
      meta: {
        companyId: companyApiKeys.company.id,
        companyIolId: companyApiKeys.company.featureFlags.IOL_INTEGRATION ? companyApiKeys.company.iolId : undefined,
      },
    };
  }
}