-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_helper.js
174 lines (160 loc) · 5.22 KB
/
db_helper.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
'use strict';
module.change_code = 1;
var _ = require('lodash');
var moment = require('moment');
var EVENTS_TABLE_NAME = 'BookedEvents';
// var dynasty = require('dynasty')({});
// !IMPORTANT! Before uploading to the Amazon Devs portal comment
// from line 11 to 18 and decomment line 7.
var localUrl = 'http://localhost:8000';
var localCredentials = {
region: 'us-east-1',
accessKeyId: 'fake',
secretAccessKey: 'fake'
};
var localDynasty = require('dynasty')(localCredentials, localUrl);
var dynasty = localDynasty;
function DbHelper(bookedEventsTable) {
if (typeof bookedEventsTable == 'undefined') {
this.bookedEventsTable = function() {
return dynasty.table(EVENTS_TABLE_NAME);
};
} else {
this.bookedEventsTable = bookedEventsTable
}
}
var overlap = function(r1, r2) {
var start1 = moment(r1.Date + " " + r1.StartTime);
var start2 = moment(r2.Date + " " + r2.StartTime);
var duration1 = moment.duration(r1.Duration, moment.ISO_8601).asMinutes();
var duration2 = moment.duration(r2.Duration, moment.ISO_8601).asMinutes();
var end1 = start1.clone().add(duration1, 'minutes');
var end2 = start2.clone().add(duration1, 'minutes');
return (Math.max(start1, start2) < Math.min(end1, end2));
};
DbHelper.prototype.createBookedEventsTable = function() {
return dynasty.describe(EVENTS_TABLE_NAME)
.catch(function(error) {
console.log("createBookedEventTable::error: ", error);
return dynasty.create(EVENTS_TABLE_NAME, {
key_schema: {
hash: ['RoomDate', 'string'],
range: ['Name', 'string']
}
});
});
};
DbHelper.prototype.addRecord = function(record) {
var that = this;
return that.bookedEventsTable().findAll(record.RoomDate)
.then(function(dayRecords){
var overlaps = 0;
dayRecords.forEach(function(dayRecord) {
if (overlap(dayRecord, record)) {
overlaps += 1;
}
});
if (overlaps === 0) {
that.bookedEventsTable().insert(record)
.catch(function(error){
console.log(error);
overlaps = -1;
});
}
return overlaps;
})
.catch(function(error){
console.log(error);
});
};
DbHelper.prototype.readRoomDateRecords = function(roomDate) {
var that = this;
return that.bookedEventsTable().findAll(roomDate)
.then(function(records) {
console.log("HERE1");
return records;
})
.catch(function(error){
console.log("HERE2");
console.log(error);
});
};
DbHelper.prototype.readRoomDateRecordsForNow = function(roomDate) {
var that = this;
return that.bookedEventsTable().findAll(roomDate)
.then(function(records) {
var ongoingEvent = records.find(function(record){
var now = moment();
var start = moment(new Date().toISOString().slice(0,10) + " " + record.StartTime);
var duration = moment.duration(record.Duration, moment.ISO_8601).asMinutes();
var end = start.clone().add(duration, 'minutes');
if (start <= now && now <= end) {
return record;
}
});
return ongoingEvent;
})
.catch(function(error){
console.log(error);
});
};
DbHelper.prototype.readRoomDateRecordsForTime = function(roomDate, date2, time) {
var that = this;
return that.bookedEventsTable().findAll(roomDate)
.then(function(records) {
var ongoingEvent = records.find(function(record){
console.log("1", record)
var searchTime = moment(new Date(date2).toISOString().slice(0,10) + " " + time);
var start = moment(new Date(record.Date).toISOString().slice(0,10) + " " + record.StartTime);
var duration = moment.duration(record.Duration, moment.ISO_8601).asMinutes();
var end = start.clone().add(duration, 'minutes');
console.log("2", record)
if (start <= searchTime && searchTime <= end) {
return record;
}
});
console.log("3", ongoingEvent)
return ongoingEvent;
})
.catch(function(error){
console.log("after catch");
console.log(error);
});
};
DbHelper.prototype.deleteRoomDateRecord = function(roomDate, eventName) {
var that = this;
return that.bookedEventsTable().findAll(roomDate)
.then(function(records) {
var deletedEvents = 0;
records.forEach(function(record) {
if (record.Name === eventName) {
deletedEvents += 1;
that.bookedEventsTable().remove({hash: roomDate, range: eventName})
.catch(function(error){
console.log(error);
deletedEvents = -1;
});
}
});
return deletedEvents;
})
.catch(function(error){
console.log(error);
});
};
// Only uncomment the lines below if you need to add some sample bookings to
// DynamoDB Local. To do that, also uncomment the intent at the end of index.js.
// The sample bookings are stored in localSampleRecords.json.
// var fs = require('fs');
// var sampleRecords;
// fs.readFile('apps/book_event/localSampleRecords.json', 'utf8', function (err, data) {
// if (err) throw err;
// sampleRecords = JSON.parse(data);
// });
//
// DbHelper.prototype.addSampleRecords = function() {
// sampleRecords.Items.forEach(function(item) {
// this.addRecord(item);
// });
// };
module.exports = DbHelper;