del user-del also children & links of children

This commit is contained in:
Aran Zaiger 2020-05-26 14:52:36 +03:00
parent 3bfa60db28
commit a73ec272a8
3 changed files with 91 additions and 67 deletions

View file

@ -5,6 +5,7 @@ const Link = use('App/Models/Link');
const IceServer = use('App/Models/IceServer');
const EmailUtils = use('App/Utils/EmailUtils');
const UserChildUtils = use('App/Utils/UserChildUtils');
class AdminApiController {
async getUsers({ response }) {
const users = await User.all();
@ -16,16 +17,25 @@ class AdminApiController {
}
async deleteUser({ request, response }) {
console.log('in delete user')
const { id } = request.params
const user = await User.find(id)
let userLinks = await user.links().fetch();
const links = userLinks.rows
//my links
const links = userLinks.rows;
//children and children links
const childrenLinks = links.filter(l => l.is_parent)
for (const link of childrenLinks) {
await UserChildUtils.deleteChild(link.child_id)
}
const promises = [...links.map(l => (l.delete())), user.delete()];
return await Promise.all(promises);
}
async addStunServer({ request, response }) {}
async addTurnServer({ request, response }) {}

View file

@ -1,20 +1,19 @@
const Link = use('App/Models/Link');
const Child = use('App/Models/Child');
class UserChildUtils {
static async isUserConnectedToChild(user_id, child_id) {
const links = await Link.query().where({user_id, child_id}).fetch();
const links = await Link.query().where({ user_id, child_id }).fetch();
return !!links.rows.length;
}
static async isParentOf(user_id, child_id) {
const links =
await Link.query().where({user_id, child_id, is_parent: true}).fetch();
await Link.query().where({ user_id, child_id, is_parent: true }).fetch();
return !!links.rows.length;
}
static async getChildParents(child_id) {
const links = await Link.query().where({child_id, is_parent: true}).fetch();
const links = await Link.query().where({ child_id, is_parent: true }).fetch();
const parents = await Promise.all(links.rows.map(async l => {
return l.user().fetch();
}));
@ -22,8 +21,8 @@ class UserChildUtils {
}
static async getUserConnections(user_id) {
const links = await Link.query().where({user_id}).fetch();
const connections = await links.rows.reduce(async (_prev, link) => {
const links = await Link.query().where({ user_id }).fetch();
const connections = await links.rows.reduce(async(_prev, link) => {
const prev = await _prev;
const is_parent = link.is_parent;
const child = await link.child().fetch();
@ -44,26 +43,36 @@ class UserChildUtils {
})
}
return prev;
}, Promise.resolve({children: [], connections: []}));
}, Promise.resolve({ children: [], connections: [] }));
return connections;
}
static async getChildConnections(child_id) {
const links =
await Link.query().where({child_id, is_parent: false}).fetch();
await Link.query().where({ child_id, is_parent: false }).fetch();
const connections = await Promise.all(links.rows.map(async l => {
return l.user().fetch();
}));
return connections;
}
static async addConnection(child_id, user_id, is_parent = false) {
const link = await Link.create({child_id, user_id, is_parent});
const link = await Link.create({ child_id, user_id, is_parent });
const user = await link.user().fetch();
const child = await link.child().fetch();
return {
user, child, is_parent
user,
child,
is_parent
}
}
static async deleteChild(child_id) {
const child = await Child.find(child_id)
const childLinks = await child.links().fetch();
const promises = [...childLinks.rows.map(l => (l.delete())), child.delete()];
return await Promise.all(promises);
}
}
module.exports = UserChildUtils;

View file

@ -4,7 +4,7 @@
test
</Modal>
<Modal title="DeleteUser" :isActive="showDeleteUser" @close="showDeleteUser=false; currentUser=null" acceptText="Delete" rejectText="Cancel" @accept="deleteUser(currentUser)">
Are you sure you want to delete {{user.name}}?
Are you sure you want to delete {{currentUser !== null ? currentUser.name: ''}}?
</Modal>
<nav class="level">
<div class="level-left">
@ -55,6 +55,7 @@
</td>
<td>
<button v-if="!user.is_admin" class="button" @click="onDeleteClicked(user)">Delete</button>
<!-- <button v-if="!user.is_admin" class="button" @click="onEditClicked(user)">Edit</button> -->
</td>
</tr>
</table>
@ -85,6 +86,10 @@ export default {
this.showDeleteUser = true;
this.currentUser = user;
},
// onEditClicked(user){
// //this.showEditUser = true;
// //this.currentUser = user;
// },
...mapActions(["getUsers"])
},
async created() {