2022-09-27 13:44:44 +01:00
|
|
|
const { BeanModel } = require("redbean-node/dist/bean-model");
|
|
|
|
const { R } = require("redbean-node");
|
|
|
|
const dayjs = require("dayjs");
|
2022-09-27 17:20:17 +01:00
|
|
|
const { log } = require("../../src/util");
|
2022-09-27 13:44:44 +01:00
|
|
|
|
|
|
|
class MaintenanceTimeslot extends BeanModel {
|
|
|
|
|
|
|
|
async toPublicJSON() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async toJSON() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {Maintenance} maintenance
|
|
|
|
* @param {dayjs} startFrom (For recurring type only) Generate Timeslot from this date, if it is smaller than the current date, it will use the current date instead. As generating a passed timeslot is meaningless.
|
|
|
|
* @param {boolean} removeExist Remove existing timeslot before create
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async generateTimeslot(maintenance, startFrom = null, removeExist = false) {
|
|
|
|
if (!startFrom) {
|
|
|
|
startFrom = dayjs();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removeExist) {
|
|
|
|
await R.exec("DELETE FROM maintenance_timeslot WHERE maintenance_id = ? ", [
|
|
|
|
maintenance.id
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:20:17 +01:00
|
|
|
if (maintenance.strategy === "manual") {
|
|
|
|
log.debug("maintenance", "No need to generate timeslot for manual type");
|
|
|
|
} else if (maintenance.strategy === "single") {
|
2022-09-27 13:44:44 +01:00
|
|
|
let bean = R.dispense("maintenance_timeslot");
|
|
|
|
bean.maintenance_id = maintenance.id;
|
2022-09-27 17:48:15 +01:00
|
|
|
bean.start_date = maintenance.start_date;
|
|
|
|
bean.end_date = maintenance.end_date;
|
2022-09-27 13:44:44 +01:00
|
|
|
bean.generated_next = true;
|
|
|
|
await R.store(bean);
|
|
|
|
} else {
|
|
|
|
throw new Error("Unknown maintenance strategy");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MaintenanceTimeslot;
|