forked from SAP-samples/sme-partner-reference-application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreuse.js
92 lines (78 loc) · 2.52 KB
/
reuse.js
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
"use strict";
// ----------------------------------------------------------------------------
// Constants
const color = {
grey: 0,
red: 1,
yellow: 2,
green: 3
};
const authorReadingStatusCode = {
inPreparation: 0,
published: 1,
booked: 2,
completed: 3, // not used
blocked: 4,
cancelled: 5 // not used
};
const participantStatusCode = {
inProcess: 1,
confirmed: 2,
cancelled: 3
};
// ----------------------------------------------------------------------------
// Reuse functions
// Reuse function to check the formating of an e-mail address
function validateEmail(email) {
const regexEmail = /^\w+([\\.-]?\w+)*@\w+([\\.-]?\w+)*(\.\w{2,3})+$/;
if (email.match(regexEmail)) {
return true;
} else {
return false;
}
}
// Reuse function to check the formating of an phone number
function validatePhone(phone) {
const regexPhone = /^\+(?:[0-9] ?){6,14}[0-9]$/;
if (phone.match(regexPhone)) {
return true;
} else {
return false;
}
}
// Emit event message to event mesh
async function emitAuthorReadingEvent(req, id, eventMeshTopic) {
// Return updated reading event data
const authorReadings = await SELECT.from("sap.samples.authorreadings.AuthorReadings").where({ ID: id });
const authorReading = authorReadings[0];
try {
const msg = await cds.connect.to("outbound_messaging");
await msg.emit( eventMeshTopic, { authorReading, tenant:req.user.tenant } );
console.log( "====== Inside reuse function to emit Author Reading event messages ======" );
console.log( `Event message emitted for topic ${eventMeshTopic} and author reading ${authorReading.identifier} and Tenant ID:${req.user.tenant} ` );
req.info(200, 'EMIT_MESSAGE_SUCCESS', [authorReading.identifier, eventMeshTopic]);
const text = `Event notification for topic ${eventMeshTopic} emitted successully.`;
let eventMessages;
if (authorReading.eventMeshMessage) {
eventMessages = authorReading.eventMeshMessage + "\n" + text;
} else {
eventMessages = text;
};
console.log( "Event message log: ", eventMessages);
let authorReadingUpdate = await UPDATE("sap.samples.authorreadings.AuthorReadings")
.set({ eventMeshMessage: eventMessages })
.where({ ID: id });
if (authorReadingUpdate) { console.log( "Event message log update successfull") }
} catch (error) {
req.error(error);
}
}
// Publish constants and functions
module.exports = {
color,
authorReadingStatusCode,
participantStatusCode,
validateEmail,
validatePhone,
emitAuthorReadingEvent
};