-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventRegistration.js
74 lines (69 loc) · 2.18 KB
/
eventRegistration.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
exports.handler = function(context, event, callback) {
let eventId = event.EventId;
let caller = event.From;
caller = caller.substring(1);
postUrl = context.BASE_URI + '/event-registrations?populate=event';
findUrl = context.BASE_URI + '/event-registrations?filters[$and][0][EventId][$eq]=' + eventId + '&filters[$and][1][PhoneNumber][$contains]=' + caller + '&populate=event';
auth = 'Bearer ' + context.EXT_API_TOK1 + context.EXT_API_TOK2;
var got = require('got');
got(findUrl, {
responseType: 'json',
headers: {
"Authorization": auth
}
})
.then(function(response) {
const resp = response.body;
if (typeof resp.data[0] != "undefined") {
let name = resp.data[0].attributes.event.data.attributes.EventName;
let location = resp.data[0].attributes.event.data.attributes.Location;
let map = resp.data[0].attributes.event.data.attributes.MapLink;
let eventdate = resp.data[0].attributes.event.data.attributes.DateTime;
var found = {
"attributes": {
"Registered": "already",
"event": {
"data": {
"attributes": {
"EventName": name,
"DateTime": eventdate,
"Location": location,
"MapLink": map
}
}
}
}
};
console.log('registration found');
callback(null, found);
} else {
got(postUrl, {
method: "POST",
responseType: 'json',
headers: {
"Content-Type": "application/json",
"Authorization": auth
},
json: {
"data": {
"PhoneNumber": event.From,
"Registered": true,
"event": event.EventId,
"EventId": event.EventId,
"caller": event.Caller
}
},
})
.then(function(response) {
console.log(response.body.data)
callback(null, response.body.data);
})
.catch(function(error) {
callback(error)
});
}
})
.catch(function(error) {
callback(error)
});
};