@teaploy/megaprint
Version:
Megaprint npm integration
53 lines (52 loc) • 2.68 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
/* eslint-disable no-console */
const xml2js_1 = require("xml2js");
const Validations = require("../validations");
const utils_1 = require("../utils");
class Client {
constructor(taxNumber, configuration) {
if (!Validations.nit(taxNumber)) {
throw new Error('Invalid tax number');
}
if (!configuration) {
throw new Error('Invalid configuration');
}
this.configuration = configuration;
this.taxNumber = taxNumber;
}
getData() {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
yield this.configuration.init();
const body = new xml2js_1.Builder({ headless: true, cdata: true }).buildObject({
RetornaDatosClienteRequest: {
nit: {
_: this.taxNumber.replace('-', ''),
},
},
});
const clientData = yield utils_1.Request.post(utils_1.Actions.getClientData, body, this.configuration.environment, this.configuration.apiToken);
console.log(clientData);
if (clientData.RetornaDatosClienteResponse.tipo_respuesta[0] === '1') {
console.error(clientData.RetornaDatosClienteResponse.listado_errores[0].error);
throw new Error('Error getting client Data');
}
return {
name: (_a = clientData === null || clientData === void 0 ? void 0 : clientData.RetornaDatosClienteResponse) === null || _a === void 0 ? void 0 : _a.nombre[0],
addresses: (_c = (_b = clientData === null || clientData === void 0 ? void 0 : clientData.RetornaDatosClienteResponse) === null || _b === void 0 ? void 0 : _b.direcciones[0]) === null || _c === void 0 ? void 0 : _c.direccion.map((address) => address),
};
});
}
}
exports.Client = Client;