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

25.81% Statements 8/31
0% Branches 0/6
0% Functions 0/6
20.69% Lines 6/29

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 114 115 116 117 118 1191x 1x             1x       1x     1x       1x                                                                                                                                                                                                      
import { bind, /* inject, */ BindingScope } from '@loopback/core';
import {
  AnyObject,
  FilterBuilder,
  Options,
  repository,
} from '@loopback/repository';
import { BaseResponses, CompanyParty } from '../models';
import {
  CompanyPartyRepository,
  CompanyPartyViewRepository, CompanyPersonaRepository,
} from '../repositories';
import { BaseService } from './base.service';
import { CompanyPartyView } from '@logchain/models/views';
import { CompanyPartyDto } from '@logchain/dtos';
import { HttpErrors } from '@loopback/rest';
import { ID, StringFormat } from '@logchain/types';
 
@bind({ scope: BindingScope.TRANSIENT })
export class CompanyPartyService extends BaseService<CompanyParty> {
 
  constructor(
    @repository(CompanyPartyRepository)
    public comp_party_repo: CompanyPartyRepository,
    @repository(CompanyPartyViewRepository)
    public comp_party_view_repo: CompanyPartyViewRepository,
    @repository(CompanyPersonaRepository)
    public comp_persona_repo: CompanyPersonaRepository,
  ) {
    super();
  }
 
  /**
   * Get company documents based on filter.
   */
  async find(): Promise<BaseResponses<CompanyPartyView>> {
    const filter_builder = new FilterBuilder<CompanyPartyView>();
    filter_builder.filter = this.filter_builder.filter as AnyObject;
 
    return this.comp_party_view_repo.findWithPaging(filter_builder.build());
  }
 
  /**
   * Create new company party
   * @param  {CompanyPartyDto} data
   * @param  {Options} options?
   * @returns Promise
   */
  async create(data: CompanyPartyDto, options?: Options): Promise<CompanyParty> {
    const { party_persona_id, party_company_id } = data;
 
    // Check if company_persona is exists.
    const comp_persona = await this.comp_persona_repo.findOne({
      where: {
        company_id: party_company_id,
        persona_id: party_persona_id
      },
      include: ['company', 'persona'],
    }, options);
    if (!comp_persona) {
      throw new HttpErrors.BadRequest(`The company persona not found`);
    }
 
    // Check if the party already exists.
    const { count: existing } = await this.comp_party_repo.count({ company_id: this.current_user.company_id, company_persona_id: comp_persona.id }, options)
    if (existing) {
      throw new HttpErrors.BadRequest(`The party already exists: ${comp_persona.company.name} - ${comp_persona.persona.title}`);
    }
 
    return this.comp_party_repo.create({
      company_id: this.current_user.company_id,
      company_persona_id: comp_persona.id,
    }, 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 | string, options?: Options): Promise<CompanyParty & { activity?: StringFormat } & { id: ID }>  {
    const deletedParty = this.comp_party_repo.findById(id as number, options);
 
    await this.comp_party_repo.deleteById(id as number, options);
    
    return deletedParty;
  }
 
  /**
   * 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: [
        {
          company_party_name: ilike_value,
        },
      ],
    });
  }
}