Skip to content

Commit

Permalink
Fully implement moving users
Browse files Browse the repository at this point in the history
  • Loading branch information
petschekr committed Apr 17, 2016
1 parent 0ef36e4 commit 8a45314
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
37 changes: 35 additions & 2 deletions World Perspectives/public/components/admin/admin-users.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<iron-ajax id="delete-user-ajax" method="DELETE" url="/admin/user" handle-as="json" on-response="deleteUserResponse" on-error="handleError"></iron-ajax>
<iron-ajax id="delete-users-ajax" method="DELETE" url="/admin/user" handle-as="json" on-response="deleteUsersResponse" on-error="handleError"></iron-ajax>
<iron-ajax id="move-user-search-ajax" url="/admin/move" handle-as="json" on-response="moveUserSearchResponse" on-error="handleError"></iron-ajax>
<iron-ajax id="move-user-ajax" method="POST" handle-as="json" on-response="moveUserResponse" on-error="handleError"></iron-ajax>
<content-section>
<h2>Users</h2>
<iron-ajax id="users-ajax" auto url="/admin/user" handle-as="json" last-response="{{users}}" on-error="handleError" params="{{userListParams}}"></iron-ajax>
Expand Down Expand Up @@ -220,7 +221,7 @@ <h3 style="margin: 5px 0">[[moveUserInfo.name]]</h3>
<div style="overflow: auto;">[[item.title.formatted]]</div>
</div>
<span class="flex"></span>
<paper-dropdown-menu label="Move to..." style="width: 49%; margin-top: -10px;" disabled$="[[item.mandatory]]">
<paper-dropdown-menu class="moveDropdown" label="Move to..." style="width: 49%; margin-top: -10px;" disabled$="[[item.mandatory]]">
<paper-listbox class="dropdown-content" selected$="[[_computeSelectedSession(item, sessions)]]">
<paper-item>Free</paper-item>
<template is="dom-repeat" items="[[sessions]]" as="session">
Expand All @@ -235,9 +236,12 @@ <h3 style="margin: 5px 0">[[moveUserInfo.name]]</h3>
</paper-item>
</template>
<template is="dom-if" if="[[moveUserData.length]]">
<template is="dom-if" if="[[!moveUserInfo.registered]]">
<p>This user hasn't registered yet. Saving changes will mark them as registered.</p>
</template>
<div class="flex-container">
<span class="flex"></span>
<paper-button raised>Save changes</paper-button>
<paper-button raised on-click="moveUser">Save changes</paper-button>
<span class="flex"></span>
</div>
</template>
Expand Down Expand Up @@ -503,6 +507,35 @@ <h2>Delete Users</h2>
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)
Expand Down
70 changes: 69 additions & 1 deletion World Perspectives/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8a45314

Please sign in to comment.