IO-3076 updates to usage report.
This commit is contained in:
@@ -8,48 +8,54 @@ const moment = require("moment-timezone");
|
|||||||
const converter = require("json-2-csv");
|
const converter = require("json-2-csv");
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
const queries = require("../graphql-client/queries");
|
const queries = require("../graphql-client/queries");
|
||||||
|
const InstanceMgr = require("../utils/instanceMgr").default;
|
||||||
|
|
||||||
exports.default = async (req, res) => {
|
exports.default = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
logger.log("usage-report-email-start", "debug", req?.user?.email, null, {});
|
logger.log("usage-report-email-start", "debug", req?.user?.email, null, {});
|
||||||
|
|
||||||
|
if (InstanceMgr({ rome: false, imex: true })) {
|
||||||
|
//Disable for ImEX at the moment.
|
||||||
|
res.sendStatus(403);
|
||||||
|
logger.log("usage-report-email-forbidden", "warn", req?.user?.email, null, {});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== "production") {
|
if (process.env.NODE_ENV !== "production") {
|
||||||
res.sendStatus(403);
|
res.sendStatus(403);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Only process if the appropriate token is provided.
|
// Validate using autohouse token header.
|
||||||
if (req.headers["x-imex-auth"] !== process.env.AUTOHOUSE_AUTH_TOKEN) {
|
if (req.headers["x-imex-auth"] !== process.env.AUTOHOUSE_AUTH_TOKEN) {
|
||||||
res.sendStatus(401);
|
res.sendStatus(401);
|
||||||
logger.log("usage-report-email-forbidden", "warn", req?.user?.email, null, {});
|
logger.log("usage-report-email-forbidden", "warn", req?.user?.email, null, {});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Query the elements.
|
//Query the usage data.
|
||||||
const queryResults = await client.request(queries.STATUS_UPDATE, {
|
const queryResults = await client.request(queries.STATUS_UPDATE, {
|
||||||
today: moment().startOf("day").subtract(3, "days"),
|
today: moment().startOf("day").subtract(3, "days"),
|
||||||
period: moment().subtract(90, "days").startOf("day")
|
period: moment().subtract(90, "days").startOf("day")
|
||||||
});
|
});
|
||||||
|
|
||||||
//Massage the data.
|
//Massage the data.
|
||||||
const shopList = [];
|
const shopList = queryResults.bodyshops.map((shop) => ({
|
||||||
queryResults.bodyshops.forEach((shop) => {
|
"Shop Name": shop.shopname,
|
||||||
shopList.push({
|
"Days Since Creation": moment().diff(moment(shop.created_at), "days"),
|
||||||
"Shop Name": shop.shopname,
|
"Jobs Created": shop.jobs_created.aggregate.count,
|
||||||
"Days Since Creation": moment().diff(moment(shop.created_at), "days"),
|
"Jobs Updated": shop.jobs_updated.aggregate.count,
|
||||||
"Jobs Created": shop.jobs_created.aggregate.count,
|
"Owners Created": shop.owners_created.aggregate.count,
|
||||||
"Jobs Updated": shop.jobs_updated.aggregate.count,
|
"Owners Updated": shop.owners_updated.aggregate.count,
|
||||||
"Owners Created": shop.owners_created.aggregate.count,
|
"Vehicles Created": shop.vehicles_created.aggregate.count,
|
||||||
"Owners Updated": shop.owners_updated.aggregate.count,
|
"Vehicles Updated": shop.vehicles_updated.aggregate.count,
|
||||||
"Vehicles Created": shop.vehicles_created.aggregate.count,
|
"Tasks Created": shop.tasks_created.aggregate.count,
|
||||||
"Vehicles Updated": shop.vehicles_updated.aggregate.count,
|
"Tasks Updated": shop.tasks_updated.aggregate.count
|
||||||
"Tasks Created": shop.tasks_created.aggregate.count,
|
}));
|
||||||
"Tasks Updated": shop.tasks_updated.aggregate.count
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
const csv = converter.json2csv(shopList, { emptyFieldValue: "" });
|
const csv = converter.json2csv(shopList, { emptyFieldValue: "" });
|
||||||
emailer
|
emailer
|
||||||
.sendTaskEmail({
|
.sendTaskEmail({
|
||||||
to: ["patrick.fic@convenient-brands.com"],
|
to: ["patrick.fic@convenient-brands.com", "bradley.rhoades@convenient-brands.com"],
|
||||||
subject: `RO Usage Report - ${moment().format("MM/DD/YYYY")}`,
|
subject: `RO Usage Report - ${moment().format("MM/DD/YYYY")}`,
|
||||||
text: `
|
text: `
|
||||||
Usage Report for ${moment().format("MM/DD/YYYY")} for Rome Online Customers.
|
Usage Report for ${moment().format("MM/DD/YYYY")} for Rome Online Customers.
|
||||||
@@ -72,8 +78,8 @@ Notes:
|
|||||||
stack: error.stack
|
stack: error.stack
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
res.sendStatus(200);
|
||||||
return res.sendStatus(200);
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.log("usage-report-email-error", "ERROR", req?.user?.email, null, {
|
logger.log("usage-report-email-error", "ERROR", req?.user?.email, null, {
|
||||||
error: error.message,
|
error: error.message,
|
||||||
|
|||||||
Reference in New Issue
Block a user