All files / src/controllers/public flow-default.controller.ts

68% Statements 17/25
100% Branches 0/0
0% Functions 0/3
65.22% Lines 15/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 1231x 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 { intercept, service } from '@loopback/core';
import { Where } from '@loopback/repository';
import { api, get, getWhereSchemaFor, oas, param } from '@loopback/rest';
 
import { BaseController, MixinOptions } from '@logchain/controllers';
import { EnsurePagingCorrectInterceptor, serialize } from '@logchain/interceptors';
import { buildResponseObject, buildResponseObjectById, limitSpec, offsetSpec, qSpec, sortSpec, } from '@logchain/apidefs';
import { Constants, Module, CONTENT_TYPE } from '@logchain/configs';
import { FlowDefaultDto } from '@logchain/dtos';
import { BaseResponses, FlowDefault } from '@logchain/models';
import { FlowDefaultService } from '@logchain/services';
 
const options: MixinOptions = {
  basePath: '/flows-default',
  modelClass: FlowDefault,
  modelDtoClass: FlowDefaultDto,
  admin: false,
  schemaOptions: {
    findById: {
      exclude: [],
      authorize: {
        resource: Module.Public,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
    find: {
      exclude: [
        'main_contact',
        'tags',
        'google_map_link',
        'privacy_status',
        'country_id',
      ],
      authorize: {
        resource: Module.Public,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
  },
};
 
@oas.tags('Flow Default')
@api({ basePath: '/' })
@authenticate('aws-cognito')
export class FlowDefaultController extends BaseController {
  constructor(
    @service(FlowDefaultService)
    public selfService: FlowDefaultService,
  ) {
    super();
  }
  /**
   * Get model instance by id
   * @param id The ID of model
   * @returns Model instance
   */
  @get(`/${options.basePath}/{id}`, {
    responses: {
      '200': {
        description: `${options.modelClass.modelName} model instance`,
        content: {
          [CONTENT_TYPE.JSON]: {
            schema: buildResponseObjectById(
              options.modelDtoClass,
              options.admin,
            ),
          },
        },
      },
    },
  })
  @authorize(options.schemaOptions['findById'].authorize)
  @intercept(serialize(FlowDefaultDto))
  async findById(
    @param.path.integer('id') id: number,
    @param.query.integer('step') step: number,
    @param.query.integer('step_id') step_id: number,
    @param.query.integer('group_id') group_id: number,
    @param.query.boolean('r_journey') r_journey: boolean,
  ) {
    this.selfService.ensureRightAccessRecord();
    return this.selfService.getByIdOrIdentifier(id, {
      is_private: this.is_private,
      step,
      step_id,
      group_id,
      r_journey,
    });
  }
 
  /**
   * Find list of default flow 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(FlowDefaultDto))
  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<FlowDefault>,
  ): Promise<BaseResponses<FlowDefault>> {
    this.selfService.buildBaseFilter(offset, limit, sort, where);
    await this.selfService.buildSearchQuery(q);
    this.selfService.ensureRightAccessRecord();
 
    return this.selfService.find({ is_private: this.is_private });
  }
}