All files / src/controllers/public fleet.controller.ts

80% Statements 16/20
100% Branches 0/0
0% Functions 0/2
77.78% Lines 14/18

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 1041x 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 {oas, param, patch, requestBody} from '@loopback/rest';
 
import {BaseController, MixinOptions, ExtendOptions, createBaseControllerFactory} from '@logchain/controllers';
import {Module, Constants, CONTENT_TYPE} from '@logchain/configs';
import {FleetService} from '@logchain/services';
import {FleetView} from '@logchain/models/views';
import {FleetViewDto} from '@logchain/dtos';
import {EnsureProvideDataInterceptor} from '@logchain/interceptors';
 
/**
 * Define the MixinOptions
 */
const options: MixinOptions = {
  basePath: '/fleets',
  modelClass: FleetView,
  modelDtoClass: FleetViewDto,
  admin: false,
  schemaOptions: {
    updateById: {
      authorize: {
        resource: Module.Compliance.FleetMgmt,
        scopes: [Constants.AccessScope.Update],
      },
    },
    findById: {
      authorize: {
        resource: Module.Compliance.FleetMgmt,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
    find: {
      authorize: {
        resource: Module.Compliance.FleetMgmt,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
  },
};
 
const extendOptions: ExtendOptions = {
  isExtendRead: true,
  isExtendReadById: true,
  isExtendUpdateById: true,
};
 
/**
 * Fleet controller
 */
@oas.tags('Fleet')
@authenticate('aws-cognito')
export class FleetController extends createBaseControllerFactory<FleetView, Constructor<BaseController>>(
  BaseController,
  options,
  extendOptions,
) {
  constructor(
    @service(FleetService)
    public selfService: FleetService,
  ) {
    super();
  }
 
  /**
   * Update model instance by id
   * @param id The ID of model
   * @param model The new information of model
   * @returns Promise<void>
   */
  @patch(`/${options.basePath}/{id}`, {
    responses: {
      '204': {
        description: 'Updates the Trucking information globally. \n'
      },
    },
  })
  @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: {
              vehicle_no: {type: 'string'},
              delivered_by: {type: 'string'},
              delivery_note: {type: 'string'},
              reason_for_change: {type: 'string'},
            },
          },
        },
      },
    })
    payload: Partial<FleetViewDto>,
  ): Promise<void> {
    this.selfService.ensureRightAccessRecord();
    await this.selfService.updateById(id, payload);
  }
}