seepur/app/Utils/UserUtils.js
Aran Zaiger 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
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;