-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathentityCalculations.js
More file actions
335 lines (292 loc) · 10.4 KB
/
entityCalculations.js
File metadata and controls
335 lines (292 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
'use strict';
const { httpCodes, poetrySlamStatusCode, visitStatusCode } = require('./codes');
// ----------------------------------------------------------------------------
// Implementation of reuse functions
// ----------------------------------------------------------------------------
const DATE_DAYS_MULTIPLIER = 24 * 60 * 60 * 1000;
// Default the freeVisitorSeats to the maximumVisitorsNumber
async function calculatePoetrySlamData(id, req, additionalVisits = 0) {
const tableExtension = req.target.name.endsWith('drafts') ? '.drafts' : '';
const changedData = {};
if (!id) {
console.error('Poetry Slam ID not found');
req.error(httpCodes.internal_server_error, 'POETRYSLAM_NOT_FOUND', [id]);
return;
}
// Target.name of the request includes the originally changed entity. In case it is the draft, check in the draft table.
const poetrySlam = await SELECT.one
.from(`PoetrySlamService.PoetrySlams${tableExtension}`)
.columns('status_code', 'number', 'maxVisitorsNumber', 'freeVisitorSeats')
.where({ ID: id });
// If poetry slam was not found, throw an error
if (!poetrySlam) {
console.error('Poetry Slam not found');
req.error(httpCodes.internal_server_error, 'POETRYSLAM_NOT_FOUND', [id]);
return;
}
let visitConfirmedCount = 0;
// Request contains visits
if (req.data?.visits?.length) {
visitConfirmedCount =
req.data.visits.filter(
(visit) => visit.status_code === visitStatusCode.booked
)?.length || 0;
} else {
// Request does not contain data in all cases. If it is not included, the data needs to be read from the database
// Read and calculate the confirmed visits
let visit = await SELECT.one
.from(`PoetrySlamService.Visits${tableExtension}`)
.columns('parent_ID', 'count(*) as visitConfirmedCount')
.where({ parent_ID: id, status_code: visitStatusCode.booked })
.groupBy('parent_ID');
visitConfirmedCount = visit?.visitConfirmedCount ?? 0;
}
visitConfirmedCount = visitConfirmedCount + additionalVisits;
// In case maxVisitorsNumber was changed, use the changed
const maxVisitorsNumber =
req.data?.maxVisitorsNumber || poetrySlam.maxVisitorsNumber;
// Calculate the free seats
changedData.freeVisitorSeats = Math.max(
maxVisitorsNumber - visitConfirmedCount,
0
);
// Calculation is required for update case
// Set the status of reading event to booked, if there are no free seats and status is published
changedData.status_code = calculatePoetrySlamStatus(
poetrySlam.status_code,
changedData.freeVisitorSeats
);
return changedData;
}
// Calculate the status in cases of changes
function calculatePoetrySlamStatus(currentStatus, freeVisitorSeats) {
if (
currentStatus === poetrySlamStatusCode.published &&
freeVisitorSeats === 0
) {
return poetrySlamStatusCode.booked;
} else if (
currentStatus === poetrySlamStatusCode.booked &&
freeVisitorSeats > 0
) {
// Check if there are free seats and status was booked, then change the status to published
return poetrySlamStatusCode.published;
}
return currentStatus;
}
// Update poetry slam status and free visitor seats
async function updatePoetrySlam(
poetrySlamID,
newStatus,
newFeeVisitorSeats,
req,
errorMessage,
successMessage
) {
const updateValues = {
status_code: newStatus
};
if (newFeeVisitorSeats) {
updateValues.freeVisitorSeats = newFeeVisitorSeats;
}
const result = await UPDATE(
`PoetrySlamService.PoetrySlams${
req.target.name.endsWith('drafts') ? '.drafts' : ''
}`
)
.set(updateValues)
.where({ ID: poetrySlamID });
if (result !== 1) {
console.error('Update Poetry Slam failed');
req.error(httpCodes.internal_server_error, errorMessage.text, [
errorMessage.param
]);
return false;
}
if (successMessage) {
req.info(httpCodes.ok, successMessage.text, [successMessage.param]);
}
return true;
}
// Helper function to convert an object to an array
function convertToArray(x) {
if (!x) {
console.error('Input not defined - Cannot convert to array');
return [];
}
return Array.isArray(x) ? x : [x];
}
// Timezone offset is used to use function toISOString() without changing the returned date value
function subtractDaysFormatRFC3339(date, days = 0) {
const generatedDate = new Date(date);
generatedDate.setTime(generatedDate.getTime() - DATE_DAYS_MULTIPLIER * days); //40 days
generatedDate.setMinutes(
generatedDate.getTimezoneOffset() > 0
? generatedDate.getMinutes() + generatedDate.getTimezoneOffset()
: generatedDate.getMinutes() - generatedDate.getTimezoneOffset()
);
return generatedDate.toISOString().substring(0, 10) + 'T00:00:00.0000000Z';
}
// Create a project in a ERP system
async function createProject(req, srv, ConnectorClass, successText, errorText) {
const connector = await ConnectorClass.createConnectorInstance(req);
if (!connector.isConnected()) {
console.info(errorText);
req.warn(httpCodes.internal_server_error, errorText);
return;
}
const poetrySlamID = req.params[req.params.length - 1].ID;
const poetrySlam = await SELECT.one
.from('PoetrySlamService.PoetrySlams')
.where({ ID: poetrySlamID });
// Allow action for active entity instances only
if (!poetrySlam) {
console.error('Poetry Slam not found');
req.error(httpCodes.bad_request, 'ACTION_CREATE_PROJECT_DRAFT');
return;
}
if (
poetrySlam.projectSystem &&
poetrySlam.projectSystem !== ConnectorClass.ERP_SYSTEM
) {
console.info(errorText);
req.warn(httpCodes.internal_server_error, errorText);
return;
}
const poetrySlamIdentifier = poetrySlam.number;
const poetrySlamTitle = poetrySlam.title;
const poetrySlamDate = poetrySlam.dateTime;
try {
connector.projectDataRecord(
poetrySlamIdentifier,
poetrySlamTitle,
poetrySlamDate
);
// Check and create the project instance
// If the project already exist, then read and update the local project elements in entity poetrySlams
let remoteProject = await connector.getRemoteProjectData(srv);
if (!remoteProject?.projectID) {
remoteProject = await connector.insertRemoteProjectData();
}
if (!remoteProject?.projectID) {
console.info(
'Remote Project ID is not available. PoetrySlam could not be updated.'
);
req.warn(
httpCodes.internal_server_error,
'ACTION_CREATE_PROJECT_FAILED',
[poetrySlamIdentifier]
);
return;
}
// Generate remote Project URL and update the URL
// Update project elements in entity poetrySlams
await UPDATE.entity('PoetrySlamService.PoetrySlams')
.set({
projectID: remoteProject.projectID,
projectObjectID: remoteProject.projectObjectID,
projectSystem: ConnectorClass.ERP_SYSTEM
})
.where({ ID: poetrySlamID });
req.info(httpCodes.ok, successText, [remoteProject.projectID]);
} catch (error) {
// App reacts error tolerant in case of calling the remote service, mostly if the remote service is not available of if the destination is missing
console.error(`ACTION_CREATE_PROJECT_FAILED; ${error}`);
req.warn(httpCodes.internal_server_error, 'ACTION_CREATE_PROJECT_FAILED', [
poetrySlamIdentifier
]);
}
}
// Create a purchase order in a ERP system
async function createPurchaseOrder(
req,
ConnectorClass,
successText,
errorText
) {
const connector = await ConnectorClass.createConnectorInstance(req);
if (!connector.isConnected()) {
console.info(errorText);
req.warn(httpCodes.internal_server_error, errorText);
return;
}
const poetrySlamID = req.params[req.params.length - 1].ID;
const poetrySlam = await SELECT.one
.from('PoetrySlamService.PoetrySlams')
.where({ ID: poetrySlamID });
// Allow action for active entity instances only
if (!poetrySlam) {
console.error('Poetry Slam not found');
req.error(httpCodes.bad_request, 'ACTION_CREATE_PURCHASE_ORDER_DRAFT');
return;
}
if (
poetrySlam.purchaseOrderSystem &&
poetrySlam.purchaseOrderSystem !== ConnectorClass.ERP_SYSTEM
) {
console.info(errorText);
req.warn(httpCodes.internal_server_error, errorText);
return;
}
// With purchase order the read is not possible. It is always a create. Remove the purchase order first
if (poetrySlam.purchaseOrderID) {
console.error('Purchase Order ID given. Read not possible.');
req.error(httpCodes.internal_server_error, errorText);
return;
}
const poetrySlamIdentifier = poetrySlam.number;
const poetrySlamDescription = poetrySlam.description;
const poetrySlamTitle = poetrySlam.title;
const poetrySlamDate = poetrySlam.dateTime;
const poetrySlamMaxVisitorsNumber = poetrySlam.maxVisitorsNumber;
const poetrySlamsVisitorsFeeAmount = poetrySlam.visitorsFeeAmount;
try {
connector.purchaseOrderDataRecord(
poetrySlamIdentifier,
poetrySlamTitle,
poetrySlamDescription,
poetrySlamDate,
poetrySlamMaxVisitorsNumber,
poetrySlamsVisitorsFeeAmount
);
// Create the purchase Order instance
const remotePurchaseOrder = await connector.insertRemotePurchaseOrderData();
if (!remotePurchaseOrder?.purchaseOrderID) {
console.info(
'Remote Purchase Order ID is not available. PoetrySlam could not be updated.'
);
req.warn(
httpCodes.internal_server_error,
'ACTION_CREATE_PURCHASE_ORDER_FAILED',
[poetrySlamIdentifier]
);
return;
}
// Update purchase order elements in entity poetrySlams
await UPDATE.entity('PoetrySlamService.PoetrySlams')
.set({
purchaseOrderID: remotePurchaseOrder.purchaseOrderID,
purchaseOrderObjectID: remotePurchaseOrder.purchaseOrderObjectID,
purchaseOrderSystem: ConnectorClass.ERP_SYSTEM
})
.where({ ID: poetrySlamID });
req.info(httpCodes.ok, successText, [remotePurchaseOrder.purchaseOrderID]);
} catch (error) {
// App reacts error tolerant in case of calling the remote service, mostly if the remote service is not available of if the destination is missing
console.info(`ACTION_CREATE_PURCHASE_ORDER_FAILED; ${error}`);
req.warn(
httpCodes.internal_server_error,
'ACTION_CREATE_PURCHASE_ORDER_FAILED',
[poetrySlamIdentifier]
);
}
}
// Publish constants and functions
module.exports = {
calculatePoetrySlamData,
updatePoetrySlam,
convertToArray,
subtractDaysFormatRFC3339,
createProject,
createPurchaseOrder
};