All files / src/controllers/private trade-lane.controller.ts

59.38% Statements 19/32
0% Branches 0/36
0% Functions 0/4
56.67% Lines 17/30

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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 2191x   1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x                                                                                                                                                                     1x                           1x                                                     1x                                                                                       1x                                       1x                                
import {authorize} from '@loopback/authorization';
import {Where} from '@loopback/repository';
import {authenticate} from '@loopback/authentication';
import {Constructor, intercept, service} from '@loopback/core';
import {api, oas, get, param, getWhereSchemaFor, requestBody, put, post} from '@loopback/rest';
 
import {EnsurePagingCorrectInterceptor, EnsureProvideDataInterceptor, serialize} from '@logchain/interceptors';
import {createBaseControllerFactory, ExtendOptions, BaseController, MixinOptions} from '@logchain/controllers';
import {Module, Constants, CONTENT_TYPE} from '@logchain/configs';
import {TradeLaneDto, TradeLaneFlowDto} from '@logchain/dtos';
import {BaseResponses, TradeLane, TradeLaneRelations} from '@logchain/models';
import {TradeLaneService} from '@logchain/services';
import {buildResponseObject, countrySpec, limitSpec, offsetSpec, qSpec, sortSpec, upsertFlowSchema} from '@logchain/apidefs';
 
const options: MixinOptions = {
  basePath: '/trade-lanes',
  modelClass: TradeLane,
  modelDtoClass: TradeLaneDto,
  admin: true,
  schemaOptions: {
    create: {
      title: 'NewTradeLaneDtoWithCompanyId',
      exclude: [
        'created_name',
        'modified_name',
        'pol_country',
        'pod_country',
        'default_flow',
        'flows',
        'pod_name',
        'pol_name',
        'pol_country_id',
        'pod_country_id',
      ],
      authorize: {
        resource: Module.LogChain.TradeLaneMgmt,
        scopes: [Constants.AccessScope.Create],
      },
    },
    updateById: {
      title: 'UpdateTradeLaneDtoWithCompanyId',
      exclude: [
        'created_name',
        'modified_name',
        'pol_country',
        'pod_country',
        'default_flow',
        'flows',
        'pod_name',
        'pol_name',
        'pol_country_id',
        'pod_country_id',
        'cargo_type',
      ],
      authorize: {
        resource: Module.LogChain.TradeLaneMgmt,
        scopes: [Constants.AccessScope.Update],
      },
    },
    deleteById: {
      exclude: [],
      authorize: {
        resource: Module.LogChain.TradeLaneMgmt,
        scopes: [Constants.AccessScope.Delete],
      },
    },
    findById: {
      exclude: ['pod_country_id', 'pol_country_id', 'created_by', 'modified_by'],
      authorize: {
        resource: Module.LogChain.TradeLaneMgmt,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
    find: {
      exclude: [],
      authorize: {
        resource: Module.LogChain.TradeLaneMgmt,
        scopes: [Constants.AccessScope.Retrieve],
      },
    },
    upsertFlow: {
      authorize: {
        resource: Module.LogChain.TradeLaneMgmt,
        scopes: [Constants.AccessScope.Create, Constants.AccessScope.Update],
      },
      jsonSchema: upsertFlowSchema
    },
    copy: {
      exclude: [],
      authorize: {
        resource: Module.Admin.TradeLaneMgmt,
        scopes: [Constants.AccessScope.Create],
      },
    },
  },
};
 
const extendOptions: ExtendOptions = {
  isExtendCreate: true,
  isExtendReadById: true,
  isExtendUpdateById: true,
  isExtendDeleteById: true,
};
 
/**
 * TradeLanes controller
 */
@oas.tags('Trade Lane')
@api({basePath: '/admin'})
@intercept(serialize(TradeLaneDto))
@authenticate('aws-cognito')
export class TradeLanePrivateController extends createBaseControllerFactory<TradeLane, Constructor<BaseController>>(
  BaseController,
  options,
  extendOptions,
) {
  constructor(
    @service(TradeLaneService)
    public selfService: TradeLaneService,
  ) {
    super();
  }
 
  /**
   * Find list of `modelClass` 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(TradeLaneDto))
  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<TradeLane>,
    @param(countrySpec) country: number,
  ): Promise<BaseResponses<TradeLane>> {
    this.selfService.buildBaseFilter(offset, limit, sort, where);
    await this.selfService.buildSearchQuery(q);
    await this.selfService.buildCountryQuery(country);
    return this.selfService.find({is_private: this.is_private});
  }
 
  /**
   * Create or update reference flow.
   * @param id The id of trade-lane.
   * @param data The data to create reference flow.
   */
  @put(`/${options.basePath}/{id}/flow`, {
    responses: {
      '200': {
        description: 'The ID of reference flow',
        content: {
          [CONTENT_TYPE.JSON]: {
            schema: {
              type: 'object',
              properties: {
                ref_flow: {
                  type: 'object', 
                },
              },
            },
          },
        },
        '204': {
          description: 'Reference Flow PUT success',
        },
      },
    }
  })
  @intercept(EnsureProvideDataInterceptor.BINDING_KEY)
  @authorize(options.schemaOptions['upsertFlow'].authorize)
  async upsertFlow(
    @param.path.integer('id') id: number,
    @requestBody(options.schemaOptions['upsertFlow'].jsonSchema)
    data: TradeLaneFlowDto,
  ): Promise<TradeLane> {
    return this.selfService.upsertRefFlow(id, data);
  }
 
  /**
   * Copy a trade-lane.
   * @param id The id of trade-lane.
   */
  @post(`/${options.basePath}/{id}/copy`, {
    responses: {
      '200': {
        description: 'The data of copied trade-lane',
      },
    }
  })
  @authorize(options.schemaOptions['copy'].authorize)
  async copy(
    @param.path.integer('id') id: number,
  ): Promise<TradeLane & TradeLaneRelations> {
    const { copied, created } = await this.selfService.copy(id);
 
    // Create a log for create action
    const action1 = created?.activity?.args?.length ? created?.activity.args[0] as string : undefined;
    this.writeLog(options.modelClass, id, created, created.activity, action1);
 
    // Create a log for copy action
    const action2 = copied?.activity?.args?.length ? copied?.activity.args[0] as string : undefined;
    this.writeLog(options.modelClass, id, copied, copied.activity, action2);
 
    return copied;
  }
}