Guest User

Untitled

a guest
Oct 15th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. module.exports = app => {
  2. const User = app.datasource.models.User
  3. const Errors = require('../../errors/user/pt-br')
  4. const Regex = require('../../helpers/regex')
  5. const Validate = require('../../helpers/validate')
  6. return {
  7. create: (req, res, next) => {
  8. const required = ['name', 'email', 'phone', 'password']
  9. const error = Validate.request(req, required, Errors)
  10. error ? res.status(400).json(error) : next()
  11. },
  12.  
  13. listOne: (req, res, next) => Validate.isNumber(req.params.id, res, next, Errors.idInvalid),
  14.  
  15. update: (req, res, next) => {
  16. const query = {where: req.params, raw: true}
  17. Validate.searchQuery(User, query)
  18. .then(Validate.isEmptyObject(res, Errors.userNotExist))
  19. .then(objectSearch => Validate.isUpload(req.file, objectSearch.avatar, next))
  20. .catch(Validate.reject(res))
  21. },
  22.  
  23. delete: (req, res, next) => Validate.isNumber(req.params.id, res, next, Errors.idInvalid),
  24.  
  25. unique: (req, res, next) => {
  26. const object = Validate.tratmentPhone(req.body.phone, Regex, next)
  27. const query = {
  28. where: {
  29. $or: [
  30. {$and: [{ddd: object.ddd}, {ddi: object.ddi}, {ddd: object.ddd}, {number: object.number}]},
  31. {email: {$eq: req.body.email}}
  32. ]
  33. }
  34. }
  35. Validate.searchQuery(User, query)
  36. .then(Validate.isEmptyObjectNext(res, next, Errors.userExist))
  37. .catch(Validate.reject(res))
  38. },
  39. forgot: (req, res, next) => {
  40. const query = {
  41. where: { email: { $eq: req.body.email } }
  42. }
  43. Validate.searchQuery(User, query)
  44. .then(Validate.isEmptyObjectNext(res, next, Errors.forgotNotExist))
  45. .catch(Validate.reject(res))
  46. },
  47. forgotValidate: (req, res, next) => {
  48. const query = {
  49. where: { forgot: { $eq: req.body.forgot } }
  50. }
  51. Validate.searchQuery(User, query)
  52. .then(Validate.isEmptyObjectNext(res, next, Errors.forgotValidate))
  53. .catch(Validate.reject(res))
  54. },
  55. password: (req, res, next) => {
  56. const required = ['forgot', 'password']
  57. const errors = Validate.request(req, required, Errors)
  58. if (errors) {
  59. res.status(400).json(errors)
  60. } else {
  61. const query = { where: { forgot: { $eq: req.body.forgot } } }
  62. if (req.body.password1 === req.body.password2) {
  63. Validate.searchQuery(User, query)
  64. .then(Validate.isEmptyObjectNext(res, next, Errors.userNotExist))
  65. .catch(Validate.reject(res))
  66. } else {
  67. res.status(400).json(Errors.passwordNotEquals)
  68. }
  69. }
  70. },
  71. resend: (req, res, next) => {
  72. if (isNaN(req.params.id)) {
  73. res.status(400).json([Errors.idInvalid])
  74. } else {
  75. const query = {where: req.params}
  76. Validate.searchQuery(User, query)
  77. .then(Validate.isEmptyObjectNext(res, next, Errors.userNotExist))
  78. .catch(Validate.reject(res))
  79. }
  80. },
  81. activeCode: (req, res, next) => {
  82. const active = parseInt(req.params.active)
  83. if (isNaN(active)) {
  84. res.status(400).json([Errors.activeInvalid])
  85. } else {
  86. const query = { where: { active: { $eq: active } } }
  87. Validate.searchQuery(User, query)
  88. .then(Validate.isEmptyObjectNext(res, next, Errors.activeInvalid))
  89. .catch(Validate.reject(res))
  90. }
  91. }
  92. }
  93. }
Add Comment
Please, Sign In to add comment