This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
reuse.js
176 lines (154 loc) · 6.14 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
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
"use strict";
// Include SAP Cloud SDK reuse functions
const { getDestination, retrieveJwt } = require("@sap-cloud-sdk/connectivity");
// ----------------------------------------------------------------------------
// 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);
}
*/
}
// Reuse function to get the ERP URL
async function getDestinationURL(req, destinationName) {
let destinationURL;
try {
// Read the destination details using the SAP Cloud SDK reusable getDestination function:
// The JWT-token contains the subaccount information, such that the function works for single tenant as well as for multi-tenant apps:
// - Single tenant: Get destination from the subaccount that hosts the app.
// - Multi tenant: Get destination from subscriber subaccount.
const destination = await getDestination({ destinationName: destinationName, jwt: retrieveJwt(req) });
if(destination){
console.log("Get ERP destination URL: " + destination.url);
destinationURL = destination.url;
}
else{
// No destination found
console.log("Get ERP destination URL: " + destinationName + " not found");
}
} catch (error) {
// App reacts error tolerant if the destination cannot be retrieved
console.log("GET_DESTINATION" + "; " + error);
}
return destinationURL;
}
// Reuse function to check if BTP destination exists
async function checkDestination(req, destinationName) {
try {
// Check if the destination exist using the SAP Cloud SDK reusable getDestination function:
// The JWT-token contains the subaccount information, such that the function works for single tenant as well as for multi-tenant apps:
// - Single tenant: Get destination from the subaccount that hosts the app.
// - Multi tenant: Get destination from subscriber subaccount.
const destination = await getDestination({ destinationName: destinationName, jwt: retrieveJwt(req) });
if(destination){
console.log("Check ERP destination: " + destinationName + " found");
return true;
}
else{
console.log("Check ERP destination: " + destinationName + " not found");
return false;
}
} catch (error) {
// App reacts error tolerant if the destination is missing
console.log("CHECK_DESTINATION" + "; " + error);
}
}
// Reuse function to get the ERP Name
async function getDestinationDescription(req, destinationName) {
let destinationDescription;
try {
// Read the destination details using the SAP Cloud SDK reusable getDestinationDescription function:
// The JWT-token contains the subaccount information, such that the function works for single tenant as well as for multi-tenant apps:
// - Single tenant: Get destination from the subaccount that hosts the app.
// - Multi tenant: Get destination from subscriber subaccount.
const destination = await getDestination({ destinationName: destinationName, jwt: retrieveJwt(req) });
if(destination){
for(var originalProperty in destination.originalProperties){
if (originalProperty == "Description"){
destinationDescription = destination.originalProperties[originalProperty];
break;
}
}
}
} catch (error) {
// App reacts error tolerant if the destination is missing
console.log("GET_DESTINATION_DESCRIPTION" + "; " + error);
}
return destinationDescription;
}
// Publish constants and functions
module.exports = {
color,
authorReadingStatusCode,
participantStatusCode,
validateEmail,
validatePhone,
emitAuthorReadingEvent,
getDestinationURL,
checkDestination,
getDestinationDescription
};