Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Last active February 17, 2019 11:26
Show Gist options
  • Select an option

  • Save brainysmurf/4f9fe995871eca03617343b16dd3d990 to your computer and use it in GitHub Desktop.

Select an option

Save brainysmurf/4f9fe995871eca03617343b16dd3d990 to your computer and use it in GitHub Desktop.
function testDailyTrigger() {
var e = {}, f = {};
f.test = true;
f.day = app.libraries.interface.dates.today();
//e.day.add(1, "days");
f.agents = ['email@example.com'];
f.updateSite = false;
triggerDailyTrigger({}, f);
}
function testAbsencesTrigger() {
var e = {}, f = {};
f.test = true;
f.agents = [];
f.test = false;
f.day = app.libraries.interface.dates.today();
triggerAbsencesTrigger(e, f);
}
function report(data) {
return; // turn off
var template = app.backend.templateInterface.load('frontend.report', {data:data}).getContent();
app.config.reportAdmins.forEach(function (agent) {
MailApp.sendEmail(agent, "Daily Notices Report", '', {
htmlBody:template
});
});
}
function triggerAbsencesTrigger(e) {
var f = {};
f.agents = [];
triggerDailyTrigger(e, f); // will build with no agents, i.e. no emails
}
function triggerDailyTrigger(e, f) {
var startTime = new Date().getTime();
var reportData = [];
// First check to see if I should be running at all.
// e parameter can be undefined if run from code console
// or is defined, but f.day is not in that case
// or I define it manually in order to test.
// Reads in holidays column from sheet and converts them into usable dates.
// TODO: Look at the archive to decide what to run with
// if this is equal today, then go ahead, otherwise, just return
f = f ? f : {
test: false,
}
if (typeof f.day === 'undefined') f.day = app.libraries.interface.dates.today(); // TODO: Is there a way to make this a setting on the spreadsheet? Dropdown?
if (typeof f.test === 'undefined') f.test = false;
if (typeof f.agents == 'undefined') f.agents = null;
if (typeof f.updateSite == 'undefined') f.updateSite = true;
app.options.debug = f.test; // This also determines who the agents are
var day = f.day.clone();
if (!f.test) {
// Running from a real daily trigger, so determine if we should stop running
// We can tell if we are supposed to run or not, by lookin at the Archive sheet.
var checkDate = app.config.lastUpdated;
checkDate = app.libraries.moment(checkDate);
if (!day.isSame(checkDate, "day")) {
reportData.push({
message: "Daily Notices did not run today, because it appears that there is no school.",
});
report(reportData);
return;
}
}
var beforeBuildTime = new Date().getTime();
var notices = app.backend.build.buildNoticesForDateByKind(day, 'Staff Notices');
if (typeof notices === 'undefined') {
reportData.push({
message: "No notices to send today!"
});
report(reportData);
return;
}
var agents = f.agents || app.backend.build.buildAgents(f);
var beforeEmailTime = new Date().getTime();
notices.title = "Digest for {}".format(day.format(app.config.dateFormats.long));
app.backend.build.emailAgents(agents, notices, {view:'titlesHtml'});
reportData.push({
message: "Emailing the agents took {} seconds".format(
app.libraries.iMoment.reportSeconds(beforeEmailTime)
)
});
if (f.updateSite) {
var beforeUpdateSite = new Date().getTime();
// update individual site and today's notices
app.backend.build.updateSite(day, notices, {todaysNotices:true});
// build placeholder site for next day
var nothingToSeeHere = {html: "There are no notices here (yet)", kind:notices.kind};
app.backend.build.updateSite(notices.nextDay, nothingToSeeHere, {todaysNotices:false});
reportData.push({
message: "Updating the site took {} seconds".format(
app.libraries.iMoment.reportSeconds(beforeUpdateSite)
)
});
}
report(reportData);
}
function absencesTrigger(e) {
var day = today;
// build just the absences for the email
var absentNotices = buildNoticesForDateByKind(day, 'Staff', {
filterBySection:"Absences",
title: "Absences Update for " + day.format(longDateFormat)
});
var agents = buildAgents(e);
emailAgents(agents, day, absentNotices, {view: 'html'});
var allNotices = buildNoticesForDateByKind(day, 'Staff');
updateSite(day, allNotices)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment