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

72% Statements 18/25
100% Branches 0/0
0% Functions 0/3
69.57% Lines 16/23

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 1421x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x     1x                                                                 1x               1x                                     1x                                                                                                         1x                            
import { authorize } from '@loopback/authorization';
import { intercept, service, Constructor } from '@loopback/core';
import { requestBody, oas, patch, param, api, get, getWhereSchemaFor } from '@loopback/rest';
import { authenticate } from '@loopback/authentication';
 
import { BaseController, MixinOptions, ExtendOptions, createBaseControllerFactory } from '@logchain/controllers';
import { EnsureProvideDataInterceptor, EnsurePagingCorrectInterceptor, serialize } from '@logchain/interceptors';
import { CompanyRegulatory, Status, BaseResponses, CompanyRegulatoryHistories } from '@logchain/models';
import { CompanyRegulatoryService, CompanyRegulatoryHistoriesService } from '@logchain/services';
import { Module, Constants, CONTENT_TYPE } from '@logchain/configs';
import { CompanyRegulatoryDto, CompanyRegulatoryHistoriesDto } from '@logchain/dtos';
import { buildResponseObject, limitSpec, offsetSpec, qSpec, sortSpec } from '@logchain/apidefs';
import { Where } from '@loopback/repository';
 
const options: MixinOptions = {
  basePath: '/companies-regulatory',
  modelClass: CompanyRegulatory,
  modelDtoClass: CompanyRegulatoryDto,
  schemaOptions: {
    updateById: {
      authorize: {
        resource: Module.LogChain.CompanyMgmt,
        scopes: [Constants.AccessScope.Update],
      },
      exclude: ['company_id', 'uen', 'tradeweb_id']
    },
    findById: {
      authorize: {
        resource: Module.LogChain.CompanyMgmt,
        scopes: [Constants.AccessScope.Update],
      },
    },
    find: {
      authorize: {
        resource: Module.LogChain.CompanyMgmt,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
    findCompanyRegulatoryHistories: {
      authorize: {
        resource: Module.LogChain.CompanyMgmt,
        scopes: [Constants.AccessScope.Retrieve],
      },
    }
  },
};
 
const extendOptions: ExtendOptions = {
  isExtendRead: true,
  isExtendReadById: true,
}
 
@oas.tags('Company Regulatory')
@api({ basePath: '/admin' })
@authenticate('aws-cognito')
export class CompanyRegulatoryPrivateController extends createBaseControllerFactory<CompanyRegulatory, Constructor<BaseController>>(BaseController, options, extendOptions) {
  constructor(
    @service(CompanyRegulatoryService)
    public selfService: CompanyRegulatoryService,
    @service(CompanyRegulatoryHistoriesService)
    public companyRegulatoryHistoriesService: CompanyRegulatoryHistoriesService
  ) {
    super();
  }
 
  @patch(`/${options.basePath}/{id}`, {
    responses: {
      '204': {
        description: `${options.modelClass.modelName} PATCH success`,
      },
    },
  })
  @intercept(EnsureProvideDataInterceptor.BINDING_KEY)
  @authorize(options.schemaOptions['updateById'].authorize)
  async updateById(
    @param.path.integer('id') id: number,
    @requestBody({
      content: {
        [CONTENT_TYPE.JSON]: {
          schema: {
            type: 'object',
            properties: {
              duration_start: { type: "string", format: 'date-time' },
              duration_end: {
                type: "string", format: 'date-time',
                formatMinimum: {
                  "$data": "1/duration_start"
                }
              },
              status: { type: "number", enum: [Status.CompanyRegulatory.Accepted, Status.CompanyRegulatory.Rejected, Status.CompanyRegulatory.Expired] },
              comment: { type: "string", maxLength: 300 },
            },
            allOf: [
              {
                if: {
                  properties: {
                    status: { const: Status.CompanyRegulatory.Rejected }
                  }
                },
                then: { required: ["comment"] }
              },
              {
                if: {
                  properties: {
                    status: { const: Status.CompanyRegulatory.Expired }
                  }
                },
                then: { required: ["duration_start", "duration_end"] }
              }
            ]
          },
        },
      },
    })
    model: Partial<CompanyRegulatory> & { comment: string },
  ): Promise<void> {
    await this.selfService.updateByIdOrIdentifier(id, model);
  }
 
  @get(`/companies/{id}/regulatory-histories`, {
    responses: {
      '200': buildResponseObject(CompanyRegulatoryHistoriesDto, options.admin),
    },
  })
  @intercept(EnsurePagingCorrectInterceptor.BINDING_KEY)
  @authorize(options.schemaOptions['findCompanyRegulatoryHistories'].authorize)
  @intercept(serialize(CompanyRegulatoryHistoriesDto))
  async findCompanyRegulatoryHistories(
    @param(offsetSpec) offset: number,
    @param(limitSpec) limit: number,
    @param(sortSpec) sort: Array<string>,
    @param(qSpec) q: string,
    @param.query.object('where', getWhereSchemaFor(CompanyRegulatory))
    where: Where<CompanyRegulatoryHistories>,
    @param.path.integer('id') id: number,
  ): Promise<BaseResponses<CompanyRegulatoryHistories>> {
    this.companyRegulatoryHistoriesService.buildBaseFilter(offset, limit, sort, where);
    await this.companyRegulatoryHistoriesService.buildSearchQuery(q);
    return this.companyRegulatoryHistoriesService.findCompanyRegulatoryHistories(id, { is_private: this.is_private });
  }
}