From a114b1aaafbc85c37e5052034e35dae756b74b2c Mon Sep 17 00:00:00 2001 From: "Peter.Morton" Date: Wed, 26 Jul 2023 16:57:48 -0500 Subject: [PATCH] use authKey --- src/api/routes/unified-data-gateway.js | 66 +++++--------------------- 1 file changed, 12 insertions(+), 54 deletions(-) diff --git a/src/api/routes/unified-data-gateway.js b/src/api/routes/unified-data-gateway.js index 3ae4349..65941e7 100644 --- a/src/api/routes/unified-data-gateway.js +++ b/src/api/routes/unified-data-gateway.js @@ -1,46 +1,18 @@ import { Router } from "express"; import axios from "axios"; -import { URLSearchParams } from "url"; +import { logger } from "../../utils/index.js"; +// import { URLSearchParams } from "url"; import { inspect } from "util"; +import localStorage from "local-storage"; const router = Router(); -router.get("/", (req, res) => { - // token in session -> get user data and send it back to the vue app - if (req.session.token) { - query(req.query.referenceId); - } - // no token -> send nothing - else { - const params = new URLSearchParams({ - grant_type: "password", - username: process.env.EO_API_USERNAME, - password: process.env.EO_API_PASSWORD, - scope: process.env.EO_API_SCOPE, - client_id: process.env.EO_API_CLIENT_ID, - client_secret: process.env.EO_API_SECRET, - }); +router.get("/", (req, res, next) => { - axios - .post(process.env.EO_API_ACCESS_TOKEN_URL, params.toString(), { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - }) - .then((result) => { - // save token to session - req.session.token = result.data.access_token; - console.log(result); - - query(req.query.referenceId); - }) - .catch((err) => { - sendError(err, res); - }); - } + query(req.query.referenceId); function query(referenceId) { - console.log("Executing Query"); + logger.info(`Executing Query with ${referenceId}`); const query = `query ($startTime: DateTime, $endTime: DateTime) { findContactsCompletedBetween(startTime: $startTime, endTime: $endTime, filter: {interactionTypes : EMAIL}) { @@ -121,16 +93,17 @@ router.get("/", (req, res) => { ); const endTime = new Date(new Date().setHours(new Date().getHours() - 1)); + const { host, tenant, token } = localStorage(req.query.authKey); axios .post( - process.env.EO_API_UDG_URL, + `${host}/unified-data-gateway/${tenant}/graphql`, JSON.stringify({ query, variables: { startTime, endTime }, }), { headers: { - Authorization: `OIDC_id_token ${req.session.token}`, + Authorization: `OIDC_id_token ${token}`, "Content-Type": "application/json", }, } @@ -138,10 +111,9 @@ router.get("/", (req, res) => { .then((result) => { const contacts = result.data.data.findContactsCompletedBetween.edges; - // Log error to console if (result.data.errors && result.data.errors.length > 0) { result.data.errors.forEach(function (error) { - console.log("ERROR: Errors in results - " + error.message); + logger.error("ERROR: Errors in results - " + error.message); }); // TODO: Should keep errors for filteredContacts @@ -212,7 +184,7 @@ router.get("/", (req, res) => { result.data.data.summary = summary; if (result.data) { - console.log( + logger.debug( inspect(summary, { showHidden: false, depth: null, @@ -232,22 +204,8 @@ router.get("/", (req, res) => { .catch((err) => { // bin the token on error req.session.destroy(); - sendError(err, res); + next(err) }); } }); -function sendError(err, res) { - console.error(err); - let errStatus = 500; - if (err.response) errStatus = err.response.status; - res.status(errStatus).send({ - errors: [ - { - status: errStatus, - title: err.code, - detail: err.message, - }, - ], - }); -} export default router;