create usuario casi listo
This commit is contained in:
@@ -1,37 +1,49 @@
|
||||
require('../../config/config')
|
||||
const bcrypt = require('bcrypt')
|
||||
const validador = require('../validador')
|
||||
const Usuario = require('../../db/tables/Usuario')
|
||||
|
||||
const create = (body) => {
|
||||
return Usuario.create({
|
||||
idUsuario: body.idUsuario,
|
||||
password: bcrypt.hashSync(body.password, Number(process.env.SALT_ROUNDS)),
|
||||
nombre: body.nombre,
|
||||
apPaterno: body.apPaterno,
|
||||
apMaterno: body.apMaterno,
|
||||
correo: body.correo,
|
||||
telefono: body.telefono,
|
||||
identificacion: '',
|
||||
foto: '',
|
||||
idTipoUsuario: 2,
|
||||
idCarreraPlantel: body.idCarreraPlantel,
|
||||
})
|
||||
.then((res) => {
|
||||
console.log(res)
|
||||
return { mensaje: 'Se creo un usuario correctamente' }
|
||||
const create = async (body) => {
|
||||
let idUsuario = await validador.validarId(body.idUsuario).then((res) => res)
|
||||
let password = await validador
|
||||
.validarTexto(body.password)
|
||||
.then((res) => bcrypt.hashSync(res, Number(process.env.SALT_ROUNDS)))
|
||||
let nombre = await validador.validarTexto(body.nombre).then((res) => res)
|
||||
let apPaterno = await validador
|
||||
.validarTexto(body.apPaterno)
|
||||
.then((res) => res)
|
||||
let apMaterno = await validador
|
||||
.validarTexto(body.apMaterno)
|
||||
.then((res) => res)
|
||||
let correo = await validador.validarEmail(body.email).then((res) => res)
|
||||
let telefono = ''
|
||||
let idCarreraPlantel = await validador
|
||||
.validarId(body.idCarreraPlantel)
|
||||
.then((res) => res)
|
||||
let idTipoUsuario = 1
|
||||
|
||||
return Usuario.findOne({ where: { idUsuario } }).then((res) => {
|
||||
if (res) validador.error('El numero de cuanta ya a sido registrado.', 400)
|
||||
let data = {
|
||||
idUsuario,
|
||||
password,
|
||||
nombre,
|
||||
apPaterno,
|
||||
apMaterno,
|
||||
correo,
|
||||
telefono,
|
||||
identificacion: '',
|
||||
foto: '',
|
||||
idTipoUsuario,
|
||||
idCarreraPlantel,
|
||||
}
|
||||
return Usuario.create(data).then((res) => {
|
||||
return {
|
||||
code: 201,
|
||||
message: 'Se registro correctamente el numero de cuenta',
|
||||
}
|
||||
})
|
||||
.catch((err) => {})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = create
|
||||
|
||||
// create({
|
||||
// idUsuario: 316313528,
|
||||
// password: 'hola',
|
||||
// nombre: 'Lemuel Helon',
|
||||
// apPaterno: 'Márquez',
|
||||
// apMaterno: 'Rosas',
|
||||
// correo: 'lemuel@acatlan.unam.mx',
|
||||
// telefono: '5514054674',
|
||||
// idCarreraPlantel: 163,
|
||||
// })
|
||||
|
||||
@@ -11,21 +11,24 @@ const error = (message, code) => {
|
||||
|
||||
const validarId = (numero) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!numero) reject(objetoError('Falta id', 400))
|
||||
if (!numero) reject(objetoError('Falta id.', 400))
|
||||
if (typeof numero === 'number') numero = numero.toString()
|
||||
if (typeof numero === 'string') {
|
||||
if (!validator.isNumeric(numero))
|
||||
reject(objetoError('La cadena no es un numero valido', 400))
|
||||
if (!validator.isNumeric(numero, { no_symbols: true }))
|
||||
reject(objetoError('El id no es valido.', 400))
|
||||
resolve(Number(numero))
|
||||
} else if (typeof numero === 'number') {
|
||||
if (numero < 0) reject(objetoError('El numero no es valido', 400))
|
||||
resolve(numero)
|
||||
} else reject(objetoError('Variable no valida.', 400))
|
||||
} else reject(objetoError('Variable id no valida.', 400))
|
||||
})
|
||||
}
|
||||
|
||||
const validarEmail = (email) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!email) reject(objetoError('Falta variable', 400))
|
||||
if (!email) reject(objetoError('Falta emaill', 400))
|
||||
if (typeof email === 'string') {
|
||||
if (!validator.isEmail(email))
|
||||
reject(objetoError('El email no es valido'))
|
||||
resolve(email)
|
||||
} else reject(objetoError('Variable correo no valida', 400))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -38,4 +41,13 @@ const validarTexto = (texto) => {
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { error, validarId, validarTexto, validarEmail }
|
||||
const validarNumero = (numero) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!numero) reject(objetoError('Falta variable', 400))
|
||||
if (typeof numero === 'string') {
|
||||
if (!validator.isNumeric) resolve(numero)
|
||||
} else reject(objetoError('Variable no valida', 400))
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = { error, validarId, validarTexto, validarEmail, validarNumero }
|
||||
|
||||
@@ -8,6 +8,7 @@ const route = '/usuario'
|
||||
app.get(`${route}`, (req, res) => {
|
||||
return get(req.query)
|
||||
.then((data) => {
|
||||
console.log(data)
|
||||
res.status(data.code).json(data.data)
|
||||
})
|
||||
.catch((err) => res.status(err.code).json({ err: err.message }))
|
||||
@@ -16,7 +17,7 @@ app.get(`${route}`, (req, res) => {
|
||||
app.post(`${route}/create`, (req, res) => {
|
||||
return create(req.body)
|
||||
.then((data) => {
|
||||
res.status(data.code).json(data.data)
|
||||
res.status(data.code).json(data.message)
|
||||
})
|
||||
.catch((err) => res.status(err.code).json({ err: err.message }))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user