This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathindex.js
264 lines (259 loc) · 7.72 KB
/
index.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const fs = require('fs');
const docopt = require('docopt');
const moment = require('moment');
const AWS = require('aws-sdk');
const db = new AWS.DynamoDB({
region: 'us-east-1'
});
const cli = fs.readFileSync('./cli.txt', {encoding: 'utf8'});
const input = docopt.docopt(cli, {
version: '1.0',
argv: process.argv.splice(2)
});
const getValue = (attribute, type) => {
if (attribute === undefined) {
return null;
}
return attribute[type];
};
const mapTaskItem = (item) => {
return {
tid: item.tid.N,
description: item.description.S,
created: item.created.N,
due: getValue(item.due, 'N'),
category: getValue(item.category, 'S'),
completed: getValue(item.completed, 'N')
};
};
const mapUserItem = (item) => {
return {
uid: item.uid.S,
email: item.email.S,
phone: item.phone.S
};
};
if (input['user-add'] === true) {
const params = {
Item: {
uid: {S: input['<uid>']},
email: {S: input['<email>']},
phone: {S: input['<phone>']}
},
TableName: 'todo-user',
ConditionExpression: 'attribute_not_exists(uid)'
};
db.putItem(params, (err) => {
if (err) {
console.error('error', err);
} else {
console.log('user added with uid ' + input['<uid>']);
}
});
} else if (input['user-rm'] === true) {
const params = {
Key: {
uid: {S: input['<uid>']}
},
TableName: 'todo-user'
};
db.deleteItem(params, (err) => {
if (err) {
console.error('error', err);
} else {
console.log('user removed with uid ' + input['<uid>']);
}
});
} else if (input['user-ls'] === true) {
const params = {
TableName: 'todo-user',
Limit: input['--limit']
};
if (input['--next'] !== null) {
params.ExclusiveStartKey = {
uid: {S: input['--next']}
};
}
db.scan(params, (err, data) => {
if (err) {
console.error('error', err);
} else {
console.log('users', data.Items.map(mapUserItem));
if (data.LastEvaluatedKey !== undefined) {
console.log('more users available with --next=' + data.LastEvaluatedKey.uid.S);
}
}
});
} else if (input['user'] === true) {
const params = {
Key: {
uid: {S: input['<uid>']}
},
TableName: 'todo-user'
};
db.getItem(params, (err, data) => {
if (err) {
console.error('error', err);
} else {
if (data.Item) {
console.log('user with uid ' + input['<uid>'], mapUserItem(data.Item));
} else {
console.error('user with uid ' + input['<uid>'] + ' not found');
}
}
});
} else if (input['task-add'] === true) {
const tid = Date.now();
const params = {
Item: {
uid: {S: input['<uid>']},
tid: {N: tid.toString()},
description: {S: input['<description>']},
created: {N: moment(tid).format('YYYYMMDD')}
},
TableName: 'todo-task',
ConditionExpression: 'attribute_not_exists(uid) and attribute_not_exists(tid)'
};
if (input['--dueat'] !== null) {
params.Item.due = {N: input['--dueat']};
}
if (input['<category>'] !== null) {
params.Item.category = {S: input['<category>']};
}
db.putItem(params, (err) => {
if (err) {
console.error('error', err);
} else {
console.log('task added with tid ' + tid);
}
});
} else if (input['task-rm'] === true) {
const params = {
Key: {
uid: {S: input['<uid>']},
tid: {N: input['<tid>']}
},
TableName: 'todo-task'
};
db.deleteItem(params, (err) => {
if (err) {
console.error('error', err);
} else {
console.log('task removed with tid ' + input['<tid>']);
}
});
} else if (input['task-ls'] === true) {
const yyyymmdd = moment().format('YYYYMMDD');
const params = {
KeyConditionExpression: 'uid = :uid',
ExpressionAttributeValues: {
':uid': {S: input['<uid>']}
},
TableName: 'todo-task',
Limit: input['--limit']
};
if (input['--next'] !== null) {
params.KeyConditionExpression += ' AND tid > :next';
params.ExpressionAttributeValues[':next'] = {N: input['--next']};
}
if (input['--overdue'] === true) {
params.FilterExpression = 'due < :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: yyyymmdd};
} else if (input['--due'] === true) {
params.FilterExpression = 'due = :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: yyyymmdd};
} else if (input['--withoutdue'] === true) {
params.FilterExpression = 'attribute_not_exists(due)';
} else if (input['--futuredue'] === true) {
params.FilterExpression = 'due > :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: yyyymmdd};
} else if (input['--dueafter'] !== null) {
params.FilterExpression = 'due > :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: input['--dueafter']};
} else if (input['--duebefore'] !== null) {
params.FilterExpression = 'due < :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: input['--duebefore']};
}
if (input['<category>'] !== null) {
if (params.FilterExpression === undefined) {
params.FilterExpression = '';
} else {
params.FilterExpression += ' AND ';
}
params.FilterExpression += 'category = :category';
params.ExpressionAttributeValues[':category'] = {S: input['<category>']};
}
db.query(params, (err, data) => {
if (err) {
console.error('error', err);
} else {
console.log('tasks', data.Items.map(mapTaskItem));
if (data.LastEvaluatedKey !== undefined) {
console.log('more tasks available with --next=' + data.LastEvaluatedKey.tid.N);
}
}
});
} else if (input['task-la'] === true) {
const yyyymmdd = moment().format('YYYYMMDD');
const params = {
KeyConditionExpression: 'category = :category',
ExpressionAttributeValues: {
':category': {S: input['<category>']}
},
TableName: 'todo-task',
IndexName: 'category-index',
Limit: input['--limit']
};
if (input['--next'] !== null) {
params.KeyConditionExpression += ' AND tid > :next';
params.ExpressionAttributeValues[':next'] = {N: input['--next']};
}
if (input['--overdue'] === true) {
params.FilterExpression = 'due < :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: yyyymmdd};
} else if (input['--due'] === true) {
params.FilterExpression = 'due = :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: yyyymmdd};
} else if (input['--withoutdue'] === true) {
params.FilterExpression = 'attribute_not_exists(due)';
} else if (input['--futuredue'] === true) {
params.FilterExpression = 'due > :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: yyyymmdd};
} else if (input['--dueafter'] !== null) {
params.FilterExpression = 'due > :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: input['--dueafter']};
} else if (input['--duebefore'] !== null) {
params.FilterExpression = 'due < :yyyymmdd';
params.ExpressionAttributeValues[':yyyymmdd'] = {N: input['--duebefore']};
}
db.query(params, (err, data) => {
if (err) {
console.error('error', err);
} else {
console.log('tasks', data.Items.map(mapTaskItem));
if (data.LastEvaluatedKey !== undefined) {
console.log('more tasks available with --next=' + data.LastEvaluatedKey.tid.N);
}
}
});
} else if (input['task-done'] === true) {
const yyyymmdd = moment().format('YYYYMMDD');
const params = {
Key: {
uid: {S: input['<uid>']},
tid: {N: input['<tid>']}
},
UpdateExpression: 'SET completed = :yyyymmdd',
ExpressionAttributeValues: {
':yyyymmdd': {N: yyyymmdd}
},
TableName: 'todo-task'
};
db.updateItem(params, (err) => {
if (err) {
console.error('error', err);
} else {
console.log('task completed with tid ' + input['<tid>']);
}
});
}