All files / src/controllers/public company-document.controller.ts

80% Statements 20/25
100% Branches 0/0
0% Functions 0/3
78.26% Lines 18/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 142 143 144 145 146 147 148 1491x 1x 1x 1x   1x 1x     1x 1x 1x 1x 1x 1x         1x                     1x                                                                                                 1x                 1x                                                 1x                         1x                                            
import { authenticate } from '@loopback/authentication';
import { authorize } from '@loopback/authorization';
import { Constructor, intercept, service } from '@loopback/core';
import { get, getModelSchemaRef, oas, param, patch, requestBody, RequestBodyObject, } from '@loopback/rest';
 
import { EnsureProvideDataInterceptor, serialize } from '@logchain/interceptors';
import {
  ExtendOptions, createBaseControllerFactory, BaseController, MixinOptions,
} from '@logchain/controllers';
import { buildResponseObjectById } from '@logchain/apidefs';
import { Module, Constants, CONTENT_TYPE } from '@logchain/configs';
import { CompanyDocumentDto } from '@logchain/dtos';
import { CompanyDocument } from '@logchain/models';
import { CompanyDocumentService } from '@logchain/services';
const { getByFormId } = require('@logchain/apidefs/schemas/company-document.schema');
 
/**
 * Define custom response body
 */
const get_company_document_by_form_id: RequestBodyObject = {
  description: 'The request body for get company documents by form id.',
  content: {
    [CONTENT_TYPE.JSON]: {
      schema: getByFormId(),
    },
  },
};
/**
 * Define the MixinOptions
 */
const options: MixinOptions = {
  basePath: '/templates',
  modelClass: CompanyDocument,
  modelDtoClass: CompanyDocumentDto,
  admin: false,
  schemaOptions: {
    find: {
      authorize: {
        resource: [
          Module.Compliance.TemplateMgmt,
          Module.Intermodal.EntryMgmt,
        ].join(','),
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
    findByFormId: {
      authorize: {
        resource: [
          Module.Compliance.TemplateMgmt,
          Module.Intermodal.EntryMgmt,
        ].join(','),
        scopes: [Constants.AccessScope.Retrieve],
      },
      jsonSchema: get_company_document_by_form_id,
    },
    updateByFormId: {
      exclude: [
        'id',
        'flow_id',
        'company_id',
        'feature_id',
        'module_id',
        'form_id',
        'created_date',
        'created_by',
        'modified_date',
        'modified_by',
      ],
      authorize: {
        resource: [
          Module.Compliance.TemplateMgmt,
          Module.Intermodal.EntryMgmt,
        ].join(','),
        scopes: [Constants.AccessScope.Update],
      },
    },
  },
};
 
const extendOptions: ExtendOptions = {
  isExtendRead: true,
};
 
/**
 * Company templates controller
 */
@oas.tags('Company Templates')
@authenticate('aws-cognito')
export class CompanyDocumentController extends createBaseControllerFactory<CompanyDocument, Constructor<BaseController>>(BaseController, options, extendOptions) {
  constructor(
    @service(CompanyDocumentService)
    public selfService: CompanyDocumentService,
  ) {
    super();
  }
 
  @get(`/${options.basePath}/{form_id}`, {
    responses: {
      '200': {
        description: 'The company documents',
        content: {
          [CONTENT_TYPE.JSON]: {
            schema: buildResponseObjectById(
              options.modelDtoClass,
              options.admin,
            ),
          },
        },
      },
    },
  })
  @authorize(options.schemaOptions['findByFormId'].authorize)
  @intercept(serialize(CompanyDocumentDto))
  async findByFormId(@param.path.integer('form_id') form_id: number) {
    return this.selfService.getByFormId(form_id);
  }
 
  @patch(`/${options.basePath}/{form_id}`, {
    responses: {
      '204': {
        description: `${options.modelClass.modelName} PATCH success`,
      },
    },
  })
  @intercept(EnsureProvideDataInterceptor.BINDING_KEY)
  @authorize(options.schemaOptions['updateByFormId'].authorize)
  async updateById(
    @param.path.integer('form_id') form_id: number,
    @requestBody({
      content: {
        [CONTENT_TYPE.JSON]: {
          schema: getModelSchemaRef(options.modelDtoClass, {
            partial: true,
            exclude: options.schemaOptions['updateByFormId'].exclude,
          }),
        },
      },
    })
    model: CompanyDocumentDto,
  ): Promise<void> {
    const updated = await this.selfService.updateByFormId(form_id, model);
 
    this.writeLog(options.modelClass, updated.id, updated, {
      str: '{2}',
      args: [updated.name],
    });
  }
}