123 lines
3.0 KiB
JavaScript
123 lines
3.0 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");
|
|
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("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("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) });
|
|
return amount.toFormat();
|
|
});
|
|
|
|
Handlebars.registerHelper("moment", function (context, block) {
|
|
if (context && context.hash) {
|
|
block = _.cloneDeep(context);
|
|
context = undefined;
|
|
}
|
|
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.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));
|
|
};
|