IO-1685 Task Scheduler.

This commit is contained in:
Patrick Fic
2022-02-10 16:30:49 -08:00
parent e383b0800c
commit 30afe97fba
4 changed files with 5370 additions and 0 deletions

38
server/tasks/tasks.js Normal file
View File

@@ -0,0 +1,38 @@
const path = require("path");
require("dotenv").config({
path: path.resolve(
process.cwd(),
`.env.${process.env.NODE_ENV || "development"}`
),
});
const axios = require("axios");
const client = require("../graphql-client/graphql-client").client;
const emailer = require("../email/sendemail");
const logger = require("../utils/logger");
exports.taskHandler = async (req, res) => {
try {
const { bodyshopid, query, variables, text, to, subject } = req.body;
//Run the query
const response = await client.request(query, variables);
//Massage the data
//Send the email
const rootElement = response[Object.keys(response)[0]]; //This element shoudl always be an array.
let converter = require("json-2-csv");
converter.json2csv(rootElement, (err, csv) => {
if (err) {
res.status(500).json(err);
}
emailer.sendTaskEmail({
to,
subject,
text,
attachments: [{ filename: "query.csv", content: csv }],
});
res.status(200).send(csv);
});
} catch (error) {
res.status(500).json({ error });
}
};