-
Notifications
You must be signed in to change notification settings - Fork 48
/
func.js
127 lines (107 loc) · 3.2 KB
/
func.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
const fdk=require('@fnproject/fdk');
const process = require('process');
const NoSQLClient = require('oracle-nosqldb').NoSQLClient;
const Region = require('oracle-nosqldb').Region;
const ServiceType = require('oracle-nosqldb').ServiceType;
const url = require('url');
let client;
let lim = 15;
process.on('exit', function(code) {
if (client) {
console.log("\close client on exit");
client.close();
}
return code;
});
fdk.handle(async function(input, ctx){
let ticketNo;
let confNo;
let endPoint;
let sql;
// Reading parameters from standard input for TEST purposes
if (input && input.endPoint)
endPoint = input.endPoint;
if (input && input.ticketNo)
ticketNo = input.ticketNo;
if (input && input.confNo)
confNo = input.confNo;
if (input && input.sql)
sql = input.sql;
// Reading parameters sent by the httpGateway
let hctx = ctx.httpGateway
if (hctx && hctx.requestURL) {
var adr = hctx.requestURL;
var q = url.parse(adr, true);
endPoint = q.pathname.split('/')[2]
ticketNo = q.query.ticketNo
confNo = q.query.confNo
}
if ( !client ) {
client = createClientResource();
}
let rows;
if (endPoint == "getBagInfoByTicketNumber") {
rows = getBagInfoByTicketNumber(ticketNo);
}
else if (endPoint == "getPassengersAffectedByFlight") {
const statementQry1 = `SELECT d.ticketNo as ticketNo, d.fullName as fullName, d.contactPhone as contactInfo, size(d.bagInfo) as numBags FROM demo d WHERE d.bagInfo.flightLegs.flightNo =ANY 'BM715'`
rows = {'message': endPoint + " under construction." , "sql" : statementQry1, "index" : "bagInfo.flightLegs.flightNo", "endPoint":"executeSQL"}
}
else if ((endPoint == "getByConfirmationCode") && (confNo)) {
const statementQryConfCode = `SELECT * FROM demo WHERE confNo = "${confNo}"`;
rows = executeQuery(statementQryConfCode);
}
else if ((endPoint == "getByConfirmationCode") && !(confNo)) {
rows = {'message': endPoint + " missing parameter confNo"}
hctx.statusCode=500;
}
else if ((endPoint == "executeSQL") && (sql)) {
rows = executeQuery(sql);
}
else {
rows = {'message': endPoint + " not managed"}
hctx.statusCode=500;
}
//if (client) {
// client.close();
//}
return rows;
return process.version;
}, {});
async function getBagInfoByTicketNumber (ticketNo) {
const statementQry1 = `SELECT * FROM demo LIMIT ${lim}`;
const statementQry2 = `SELECT * FROM demo WHERE ticketNo = "${ticketNo}"`;
let result;
if (ticketNo)
result = executeQuery(statementQry2);
else
result = executeQuery(statementQry1);
return result;
}
async function executeQuery (statement) {
const rows = [];
let cnt ;
let res;
try {
do {
res = await client.query(statement, { continuationKey:cnt});
rows.push.apply(rows, res.rows);
cnt = res.continuationKey;
} while(res.continuationKey != null);
}
catch(err) {
return err;
}
return rows;
}
function createClientResource() {
return new NoSQLClient({
region: process.env.NOSQL_REGION,
compartment:process.env.NOSQL_COMPARTMENT_ID,
auth: {
iam: {
useResourcePrincipal: true
}
}
});
}