Merged in release/2022-02-18 (pull request #404)
Updated task scheduler. Approved-by: Patrick Fic
This commit is contained in:
@@ -9,30 +9,84 @@ 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 logger = require("../utils/logger");
|
||||||
|
const moment = require("moment-timezone");
|
||||||
exports.taskHandler = async (req, res) => {
|
exports.taskHandler = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { bodyshopid, query, variables, text, to, subject } = req.body;
|
const { bodyshopid, query, variables, text, to, subject, timezone } =
|
||||||
|
req.body;
|
||||||
//Run the query
|
//Run the query
|
||||||
|
|
||||||
|
//Check the variables to see if they are an object.
|
||||||
|
Object.keys(variables).forEach((key) => {
|
||||||
|
if (typeof variables[key] === "object") {
|
||||||
|
if (variables[key].function) {
|
||||||
|
variables[key] = functionMapper(variables[key].function, timezone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const response = await client.request(query, variables);
|
const response = await client.request(query, variables);
|
||||||
//Massage the data
|
//Massage the data
|
||||||
//Send the email
|
//Send the email
|
||||||
const rootElement = response[Object.keys(response)[0]]; //This element shoudl always be an array.
|
const rootElement = response[Object.keys(response)[0]]; //This element shoudl always be an array.
|
||||||
let converter = require("json-2-csv");
|
let converter = require("json-2-csv");
|
||||||
converter.json2csv(rootElement, (err, csv) => {
|
converter.json2csv(
|
||||||
if (err) {
|
rootElement,
|
||||||
res.status(500).json(err);
|
(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 }],
|
||||||
});
|
});
|
||||||
res.status(200).send(csv);
|
res.status(200).send(csv);
|
||||||
});
|
},
|
||||||
|
{ emptyFieldValue: "" }
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error });
|
res.status(500).json({ error: error.message, stack: error.stackTrace });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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":
|
||||||
|
return moment().tz(timezone);
|
||||||
|
default:
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user