Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module.exports = app => {
- const User = app.datasource.models.User
- const Errors = require('../../errors/user/pt-br')
- const Regex = require('../../helpers/regex')
- const Validate = require('../../helpers/validate')
- return {
- create: (req, res, next) => {
- const required = ['name', 'email', 'phone', 'password']
- const error = Validate.request(req, required, Errors)
- error ? res.status(400).json(error) : next()
- },
- listOne: (req, res, next) => Validate.isNumber(req.params.id, res, next, Errors.idInvalid),
- update: (req, res, next) => {
- const query = {where: req.params, raw: true}
- Validate.searchQuery(User, query)
- .then(Validate.isEmptyObject(res, Errors.userNotExist))
- .then(objectSearch => Validate.isUpload(req.file, objectSearch.avatar, next))
- .catch(Validate.reject(res))
- },
- delete: (req, res, next) => Validate.isNumber(req.params.id, res, next, Errors.idInvalid),
- unique: (req, res, next) => {
- const object = Validate.tratmentPhone(req.body.phone, Regex, next)
- const query = {
- where: {
- $or: [
- {$and: [{ddd: object.ddd}, {ddi: object.ddi}, {ddd: object.ddd}, {number: object.number}]},
- {email: {$eq: req.body.email}}
- ]
- }
- }
- Validate.searchQuery(User, query)
- .then(Validate.isEmptyObjectNext(res, next, Errors.userExist))
- .catch(Validate.reject(res))
- },
- forgot: (req, res, next) => {
- const query = {
- where: { email: { $eq: req.body.email } }
- }
- Validate.searchQuery(User, query)
- .then(Validate.isEmptyObjectNext(res, next, Errors.forgotNotExist))
- .catch(Validate.reject(res))
- },
- forgotValidate: (req, res, next) => {
- const query = {
- where: { forgot: { $eq: req.body.forgot } }
- }
- Validate.searchQuery(User, query)
- .then(Validate.isEmptyObjectNext(res, next, Errors.forgotValidate))
- .catch(Validate.reject(res))
- },
- password: (req, res, next) => {
- const required = ['forgot', 'password']
- const errors = Validate.request(req, required, Errors)
- if (errors) {
- res.status(400).json(errors)
- } else {
- const query = { where: { forgot: { $eq: req.body.forgot } } }
- if (req.body.password1 === req.body.password2) {
- Validate.searchQuery(User, query)
- .then(Validate.isEmptyObjectNext(res, next, Errors.userNotExist))
- .catch(Validate.reject(res))
- } else {
- res.status(400).json(Errors.passwordNotEquals)
- }
- }
- },
- resend: (req, res, next) => {
- if (isNaN(req.params.id)) {
- res.status(400).json([Errors.idInvalid])
- } else {
- const query = {where: req.params}
- Validate.searchQuery(User, query)
- .then(Validate.isEmptyObjectNext(res, next, Errors.userNotExist))
- .catch(Validate.reject(res))
- }
- },
- activeCode: (req, res, next) => {
- const active = parseInt(req.params.active)
- if (isNaN(active)) {
- res.status(400).json([Errors.activeInvalid])
- } else {
- const query = { where: { active: { $eq: active } } }
- Validate.searchQuery(User, query)
- .then(Validate.isEmptyObjectNext(res, next, Errors.activeInvalid))
- .catch(Validate.reject(res))
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment