@openinc/parse-server-opendash
Version:
Parse Server Cloud Code for open.INC Stack.
81 lines (80 loc) • 4.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.init = init;
const __1 = require("..");
const catchError_1 = require("../helper/catchError");
const types_1 = require("../types");
/**
* This handles all config changes for a key starting with 'OPENSERVICE_' in class OD3_Config
*/
async function init() {
(0, __1.beforeSaveHook)(types_1.Config, async (request) => {
const { object, original, user } = request;
await (0, __1.defaultHandler)(request);
//Set custom acl for all keys starting with 'OPENSERVICE_'
//Read should only be allowed for tenant users
//Write should be allowed for tenant users
if (object.get("key").startsWith("OPENSERVICE_")) {
let acl = object.getACL();
const tenant = object.get("tenant");
if (!acl) {
acl = new Parse.ACL();
}
acl.setRoleReadAccess("od-admin", true);
acl.setRoleWriteAccess("od-admin", true);
if (tenant?.id) {
acl.setPublicReadAccess(false);
acl.setPublicWriteAccess(false);
acl.setRoleReadAccess(`od-tenant-admin-${tenant?.id}`, true);
acl.setRoleWriteAccess(`od-tenant-admin-${tenant?.id}`, true);
acl.setRoleReadAccess(`od-tenant-user-${tenant?.id}`, true);
acl.setRoleWriteAccess(`od-tenant-user-${tenant?.id}`, true);
}
object.setACL(acl);
}
});
(0, __1.afterSaveHook)(types_1.Config, async ({ object, original, user }) => {
if (object.get("key").startsWith("OPENSERVICE_")) {
console.log("Config changed: ", object.get("key"));
console.log("Check Meta Fields in Maintenance_Schedule_Step...");
//Get all Entries in OD3_Maintenance_Step and remove all entries in field "fields" (type: array) that are not in the config
//object.value can look like this: [{"label":"PSA","allowedValues":[],"required":false},{"label":"Typ","allowedValues":[],"required":false,"onlyOneAllowedValue":false},{"label":"Material","allowedValues":["@OD3_Maintenance_Item"],"required":false,"onlyOneAllowedValue":false},{"label":"Dienstleister","allowedValues":["@OD3_COMPANY","@OD3_CONTACT"],"required":false,"onlyOneAllowedValue":false}]
//fields in Maintenance_Step can look like this: [{"value":"Handschuhe","metafield":{"label":"PSA","allowedValues":[],"required":false}},{"value":"Auffüllen","metafield":{"label":"Typ","allowedValues":[],"required":false,"onlyOneAllowedValue":false}}]
const metafieldsconfig = JSON.parse(object.get("value"));
const [maintenanceStepQueryError, maintenanceStepQuery] = await (0, catchError_1.catchError)(new Parse.Query(types_1.Maintenance_Schedule_Step).find({
useMasterKey: true,
}));
if (maintenanceStepQueryError) {
console.error("Error while querying Maintenance_Step objects", maintenanceStepQueryError);
return;
}
for (const maintenanceStep of maintenanceStepQuery) {
const fields = maintenanceStep.get("fields");
if (fields) {
//Loop through fields and remove all fields that are not in the config
for (let i = 0; i < fields.length; i++) {
if (!metafieldsconfig.find((el) => el.label === fields[i].metafield?.label)) {
console.log("Remove field:", fields[i].metafield?.label);
fields.splice(i, 1);
i--;
}
}
//Loop through config and add all fields that are not in the fields array
for (const metafield of metafieldsconfig) {
if (!fields.find((el) => el.metafield?.label === metafield.label)) {
console.log("Add field:", metafield.label);
fields.push({
value: { value: "", classname: undefined },
metafield,
});
}
}
maintenanceStep.set("fields", fields);
console.log("Save Maintenance_Schedule_Step " + maintenanceStep.id);
await maintenanceStep.save(null, { useMasterKey: true });
}
}
console.log("Meta Fields in Maintenance_Schedule_Template checked. Done.");
}
});
}