From 3b5dca38d191406648ff7ec518447b04e3186964 Mon Sep 17 00:00:00 2001 From: "Lemuel H. Marquez Rosas" Date: Sun, 4 Oct 2020 23:04:19 -0500 Subject: [PATCH] usuario casi listo --- src/controller/usuario/create.js | 38 ++++++++++---------- src/controller/usuario/get.js | 59 ++++++++++++++++++++++++++++---- src/routes/usuario.js | 7 ++-- 3 files changed, 76 insertions(+), 28 deletions(-) diff --git a/src/controller/usuario/create.js b/src/controller/usuario/create.js index 37856ca..a350b7c 100644 --- a/src/controller/usuario/create.js +++ b/src/controller/usuario/create.js @@ -16,34 +16,36 @@ const create = async (body) => { .validarTexto(body.apMaterno) .then((res) => res) let correo = await validador.validarEmail(body.email).then((res) => res) - let telefono = '' + let telefono = body.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 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', } }) - }) } module.exports = create diff --git a/src/controller/usuario/get.js b/src/controller/usuario/get.js index b8fdcfb..66b581f 100644 --- a/src/controller/usuario/get.js +++ b/src/controller/usuario/get.js @@ -1,12 +1,59 @@ +const validador = require('../validador') const Usuario = require('../../db/tables/Usuario') +const TipoUsuario = require('../../db/tables/TipoUsuario') +const CarreraPlantel = require('../../db/tables/CarreraPlantel') +const Carrera = require('../../db/tables/Carrera') +const Plantel = require('../../db/tables/Plantel') +const Prestamo = require('../../db/tables/Prestamo') const get = async (body) => { - console.log(body) - return Usuario.findAll({ where: { idUsuario: body.idUsuario } }) - .then((res) => { - return res - }) - .catch((err) => {}) + let idUsuario = await validador.validarId(body.idUsuario).then((res) => res) + let busqueda = { + where: { idUsuario }, + include: [ + { model: TipoUsuario }, + { + model: CarreraPlantel, + include: [{ model: Plantel }, { model: Carrera }], + }, + ], + } + + return Usuario.findOne(busqueda).then((res) => { + if (!res) validador.error('No existe este nĂºmero de cuenta.', 400) + return { + code: 200, + data: { + idUsuario: res.idUsuario, + nombre: res.nombre, + apPaterno: res.apPaterno, + apMaterno: res.apMaterno, + correo: res.correo, + telefono: res.telefono, + // identificacion:, + // foto: , + baneado: res.baneado, + createdAt: res.createdAt, + updatedAt: res.updatedAt, + TipoUsuario: { + idTipoUsuario: res.TipoUsuario.idTipoUsuario, + nombre: res.TipoUsuario.nombre, + }, + CarreraPlantel: { + idCarreraPlantel: res.CarreraPlantel.idCarreraPlantel, + Plantel: { + idPlantel: res.CarreraPlantel.Plantel.idPlantel, + plantel: res.CarreraPlantel.Plantel.plantel, + }, + Carrera: { + idCarrera: res.CarreraPlantel.Carrera.idCarrera, + carrera: res.CarreraPlantel.Carrera.carrera, + nivel: res.CarreraPlantel.Carrera.nivel, + }, + }, + }, + } + }) } module.exports = get diff --git a/src/routes/usuario.js b/src/routes/usuario.js index ba39277..e1760cc 100644 --- a/src/routes/usuario.js +++ b/src/routes/usuario.js @@ -8,8 +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) + res.status(data.code).json({ usuario: data.data }) }) .catch((err) => res.status(err.code).json({ err: err.message })) }) @@ -17,7 +16,7 @@ app.get(`${route}`, (req, res) => { app.post(`${route}/create`, (req, res) => { return create(req.body) .then((data) => { - res.status(data.code).json(data.message) + res.status(data.code).json({ message: data.message }) }) .catch((err) => res.status(err.code).json({ err: err.message })) }) @@ -25,7 +24,7 @@ app.post(`${route}/create`, (req, res) => { app.post(`${route}/login`, (req, res) => { return login(req.body) .then((data) => { - res.status(data.code).json(data.data) + res.status(data.code).json({ usuario: data.data }) }) .catch((err) => res.status(err.code).json({ err: err.message })) })