diff --git a/app/Controllers/Http/AdminApiController.js b/app/Controllers/Http/AdminApiController.js index abef3e0..9cbadfc 100644 --- a/app/Controllers/Http/AdminApiController.js +++ b/app/Controllers/Http/AdminApiController.js @@ -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 }) {} diff --git a/app/Utils/UserChildUtils.js b/app/Utils/UserChildUtils.js index 3186d1d..2899c07 100644 --- a/app/Utils/UserChildUtils.js +++ b/app/Utils/UserChildUtils.js @@ -1,69 +1,78 @@ - - 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(); - 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(); - return !!links.rows.length; - } - - static async getChildParents(child_id) { - 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(); - })); - return parents; - } - - static async getUserConnections(user_id) { - 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(); - if (is_parent) { - const parents = await UserChildUtils.getChildParents(child.id); - const nonSameUserParents = parents.filter(p => p.id != user_id); - prev.children.push({ - ...child.toJSON(), - connections: [ - ...await UserChildUtils.getChildConnections(child.id), - ...nonSameUserParents - ] - }) - } else { - prev.connections.push({ - ...child.toJSON(), - connections: [...await UserChildUtils.getChildParents(child.id)] - }) - } - return prev; - }, Promise.resolve({children: [], connections: []})); - return connections; - } - - static async getChildConnections(child_id) { - const links = - 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 user = await link.user().fetch(); - const child = await link.child().fetch(); - return { - user, child, is_parent + static async isUserConnectedToChild(user_id, child_id) { + 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(); + return !!links.rows.length; + } + + static async getChildParents(child_id) { + 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(); + })); + return parents; + } + + static async getUserConnections(user_id) { + 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(); + if (is_parent) { + const parents = await UserChildUtils.getChildParents(child.id); + const nonSameUserParents = parents.filter(p => p.id != user_id); + prev.children.push({ + ...child.toJSON(), + connections: [ + ...await UserChildUtils.getChildConnections(child.id), + ...nonSameUserParents + ] + }) + } else { + prev.connections.push({ + ...child.toJSON(), + connections: [...await UserChildUtils.getChildParents(child.id)] + }) + } + return prev; + }, Promise.resolve({ children: [], connections: [] })); + return connections; + } + + static async getChildConnections(child_id) { + const links = + 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 user = await link.user().fetch(); + const child = await link.child().fetch(); + return { + 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; \ No newline at end of file diff --git a/resources/scripts/applications/admin/views/home.vue b/resources/scripts/applications/admin/views/home.vue index f9336c6..1036304 100644 --- a/resources/scripts/applications/admin/views/home.vue +++ b/resources/scripts/applications/admin/views/home.vue @@ -4,7 +4,7 @@ test - Are you sure you want to delete {{user.name}}? + Are you sure you want to delete {{currentUser !== null ? currentUser.name: ''}}? @@ -55,6 +55,7 @@ Delete + @@ -85,6 +86,10 @@ export default { this.showDeleteUser = true; this.currentUser = user; }, + // onEditClicked(user){ + // //this.showEditUser = true; + // //this.currentUser = user; + // }, ...mapActions(["getUsers"]) }, async created() {