2
1

accounts.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Response } from 'express'
  2. import { AccountModel } from '../../models/account/account'
  3. import * as Bluebird from 'bluebird'
  4. import { MAccountDefault } from '../../typings/models'
  5. function doesAccountIdExist (id: number, res: Response, sendNotFound = true) {
  6. const promise = AccountModel.load(id)
  7. return doesAccountExist(promise, res, sendNotFound)
  8. }
  9. function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
  10. const promise = AccountModel.loadLocalByName(name)
  11. return doesAccountExist(promise, res, sendNotFound)
  12. }
  13. function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
  14. const promise = AccountModel.loadByNameWithHost(nameWithDomain)
  15. return doesAccountExist(promise, res, sendNotFound)
  16. }
  17. async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) {
  18. const account = await p
  19. if (!account) {
  20. if (sendNotFound === true) {
  21. res.status(404)
  22. .send({ error: 'Account not found' })
  23. .end()
  24. }
  25. return false
  26. }
  27. res.locals.account = account
  28. return true
  29. }
  30. // ---------------------------------------------------------------------------
  31. export {
  32. doesAccountIdExist,
  33. doesLocalAccountNameExist,
  34. doesAccountNameWithHostExist,
  35. doesAccountExist
  36. }