All files / libs/tenants/companies/core/src/lib/use-cases update-logo.use-case.ts

31.08% Statements 23/74
33.33% Branches 1/3
50% Functions 1/2
31.08% Lines 23/74

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 751x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x                                                                                                       1x  
import { BadRequestException, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
 
import { Company } from '@amalia/core/models';
import { assert } from '@amalia/ext/typescript';
import { AuthenticatedContext } from '@amalia/kernel/auth/types';
import { CloudStorageUploadService } from '@amalia/vendors/google-cloud/cloud-storage';
import { InjectGcpConfig, type GcpConfig } from '@amalia/vendors/google-cloud/config';
 
import { UploadLogoDto } from '../dto/uploadLogo.dto';
 
export class UpdateLogoUseCase {
  public constructor(
    private readonly cloudStorageUploadService: CloudStorageUploadService,
    @InjectRepository(Company)
    private readonly companyRepository: Repository<Company>,
    @InjectGcpConfig()
    private readonly gcpConfig: GcpConfig,
  ) {}
 
  public async execute({
    authenticatedContext,
    updateLogo,
  }: {
    authenticatedContext: AuthenticatedContext;
    updateLogo: UploadLogoDto;
  }): Promise<string | null> {
    const company = await this.companyRepository.findOne({ where: { id: authenticatedContext.user.companyId } });

    assert(company && company.id === authenticatedContext.user.companyId, new NotFoundException('Company not found'));

    if (updateLogo.file === null) {
      await this.companyRepository.save({
        ...company,
        customization: {
          ...company.customization,
          logoUrl: undefined,
        },
      });

      return null;
    }

    // Add validation before uploadImageToBucket call
    const base64Prefix = updateLogo.file.base64.split(',').at(0);
    const validImageTypes = ['data:image/jpeg', 'data:image/png', 'data:image/svg+xml'];
    assert(
      validImageTypes.some((type) => base64Prefix?.startsWith(type)),
      new BadRequestException('Invalid image format. Only JPEG, PNG, and SVG are allowed.'),
    );

    const logoUrl = await this.cloudStorageUploadService.uploadImageToBucket(
      { id: authenticatedContext.user.companyId },
      updateLogo.file.base64,
      {
        bucketName: this.gcpConfig.buckets.apiStorage,
        folder: 'logo',
        fileName: updateLogo.file.name,
        isPublic: true,
      },
    );

    await this.companyRepository.save({
      ...company,
      customization: {
        ...company.customization,
        logoUrl,
      },
    });

    return logoUrl;
  }
}