All files / src/controllers/public product-hazard.controller.ts

56.67% Statements 17/30
0% Branches 0/2
0% Functions 0/6
53.57% Lines 15/28

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                                                       1x                                      
import { authorize } from '@loopback/authorization';
import { authenticate } from '@loopback/authentication';
import { Where } from '@loopback/repository';
import { inject, intercept, service } from '@loopback/core';
import { oas, post, Request, Response, requestBody, RestBindings, get, param, getWhereSchemaFor } from '@loopback/rest';
 
import { BaseController, MixinOptions } from '@logchain/controllers';
import { ProductHazardDto } from '@logchain/dtos';
import { BaseResponses, ProductHazard } from '@logchain/models';
import { ProductHazardService } from '@logchain/services';
import { FILE_UPLOAD_SERVICE, FileUploadHandler } from '@logchain/csv-parser';
import { buildResponseObject, countrySpec, limitSpec, offsetSpec, qSpec, sortSpec } from '@logchain/apidefs';
import { EnsurePagingCorrectInterceptor, serialize } from '@logchain/interceptors';
 
/**
 * Define the MixinOptions for find
 */
const options: MixinOptions = {
  basePath: '/product-hazards',
  modelClass: ProductHazard,
  modelDtoClass: ProductHazardDto,
  admin: false,
  schemaOptions: {
    find: {
      exclude: ['created_date', 'created_by', 'modified_date', 'modified_by'],
    },
  },
};
 
@oas.tags('ProductHazard')
@authenticate('aws-cognito')
export class ProductHazardController extends BaseController {
  constructor(
    @service(ProductHazardService)
    public selfService: ProductHazardService,
    @inject(FILE_UPLOAD_SERVICE)
    private handler: FileUploadHandler,
  ) {
    super();
  }
 
  /**
   * Find list of product-hazard 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(ProductHazardDto))
  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<ProductHazard>,
    @param(countrySpec) country: number,
  ): Promise<BaseResponses<ProductHazard>> {
    this.selfService.buildBaseFilter(offset, limit, sort, where);
    await this.selfService.buildSearchQuery(q);
    await this.selfService.buildCountryQuery(country);
    return this.selfService.find();
  }
 
  @post(`${options.basePath}-upload`, {
    responses: {
      200: {
        content: {
          'application/json': {
            schema: {
              type: 'object',
            },
          },
        },
      },
    },
  })
  async upload(
    @requestBody.file()
    request: Request,
    @inject(RestBindings.Http.RESPONSE) response: Response,
  ): Promise<object> {
    return new Promise<object>((resolve, reject) => {
      this.handler(request, response, (err: unknown) => {
        if (err) {
          reject(err)
        } else {
          this.selfService.createProductHazardFromCSV(request.files)
            .then((results) => {
              resolve({ results });
            }).catch(reject);
        }
      });
    });
  }
}