forked from paullessing/demo-env-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
119 lines (104 loc) · 2.89 KB
/
database.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
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
const tableName = exports.tableName = 'demo-env-slackbot.environments';
// exports.getItem = function getItem(id, done) {
// const params = {
// TableName: tableName,
// Key: {
// id: id
// }
// };
//
// docClient.get(params, function(err, data) {
// if (err) {
// console.log(err);
// done(err);
// } else {
// console.log(data);
// done(null, data.Item);
// }
// });
// }
exports.getAllEnvironments = function getAllEnvironments() {
return new Promise((resolve, reject) => {
const params = {
TableName: tableName
};
docClient.scan(params, onScan);
let allItems = [];
function onScan(err, data) {
if (err) {
console.error('Unable to scan the table. Error JSON:', JSON.stringify(err, null, 2));
reject(err);
} else {
// print all the movies
console.log('Scan succeeded.');
allItems = allItems.concat(data.Items);
// continue scanning if we have more movies, because
// scan can retrieve a maximum of 1MB of data
if (typeof data.LastEvaluatedKey !== 'undefined') {
console.log('Scanning for more...');
params.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.scan(params, onScan);
} else {
resolve(allItems);
}
}
}
});
};
/**
* Mark an environment as busy.
* @param username
* @param environment
* @param time Can be empty to un-mark
* @param claimDurationHours {number} optional
* @returns {Promise}
*/
exports.markEnvironment = function markEnvironment(username, environment, time, claimDurationHours) {
const item = {
environment,
username,
time: time.toString(),
claimDurationHours
};
return new Promise((resolve, reject) => {
const itemToInsert = {
'TableName': tableName,
'Item' : item
};
console.log('Putting:', itemToInsert);
docClient.put(itemToInsert, (error) => {
console.log('Finished inserting' + (error ? ' with error' : ''), error);
if (error) {
reject(error);
} else {
resolve();
}
});
})
};
exports.getEnvironment = function getEnvironment(environment) {
return new Promise((resolve, reject) => {
const itemToFetch = {
'TableName': tableName,
'Key': { environment }
};
console.log('Getting:', itemToFetch);
docClient.get(itemToFetch, (error, data) => {
console.log('Finished getting' + (error ? ' with error' : ''), error);
if (error) {
reject(error);
} else {
const item = data.Item;
const result = {
environment: item.environment,
username: item.username,
time: new Date(item.time),
claimDurationHours: item.claimDurationHours
};
resolve(result);
}
});
})
};