From 8a45314b30373c39615b65708f37476c1965eb1e Mon Sep 17 00:00:00 2001 From: Ryan Petschek Date: Sat, 16 Apr 2016 23:14:50 -0400 Subject: [PATCH] Fully implement moving users --- .../public/components/admin/admin-users.html | 37 +++++++++- World Perspectives/routes/admin.ts | 70 ++++++++++++++++++- 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/World Perspectives/public/components/admin/admin-users.html b/World Perspectives/public/components/admin/admin-users.html index fde09e9..36c6b3d 100644 --- a/World Perspectives/public/components/admin/admin-users.html +++ b/World Perspectives/public/components/admin/admin-users.html @@ -127,6 +127,7 @@ +

Users

@@ -220,7 +221,7 @@

[[moveUserInfo.name]]

[[item.title.formatted]]
- + Free @@ -503,6 +507,35 @@

Delete Users

this.moveUserInfo = response.user; } }, + moveUser: function () { + var dropdowns = document.getElementsByClassName("moveDropdown"); + var slugs = []; + for (var i = 0; i < dropdowns.length; i++) { + slugs.push(dropdowns[i].selectedItem.slug || null); + } + var sendData = { + "username": this.moveUserInfo.username, + "slugs": slugs + }; + + this.$.loading.open(); + this.$["move-user-ajax"].url = "/admin/move/" + sendData.username; + this.$["move-user-ajax"].contentType = "application/json"; + this.$["move-user-ajax"].body = sendData; + this.$["move-user-ajax"].generateRequest(); + }, + moveUserResponse: function () { + this.$.loading.close(); + var response = this.$["move-user-ajax"].lastResponse; + if (!response.success) { + this.$.error.show(response.message); + } + else { + this.$.done.show(response.message); + } + this.$["move-user-search-ajax"].generateRequest(); + this.refreshUserList(); + }, importUsers: function () { var file = this.$$('input[type="file"]'); if (file.files.length !== 1) diff --git a/World Perspectives/routes/admin.ts b/World Perspectives/routes/admin.ts index e66ba08..6ad9281 100644 --- a/World Perspectives/routes/admin.ts +++ b/World Perspectives/routes/admin.ts @@ -398,7 +398,75 @@ router.route("/move/:name") }).catch(common.handleError.bind(response)); }) .post(postParser, function (request, response) { - + var {username, slugs}: { username: string, slugs: string[] } = request.body; + Promise.all([ + db.cypherAsync({ + "query": "MATCH (u:User {username: {username}}) RETURN u.name AS name, u.username AS username, u.registered AS registered", + "params": { + username: username + } + }), + db.cypherAsync({ + "query": "MATCH(item:ScheduleItem {editable: true }) RETURN item.title AS title, item.start AS startTime, item.end AS endTime" + }), + db.cypherAsync({ + "query": "MATCH (u:User {username: {username}})-[r:ATTENDS]->(s:Session) RETURN s.title AS title, s.slug AS slug, s.startTime AS startTime, s.endTime AS endTime, s.type AS type", + "params": { + username: username + } + }), + db.cypherAsync({ + "query": "MATCH (u:User {username: {username}})-[r:PRESENTS]->(s:Session) RETURN s.title AS title, s.slug AS slug, s.startTime AS startTime, s.endTime AS endTime, s.type AS type", + "params": { + username: username + } + }), + db.cypherAsync({ + "query": "MATCH (u:User {username: {username}})-[r:MODERATES]->(s:Session) RETURN s.title AS title, s.slug AS slug, s.startTime AS startTime, s.endTime AS endTime, s.type AS type", + "params": { + username: username + } + }), + ]).spread(function (users: User[], editablePeriods: any[], attends: any[], presents: any[], moderates: any[]) { + if (users.length !== 1) { + response.json({ "success": false, "message": "User not found" }); + return Promise.reject(new IgnoreError()); + } + var user = users[0]; + if (editablePeriods.length !== slugs.length) { + response.json({ "success": false, "message": "Incorrect number of changes for editable periods" }); + return Promise.reject(new IgnoreError()); + } + // Remove from already registered sessions + return db.cypherAsync({ + "query": "MATCH (u:User {username: {username}})-[r:ATTENDS]->(s:Session) SET s.attendees = s.attendees - 1 DELETE r REMOVE u.hasFree, u.timeOfFree", + "params": { + username: username + } + }); + }).then(function () { + return Promise.map(slugs, function (slug) { + // Frees have null as their slug + if (!slug) + return; + return db.cypherAsync({ + "query": ` + MATCH (user:User {username: {username}}) + MATCH (session:Session {slug: {slug}}) + CREATE (user)-[r:ATTENDS]->(session) + SET session.attendees = session.attendees + 1 + SET user.registered = true`, + "params": { + username: username, + slug: slug + } + }); + }); + }).then(function () { + response.json({ "success": true, "message": "User moved successfully" }); + }).catch(IgnoreError, function () { + // Response has already been handled if this error is thrown + }).catch(common.handleError.bind(response)); }); router.route("/session") .get(function (request, response) {