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 | 1x 1x 1x 1x 1x 1x 1x 1x 11x | /* eslint-disable @typescript-eslint/no-explicit-any */
import { AjvKeyword, RestTags } from '@loopback/rest';
import type { Application } from '@loopback/core';
import type { KeywordDefinition } from 'ajv';
import type { CalistaDropdownRepository, LocodeRepository } from '@logchain/repositories';
// https://github.com/ruanmartinelli/is-container
function isContainer(code: string): boolean {
if (!code) return false;
if (typeof code !== 'string') return false;
const alphabet: { [letter: string]: number } = {
'A': 10, 'B': 12, 'C': 13, 'D': 14, 'E': 15, 'F': 16, 'G': 17, 'H': 18, 'I': 19,
'J': 20, 'K': 21, 'L': 23, 'M': 24, 'N': 25, 'O': 26, 'P': 27, 'Q': 28, 'R': 29,
'S': 30, 'T': 31, 'U': 32, 'V': 34, 'W': 35, 'X': 36, 'Y': 37, 'Z': 38
};
code = code.toUpperCase();
const invalidLength = code.length !== 11;
const isISOFormat = /^[A-Z]{4}\d{7}/.test(code);
if (invalidLength || !isISOFormat) return false;
let sum = 0;
const checkDigit = code.substr(10);
code
.substr(0, 10)
.split('')
.map((char: string, index: number) => {
let n = Number(char);
if (index < 4) n = alphabet[char];
n *= Math.pow(2, index);
sum += n;
});
sum %= 11;
sum %= 10;
return sum === Number(checkDigit);
};
export class RegulatoryDeclarationsValidator {
constructor(
private app: Application,
public calistaDropdownRepo: CalistaDropdownRepository,
public locodeRepo: LocodeRepository
) {
this.bindAjvKeywords();
}
private bindAjvKeywords() {
const keywords: KeywordDefinition[] = [
{
keyword: 'decTypeMustExists',
type: 'string',
validate: async (...args: any[]) => {
const [, , , , data] = args;
const decType = await this.calistaDropdownRepo.count({
parent_code: data.car_type, dropdown_code: 'dec_type', code: data.dec_type
});
return !!decType.count
},
},
{
keyword: 'codeMustExists',
type: 'string',
validate: async (...args: any[]) => {
const [{ refCode }, , , , data, code] = args;
const dropdownCode = refCode ?? code;
const dbCode = await this.calistaDropdownRepo.count({ dropdown_code: dropdownCode, code: String(data[code]) });
return !!dbCode.count;
},
},
{
keyword: 'locodeMustExists',
type: 'object',
validate: async (...args: any[]) => {
const [, , , , data, code] = args;
const dbLocode = await this.calistaDropdownRepo.count({ dropdown_code: 'loc_code', code: data[code].loc_code });
return !!dbLocode.count;
},
},
{
keyword: 'isContainer',
type: 'string',
validate: async (...args: any[]) => {
const [, data] = args;
return isContainer(data);
},
},
{
keyword: 'locodeMustExists',
type: 'string',
validate: async (...args: any[]) => {
const [, data] = args;
const dbCode = await this.locodeRepo.count({ code: data });
return !!dbCode.count
},
},
{
keyword: 'currencyFormat',
type: 'number',
validate: async (...args: any[]) => /^(0|([1-9]{1}([0-9]{0,12})))(\.[0-9]{1,2})?$/g.test(args[1]?.toString()),
},
{
keyword: 'exchangeRateFormat',
type: 'number',
validate: async (...args: any[]) => /^(0|([1-9]{1}([0-9]{0,3})))(\.[0-9]{1,6})?$/g.test(args[1]?.toString()),
},
{
keyword: 'percentFormat',
type: 'number',
validate: async (...args: any[]) => /^(0|([1-9]{1}([0-9]{0,2})))(\.[0-9]{1,3})?$/g.test(args[1]?.toString()),
},
{
keyword: 'weightFormat',
type: 'number',
validate: async (...args: any[]) => /^(0|([1-9]{1}([0-9]{0,10})))(\.[0-9]{1,3})?$/g.test(args[1]?.toString()),
},
{
keyword: 'packFormat',
type: 'number',
validate: async (...args: any[]) => /^(0|([1-9]{1}([0-9]{0,7})))$/g.test(args[1]?.toString()),
},
{
keyword: 'applyPattern',
type: 'number',
validate: async (...args: any[]) => {
const [{ pattern }, data] = args;
return new RegExp(pattern, 'g').test(data?.toString());
},
}
];
for (const keyword of keywords) {
this.app.bind<AjvKeyword>(`ajv.keywords.${keyword.keyword}`).to({
keyword: keyword.keyword,
async: true,
type: keyword.type,
validate: (keyword as any).validate,
}).tag(RestTags.AJV_KEYWORD);
}
}
}
|