2021-09-16 14:48:28 +00:00
|
|
|
const { R } = require("redbean-node");
|
2021-09-20 08:22:18 +00:00
|
|
|
const { checkLogin, setSettings } = require("../util-server");
|
2021-09-16 14:48:28 +00:00
|
|
|
const dayjs = require("dayjs");
|
2021-09-19 11:04:51 +00:00
|
|
|
const { debug } = require("../../src/util");
|
2021-09-16 14:48:28 +00:00
|
|
|
|
|
|
|
module.exports.statusPageSocketHandler = (socket) => {
|
|
|
|
|
|
|
|
// Post or edit incident
|
|
|
|
socket.on("postIncident", async (incident, callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
await R.exec("UPDATE incident SET pin = 0 ");
|
|
|
|
|
|
|
|
let incidentBean;
|
|
|
|
|
|
|
|
if (incident.id) {
|
|
|
|
incidentBean = await R.findOne("incident", " id = ?", [
|
|
|
|
incident.id
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (incidentBean == null) {
|
|
|
|
incidentBean = R.dispense("incident");
|
|
|
|
}
|
|
|
|
|
|
|
|
incidentBean.title = incident.title;
|
|
|
|
incidentBean.content = incident.content;
|
|
|
|
incidentBean.style = incident.style;
|
|
|
|
incidentBean.pin = true;
|
2021-09-19 11:04:51 +00:00
|
|
|
|
|
|
|
if (incident.id) {
|
|
|
|
incidentBean.lastUpdatedDate = R.isoDateTime(dayjs.utc());
|
|
|
|
} else {
|
|
|
|
incidentBean.createdDate = R.isoDateTime(dayjs.utc());
|
|
|
|
}
|
|
|
|
|
2021-09-16 14:48:28 +00:00
|
|
|
await R.store(incidentBean);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
incident: incidentBean.toPublicJSON(),
|
2021-09-16 14:57:34 +00:00
|
|
|
});
|
2021-09-16 14:48:28 +00:00
|
|
|
} catch (error) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: error.message,
|
2021-09-16 14:57:34 +00:00
|
|
|
});
|
2021-09-16 14:48:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("unpinIncident", async (callback) => {
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
|
|
|
await R.exec("UPDATE incident SET pin = 0 WHERE pin = 1");
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
2021-09-16 14:57:34 +00:00
|
|
|
});
|
2021-09-16 14:48:28 +00:00
|
|
|
} catch (error) {
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: error.message,
|
2021-09-16 14:57:34 +00:00
|
|
|
});
|
2021-09-16 14:48:28 +00:00
|
|
|
}
|
|
|
|
});
|
2021-09-19 11:04:51 +00:00
|
|
|
|
|
|
|
// Save Status Page
|
2021-09-20 08:22:18 +00:00
|
|
|
socket.on("saveStatusPage", async (config, imgDataUrl, publicGroupList, callback) => {
|
2021-09-19 11:04:51 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
checkLogin(socket);
|
|
|
|
|
2021-09-20 12:44:07 +00:00
|
|
|
// Save Config
|
|
|
|
await setSettings("statusPage", config);
|
|
|
|
|
2021-09-19 11:04:51 +00:00
|
|
|
await R.transaction(async (trx) => {
|
2021-09-20 08:22:18 +00:00
|
|
|
|
|
|
|
// Save Icon
|
|
|
|
|
|
|
|
// Save Public Group List
|
2021-09-19 11:04:51 +00:00
|
|
|
const groupIDList = [];
|
|
|
|
let groupOrder = 1;
|
|
|
|
|
|
|
|
for (let group of publicGroupList) {
|
|
|
|
let groupBean;
|
|
|
|
if (group.id) {
|
|
|
|
groupBean = await trx.findOne("group", " id = ? AND public = 1 ", [
|
|
|
|
group.id
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
groupBean = R.dispense("group");
|
|
|
|
}
|
|
|
|
|
|
|
|
groupBean.name = group.name;
|
|
|
|
groupBean.public = true;
|
|
|
|
groupBean.weight = groupOrder++;
|
|
|
|
|
|
|
|
await trx.store(groupBean);
|
|
|
|
|
|
|
|
await trx.exec("DELETE FROM monitor_group WHERE group_id = ? ", [
|
|
|
|
groupBean.id
|
|
|
|
]);
|
|
|
|
|
|
|
|
let monitorOrder = 1;
|
|
|
|
for (let monitor of group.monitorList) {
|
|
|
|
let relationBean = R.dispense("monitor_group");
|
|
|
|
relationBean.weight = monitorOrder++;
|
|
|
|
relationBean.group_id = groupBean.id;
|
|
|
|
relationBean.monitor_id = monitor.id;
|
|
|
|
await trx.store(relationBean);
|
|
|
|
}
|
|
|
|
|
|
|
|
groupIDList.push(groupBean.id);
|
|
|
|
group.id = groupBean.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete groups that not in the list
|
|
|
|
debug("Delete groups that not in the list");
|
|
|
|
const slots = groupIDList.map(() => "?").join(",");
|
|
|
|
await trx.exec(`DELETE FROM \`group\` WHERE id NOT IN (${slots})`, groupIDList);
|
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: true,
|
|
|
|
publicGroupList,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch (error) {
|
2021-09-20 08:22:18 +00:00
|
|
|
console.log(error);
|
2021-09-19 11:04:51 +00:00
|
|
|
|
|
|
|
callback({
|
|
|
|
ok: false,
|
|
|
|
msg: error.message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-16 14:57:34 +00:00
|
|
|
};
|