All files / src/controllers/private company.controller.ts

66.67% Statements 18/27
0% Branches 0/3
0% Functions 0/3
64% Lines 16/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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 1541x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x   1x                                                                                                                 1x                     1x                                             1x                                                                                   1x            
import { authenticate } from '@loopback/authentication';
import { Constructor, intercept, service } from '@loopback/core';
import { Where } from '@loopback/repository';
import { api, get, getWhereSchemaFor, oas, param, post, requestBody } from '@loopback/rest';
 
import { EnsurePagingCorrectInterceptor, serialize } from '@logchain/interceptors';
import { BaseController, createBaseControllerFactory, ExtendOptions, MixinOptions, } from '@logchain/controllers';
import { buildResponseObject, countrySpec, limitSpec, offsetSpec, personaSpec, qSpec, sortSpec, } from '@logchain/apidefs';
import { Module, Constants } from '@logchain/configs';
import { CompanyDto, CompanyLicenceDto } from '@logchain/dtos';
import { Company } from '@logchain/models';
import { CompanyService } from '@logchain/services';
import { authorize } from '@loopback/authorization';
 
const options: MixinOptions = {
  basePath: '/companies',
  modelClass: Company,
  modelDtoClass: CompanyDto,
  admin: true,
  schemaOptions: {
    create: {
      exclude: ['main_contact_id', 'status', 'parent_company_id'],
      authorize: {
        resource: Module.LogChain.CompanyMgmt,
        scopes: [Constants.AccessScope.Update],
      },
    },
    updateById: {
      exclude: ['main_contact_id', 'parent_company_id'],
      authorize: {
        resource: Module.LogChain.CompanyMgmt,
        scopes: [Constants.AccessScope.Update],
      },
    },
    deleteById: {
      exclude: [],
      authorize: {
        resource: Module.LogChain.CompanyMgmt,
        scopes: [Constants.AccessScope.Delete],
      },
    },
    findById: {
      exclude: [],
      authorize: {
        resource: Module.LogChain.CompanyMgmt,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
    find: {
      exclude: [
        'main_contact',
        'tags',
        'google_map_link',
        'privacy_status',
        'country_id',
      ],
      authorize: {
        resource: Module.LogChain.CompanyMgmt,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
    createOrUpdateLicence: {
      exclude: ['id', 'created_date', 'modified_date'],
      authorize: {
        resource: Module.LogChain.LicenceMgmt,
        scopes: [Constants.AccessScope.Update],
      },
    },
  },
};
 
const extendOptions: ExtendOptions = {
  isExtendCreate: true,
  isExtendReadById: true,
  isExtendUpdateById: true,
  isExtendDeleteById: true,
}
 
@oas.tags('Company')
@api({ basePath: '/admin' })
@intercept(serialize(CompanyDto))
@authenticate('aws-cognito')
export class CompanyPrivateController extends createBaseControllerFactory<Company, Constructor<BaseController>>(BaseController, options, extendOptions) {
  constructor(
    @service(CompanyService)
    public selfService: CompanyService,
  ) {
    super();
  }
 
  /**
   * Get companies based on query
   * @param offset The offset number
   * @param limit The limit number
   * @param sort The sort array
   * @param where The query object
   */
  @intercept(serialize(CompanyDto))
  @intercept(EnsurePagingCorrectInterceptor.BINDING_KEY)
  @authorize(options.schemaOptions['find'].authorize)
  @get(`${options.basePath}`, {
    responses: {
      '200': buildResponseObject(CompanyDto, options.admin),
    },
  })
  async find(
    @param(offsetSpec) offset: number,
    @param(limitSpec) limit: number,
    @param(sortSpec) sort: string[],
    @param(qSpec) q: string,
    @param(countrySpec) country: number,
    @param(personaSpec) personaId: number,
    @param.query.object('where', getWhereSchemaFor(Company))
    where: Where<Company> = {},
  ) {
    this.selfService.buildBaseFilter(offset, limit, sort, where);
    await this.selfService.buildSearchQuery(q);
    await this.selfService.buildCountryQuery(country);
 
    // get companies by persona id.
    if (personaId) {
      await this.selfService.imposeGetCompaniesByPersona(personaId);
    }
    return this.selfService.find();
  }
 
  /**
   * Create new company licence
   */
  @post(`/${options.basePath}/licence`, {
    responses: {
      '200': {
        description: 'Create new company licence',
      },
      '400': {
        description: 'Validation Errors (Bad Request)',
      },
      '401': {
        description: 'Unauthorized Error',
      },
      '500': {
        description: 'Internal Server Error (System Error)',
      },
    },
  })
  @intercept(serialize(CompanyLicenceDto))
  @authorize(options.schemaOptions['createOrUpdateLicence'].authorize)
  async createOrUpdateLicence(
    @requestBody(options.schemaOptions['createOrUpdateLicence'].requestBody)
    payload: CompanyLicenceDto
  ) {
    return this.selfService.createOrUpdateLicence(payload);
  }
}