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 IceServer = use('App/Models/IceServer');
const EmailUtils = use('App/Utils/EmailUtils'); const EmailUtils = use('App/Utils/EmailUtils');
const UserChildUtils = use('App/Utils/UserChildUtils');
class AdminApiController { class AdminApiController {
async getUsers({ response }) { async getUsers({ response }) {
const users = await User.all(); const users = await User.all();
@ -16,16 +17,25 @@ class AdminApiController {
} }
async deleteUser({ request, response }) { async deleteUser({ request, response }) {
console.log('in delete user')
const { id } = request.params const { id } = request.params
const user = await User.find(id) const user = await User.find(id)
let userLinks = await user.links().fetch(); 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()]; const promises = [...links.map(l => (l.delete())), user.delete()];
return await Promise.all(promises); return await Promise.all(promises);
} }
async addStunServer({ request, response }) {} async addStunServer({ request, response }) {}
async addTurnServer({ request, response }) {} async addTurnServer({ request, response }) {}

View file

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

View file

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