UNPKG

bcucotizaciones

Version:

An easy to use soap client for currency price consulting agains Uruguayan central bank (BCU)

122 lines 5.65 kB
"use strict"; 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 Currencies_1 = require("../currencies/Currencies"); class BCUClient { constructor() { this.PRICES_WSDL = 'https://cotizaciones.bcu.gub.uy/wscotizaciones/servlet/awsbcucotizaciones?wsdl'; this.LAST_CLOSING_DATE = 'https://cotizaciones.bcu.gub.uy/wscotizaciones/servlet/awsultimocierre?wsdl'; this.CURRENCIES_WSDL = 'https://cotizaciones.bcu.gub.uy/wscotizaciones/servlet/awsbcumonedas?wsdl'; this.CURRENCY_GROUPS = [0, 1]; } getQuote(currencyCode, date) { return __awaiter(this, void 0, void 0, function* () { if (!Currencies_1.Currencies[currencyCode]) { throw new BCUException_1.BCUException('The currency code does not exist, please check the enabled currencies.'); } if (date && !this.isValidQuoteDate(date)) { throw new BCUException_1.BCUException('The quote date must be today or earlier'); } if (!date) { const lastClosingDate = yield this.getLastClosingDate(); date = lastClosingDate.lastClosingDate.toISOString().substring(0, 10); } const inputData = { Entrada: { Moneda: { item: [currencyCode] }, FechaDesde: date, FechaHasta: date, Grupo: 0, }, }; const client = yield soap.createClientAsync(this.PRICES_WSDL); const result = yield client.ExecuteAsync(inputData); const purchaseExchangeRate = result[0].Salida.datoscotizaciones['datoscotizaciones.dato'][0].TCC; const sellingExchangeRate = result[0].Salida.datoscotizaciones['datoscotizaciones.dato'][0].TCV; const isoCode = result[0].Salida.datoscotizaciones['datoscotizaciones.dato'][0].CodigoISO; return { purchaseExchangeRate: purchaseExchangeRate, sellingExchangeRate: sellingExchangeRate, isoCode: isoCode, }; }); } getLastClosingDate() { return __awaiter(this, void 0, void 0, function* () { const client = yield soap.createClientAsync(this.LAST_CLOSING_DATE); const response = yield client.ExecuteAsync({}); return { lastClosingDate: response[0].Salida.Fecha }; }); } getCurrencies(group) { return __awaiter(this, void 0, void 0, function* () { if (!group) { group = 0; } if (!this.CURRENCY_GROUPS.includes(group)) { throw new BCUException_1.BCUException('The currency group must be 0 or 1'); } const client = yield soap.createClientAsync(this.CURRENCIES_WSDL); const parameters = { Entrada: { Grupo: group }, }; const response = yield client.ExecuteAsync(parameters); let currencies = []; response[0].Salida['wsmonedasout.Linea'].forEach((m) => { const currency = { code: m.Codigo, name: m.Nombre }; currencies.push(currency); }); return currencies; }); } isValidQuoteDate(date) { const today = new Date(); const selectedDate = new Date(date); return selectedDate <= today; } } exports.default = BCUClient; //# sourceMappingURL=BCUClient.js.map