214 lines
4.7 KiB
JavaScript
214 lines
4.7 KiB
JavaScript
const path = require("path");
|
|
const moment = require("moment");
|
|
require("dotenv").config({
|
|
path: path.resolve(
|
|
process.cwd(),
|
|
`.env.${process.env.NODE_ENV || "development"}`
|
|
),
|
|
});
|
|
var _ = require("lodash");
|
|
const Handlebars = require("handlebars");
|
|
const phone = require("phone");
|
|
var Dinero = require("dinero.js");
|
|
Dinero.defaultCurrency = "CAD";
|
|
Dinero.globalLocale = "en-CA";
|
|
|
|
//Usage: {{moment appointments_by_pk.start format="dddd, DD MMMM YYYY"}}
|
|
|
|
Handlebars.registerHelper("round", function (context, block) {
|
|
if (context && context.hash) {
|
|
block = _.cloneDeep(context);
|
|
context = undefined;
|
|
}
|
|
try {
|
|
return context.toFixed(2);
|
|
} catch {
|
|
return context;
|
|
}
|
|
});
|
|
|
|
Handlebars.registerHelper("dinerof", function (context, block) {
|
|
if (context && context.hash) {
|
|
block = _.cloneDeep(context);
|
|
context = undefined;
|
|
}
|
|
var amount = Dinero(context);
|
|
if (context) {
|
|
return amount.toFormat();
|
|
}
|
|
return "";
|
|
});
|
|
|
|
Handlebars.registerHelper("phonef", function (context, block) {
|
|
if (context && context.hash) {
|
|
block = _.cloneDeep(context);
|
|
context = undefined;
|
|
}
|
|
var ph = phone(context)[0];
|
|
if (context) {
|
|
return ph;
|
|
}
|
|
return "";
|
|
});
|
|
|
|
Handlebars.registerHelper("partType", function (context, block) {
|
|
if (!context) return "";
|
|
|
|
switch (context.toUpperCase()) {
|
|
case "PAA":
|
|
return "Aftermarket";
|
|
case "PAE":
|
|
return "Existing";
|
|
case "PAN":
|
|
return "OEM";
|
|
case "PAO":
|
|
return "Other";
|
|
case "PAS":
|
|
return "Sublet";
|
|
case "PASL":
|
|
return "Sublet";
|
|
case "PAL":
|
|
return "LKQ";
|
|
case "PAM":
|
|
return "Remanufactured";
|
|
case "PAC":
|
|
return "Chrome";
|
|
case "PAP":
|
|
return "OEM Partial";
|
|
case "PAR":
|
|
return "Record";
|
|
default:
|
|
return context;
|
|
}
|
|
});
|
|
|
|
Handlebars.registerHelper("lbrType", function (context, block) {
|
|
if (!context) return "";
|
|
|
|
switch (context.toUpperCase()) {
|
|
case "LAA":
|
|
return "Aluminum";
|
|
case "LAB":
|
|
return "Body";
|
|
case "LAD":
|
|
return "Diagnostic";
|
|
case "LAF":
|
|
return "Frame";
|
|
case "LAG":
|
|
return "Glass";
|
|
case "LAM":
|
|
return "Mechanical";
|
|
case "LAR":
|
|
return "Refinish";
|
|
case "LAS":
|
|
return "Structural";
|
|
case "LAU":
|
|
return "Detail";
|
|
|
|
default:
|
|
return context;
|
|
}
|
|
});
|
|
|
|
Handlebars.registerHelper("objectKeys", function (obj, block) {
|
|
var accum = "";
|
|
|
|
obj &&
|
|
Object.keys(obj).map((key) => {
|
|
accum += block.fn({ key, value: obj[key] });
|
|
});
|
|
return accum;
|
|
});
|
|
|
|
Handlebars.registerHelper("dinero", function (context, block) {
|
|
if (context && context.hash) {
|
|
block = _.cloneDeep(context);
|
|
context = undefined;
|
|
}
|
|
var amount = Dinero({
|
|
amount: Math.round((context || 0) * 100),
|
|
currency: "CAD",
|
|
});
|
|
return amount.toFormat();
|
|
});
|
|
|
|
Handlebars.registerHelper("moment", function (context, block) {
|
|
if (context && context.hash) {
|
|
block = _.cloneDeep(context);
|
|
context = undefined;
|
|
}
|
|
|
|
if (!!!context) return "";
|
|
|
|
var date = moment(context);
|
|
|
|
if (block.hash.timezone) {
|
|
date.tz(block.hash.timezone);
|
|
}
|
|
|
|
var hasFormat = false;
|
|
|
|
// Reset the language back to default before doing anything else
|
|
date.locale("en");
|
|
|
|
for (var i in block.hash) {
|
|
if (i === "format") {
|
|
hasFormat = true;
|
|
} else if (date[i]) {
|
|
date = date[i](block.hash[i]);
|
|
} else {
|
|
console.log('moment.js does not support "' + i + '"');
|
|
}
|
|
}
|
|
|
|
if (hasFormat) {
|
|
date = date.format(block.hash.format);
|
|
}
|
|
return date;
|
|
});
|
|
|
|
Handlebars.registerHelper("duration", function (context, block) {
|
|
if (context && context.hash) {
|
|
block = _.cloneDeep(context);
|
|
context = 0;
|
|
}
|
|
var duration = moment.duration(context);
|
|
var hasFormat = false;
|
|
|
|
// Reset the language back to default before doing anything else
|
|
duration = duration.lang("en");
|
|
|
|
for (var i in block.hash) {
|
|
if (i === "format") {
|
|
hasFormat = true;
|
|
} else if (duration[i]) {
|
|
duration = duration[i](block.hash[i]);
|
|
} else {
|
|
console.log('moment.js duration does not support "' + i + '"');
|
|
}
|
|
}
|
|
|
|
if (hasFormat) {
|
|
duration = duration.format(block.hash.format);
|
|
}
|
|
return duration;
|
|
});
|
|
|
|
exports.render = (req, res) => {
|
|
//Perform request validation
|
|
let view;
|
|
console.log("[HJS Render] New Render Request.");
|
|
|
|
//console.log("[HJS Render] Context", req.body.context);
|
|
if (req.body.context.bodyshop.template_header) {
|
|
console.log("[HJS Render] Including Header");
|
|
//view = req.body.view;
|
|
view = `${req.body.context.bodyshop.template_header}${req.body.view}`;
|
|
} else {
|
|
console.log("[HJS Render] No header to include.");
|
|
view = req.body.view;
|
|
}
|
|
var template = Handlebars.compile(view);
|
|
res.send(template(req.body.context));
|
|
};
|