All files / src/services company-field.service.ts

29.63% Statements 8/27
0% Branches 0/8
0% Functions 0/6
24% Lines 6/25

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 1141x 1x             1x 1x   1x       1x                                                                                                                                                                                                    
import { bind, /* inject, */ BindingScope } from '@loopback/core';
import {
  AnyObject,
  FilterBuilder,
  Options,
  repository,
} from '@loopback/repository';
import { BaseResponses, CompanyField } from '../models';
import { CompanyFieldRepository } from '../repositories';
import { BaseService } from './base.service';
import { CompanyFieldDto } from '@logchain/dtos';
import { HttpErrors } from '@loopback/rest';
import { ID, StringFormat } from '@logchain/types';
 
@bind({ scope: BindingScope.TRANSIENT })
export class CompanyFieldService extends BaseService<CompanyField> {
 
  constructor(
    @repository(CompanyFieldRepository)
    public comp_field_repo: CompanyFieldRepository,
  ) {
    super();
  }
 
  /**
   * Get company documents based on filter.
   */
  async find(): Promise<BaseResponses<CompanyField>> {
    const filter_builder = new FilterBuilder<CompanyField>();
    filter_builder.filter = this.filter_builder.filter as AnyObject;
 
    return this.comp_field_repo.findWithPaging(filter_builder.build());
  }
 
  /**
   * Create new company Field
   * @param  {CompanyFieldDto} data
   * @param  {Options} options?
   * @returns Promise
   */
  async create(data: CompanyFieldDto, options?: Options): Promise<CompanyField> {
    const { company_id, field_name_db, field_name_xml } = data;
 
    // Check if the Field already exists.
    const { count: existing } = await this.comp_field_repo.count({
      company_id,
      field_name_db,
      field_name_xml
    }, options)
    if (existing) {
      throw new HttpErrors.BadRequest(`The Field already exists: ${company_id} - ${field_name_db} - ${field_name_xml}`);
    }
 
    return this.comp_field_repo.create({
      company_id,
      field_name_db,
      field_name_xml
    }, options);
  }
 
  /**
   * Deletes a record by its ID or identifier.
   *
   * @param {number | string} id - The ID or identifier of the record to delete.
   * @param {Options} options - Optional parameters for the delete operation.
   */
  async deleteByIdOrIdentifier(id: number, options?: Options): Promise<CompanyField & { activity?: StringFormat } & { id: ID }> {
    const deletedField = await this.comp_field_repo.findOne({
      where: {
        id,
        company_id: this.current_user.company_id
      }
    }, options);
 
    if (deletedField) {
      await this.comp_field_repo.deleteById(id as number, options);
    }
 
    return deletedField ? deletedField : {} as CompanyField & { activity?: StringFormat } & { id: ID };
  }
 
  /**
   * Ensure get right records.
   */
  public ensureRightAccessRecord(): void {
    this.filter_builder.impose({
      company_id: this.current_user.company_id,
    });
  }
 
  /**
   * Builds the query for user.
   * @param q The query string.
   */
  async buildSearchQuery(q?: string) {
    if (!q) {
      return;
    }
 
    const ilike_value = this.buildILikeValue(q);
    // FILTER: name Should be a LIKE search
    this.filter_builder.impose({
      or: [
        {
          field_name_db: ilike_value,
        },
        {
          field_name_xml: ilike_value,
        },
      ],
    });
  }
}