@openinc/parse-server-opendash
Version:
Parse Server Cloud Code for open.INC Stack.
141 lines (140 loc) • 6.99 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// import {
// changeLanguage,
// getCurrentLanguageSync,
// useTranslation,
// } from "@opendash/i18n";
const node_process_1 = __importDefault(require("node:process"));
const node_worker_threads_1 = require("node:worker_threads");
const node_js_1 = __importDefault(require("parse/node.js"));
const index_js_1 = require("../../../features/config/index.js");
const index_js_2 = require("../../../features/notifications/index.js");
const openinc_openservice_save_ticket_data_js_1 = require("../../../functions/openinc-openservice-save-ticket-data.js");
const catchError_js_1 = require("../../../helper/catchError.js");
const Config_js_1 = require("../../../types/Config.js");
const Maintenance_Schedule_Template_js_1 = require("../../../types/Maintenance_Schedule_Template.js");
const Notification_js_1 = require("../../../types/Notification.js");
const Notification_Setting_js_1 = require("../../../types/Notification_Setting.js");
function l(message) {
if (node_worker_threads_1.parentPort) {
node_worker_threads_1.parentPort.postMessage(message);
}
}
(async () => {
l("Initializing Parse with worker data: " + JSON.stringify(node_worker_threads_1.workerData));
node_js_1.default.initialize(node_worker_threads_1.workerData.ParseAppId, node_worker_threads_1.workerData.ParseJSKey !== "unused" ? node_worker_threads_1.workerData.ParseJSKey : undefined, node_worker_threads_1.workerData.ParseMasterKey);
node_js_1.default.serverURL = node_worker_threads_1.workerData.ParseServerURL;
l("Schedule is due.");
// Check OD3_Config class for key ConfigKeys.OpenService.createTicketOnSchedule. If value is true, proceed with ticket creation
const [configError, config] = await (0, catchError_js_1.catchError)(new node_js_1.default.Query(Config_js_1.Config.className)
.equalTo("key", index_js_1.ConfigKeys.OpenService.createTicketOnSchedule)
.first({ useMasterKey: true }));
//Get schedule object from worker data
const [scheduleError, schedule] = await (0, catchError_js_1.catchError)(new node_js_1.default.Query(Maintenance_Schedule_Template_js_1.Maintenance_Schedule_Template.className).get(node_worker_threads_1.workerData.scheduleId, { useMasterKey: true }));
if (configError) {
l("Error while querying OD3_Config for 'createTicketOnSchedule' key: " +
configError);
return;
}
if (scheduleError || schedule === undefined) {
l("Error while querying Maintenance_Schedule_Template object: " +
scheduleError);
return;
}
if (config === undefined || !/true/.test(config.get("value"))) {
l("Config 'createTicketOnSchedule' not set or false, skipping ticket creation for schedule.");
}
else {
//Get all sources from schedule object
const [sourcesError, sources] = await (0, catchError_js_1.catchError)(schedule
.relation("sources")
.query()
.find({ useMasterKey: true }));
if (sourcesError) {
l("Error while querying sources from Maintenance_Schedule_Template object: " +
sourcesError);
return;
}
for await (const source of sources) {
//Create a new ticket
await (0, openinc_openservice_save_ticket_data_js_1.saveTicketData)({
title: "",
source: source.id,
});
}
}
l("Config 'createTicketOnSchedule' is true, creating new ticket object");
const [notificationSettingsError, notificationSettings] = await (0, catchError_js_1.catchError)(new node_js_1.default.Query(Notification_Setting_js_1.Notification_Setting)
.equalTo("key", index_js_2.Notifications.OpenService.schedule_due)
.find({ useMasterKey: true }));
if (notificationSettingsError) {
l("Error while querying Notification_Setting for 'Notifications.OpenService.schedule_due' key: " +
notificationSettingsError);
return;
}
const recipients = [];
const users = notificationSettings.map((setting) => setting
.relation("users")
.query()
.include("settings")
.find({ useMasterKey: true }));
for await (const user of users) {
recipients.push(...user);
}
const rolesPromises = notificationSettings.map((setting) => setting.relation("roles").query().find({ useMasterKey: true }));
for await (const roles of rolesPromises) {
const usersFromRoles = roles.map((role) => role
.relation("users")
.query()
.include("settings")
.find({ useMasterKey: true }));
for await (const usersFromRole of usersFromRoles) {
recipients.push(...usersFromRole);
}
}
//Filter out duplicates
recipients.filter((user, index, self) => {
return (index ===
self.findIndex((t) => t.id === user.id && t.className === user.className));
});
l("Recipients for notification: " + JSON.stringify(recipients));
//TODO: When available as ESM, use i18n module to get the correct language for the user
for await (const recipient of recipients) {
l("Creating new notification object for user: " + recipient.id);
//Get preferred_language from user object
const language = recipient.get("settings")
? recipient.get("settings").get("preferred_language")
: "deu";
// if (getCurrentLanguageSync() !== language) {
// changeLanguage(language);
// }
//Get the correct notification title and description based on the user's preferred language
// const t = useTranslation();
const findtitle = require(`../i18n/${language}.json`)["maintenance.schedule.notification.title"];
const finddescription = require(`../i18n/${language}.json`)["maintenance.schedule.notification.description"];
//Create a new notification object in the database, class name: OD3_Notification
const notificationObject = new node_js_1.default.Object(Notification_js_1.Notification.className);
notificationObject.set("title",
// t("server:maintenance.schedule.notification.title")
findtitle);
notificationObject.set("description",
// t("server:maintenance.schedule.notification.description", {
// schedule:
// schedule.get("title") !== undefined ? schedule.get("title")! : "",
// })
finddescription);
notificationObject.set("user", recipient);
await notificationObject.save(null, { useMasterKey: true });
}
// signal to parent that the job is done
if (node_worker_threads_1.parentPort) {
l("done");
}
else {
node_process_1.default.exit(0);
}
})();