Created
November 23, 2019 12:05
-
-
Save henriqueweiand/58e09a46caa91ddf6896b06e84eb77a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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