Skip to content

Instantly share code, notes, and snippets.

@ktutnik
Last active June 6, 2019 01:35
Show Gist options
  • Select an option

  • Save ktutnik/74623d445c39f50e4171c27103182b74 to your computer and use it in GitHub Desktop.

Select an option

Save ktutnik/74623d445c39f50e4171c27103182b74 to your computer and use it in GitHub Desktop.
export class UsersController {
// --------- other methods removed for clarity
@ownerOrAdmin()
@route.put(":id")
async modify(id: number, data: User, @bind.user() user:LoginUser) {
const audit = <Audit>{
userId: user.userId,
action: "Modify",
resource: "Users",
data: JSON.stringify([id, <User>{ ...data, password: "*****", email: "*****" }])
}
try {
const password = await bcrypt.hash(data.password, 10)
const result = await db("User").update({ ...data, password }).where({ id })
await db("Audit").insert(<Audit>{ ...audit, status: "Success" })
return result;
}
catch (e) {
await db("Audit").insert(<Audit>{ ...audit, status: "Error" })
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment