UNPKG

@central-credit/engine

Version:

Engine to handle the Serasa requests

87 lines 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const models_1 = require("../models"); const rest_1 = require("@loopback/rest"); // import { Layout, LayoutRecord, LayoutRecordType } from '../layouts/base.layout' class LayoutBuilder { constructor(layout) { this.layout = layout; } addParameter(paramName, paramValue) { this.parameters = Object.assign(Object.assign({}, this.parameters), { [paramName]: paramValue }); } getValueParameter(value) { if (value) { if (String(value).charAt(0) === ':') { const paramValue = this.parameters[value.slice(1)]; return paramValue ? paramValue : ''; } return value; } return ''; } getFillSize(size, value) { if (value) { const valueSize = String(value).length; const fillSize = size - valueSize; return fillSize > 0 ? fillSize : 0; } return size; } getValue(record) { const { type } = record; const value = this.getValueParameter(record.value); const size = this.getFillSize(record.size, value); if (value !== '' && type === models_1.RecordFieldType.NUMBER) { return Array(size + 1) .join('0') .concat(String(value)); } return String(value).concat(Array(size + 1).join(' ')); } isProtocolValid() { const { protocol } = this.layout; const protocolSize = protocol.reduce((size, record) => { return size + record.size; }, 0); return protocolSize === 400; } isOptionsValid() { const { options } = this.layout; const optionsSize = options.reduce((size, record) => { return size + record.size; }, 0); return optionsSize === 115; } buildOptions() { if (this.isOptionsValid()) { const options = this.layout.getOrdered('options'); return options.reduce((str, record) => { return str.concat(this.getValue(record)); }, ''); } return ''; } buildProtocol() { if (this.isProtocolValid()) { const protocol = this.layout.getOrdered('protocol'); return protocol.reduce((str, record) => { return str.concat(this.getValue(record)); }, ''); } return ''; } build() { const pass = '97024725Centra@2 '; const protocol = this.buildProtocol(); const options = this.buildOptions(); const request = ''.concat(pass, protocol, options, 'T999'); // const request = ''.concat(protocol) if (request.length !== 543) { throw new rest_1.HttpErrors.PreconditionFailed(`Request Length: ${request.length}`); } return 'p='.concat(request); } } exports.LayoutBuilder = LayoutBuilder; //# sourceMappingURL=layout.builder.js.map