This commit is contained in:
Dave Richer
2023-11-20 15:02:46 -05:00
parent 54af163ddf
commit 53fc5e361f
4 changed files with 5004 additions and 7269 deletions

7191
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -22,7 +22,7 @@
"@aws-sdk/credential-provider-node": "^3.451.0", "@aws-sdk/credential-provider-node": "^3.451.0",
"@opensearch-project/opensearch": "^2.4.0", "@opensearch-project/opensearch": "^2.4.0",
"aws4": "^1.12.0", "aws4": "^1.12.0",
"axios": "^0.27.2", "axios": "^1.6.2",
"bluebird": "^3.7.2", "bluebird": "^3.7.2",
"body-parser": "^1.20.2", "body-parser": "^1.20.2",
"cloudinary": "^1.41.0", "cloudinary": "^1.41.0",
@@ -39,7 +39,7 @@
"graylog2": "^0.2.1", "graylog2": "^0.2.1",
"inline-css": "^4.0.2", "inline-css": "^4.0.2",
"intuit-oauth": "^4.0.0", "intuit-oauth": "^4.0.0",
"json-2-csv": "^3.19.0", "json-2-csv": "^5.0.1",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"moment": "^2.29.4", "moment": "^2.29.4",
"moment-timezone": "^0.5.41", "moment-timezone": "^0.5.41",

View File

@@ -1,92 +1,84 @@
const path = require("path"); const path = require("path");
require("dotenv").config({ require("dotenv").config({
path: path.resolve( path: path.resolve(
process.cwd(), process.cwd(),
`.env.${process.env.NODE_ENV || "development"}` `.env.${process.env.NODE_ENV || "development"}`
), ),
}); });
const axios = require("axios");
const client = require("../graphql-client/graphql-client").client; const client = require("../graphql-client/graphql-client").client;
const emailer = require("../email/sendemail"); const emailer = require("../email/sendemail");
const logger = require("../utils/logger");
const moment = require("moment-timezone"); const moment = require("moment-timezone");
const converter = require("json-2-csv");
exports.taskHandler = async (req, res) => { exports.taskHandler = async (req, res) => {
try { try {
const { bodyshopid, query, variables, text, to, subject, timezone } = const {bodyshopid, query, variables, text, to, subject, timezone} = req.body;
req.body;
//Run the query
//Check the variables to see if they are an object. //Check the variables to see if they are an object.
Object.keys(variables).forEach((key) => { Object.keys(variables).forEach((key) => {
if (typeof variables[key] === "object") { if (typeof variables[key] === "object") {
if (variables[key].function) { if (variables[key].function) {
variables[key] = functionMapper(variables[key].function, timezone); variables[key] = functionMapper(variables[key].function, timezone);
} }
} }
}); });
const response = await client.request(query, variables); const response = await client.request(query, variables);
//Massage the data const rootElement = response[Object.keys(response)[0]]; //This element should always be an array.
//Send the email
const rootElement = response[Object.keys(response)[0]]; //This element shoudl always be an array. const csv = converter.json2csv(rootElement, {emptyFieldValue: ""});
let converter = require("json-2-csv");
converter.json2csv(
rootElement,
(err, csv) => {
if (err) {
res.status(500).json(err);
}
emailer.sendTaskEmail({ emailer.sendTaskEmail({
to, to,
subject, subject,
text, text,
attachments: [{ filename: "query.csv", content: csv }], attachments: [{filename: "query.csv", content: csv}],
}).catch(err => {
console.error('Errors sending CSV Email.')
}); });
res.status(200).send(csv);
}, return res.status(200).send(csv);
{ emptyFieldValue: "" } } catch (error) {
); res.status(500).json({error: error.message, stack: error.stackTrace});
} catch (error) { }
res.status(500).json({ error: error.message, stack: error.stackTrace });
}
}; };
const isoformat = "YYYY-MM-DD"; const isoFormat = "YYYY-MM-DD";
function functionMapper(f, timezone) {
switch (f) {
case "date.today":
return moment().tz(timezone).format(isoformat);
case "date.now":
return moment().tz(timezone);
case "date.yesterday":
return moment().tz(timezone).subtract(1, "day").format(isoformat);
case "date.3daysago":
return moment().tz(timezone).subtract(3, "days").format(isoformat);
case "date.7daysago":
return moment().tz(timezone).subtract(7, "days").format(isoformat);
case "date.tomorrow":
return moment().tz(timezone).add(1, "day").format(isoformat);
case "date.3daysfromnow":
return moment().tz(timezone).add(3, "days").format(isoformat);
case "date.7daysfromnow":
return moment().tz(timezone).add(7, "days").format(isoformat);
case "date.yesterdaytz":
return moment().tz(timezone).subtract(1, "day");
case "date.3daysagotz":
return moment().tz(timezone).subtract(3, "days");
case "date.7daysagotz":
return moment().tz(timezone).subtract(7, "days");
case "date.tomorrowtz":
return moment().tz(timezone).add(1, "day");
case "date.3daysfromnowtz":
return moment().tz(timezone).add(3, "days");
case "date.7daysfromnowtz":
return moment().tz(timezone).add(7, "days");
case "date.now": function functionMapper(f, timezone) {
return moment().tz(timezone); switch (f) {
default: case "date.today":
return f; return moment().tz(timezone).format(isoFormat);
} case "date.now":
return moment().tz(timezone);
case "date.yesterday":
return moment().tz(timezone).subtract(1, "day").format(isoFormat);
case "date.3daysago":
return moment().tz(timezone).subtract(3, "days").format(isoFormat);
case "date.7daysago":
return moment().tz(timezone).subtract(7, "days").format(isoFormat);
case "date.tomorrow":
return moment().tz(timezone).add(1, "day").format(isoFormat);
case "date.3daysfromnow":
return moment().tz(timezone).add(3, "days").format(isoFormat);
case "date.7daysfromnow":
return moment().tz(timezone).add(7, "days").format(isoFormat);
case "date.yesterdaytz":
return moment().tz(timezone).subtract(1, "day");
case "date.3daysagotz":
return moment().tz(timezone).subtract(3, "days");
case "date.7daysagotz":
return moment().tz(timezone).subtract(7, "days");
case "date.tomorrowtz":
return moment().tz(timezone).add(1, "day");
case "date.3daysfromnowtz":
return moment().tz(timezone).add(3, "days");
case "date.7daysfromnowtz":
return moment().tz(timezone).add(7, "days");
case "date.now":
return moment().tz(timezone);
default:
return f;
}
} }

4934
yarn.lock Normal file

File diff suppressed because it is too large Load Diff