bcucotizaciones
Version:
An easy to use soap client for currency price consulting agains Uruguayan central bank (BCU)
126 lines • 5.87 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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 });
const soap = __importStar(require("soap"));
const BCUException_1 = require("../exception/BCUException");
const Moneda_1 = require("../moneda/Moneda");
class ClienteBCU {
constructor() {
this.WSDL_COTIZACIONES = 'https://cotizaciones.bcu.gub.uy/wscotizaciones/servlet/awsbcucotizaciones?wsdl';
this.WSDL_ULTIMO_CIERRE = 'https://cotizaciones.bcu.gub.uy/wscotizaciones/servlet/awsultimocierre?wsdl';
this.WSDL_MONEDAS = 'https://cotizaciones.bcu.gub.uy/wscotizaciones/servlet/awsbcumonedas?wsdl';
this.GRUPOS_MONEDAS = [0, 1];
}
obtenerCotizacion(peticion) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
peticion.codigoDeMonedas.forEach((codigo) => {
if (!Moneda_1.Moneda[codigo]) {
throw new BCUException_1.BCUException('El codigo de la moneda no existe, verifique las moneda habilitadas.');
}
});
if (peticion.fecha && !this.esFechaValidaParaCotizacion(peticion.fecha)) {
throw new BCUException_1.BCUException('La fecha de cotizacion debe ser anterior o igual a la fecha actual');
}
if (!peticion.fecha) {
const fechaUltimoCierre = yield this.obtenerFechaDelUltimoCierre();
peticion.fecha = fechaUltimoCierre.fechaDeUltimoCierre.toISOString().substring(0, 10);
}
const inputData = {
Entrada: {
Moneda: { item: peticion.codigoDeMonedas },
FechaDesde: peticion.fecha,
FechaHasta: peticion.fecha,
Grupo: (_a = peticion.grupo) !== null && _a !== void 0 ? _a : 0,
},
};
const client = yield soap.createClientAsync(this.WSDL_COTIZACIONES);
const result = yield client.ExecuteAsync(inputData);
const cotizaciones = result[0].Salida.datoscotizaciones['datoscotizaciones.dato'];
return cotizaciones.map((cotizacion) => ({
fecha: cotizacion.Fecha,
tipoCambioCompra: cotizacion.TCC,
tipoCambioVenta: cotizacion.TCV,
codigoIso: cotizacion.CodigoISO,
nombre: cotizacion.Nombre,
emisor: cotizacion.Emisor,
}));
});
}
obtenerFechaDelUltimoCierre() {
return __awaiter(this, void 0, void 0, function* () {
const cliente = yield soap.createClientAsync(this.WSDL_ULTIMO_CIERRE);
const respuesta = yield cliente.ExecuteAsync({});
return { fechaDeUltimoCierre: respuesta[0].Salida.Fecha };
});
}
obtenerMonedas(grupo) {
return __awaiter(this, void 0, void 0, function* () {
if (!grupo) {
grupo = 0;
}
if (!this.GRUPOS_MONEDAS.includes(grupo)) {
throw new BCUException_1.BCUException('El grupo de moneda debe ser 0 o 1');
}
const cliente = yield soap.createClientAsync(this.WSDL_MONEDAS);
const parametros = {
Entrada: { Grupo: grupo },
};
const respuesta = yield cliente.ExecuteAsync(parametros);
let monedas = [];
respuesta[0].Salida['wsmonedasout.Linea'].forEach((m) => {
const moneda = { codigo: m.Codigo, nombre: m.Nombre };
monedas.push(moneda);
});
return monedas;
});
}
esFechaValidaParaCotizacion(fecha) {
const hoy = new Date();
const fechaElegida = new Date(fecha);
return fechaElegida <= hoy;
}
}
exports.default = ClienteBCU;
//# sourceMappingURL=ClienteBCU.js.map