use authKey
This commit is contained in:
parent
6fa75f531a
commit
a114b1aaaf
@ -1,46 +1,18 @@
|
|||||||
import { Router } from "express";
|
import { Router } from "express";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { URLSearchParams } from "url";
|
import { logger } from "../../utils/index.js";
|
||||||
|
// import { URLSearchParams } from "url";
|
||||||
import { inspect } from "util";
|
import { inspect } from "util";
|
||||||
|
import localStorage from "local-storage";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get("/", (req, res) => {
|
router.get("/", (req, res, next) => {
|
||||||
// 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,
|
|
||||||
});
|
|
||||||
|
|
||||||
axios
|
query(req.query.referenceId);
|
||||||
.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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function query(referenceId) {
|
function query(referenceId) {
|
||||||
console.log("Executing Query");
|
logger.info(`Executing Query with ${referenceId}`);
|
||||||
|
|
||||||
const query = `query ($startTime: DateTime, $endTime: DateTime) {
|
const query = `query ($startTime: DateTime, $endTime: DateTime) {
|
||||||
findContactsCompletedBetween(startTime: $startTime, endTime: $endTime, filter: {interactionTypes : EMAIL}) {
|
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 endTime = new Date(new Date().setHours(new Date().getHours() - 1));
|
||||||
|
|
||||||
|
const { host, tenant, token } = localStorage(req.query.authKey);
|
||||||
axios
|
axios
|
||||||
.post(
|
.post(
|
||||||
process.env.EO_API_UDG_URL,
|
`${host}/unified-data-gateway/${tenant}/graphql`,
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
query,
|
query,
|
||||||
variables: { startTime, endTime },
|
variables: { startTime, endTime },
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `OIDC_id_token ${req.session.token}`,
|
Authorization: `OIDC_id_token ${token}`,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -138,10 +111,9 @@ router.get("/", (req, res) => {
|
|||||||
.then((result) => {
|
.then((result) => {
|
||||||
const contacts = result.data.data.findContactsCompletedBetween.edges;
|
const contacts = result.data.data.findContactsCompletedBetween.edges;
|
||||||
|
|
||||||
// Log error to console
|
|
||||||
if (result.data.errors && result.data.errors.length > 0) {
|
if (result.data.errors && result.data.errors.length > 0) {
|
||||||
result.data.errors.forEach(function (error) {
|
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
|
// TODO: Should keep errors for filteredContacts
|
||||||
@ -212,7 +184,7 @@ router.get("/", (req, res) => {
|
|||||||
result.data.data.summary = summary;
|
result.data.data.summary = summary;
|
||||||
|
|
||||||
if (result.data) {
|
if (result.data) {
|
||||||
console.log(
|
logger.debug(
|
||||||
inspect(summary, {
|
inspect(summary, {
|
||||||
showHidden: false,
|
showHidden: false,
|
||||||
depth: null,
|
depth: null,
|
||||||
@ -232,22 +204,8 @@ router.get("/", (req, res) => {
|
|||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
// bin the token on error
|
// bin the token on error
|
||||||
req.session.destroy();
|
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;
|
export default router;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user