All files / libs/data-capture/record-models/core/src/lib customObjectDefinitions.service.ts

80.26% Statements 61/76
50% Branches 3/6
75% Functions 3/4
80.26% Lines 61/76

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 771x 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 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x  
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
 
import { CustomObjectDefinition, type Company } from '@amalia/core/models';
 
@Injectable()
export class CustomObjectDefinitionsService {
  public constructor(
    @InjectRepository(CustomObjectDefinition)
    private readonly objectDefinitionRepository: Repository<CustomObjectDefinition>,
  ) {}
 
  /**
   * Find definition by name.
   * @param company
   * @param machineName
   * @param throwIfNotFound
   * @return object definition
   * @throws {NotFoundException}
   */
  public async findByMachineName(
    company: Company,
    machineName: string,
    throwIfNotFound: boolean = true,
  ): Promise<CustomObjectDefinition | null> {
    const definition = await this.objectDefinitionRepository.findOneBy({
      company: { id: company.id },
      machineName,
    });
 
    if (!definition && throwIfNotFound) {
      throw new NotFoundException('Custom Object not found');
    }
 
    return definition;
  }
 
  /**
   * Find definition by id.
   * @param company
   * @param customObjectDefinitionId
   * @return object definition
   * @throws {NotFoundException}
   */
  public async findById(company: Company, customObjectDefinitionId: string): Promise<CustomObjectDefinition> {
    const definition = await this.objectDefinitionRepository.findOne({
      where: {
        company: { id: company.id },
        id: customObjectDefinitionId,
      },
    });

    if (!definition) {
      throw new NotFoundException();
    }

    return definition;
  }
 
  /**
   *  get all definitions for a given company
   * @param company
   * @returns
   */
  public async findAll(company: Company): Promise<CustomObjectDefinition[]> {
    return this.objectDefinitionRepository.find({
      where: {
        company: { id: company.id },
      },
      order: {
        name: 'ASC',
      },
    });
  }
}