UNPKG

@openinc/parse-server-opendash

Version:
174 lines (173 loc) 8.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initKanbanStates = initKanbanStates; exports.initCurrentTicketStates = initCurrentTicketStates; exports.initEnabledFlag = initEnabledFlag; const types_1 = require("../../../types"); /** * Initialize the kanban states table. * Creates a inbox and finished state if they do not exist. */ async function initKanbanStates() { const tenant = await new Parse.Query(types_1.Tenant) .ascending("createdAt") .first({ useMasterKey: true }); const inboxState = await new Parse.Query(types_1.Maintenance_Kanban_State) .equalTo("isInbox", true) .first({ useMasterKey: true }); const finishedState = await new Parse.Query(types_1.Maintenance_Kanban_State) .equalTo("isFinished", true) .first({ useMasterKey: true }); if (!inboxState) { const newInboxState = new types_1.Maintenance_Kanban_State({ label: "Eingang", isInbox: true, color: "#f0f0f0", order: 99, enabled: true, }); const acl = new Parse.ACL(); acl.setPublicReadAccess(false); acl.setPublicWriteAccess(false); acl.setRoleReadAccess("od-admin", true); acl.setRoleWriteAccess("od-admin", true); if (tenant) { acl.setRoleReadAccess(`od-tenant-user-${tenant.id}`, true); acl.setRoleWriteAccess(`od-tenant-user-${tenant.id}`, true); acl.setRoleReadAccess(`od-tenant-admin-${tenant.id}`, true); acl.setRoleWriteAccess(`od-tenant-admin-${tenant.id}`, true); } newInboxState.setACL(acl); await newInboxState.save(null, { useMasterKey: true }); } if (!finishedState) { const newFinishedState = new types_1.Maintenance_Kanban_State({ label: "Ausgang", isFinished: true, color: "#d4edda", order: 100, enabled: true, }); const acl = new Parse.ACL(); acl.setPublicReadAccess(false); acl.setPublicWriteAccess(false); acl.setRoleReadAccess("od-admin", true); acl.setRoleWriteAccess("od-admin", true); if (tenant) { acl.setRoleReadAccess(`od-tenant-user-${tenant.id}`, true); acl.setRoleWriteAccess(`od-tenant-user-${tenant.id}`, true); acl.setRoleReadAccess(`od-tenant-admin-${tenant.id}`, true); acl.setRoleWriteAccess(`od-tenant-admin-${tenant.id}`, true); } newFinishedState.setACL(acl); await newFinishedState.save(null, { useMasterKey: true }); } } /** * Initialize the current ticket states table. if a ticket has no entry in the current ticket states table, create one. * If the ticket has no state, initialize it with the inbox state. */ async function initCurrentTicketStates() { console.log("Initializing current ticket states table"); // Getting the tickets that have no entry in the current ticket states table (OD3_Maintenance_Ticket_Kanban_State_Current) const ticketsWithoutEntry = await new Parse.Query(types_1.Maintenance_Ticket) .doesNotMatchKeyInQuery("objectId", //@ts-ignore "ticket.objectId", new Parse.Query(types_1.Maintenance_Ticket_Kanban_State_Current).select("ticket")) .find({ useMasterKey: true }); // for each ticket not in the table, create an entry for (const ticket of ticketsWithoutEntry) { const currentTicketState = await new Parse.Query(types_1.Maintenance_Ticket_Kanban_State) .includeAll() .equalTo("ticket", ticket) .descending("createdAt") .first({ useMasterKey: true }); const tenant = ticket.get("tenant"); const user = ticket.get("user"); if (tenant === undefined || user === undefined) { console.error("Tenant or user is undefined for ticket", ticket.id); continue; } // if the ticket has a state, create an entry in the current ticket states table if (currentTicketState) { const newTicketState = new types_1.Maintenance_Ticket_Kanban_State_Current({ ticket: ticket, ticketState: currentTicketState, state: currentTicketState.get("state"), tenant: tenant, }); const acl = new Parse.ACL(); acl.setPublicReadAccess(false); acl.setPublicWriteAccess(false); acl.setRoleReadAccess("od-admin", true); acl.setRoleWriteAccess("od-admin", true); acl.setReadAccess(user.id, true); acl.setWriteAccess(user.id, true); acl.setRoleReadAccess(`od-tenant-user-${tenant.id}`, true); acl.setRoleWriteAccess(`od-tenant-user-${tenant.id}`, true); acl.setRoleReadAccess(`od-tenant-admin-${tenant.id}`, true); acl.setRoleWriteAccess(`od-tenant-admin-${tenant.id}`, true); newTicketState.setACL(acl); await newTicketState.save(null, { useMasterKey: true }); } else { // if the ticket has no state, initialize it with the inbox state const inboxState = await new Parse.Query(types_1.Maintenance_Kanban_State) .equalTo("isInbox", true) .first({ useMasterKey: true }); if (!inboxState) continue; // saving the ticket state will automatically create an entry in the current ticket states table due to the afterSave-Hook const newkanbanstate = new types_1.Maintenance_Ticket_Kanban_State({ ticket: ticket, state: inboxState, tenant: tenant, user: user, }); const acl = new Parse.ACL(); acl.setPublicReadAccess(false); acl.setPublicWriteAccess(false); acl.setRoleReadAccess("od-admin", true); acl.setRoleWriteAccess("od-admin", true); acl.setReadAccess(user.id, true); acl.setWriteAccess(user.id, true); acl.setRoleReadAccess(`od-tenant-user-${tenant.id}`, true); acl.setRoleWriteAccess(`od-tenant-user-${tenant.id}`, true); acl.setRoleReadAccess(`od-tenant-admin-${tenant.id}`, true); acl.setRoleWriteAccess(`od-tenant-admin-${tenant.id}`, true); newkanbanstate.setACL(acl); const savedkanbanstate = await newkanbanstate.save(null, { useMasterKey: true, }); const currentTicketState = await new Parse.Query(types_1.Maintenance_Ticket_Kanban_State_Current) .equalTo("ticket", savedkanbanstate.get("ticket")) .first({ useMasterKey: true }); // if the ticket is already in the current state table, update it if (currentTicketState) { currentTicketState.set("ticketState", savedkanbanstate); currentTicketState.set("state", savedkanbanstate.get("state")); await currentTicketState.save(null, { useMasterKey: true }); } // if the ticket is not in the current state table, create a new entry else { const newTicketState = new types_1.Maintenance_Ticket_Kanban_State_Current({ ticket: savedkanbanstate.get("ticket"), ticketState: savedkanbanstate, state: savedkanbanstate.get("state"), tenant: savedkanbanstate.get("tenant"), }); await newTicketState.save(null, { useMasterKey: true }); } } } console.log("Current ticket states table initialized"); } async function initEnabledFlag() { const objects = await new Parse.Query(types_1.Maintenance_Kanban_State) .limit(1000000000) .find({ useMasterKey: true }); const promises = objects.map((object) => { object.save({ enabled: object.enabled === undefined ? true : object.enabled }, { useMasterKey: true }); }); await Promise.all(promises); }