@openinc/parse-server-opendash 2.4.52 → 2.4.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.init = init;
4
+ const types_1 = require("../types");
5
+ async function init(name) {
6
+ Parse.Cloud.define(name, handleRequest, {
7
+ requireUser: true,
8
+ });
9
+ }
10
+ async function handleRequest(request) {
11
+ if (typeof request.user === "undefined") {
12
+ return { success: false, error: "User not found" };
13
+ }
14
+ console.log("Handling request with", request.params);
15
+ try {
16
+ const ticketData = await getTicketData(request.params);
17
+ return {
18
+ success: true,
19
+ data: ticketData,
20
+ };
21
+ }
22
+ catch (error) {
23
+ console.error("Error while fetching ticket data", error);
24
+ return {
25
+ success: false,
26
+ error: error,
27
+ };
28
+ }
29
+ }
30
+ async function getTicketData(params) {
31
+ const tickets = await new Parse.Query(types_1.Maintenance_Ticket)
32
+ .containedIn("objectId", params.ticketIds)
33
+ .limit(1000000)
34
+ .find({ useMasterKey: true });
35
+ const ticketData = tickets.map((ticket) => ({
36
+ ticket: ticket,
37
+ }));
38
+ // fetch daily schedule
39
+ if (params.options.dailySchedule) {
40
+ console.log("fetching daily schedule");
41
+ const schedules = await new Parse.Query(types_1.Maintenance_DailySchedule)
42
+ .include("tickets")
43
+ .matchesQuery("tickets", new Parse.Query(types_1.Maintenance_DailySchedule).matchesQuery("tickets", new Parse.Query(types_1.Maintenance_Ticket).containedIn("objectId", params.ticketIds)))
44
+ .limit(1000000)
45
+ .find({ useMasterKey: true });
46
+ const scheduleTickets = await Promise.all(schedules.map(async (schedule) => {
47
+ return {
48
+ schedule,
49
+ tickets: await schedule
50
+ .get("tickets")
51
+ .query()
52
+ .find({ useMasterKey: true }),
53
+ };
54
+ }));
55
+ ticketData.forEach((data) => {
56
+ const scheduleTicket = scheduleTickets.find((scheduleTicket) => scheduleTicket.tickets.some((ticket) => ticket.id === data.ticket.id));
57
+ if (scheduleTicket) {
58
+ data.dailySchedule = scheduleTicket.schedule;
59
+ }
60
+ });
61
+ }
62
+ // fetch downtime
63
+ if (params.options.downtime) {
64
+ console.log("fetching downtime");
65
+ const downtimes = await new Parse.Query(types_1.Maintenance_Downtime)
66
+ .descending("updatedAt")
67
+ .containedIn("ticket", params.ticketIds)
68
+ .limit(1000000)
69
+ .find({ useMasterKey: true });
70
+ ticketData.forEach((data) => {
71
+ data.downtime = downtimes.find((downtime) => downtime.get("ticket").id === data.ticket.id);
72
+ });
73
+ }
74
+ // fetch duedate
75
+ if (params.options.duedate) {
76
+ console.log("fetching duedate");
77
+ const duedates = await new Parse.Query(types_1.Maintenance_Duedate)
78
+ .descending("updatedAt")
79
+ .containedIn("ticket", params.ticketIds)
80
+ .limit(1000000)
81
+ .find({ useMasterKey: true });
82
+ ticketData.forEach((data) => {
83
+ data.duedate = duedates.find((duedate) => duedate.get("ticket").id === data.ticket.id);
84
+ });
85
+ }
86
+ // fetch frequency
87
+ if (params.options.frequency) {
88
+ console.log("fetching frequency");
89
+ const frequencies = await new Parse.Query(types_1.Maintenance_Frequency)
90
+ .descending("updatedAt")
91
+ .containedIn("ticket", params.ticketIds)
92
+ .limit(1000000)
93
+ .find({ useMasterKey: true });
94
+ ticketData.forEach((data) => {
95
+ data.frequency = frequencies.find((frequency) => frequency.get("ticket").id === data.ticket.id);
96
+ });
97
+ }
98
+ // fetch issuecategory
99
+ if (params.options.issuecategory) {
100
+ console.log("fetching issuecategory");
101
+ const issuecategories = await new Parse.Query(types_1.Maintenance_Ticket_Issuecategory)
102
+ .includeAll()
103
+ .descending("updatedAt")
104
+ .containedIn("ticket", params.ticketIds)
105
+ .limit(1000000)
106
+ .find({ useMasterKey: true });
107
+ ticketData.forEach((data) => {
108
+ data.issuecategory = issuecategories
109
+ .find((issuecategory) => issuecategory.get("ticket").id === data.ticket.id)
110
+ ?.get("issuecategory");
111
+ });
112
+ }
113
+ // fetch state
114
+ if (params.options.state) {
115
+ console.log("fetching state");
116
+ const ticketStates = await new Parse.Query(types_1.Maintenance_Ticket_Kanban_State_Current)
117
+ .descending("updatedAt")
118
+ .includeAll()
119
+ .matchesQuery("ticket", new Parse.Query(types_1.Maintenance_Ticket).containedIn("objectId", params.ticketIds))
120
+ .limit(1000000)
121
+ .find({ useMasterKey: true });
122
+ ticketData.forEach((data) => {
123
+ data.state = ticketStates.find((ts) => ts.get("ticket")?.id === data.ticket.id)?.state;
124
+ });
125
+ }
126
+ // fetch messages
127
+ if (params.options.messages) {
128
+ console.log("fetching messages");
129
+ const messageBodies = await new Parse.Query(types_1.Maintenance_Message_Body)
130
+ .include("message")
131
+ .matchesQuery("message", new Parse.Query(types_1.Maintenance_Message)
132
+ .descending("updatedAt")
133
+ .containedIn("referencedObjectId", params.ticketIds)
134
+ .limit(1000000))
135
+ .limit(1000000)
136
+ .find({ useMasterKey: true });
137
+ const messages = messageBodies
138
+ .map((msgB) => msgB.message)
139
+ .filter((msg) => !!msg);
140
+ ticketData.forEach((data) => {
141
+ data.messages = messages.filter((msg) => msg?.get("referencedObjectId") === data.ticket.id);
142
+ data.messageBodies = messageBodies.filter((msgB) => data.messages?.some((msg) => msg.id === msgB.get("message")?.id));
143
+ });
144
+ }
145
+ // fetch priority
146
+ if (params.options.priority) {
147
+ console.log("fetching priority");
148
+ const priorities = await new Parse.Query(types_1.Maintenance_Priority)
149
+ .descending("updatedAt")
150
+ .containedIn("ticket", params.ticketIds)
151
+ .limit(1000000)
152
+ .find({ useMasterKey: true });
153
+ ticketData.forEach((data) => {
154
+ data.priority = priorities.find((priority) => priority.get("ticket").id === data.ticket.id);
155
+ });
156
+ }
157
+ // fetch restriction
158
+ if (params.options.restriction) {
159
+ console.log("fetching restriction");
160
+ const restrictions = await new Parse.Query(types_1.Maintenance_Restriction)
161
+ .descending("updatedAt")
162
+ .containedIn("ticket", params.ticketIds)
163
+ .limit(1000000)
164
+ .find({ useMasterKey: true });
165
+ ticketData.forEach((data) => {
166
+ data.restriction = restrictions.find((restriction) => restriction.get("ticket").id === data.ticket.id);
167
+ });
168
+ }
169
+ // fetch source
170
+ if (params.options.source) {
171
+ console.log("fetching source");
172
+ const sources = await new Parse.Query(types_1.Maintenance_Ticket_Source)
173
+ .includeAll()
174
+ .descending("updatedAt")
175
+ .containedIn("ticket", params.ticketIds)
176
+ .limit(1000000)
177
+ .find({ useMasterKey: true });
178
+ ticketData.forEach((data) => {
179
+ data.source = sources.find((source) => source.get("ticket").id === data.ticket.id)?.source;
180
+ });
181
+ }
182
+ return ticketData;
183
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "2.4.52",
3
+ "version": "2.4.53",
4
4
  "description": "Parse Server Cloud Code for open.DASH",
5
5
  "keywords": [
6
6
  "parse",
@@ -1,43 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.init = init;
4
- const types_1 = require("../types");
5
- async function init(name) {
6
- Parse.Cloud.define(name, async function (request) {
7
- if (typeof request.user === "undefined") {
8
- return { success: false, error: "User not found" };
9
- }
10
- if (!request.params?.state) {
11
- return { success: false, error: "StateID not provided" };
12
- }
13
- const cursor = request.params?.cursor || 0;
14
- const limit = request.params?.limit || 10;
15
- const state = await new Parse.Query(types_1.Maintenance_Kanban_State).get(request.params?.state, { useMasterKey: true });
16
- const currentPagePipeline = [
17
- { $group: { _id: "ticket", doc: { $first: "$$ROOT" } } }, // Group by ticket to only get the latest state of the ticket
18
- { $match: { state: state } }, // only get tickets with the state provided
19
- { $sort: { createdAt: 1 } }, // sort by createdAt
20
- { $match: { createdAt: { $lt: new Date(cursor) } } }, // only get tickets with createdAt less than the cursor => pagination
21
- { $limit: limit }, // limit the number of tickets recieved
22
- { $replaceRoot: { newRoot: "$doc" } }, // replace the root of the document with the ticket
23
- ];
24
- const aggregatedTicketKanbanStates = await new Parse.Query(types_1.Maintenance_Ticket_Kanban_State).aggregate(currentPagePipeline,
25
- //@ts-expect-error
26
- { useMasterKey: true });
27
- const totalCountPipeline = [
28
- { $group: { _id: "ticket" } }, // Group by ticket to only get the latest state of the ticket
29
- { $match: { state: state } }, // only get tickets with the state provided
30
- { $count: "count" }, // count the number of tickets
31
- ];
32
- const totalCount = await new Parse.Query(types_1.Maintenance_Ticket_Kanban_State).aggregate(totalCountPipeline,
33
- //@ts-expect-error
34
- { useMasterKey: true });
35
- return {
36
- success: true,
37
- ticketsStates: aggregatedTicketKanbanStates ?? [],
38
- totalCount: totalCount[0]?.count ?? 0,
39
- };
40
- }, {
41
- requireUser: true,
42
- });
43
- }