-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
349 lines (298 loc) · 8.06 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const cors = require("cors");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(cors());
// parsing application/json
app.use(bodyParser.json());
// parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
const port = process.env.APP_PORT || 3000;
app.listen(port, function () {
console.log("Server is running on port " + port + "...");
console.log(`Open http://localhost:${port} in browser`);
});
const Datastore = require('nedb-promises');
const tasks = new Datastore();
tasks.insert(require("./tasks.json"));
const links = new Datastore();
links.insert(require("./links.json"));
const resources = new Datastore();
resources.insert(require("./resources.json"));
const categories = new Datastore();
categories.insert(require("./categories.json"));
const assignments = new Datastore();
assignments.insert(require("./assignments.json"));
// client side expects obj.id while DB provides obj._id
function fixID(a){
a.id = a._id;
delete a._id;
return a;
}
const taskAttributes = {
start_date: 1, end_date: 1, text: 1, progress:2, duration: 1, parent:1, details:1, opened:2, type:1, position:2
};
function safeTaskAttributes(update, obj){
for (const key in obj){
const type = taskAttributes[key];
if (type === 1) update[key] = obj[key];
else if (type === 2) update[key] = obj[key]*1;
}
return update;
}
const linkAttributes= {
target:1, source:1, type:2
};
function safeLinkAttributes(update, obj){
for (const key in obj){
const type = linkAttributes[key];
if (type === 1) update[key] = obj[key];
else if (type === 2) update[key] = obj[key]*1;
}
return update;
}
const assignmentAttributes= {
task:1, resource:1, value:2
};
function safeAssignmentAttributes(update, obj){
for (const key in obj){
const type = assignmentAttributes[key];
if (type === 1) update[key] = obj[key];
else if (type === 2) update[key] = obj[key]*1;
}
return update;
}
async function splitTask(req){
const parent = req.params.id;
await tasks.update(
{ _id: parent },
{ $set: { type:"split" }, $max:{ duration: 1, progress: 0 } },
{}
);
let sibling = 0;
const kids = await tasks.find({ parent });
if (!kids.length) {
const data = await tasks.find({ _id: parent });
const origin = data[0];
const added = await tasks.insert({
type: "task",
parent,
text: origin.text,
start_date: origin.start_date,
duration: origin.duration || 1,
progress: origin.progress || 0,
opened: origin.opened,
details: origin.details
});
sibling = fixID(added).id;
}
const added = await tasks.insert(safeTaskAttributes({}, req.body));
return { id: fixID(added).id, sibling };
}
async function deleteTask(id){
const data = await tasks.find({ parent:id });
await Promise.all(data.map(a => deleteTask(a._id)));
await links.remove({ $or: [
{ source: id },
{ target: id }
]},{
multi:true
});
await assignments.remove({ task: id },{
multi:true
});
await tasks.remove({ _id: id });
}
async function getPosition(parent){
const data = await tasks.find({ parent }).sort({ position: -1 }).limit(1);
return data[0].position + 1;
}
async function setPosition(id, mode, parent){
if (mode === "last") {
const relatedPosition = await getPosition(parent);
await tasks.update({ _id: id }, { position: relatedPosition });
} else if (mode === "first" || mode === "") {
const relatedPosition = await getPosition(parent);
await tasks.update({ $and:[ { parent: parent }, { _id: { $ne: id } } ]}, { $set:{ position: relatedPosition } });
} else throw("not supported position mode");
}
async function sendMoveQuery(id, mode, target, parent){
const data = await tasks.find({ _id: id });
const basePosition = data[0].position;
const baseParent = data[0].parent;
let relatedParent = parent, relatedPosition = 0;
if (relatedParent == -1) relatedParent = baseParent;
if (mode === "before" || mode === "after") {
const tdata = await tasks.find({ _id: target });
relatedParent = tdata[0].parent;
relatedPosition = tdata[0].position;
if (mode === "after") relatedPosition += 1;
} else if (mode === "last") {
relatedPosition = await getPosition(parent);
}
// source item removing may affect target index
if (relatedParent == baseParent && (mode === "last" || basePosition < relatedPosition)) {
relatedPosition -= 1
}
// already in place
if (relatedParent == baseParent && relatedPosition == basePosition) {
return;
}
// removing from source order
await tasks.update({ $and:[ { position: { $gt: basePosition } }, { parent: baseParent } ] }, { $inc: { position: -1 } }, { multi: true } );
// correct target order
await tasks.update({ $and:[ { position: { $gte: relatedPosition } }, { parent: relatedParent } ] }, { $inc: { position: 1 } }, { multi: true });
// adding at target position
await tasks.update({ _id: id }, { $set: { position: relatedPosition, parent: relatedParent } });
}
app.get("/tasks", async (req, res, next) => {
try {
const data = await tasks.find({}).sort({ position: 1, parent: 1 });
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.put("/tasks/:id", async (req, res, next) => {
try {
await tasks.update(
{ _id: req.params.id },
{ $set: safeTaskAttributes({}, req.body) },
{}
);
res.send({});
} catch (err) {
next(err);
}
});
app.put("/tasks/:id/split", async (req, res, next) => {
try {
const { sibling, id } = await splitTask(req);
const response = { id };
if (sibling) response.sibling = sibling;
res.send(response);
} catch (err) {
next(err);
}
});
app.delete("/tasks/:id", async (req, res, next) => {
try {
await deleteTask(req.params.id);
res.send({});
} catch(err){
next(err);
}
});
app.post("/tasks", async (req, res, next) => {
try {
const data = await tasks.insert(safeTaskAttributes({
progress:0, parent:0, text:"New Task"
}, req.body));
const mode = req.body.mode;
const parent = req.body.parent;
setPosition(data.id, mode, parent);
res.send({ id: fixID(data).id });
} catch(err){
next(err);
}
});
app.put("/tasks/:id/position", async (req, res, next) => {
try {
const id = req.params.id;
const target = req.body.target;
const parent = req.body.parent;
const mode = req.body.mode;
sendMoveQuery(id, mode, target, parent)
res.send({ id });
} catch (err) {
next(err);
}
});
app.get("/links", async (req, res, next) => {
try {
const data = await links.find({});
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.put("/links/:id", async (req, res, next) => {
try {
await links.update(
{ _id: req.params.id },
{ $set: safeLinkAttributes({}, req.body) },
{}
);
res.send({});
} catch (err) {
next(err);
}
});
app.delete("/links/:id", async (req, res, next) => {
try {
await links.remove({ _id: req.params.id });
res.send({});
} catch (err) {
next(err);
}
});
app.post("/links", async (req, res, next) => {
try {
const data = await links.insert(safeLinkAttributes({}, req.body));
res.send({ id: fixID(data).id });
} catch (err) {
next(err);
}
});
app.get("/resources", async (req, res, next) => {
try {
const data = await resources.find({});
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.get("/categories", async (req, res, next) => {
try {
const data = await categories.find({});
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.get("/assignments", async (req, res, next) => {
try {
const data = await assignments.find({});
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.put("/assignments/:id", async (req, res, next) => {
try {
await assignments.update(
{ _id: req.params.id },
{ $set: safeAssignmentAttributes({}, req.body) },
{}
);
res.send({});
} catch (err) {
next(err);
}
});
app.delete("/assignments/:id", async (req, res, next) => {
try {
await assignments.remove({ _id: req.params.id });
res.send({});
} catch (err) {
next(err);
}
});
app.post("/assignments", async (req, res, next) => {
try {
const data = await assignments.insert(safeAssignmentAttributes({}, req.body));
res.send({ id: fixID(data).id });
} catch (err) {
next(err);
}
});