admin-api-aurum-connector
Version:
Module to connect to the Admin API Aurum Core
57 lines (46 loc) • 1.67 kB
JavaScript
const { module_name, configurations_key } = require("../../utils/Constants.js");
const { logger } = require("../../utils/Logger.js");
const getInvestments = async (token, filters) => {
try {
logger.info(`module-hub-aurum-core-connector :: get investments "${module_name}" ...`);
if(!token) {
return {
responseCode: 401,
message: "A token is required to consume the API"
};
}
const config = global[configurations_key];
let endpoint = config?.investment?.getInvestments?.endpoint || "";
endpoint = endpoint.replace("{mgtwUrl}", config.mgtwUrl);
endpoint = endpoint.replace("{branchId}", config.branchId);
if(filters) {
let init = true;
for (const prop in filters) {
if(init === true) {
endpoint = `${endpoint}?${prop}=${filters[prop]}`;
} else {
endpoint = `${endpoint}&${prop}=${filters[prop]}`;
}
}
}
logger.info(`module-hub-aurum-core-connector :: get investments to endpoint: ${endpoint}`);
const response = await fetch(endpoint, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token
}
});
const responseJson = await response.json();
logger.info(`module-hub-aurum-core-connector :: get investments response: ${JSON.stringify(responseJson)}`);
return responseJson;
} catch (error) {
logger.error(`module-hub-aurum-core-connector :: Error getting investments: ${error}`);
return {
responseCode: 500,
error: error,
message: "Error getting investments",
};
}
};
module.exports = getInvestments;