This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
seepur/app/Utils/UserUtils.js
aranzaiger 8931f4bd86 - Adding adonis testing infrastracture
- example test and simple delete usr test added
- extract delete user logics from controller
2020-05-31 16:46:03 +03:00

25 lines
No EOL
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;