All files / src/apidefs common-spec.ts

82.35% Statements 28/34
57.89% Branches 11/19
80% Functions 4/5
82.35% Lines 28/34

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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285    1x 1x 1x 1x 1x         1x                             1x                                   1x                           1x                             1x                         1x                           1x                           1x                                       1x             70x                               1x             70x 70x                                                   1x           35x                           35x   35x           1x                                     1x                                             1x             49x 49x         1x                    
import { ParameterObject } from '@loopback/openapi-v3';
import { Options } from '@loopback/repository';
import { getModelSchemaRef, JsonSchemaOptions, MediaTypeObject, ResponseObject, SchemaRef } from '@loopback/rest';
import { initial } from 'lodash';
import { Constants, CONTENT_TYPE } from '@logchain/configs';
import { ValidationError } from '@logchain/utils';
const schemas = require('./schemas');
 
/**
 * The offset value.
 */
export const offsetSpec: ParameterObject = {
  description: 'The offset value.',
  required: false,
  name: 'offset',
  in: 'query',
  schema: {
    type: 'integer',
    minimum: 0,
    default: 0,
  },
};
 
/**
 * The numbers of items to return.
 */
export const limitSpec: ParameterObject = {
  description: `The number of items to return.
    If the limit greater than ${Constants.DefaultLimit},
    it auto set to ${Constants.DefaultLimit}`,
  required: false,
  name: 'limit',
  in: 'query',
  schema: {
    type: 'integer',
    minimum: 1,
    maximum: Constants.DefaultLimit,
    default: Constants.DefaultLimit,
  },
};
 
/**
 * Search keywords.
 */
export const qSpec: ParameterObject = {
  description: 'Search keywords.',
  required: false,
  name: 'q',
  in: 'query',
  schema: {
    type: 'string',
    minimum: 1,
  },
};
 
/**
 * The two-letter ISO country code that identifies the region.
 */
export const glSpec: ParameterObject = {
  description: 'The two-letter ISO country code that identifies the region.',
  required: false,
  name: 'gl',
  in: 'query',
  schema: {
    type: 'string',
    maxLength: 2,
    minLength: 2,
  },
};
 
/**
 * The country id in our DB
 */
export const countrySpec: ParameterObject = {
  description: 'The country id.',
  required: false,
  name: 'country',
  in: 'query',
  schema: {
    type: 'number',
  },
};
 
/**
 * The from date
 */
export const fromSpec: ParameterObject = {
  description: 'The from date.',
  required: false,
  name: 'from',
  in: 'query',
  schema: {
    type: 'string',
    format: 'date-time',
  },
};
 
/**
 * The to date
 */
export const toSpec: ParameterObject = {
  description: 'The to date.',
  required: false,
  name: 'to',
  in: 'query',
  schema: {
    type: 'string',
    format: 'date-time',
  },
};
 
/**
 * The sort
 */
export const sortSpec: ParameterObject = {
  description: `The sort query parameter as a list of fields to sort on (ASC or DESC)`,
  required: false,
  name: 'sort',
  in: 'query',
  schema: {
    type: 'array',
    uniqueItems: true,
    items: {
      type: 'string',
      example: 'created_date ASC',
    },
  },
};
 
/**
 * Build Response Object
 * @param modelDto - The DTO model constructor
 * @param options - Additional options
 */
export function buildResponseObject<T extends object>(
  modelDto: Function & {
    prototype: T;
  },
  admin?: boolean,
  options?: JsonSchemaOptions<T>,
): ResponseObject {
  return {
    description: `Array of ${modelDto.name.replace(
      'Dto',
      '',
    )} records on the platform.`,
    content: {
      [CONTENT_TYPE.JSON]: buildMediaTypeObject(modelDto, admin, options),
    },
  };
}
 
/**
 * Build MediaType Object
 * @param modelDto - The DTO model constructor
 * @param options - Additional options
 */
export function buildMediaTypeObject<T extends object>(
  modelDto: Function & {
    prototype: T;
  },
  admin?: boolean,
  options?: JsonSchemaOptions<T>,
): MediaTypeObject {
  const model_name = modelDto.name.replace('Dto', '');
  return {
    schema: {
      type: 'object',
      properties: {
        total: {
          type: 'number',
        },
        has_more: {
          type: 'boolean',
        },
        data: {
          type: 'array',
          items: schemas[`${model_name}Schema`]
            ? schemas[`${model_name}Schema`].get(admin)
            : getModelSchemaRef(modelDto, options),
        },
      },
    },
  };
}
 
/**
 * Get model schema without auto populate (e.g. `created_date`, `created_by`)
 * @param modelCtor - The model constructor (e.g. `Product`)
 * @param options - Additional options
 */
export function getModelSchemaRefWithoutAutoPopulate<T extends object>(
  modelCtor: Function & {
    prototype: T;
  },
  options: Options = { exclude: [] },
): SchemaRef {
  const autoPopulateFields = [
    'id',
    'identifier',
    'created_date',
    'created_by',
    'modified_date',
    'modified_by',
    'login_date',
    'uri',
    'version',
    'hash',
    'blockchain_tx',
  ];
 
  options.exclude = [...(options?.exclude ?? []), ...autoPopulateFields];
 
  return getModelSchemaRef(modelCtor, options);
}
 
/**
 * The persona id
 */
export const reqPersonaSpec: ParameterObject = {
  description:
    'The provider persona id to query: \n' +
    '* `2` - ISO Tank Depot \n' +
    '* `3` - Logistics Service Provider \n' +
    '* `6` - Shipper \n' +
    '* `8` - Trucking Company \n',
  required: true,
  name: 'persona',
  in: 'query',
  schema: {
    type: 'integer',
    enum: Object.values(Constants.RequestPersona),
  },
};
 
/**
 * The persona id
 */
export const personaSpec: ParameterObject = {
  description:
    'The provider persona id to query: \n' +
    '* `1` - Consignee \n' +
    '* `2` - ISO Tank Depot \n' +
    '* `3` - Logistics Service Provider \n' +
    '* `4` - Port \n' +
    '* `5` - Railhead \n' +
    '* `6` - Shipper \n' +
    '* `7` - Shipping Line \n' +
    '* `8` - Trucking Company \n' +
    '* `9` - Log Chain \n' +
    '* `10` - Agent \n' +
    '* `11` - Bulk Storage Terminal \n',
  required: false,
  name: 'persona',
  in: 'query',
  schema: {
    type: 'integer',
    enum: initial(Object.values(Constants.Persona).sort()), // remove persona 9
  },
};
 
export function buildResponseObjectById<T extends object>(
  modelDto: Function & {
    prototype: T;
  },
  admin?: boolean,
  options?: JsonSchemaOptions<T>,
) {
  const get_model_name = modelDto.name.replace('Dto', '');
  return schemas[`${get_model_name}Schema`]
    ? schemas[`${get_model_name}Schema`].getById(admin)
    : getModelSchemaRef(modelDto, options);
}
 
export function getSchemaByKey<T, K extends keyof T>(obj: T, key: K) {
  const schema = obj[key];
  if (!schema) {
    const error = new ValidationError(`The key ${key?.toString()} does not exist!`);
    error.code = 'SchemaNotFound';
    throw error;
  }
 
  return schema;
}