Skip to content

Instantly share code, notes, and snippets.

@henriqueweiand
Created November 23, 2019 12:05
Show Gist options
  • Save henriqueweiand/58e09a46caa91ddf6896b06e84eb77a1 to your computer and use it in GitHub Desktop.
Save henriqueweiand/58e09a46caa91ddf6896b06e84eb77a1 to your computer and use it in GitHub Desktop.
const trx = await Database.beginTransaction();
const user = await User.findOrFail(id);
if (!user) {
throw new JsonException('Usuário não localizado', 400);
}
try {
await user.merge(data);
if (roles && roles.length && isAdmin) {
await user.roles().sync(roles, trx);
}
if (permissions && permissions.length && isAdmin) {
await user.permissions().sync(permissions, trx);
}
await user.save(trx);
await trx.commit();
await user.loadMany(['roles', 'permissions', 'address']);
return user;
} catch (err) {
await trx.rollback();
throw new JsonException(err.message, err.status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment