use authKey
This commit is contained in:
parent
6fa75f531a
commit
a114b1aaaf
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user