diff --git a/package-lock.json b/package-lock.json index 9719cfc..c15142f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2483,7 +2483,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -2504,12 +2505,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2524,17 +2527,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -2651,7 +2657,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -2663,6 +2670,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2677,6 +2685,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2684,12 +2693,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2708,6 +2719,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2788,7 +2800,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2800,6 +2813,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2885,7 +2899,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -2921,6 +2936,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2940,6 +2956,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2983,12 +3000,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/server.js b/server.js index 7ba16d7..5c679b0 100644 --- a/server.js +++ b/server.js @@ -126,11 +126,60 @@ app.put('/api/v1/clients/:id', (req, res) => { const client = clients.find(client => client.id === id); /* ---------- Update code below ----------*/ + // needs to solve this + + if ((validatePriority(priority).valid == true) && (priority!=undefined)){ + //reorder priority in clients with different statuses + const clashedClient = db.prepare("SELECT * FROM clients WHERE status = ? AND priority = ? ").get( + status, + priority + ); + client.priority = priority; + client.status = status; + + // the database has the same priority and status + const priorityClash = clashedClient != undefined; + if (priorityClash) { + //extract all clients with priority equal to or greater than new client + const clientSwimLane = db.prepare("SELECT * FROM clients WHERE status = ? AND priority >= ? ORDER BY 'prioriity'").all( + status, + priority + ); + const updatePriority = db.prepare("UPDATE clients SET priority = ? WHERE id = ?"); + //add 1 to priority of all clients after (and including) the clashing client + for (let i = 0; i < clientSwimLane.length; i++) { + updatePriority.run( + clientSwimLane[i].priority + 1, //increase priority by 1 + clientSwimLane[i].id) + } + } + //update cient priority and status (if changed) + db.prepare("UPDATE clients SET status = ?, priority = ? WHERE id = ?").run( + status, + priority, + id + ); + } else if (status == "complete"){ + //insert the client as lowest priority (biggest number) with status complete + let clientPriority = newLowestPriority(); + client.priority = newLowestPriority(); + client.status = status; + const insertStmt = db.prepare("INSERT INTO clients VALUES (?, ?, ?, ?, ?) "); + insertStmt.run(client.id, client.name, client.description, status, clientPriority); + } + return res.status(200).send(clients); +}); + +const newLowestPriority = () => { + const complete_swimlane_stmt = db.prepare("SELECT * FROM clients WHERE status = 'complete' ORDER BY 'prioriity'"); + const complete_swimlane = complete_swimlane_stmt.all(); + let leastPriorityClient = complete_swimlane[complete_swimlane.length-1] + let lowestPriority = leastPriorityClient.priority; + return lowestPriority+1; +} - return res.status(200).send(clients); -}); app.listen(3001); console.log('app running on port ', 3001);