2022-01-23 06:22:00 -08:00
|
|
|
const dayjs = require("dayjs");
|
|
|
|
const { BeanModel } = require("redbean-node/dist/bean-model");
|
2022-09-25 04:38:28 -07:00
|
|
|
const { parseTimeObject, parseTimeFromTimeObject } = require("../../src/util");
|
2022-09-24 04:18:24 -07:00
|
|
|
const { isArray } = require("chart.js/helpers");
|
2022-09-25 04:38:28 -07:00
|
|
|
const { timeObjectToUTC, timeObjectToLocal } = require("../util-server");
|
2022-09-27 09:20:17 -07:00
|
|
|
const { R } = require("redbean-node");
|
2022-01-23 06:22:00 -08:00
|
|
|
|
|
|
|
class Maintenance extends BeanModel {
|
|
|
|
|
|
|
|
/**
|
2022-04-30 06:50:05 -07:00
|
|
|
* Return an object that ready to parse to JSON for public
|
2022-01-23 06:22:00 -08:00
|
|
|
* Only show necessary data to public
|
2022-09-25 04:38:28 -07:00
|
|
|
* @param {string} timezone If not specified, the timeRange will be in UTC
|
2022-04-30 06:50:05 -07:00
|
|
|
* @returns {Object}
|
2022-01-23 06:22:00 -08:00
|
|
|
*/
|
2022-09-25 04:38:28 -07:00
|
|
|
async toPublicJSON(timezone = null) {
|
2022-09-24 04:18:24 -07:00
|
|
|
|
|
|
|
let dateTimeRange = [];
|
|
|
|
if (this.start_datetime) {
|
2022-09-27 09:20:17 -07:00
|
|
|
|
|
|
|
dateTimeRange.push(dayjs.utc(this.start_datetime).toISOString());
|
2022-09-24 04:18:24 -07:00
|
|
|
if (this.end_datetime) {
|
2022-09-27 09:20:17 -07:00
|
|
|
dateTimeRange.push(dayjs.utc(this.end_datetime).toISOString());
|
2022-09-24 04:18:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let dateRange = [];
|
|
|
|
if (this.start_date) {
|
2022-09-27 09:20:17 -07:00
|
|
|
dateRange.push(dayjs.utc(this.start_date).toISOString());
|
2022-09-24 04:18:24 -07:00
|
|
|
if (this.end_date) {
|
2022-09-27 09:20:17 -07:00
|
|
|
dateRange.push(dayjs.utc(this.end_date).toISOString());
|
2022-09-24 04:18:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let timeRange = [];
|
2022-09-25 04:38:28 -07:00
|
|
|
let startTime = parseTimeObject(this.start_time);
|
2022-09-24 04:18:24 -07:00
|
|
|
timeRange.push(startTime);
|
2022-09-25 04:38:28 -07:00
|
|
|
let endTime = parseTimeObject(this.end_time);
|
2022-09-24 04:18:24 -07:00
|
|
|
timeRange.push(endTime);
|
|
|
|
|
2022-09-25 04:38:28 -07:00
|
|
|
// Apply timezone offset
|
|
|
|
if (timezone) {
|
|
|
|
if (this.start_time) {
|
|
|
|
timeObjectToLocal(startTime, timezone);
|
|
|
|
}
|
|
|
|
if (this.end_time) {
|
|
|
|
timeObjectToLocal(endTime, timezone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 04:18:24 -07:00
|
|
|
let obj = {
|
2022-01-23 06:22:00 -08:00
|
|
|
id: this.id,
|
|
|
|
title: this.title,
|
|
|
|
description: this.description,
|
2022-09-23 11:33:29 -07:00
|
|
|
strategy: this.strategy,
|
2022-09-24 04:18:24 -07:00
|
|
|
intervalDay: this.interval_day,
|
2022-09-23 11:33:29 -07:00
|
|
|
active: !!this.active,
|
2022-09-24 04:18:24 -07:00
|
|
|
dateTimeRange: dateTimeRange,
|
|
|
|
dateRange: dateRange,
|
|
|
|
timeRange: timeRange,
|
|
|
|
weekdays: (this.weekdays) ? JSON.parse(this.weekdays) : [],
|
|
|
|
daysOfMonth: (this.days_of_month) ? JSON.parse(this.days_of_month) : [],
|
2022-01-23 06:22:00 -08:00
|
|
|
};
|
2022-09-24 04:18:24 -07:00
|
|
|
|
|
|
|
if (!isArray(obj.weekdays)) {
|
|
|
|
obj.weekdays = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isArray(obj.daysOfMonth)) {
|
|
|
|
obj.daysOfMonth = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
2022-01-23 06:22:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-30 06:50:05 -07:00
|
|
|
* Return an object that ready to parse to JSON
|
2022-09-25 04:38:28 -07:00
|
|
|
* @param {string} timezone If not specified, the timeRange will be in UTC
|
2022-04-30 06:50:05 -07:00
|
|
|
* @returns {Object}
|
2022-01-23 06:22:00 -08:00
|
|
|
*/
|
2022-09-25 04:38:28 -07:00
|
|
|
async toJSON(timezone = null) {
|
|
|
|
return this.toPublicJSON(timezone);
|
2022-01-23 06:22:00 -08:00
|
|
|
}
|
2022-09-24 04:18:24 -07:00
|
|
|
|
2022-09-25 04:38:28 -07:00
|
|
|
static jsonToBean(bean, obj, timezone) {
|
2022-09-24 04:18:24 -07:00
|
|
|
if (obj.id) {
|
|
|
|
bean.id = obj.id;
|
|
|
|
}
|
|
|
|
|
2022-09-25 04:38:28 -07:00
|
|
|
// Apply timezone offset to timeRange, as it cannot apply automatically.
|
|
|
|
if (timezone) {
|
|
|
|
if (obj.timeRange[0]) {
|
|
|
|
timeObjectToUTC(obj.timeRange[0], timezone);
|
|
|
|
if (obj.timeRange[1]) {
|
|
|
|
timeObjectToUTC(obj.timeRange[1], timezone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 04:18:24 -07:00
|
|
|
bean.title = obj.title;
|
|
|
|
bean.description = obj.description;
|
|
|
|
bean.strategy = obj.strategy;
|
|
|
|
bean.interval_day = obj.intervalDay;
|
|
|
|
bean.active = obj.active;
|
|
|
|
|
|
|
|
if (obj.dateRange[0]) {
|
2022-09-27 09:20:17 -07:00
|
|
|
bean.start_date = R.isoDate(dayjs(obj.dateRange[0]).utc());
|
2022-09-24 04:18:24 -07:00
|
|
|
|
|
|
|
if (obj.dateRange[1]) {
|
2022-09-27 09:20:17 -07:00
|
|
|
bean.end_date = R.isoDate(dayjs(obj.dateRange[1]).utc());
|
2022-09-24 04:18:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj.dateTimeRange[0]) {
|
2022-09-27 09:20:17 -07:00
|
|
|
bean.start_datetime = R.isoDateTime(dayjs(obj.dateTimeRange[0]).utc());
|
2022-09-24 04:18:24 -07:00
|
|
|
|
|
|
|
if (obj.dateTimeRange[1]) {
|
2022-09-27 09:20:17 -07:00
|
|
|
bean.end_datetime = R.isoDateTime(dayjs(obj.dateTimeRange[1]).utc());
|
2022-09-24 04:18:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 04:38:28 -07:00
|
|
|
bean.start_time = parseTimeFromTimeObject(obj.timeRange[0]);
|
|
|
|
bean.end_time = parseTimeFromTimeObject(obj.timeRange[1]);
|
2022-09-24 04:18:24 -07:00
|
|
|
|
|
|
|
bean.weekdays = JSON.stringify(obj.weekdays);
|
|
|
|
bean.days_of_month = JSON.stringify(obj.daysOfMonth);
|
|
|
|
|
|
|
|
return bean;
|
|
|
|
}
|
2022-09-27 09:20:17 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SQL conditions for active maintenance
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
static getActiveMaintenanceSQLCondition() {
|
|
|
|
return `
|
|
|
|
|
|
|
|
(maintenance_timeslot.start_date <= DATETIME('now')
|
|
|
|
AND maintenance_timeslot.end_date >= DATETIME('now')
|
|
|
|
AND maintenance.active = 1)
|
|
|
|
AND
|
|
|
|
(maintenance.strategy = 'manual' AND active = 1)
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
2022-01-23 06:22:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Maintenance;
|