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

75% Statements 21/28
100% Branches 0/0
0% Functions 0/3
73.08% Lines 19/26

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 1521x 1x   1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x         1x                                     1x                                                                       1x                       1x                                             1x                                                                   1x              
import { authorize } from '@loopback/authorization';
import { authenticate } from '@loopback/authentication';
import { Where } from '@loopback/repository';
import { service, intercept, Constructor } from '@loopback/core';
import { oas, get, param, getWhereSchemaFor, RequestBodyObject, post, requestBody } from '@loopback/rest';
 
import { EnsurePagingCorrectInterceptor, serialize } from '@logchain/interceptors';
import { BaseController, createBaseControllerFactory, ExtendOptions, MixinOptions } from '@logchain/controllers';
import { CompanyPartyDto } from '@logchain/dtos';
import { CompanyParty, BaseResponses } from '@logchain/models';
import { CompanyPartyService } from '@logchain/services';
import { buildResponseObject, offsetSpec, limitSpec, sortSpec, qSpec } from '@logchain/apidefs';
import { Constants } from '@logchain/configs/constants';
import { Module } from '@logchain/configs/module';
import { CompanyPartyView } from '@logchain/models/views';
import { CONTENT_TYPE } from '@logchain/configs';
 
/**
 * Define custom response body
 */
const create_company_party: RequestBodyObject = {
  description: 'The request body for creating company party.',
  content: {
    [CONTENT_TYPE.JSON]: {
      schema: {
        type: 'object',
        properties: {
          party_persona_id: {
            type: 'number',
          },
          party_company_id: {
            type: 'number',
          },
        },
      },
    },
  },
};
 
const options: MixinOptions = {
  basePath: '/parties',
  modelClass: CompanyParty,
  modelDtoClass: CompanyPartyDto,
  admin: false,
  schemaOptions: {
    find: {
      exclude: [
        'created_by',
        'modified_by',
        'created_date',
        'modified_date',
      ],
      authorize: {
        resource: Module.Admin.PartyMgmt,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
    create: {
      authorize: {
        resource: Module.Admin.PartyMgmt,
        scopes: [Constants.AccessScope.Create],
      },
      jsonSchema: create_company_party,
      requestBody: create_company_party,
    },
    deleteById: {
      exclude: [],
      authorize: {
        resource: Module.Admin.PartyMgmt,
        scopes: [Constants.AccessScope.Delete],
      },
    },
  },
};
 
const extendOptions: ExtendOptions = {
  isExtendRead: true,
  isExtendCreate: true,
  isExtendDeleteById: true,
}
 
/**
 * Partys controller
 */
@oas.tags('Party')
@authenticate('aws-cognito')
@intercept(serialize(CompanyPartyDto))
export class CompanyPartyController extends createBaseControllerFactory<CompanyParty, Constructor<BaseController>>(BaseController, options, extendOptions) {
  constructor(
    @service(CompanyPartyService)
    public selfService: CompanyPartyService,
  ) {
    super();
  }
 
  /**
   * Find list of trade land based on query
   * @param offset The offset number
   * @param limit The limit number
   * @param sort The array order
   * @param where The query object
   */
  @get(`${options.basePath}`, {
    responses: {
      '200': buildResponseObject(options.modelDtoClass, options.admin),
    },
  })
  @intercept(EnsurePagingCorrectInterceptor.BINDING_KEY)
  @authorize(options.schemaOptions['find'].authorize)
  @intercept(serialize(CompanyPartyDto))
  async find(
    @param(offsetSpec) offset: number,
    @param(limitSpec) limit: number,
    @param(sortSpec) sort: Array<string>,
    @param(qSpec) q: string,
    @param.query.object('where', getWhereSchemaFor(options.modelClass))
    where: Where<CompanyParty>,
  ): Promise<BaseResponses<CompanyPartyView>> {
    this.selfService.buildBaseFilter(offset, limit, sort, where);
    await this.selfService.buildSearchQuery(q);
    this.selfService.ensureRightAccessRecord();
    return this.selfService.find();
  }
 
  /**
   * Create new company party
   */
  @post(`/${options.basePath}`, {
    responses: {
      '200': {
        description: 'Create new company party',
      },
      '400': {
        description: 'Validation Errors (Bad Request)',
      },
      '401': {
        description: 'Unauthorized Error',
      },
      '500': {
        description: 'Internal Server Error (System Error)',
      },
    },
  })
  @intercept(serialize(CompanyPartyDto))
  async create(
    @requestBody(options.schemaOptions['create'].requestBody)
    payload: CompanyPartyDto
  ) {
    return this.selfService.create(payload);
  }
}