-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartpill-confirmationpill.js
More file actions
89 lines (71 loc) · 2.35 KB
/
smartpill-confirmationpill.js
File metadata and controls
89 lines (71 loc) · 2.35 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
'use strict'
var aws= require('aws-sdk');
const documentClient = new aws.DynamoDB.DocumentClient({region: 'eu-west-1'});
exports.handler = async (event, context, callback) => {
const event_time = event.time; // Store event time given in a variable = morning, afternoon or night
const event_date = new Date; // Get log time from server clock in UTC time, needs to be corrected in frontend!
console.log(event_date)
const requestId = context.awsRequestId;
console.log('RequestId:' + requestId);
console.log(context);
console.log('Event: \n'+event)
var pills = []; // Store the correct pills
// Call getPills method in order to get the pills from the database
await getPills().then(data => {
data.Items.forEach(function(item) {
if(item[event_time]){
console.log(item);
pills.push(item);
}
})
console.log(pills)
}).catch((err) => {
console.error(err)
});
var event = {};
event.time = event_time;
event.date = event_date.toISOString();
// Patient name -> Hardcoded, needs to be improved for future versions
event.patient_name = 'John Stewart'
event.pills = ''; // Initialize event.pills
pills.forEach(function(pill) {
if(event.pills === ''){
event.pills = pill.pill;
}else{
event.pills = event.pills + `, ${pill.pill}`;
}
});
console.log(event);
await createEvent(requestId, event).then(() => {
callback(null, {
statusCode: 201,
body: JSON.stringify(event),
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
})
}).catch((err) => {
console.error(err)
});
};
function createEvent(requestId, event) {
var params = {
TableName: 'smartpill-history',
Item: {
'id': requestId,
'pills': event.pills,
'date': event.date,
'time': event.time,
'patient_name': event.patient_name
}
}
return documentClient.put(params).promise();
}
function getPills() {
var params = {
TableName: 'smartpill-pills',
Limit: 10
}
return documentClient.scan(params).promise();
}