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 logger = require("../utils/logger");
|
||||
const queries = require("../graphql-client/queries");
|
||||
const InstanceMgr = require("../utils/instanceMgr").default;
|
||||
|
||||
exports.default = async (req, res) => {
|
||||
try {
|
||||
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") {
|
||||
res.sendStatus(403);
|
||||
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) {
|
||||
res.sendStatus(401);
|
||||
logger.log("usage-report-email-forbidden", "warn", req?.user?.email, null, {});
|
||||
return;
|
||||
}
|
||||
|
||||
//Query the elements.
|
||||
//Query the usage data.
|
||||
const queryResults = await client.request(queries.STATUS_UPDATE, {
|
||||
today: moment().startOf("day").subtract(3, "days"),
|
||||
period: moment().subtract(90, "days").startOf("day")
|
||||
});
|
||||
|
||||
//Massage the data.
|
||||
const shopList = [];
|
||||
queryResults.bodyshops.forEach((shop) => {
|
||||
shopList.push({
|
||||
"Shop Name": shop.shopname,
|
||||
"Days Since Creation": moment().diff(moment(shop.created_at), "days"),
|
||||
"Jobs Created": shop.jobs_created.aggregate.count,
|
||||
"Jobs Updated": shop.jobs_updated.aggregate.count,
|
||||
"Owners Created": shop.owners_created.aggregate.count,
|
||||
"Owners Updated": shop.owners_updated.aggregate.count,
|
||||
"Vehicles Created": shop.vehicles_created.aggregate.count,
|
||||
"Vehicles Updated": shop.vehicles_updated.aggregate.count,
|
||||
"Tasks Created": shop.tasks_created.aggregate.count,
|
||||
"Tasks Updated": shop.tasks_updated.aggregate.count
|
||||
});
|
||||
});
|
||||
const shopList = queryResults.bodyshops.map((shop) => ({
|
||||
"Shop Name": shop.shopname,
|
||||
"Days Since Creation": moment().diff(moment(shop.created_at), "days"),
|
||||
"Jobs Created": shop.jobs_created.aggregate.count,
|
||||
"Jobs Updated": shop.jobs_updated.aggregate.count,
|
||||
"Owners Created": shop.owners_created.aggregate.count,
|
||||
"Owners Updated": shop.owners_updated.aggregate.count,
|
||||
"Vehicles Created": shop.vehicles_created.aggregate.count,
|
||||
"Vehicles Updated": shop.vehicles_updated.aggregate.count,
|
||||
"Tasks Created": shop.tasks_created.aggregate.count,
|
||||
"Tasks Updated": shop.tasks_updated.aggregate.count
|
||||
}));
|
||||
|
||||
const csv = converter.json2csv(shopList, { emptyFieldValue: "" });
|
||||
emailer
|
||||
.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")}`,
|
||||
text: `
|
||||
Usage Report for ${moment().format("MM/DD/YYYY")} for Rome Online Customers.
|
||||
@@ -72,8 +78,8 @@ Notes:
|
||||
stack: error.stack
|
||||
});
|
||||
});
|
||||
|
||||
return res.sendStatus(200);
|
||||
res.sendStatus(200);
|
||||
return;
|
||||
} catch (error) {
|
||||
logger.log("usage-report-email-error", "ERROR", req?.user?.email, null, {
|
||||
error: error.message,
|
||||
|
||||
Reference in New Issue
Block a user