25 lines
679 B
JavaScript
25 lines
679 B
JavaScript
|
const User = use('App/Models/User');
|
||
|
const UserChildUtils = use('App/Utils/UserChildUtils');
|
||
|
|
||
|
class UserUtils {
|
||
|
|
||
|
static async deleteUser(user_id) {
|
||
|
const user = await User.find(user_id)
|
||
|
let userLinks = await user.links().fetch();
|
||
|
|
||
|
//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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = UserUtils;
|