@openinc/parse-server-opendash
Version:
Parse Server Cloud Code for open.INC Stack.
81 lines (80 loc) • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllNotifications = getAllNotifications;
exports.default = initNotifications;
const __1 = require("..");
const types_1 = require("../../../types");
/**
* Returns all notifications from the OpenService plugin defined in types/config.ts as an array of Notification objects.
* @returns all notifications from the OpenService plugin
*/
function getAllNotifications() {
const notifications = []; // stores all notifications
for (const key in __1.Notifications) {
// iterate over the objects / enums of the Notifications namespace
if (Object.prototype.hasOwnProperty.call(__1.Notifications, key)) {
const element = __1.Notifications[key]; // get the object / enum
const createdNotification = createNotification(element); // create Notification objects recursively
notifications.push(...createdNotification);
}
}
return notifications;
}
/**
* Recursively creates Notification objects from the input object.
* @param input a string from an enum or an enum itself
* @returns an array of Notification objects
*/
function createNotification(input) {
const notifications = []; // all notification objects created from the input
if (typeof input === "string") {
// if the input is a string, create a Notification object from it
notifications.push(new __1.RegisteredNotification(input, input + ".label", input + ".description"));
}
else {
// if the input is an object, iterate over its keys and create Notification objects from the values
for (const key in input) {
if (Object.prototype.hasOwnProperty.call(input, key)) {
const element = input[key];
notifications.push(...createNotification(element));
}
}
}
return notifications;
}
/**
* Register all notifications in the database, if not already registered.
*/
async function registerNotifications(tenant) {
console.log("[@openinc/parse-server-opendash] Register all notifications");
getAllNotifications().forEach((notification) => {
new Parse.Query(types_1.Notification_Setting)
.equalTo("key", notification.key)
.equalTo("tenant", tenant)
.first({ useMasterKey: true })
.then((result) => {
if (!result) {
console.log("[@openinc/parse-server-opendash] Create notification", notification.key);
const newPermission = new types_1.Notification_Setting();
newPermission.set("key", notification.key);
newPermission.set("label", notification.label);
newPermission.set("tenant", tenant);
newPermission.set("description", notification.description);
newPermission.save({}, { useMasterKey: true });
}
else {
console.log("[@openinc/parse-server-opendash] Notification already exists", notification.key);
}
});
});
}
async function initNotifications() {
const tenants = await new Parse.Query(types_1.Tenant)
.descending("createdAt")
.find({ useMasterKey: true });
if (tenants) {
for await (const tenant of tenants) {
await registerNotifications(tenant);
}
}
}