From 8e97cf460510db545a384d2e48c9de222fe95605 Mon Sep 17 00:00:00 2001 From: Lemuel Marquez Date: Mon, 16 Nov 2020 12:47:46 -0600 Subject: [PATCH] nuevo servicio --- server/controller/Servicio/nuevo.js | 59 ++++++++++++++++++++++---- server/controller/Usuario/escolares.js | 13 +++++- server/db/tablas/Servicio.js | 11 ++++- server/helper/validar.js | 17 ++++++-- server/routes/Servicio.js | 27 ++++++++++++ server/routes/Usuario.js | 6 +-- server/routes/index.js | 2 + 7 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 server/routes/Servicio.js diff --git a/server/controller/Servicio/nuevo.js b/server/controller/Servicio/nuevo.js index a573d84..42b1065 100644 --- a/server/controller/Servicio/nuevo.js +++ b/server/controller/Servicio/nuevo.js @@ -1,15 +1,58 @@ +const { Op } = require('sequelize'); const Servicio = require('../../db/tablas/Servicio'); +const Usuario = require('../../db/tablas/Usuario'); +const Programa = require('../../db/tablas/Programa'); +const Carrera = require('../../db/tablas/Carrera'); +const validar = require('../../helper/validar'); -const nuevo = (body) => { - let idPrograma = body.idUsuarioResponsable; - let idAlumno = body.idAlumno; - let numeroCuenta = body.numeroCuenta; +const nuevo = async (body) => { + let idUsuario = validar.validarId(body.idUsuarioAlumno); + let idPrograma = validar.validarId(body.idUsuarioResponsable); + let idCarrera = validar.validarId(body.idCarrera); let creditos = body.creditos; - let nombre = body.nombre; - let correo = body.correo; - let fechaInicio = boyd.fechaInicio; - let fechaFin = boyd.fechaFin; + let nombre = validar.validarTexto(body.nombre); + let correo = validar.validarCorreo(body.correo); + let fechaInicio = validar.validarFecha(boyd.fechaInicio); + let fechaFin = validar.validarFecha(boyd.fechaFin); let cartaAceptacion = body.cartaAceptacion; + + return Usuario.findOne({ + where: { [Op.and]: [{ idUsuario }, { idTipoUsuario: 3 }] }, + }) + .then((res) => { + if (!res) throw new Error('Este alumno no existe en la db.'); + return Programa.findOne({ where: { idPrograma } }); + }) + .then((res) => { + if (!res) throw new Error('Este programa no existe en la db.'); + return Carrera.findOne({ where: { idCarrera } }); + }) + .then((res) => { + if (!res) throw new Error('Esta carrera no existe en la db.'); + return Servicio.findOne({ + where: { [Op.and]: [{ idUsuario }, { idStatus: { [Op.ne]: 10 } }] }, + }); + }) + .then((res) => { + if (res) + throw new Error('Este alumno ya tienen un Servicio Social activo.'); + return Servicio.create({ + idUsuario, + idPrograma, + idCarrera, + creditos, + nombre, + correo, + fechaInicio, + fechaFin, + cartaAceptacion, + }); + }) + .then((res) => { + return { + message: `Se Pre-Registro correctamente al alumno ${res.nombre}`, + }; + }); }; module.exports = nuevo; diff --git a/server/controller/Usuario/escolares.js b/server/controller/Usuario/escolares.js index 66f6541..ddbf028 100644 --- a/server/controller/Usuario/escolares.js +++ b/server/controller/Usuario/escolares.js @@ -1,7 +1,8 @@ const validar = require('../../helper/validar'); require('../../config/config'); -const Usuario = require('../../db/tablas/Usuario'); const axios = require('axios'); +const Usuario = require('../../db/tablas/Usuario'); +const Carrera = require('../../db/tablas/Carrera'); const escolares = async (body) => { let numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta); @@ -21,9 +22,17 @@ const escolares = async (body) => { ); alumno = { nombre: data.nombre.trim(), - carrera: data.carrconst.trim(), creditos: data.avance, + carrera: data.carrconst.trim(), }; + return Carrera.findOne({ where: { carrera: alumno.carrera } }); + }) + .then((res) => { + if (!res) return Carrera.create({ carrera: alumno.carrera }); + return res; + }) + .then((res) => { + alumno.idCarrera = res.idCarrera; return Usuario.findOne({ where: { usuario: numeroCuenta } }); }) .then((res) => { diff --git a/server/db/tablas/Servicio.js b/server/db/tablas/Servicio.js index e2c9e3c..dd4e09a 100644 --- a/server/db/tablas/Servicio.js +++ b/server/db/tablas/Servicio.js @@ -53,6 +53,10 @@ Servicio.init( allowNull: true, defaultValue: null, }, + carpeta: { + type: DataTypes.STRING(30), + allowNull: false, + }, cartaAceptacion: { type: DataTypes.STRING(30), allowNull: false, @@ -112,6 +116,7 @@ Servicio.belongsTo(Status, { name: 'idStatus', type: DataTypes.INTEGER, allowNull: false, + defaultValue: 1, }, }); @@ -127,7 +132,8 @@ Servicio.belongsTo(CuestionarioAlumno, { foreignKey: { name: 'idCuestionarioAlumno', type: DataTypes.INTEGER, - allowNull: false, + allowNull: true, + defaultValue: null, }, }); @@ -135,7 +141,8 @@ Servicio.belongsTo(CuestionarioPrograma, { foreignKey: { name: 'idCuestionarioPrograma', type: DataTypes.INTEGER, - allowNull: false, + allowNull: true, + defaultValue: null, }, }); diff --git a/server/helper/validar.js b/server/helper/validar.js index 546c451..03d7811 100644 --- a/server/helper/validar.js +++ b/server/helper/validar.js @@ -1,4 +1,5 @@ const validator = require('validator'); +const moment = require('moment'); const noValido = (campo) => { throw new Error(`El ${campo} no es valido.`); @@ -53,12 +54,22 @@ const validarNumeroCuenta = (numeroCuenta) => { return numeroCuenta; }; +const validarFecha = (fecha) => { + let campo = 'fecha'; + let fechaMoment = moment(fecha); + + noHay(fecha, campo); + if (fechaMoment.isValid()) noValido(campo); + return fechaMoment; +}; + module.exports = { + noValido, + yaExiste, + noHay, validarCorreo, validarId, validarTexto, - yaExiste, - noValido, - noHay, validarNumeroCuenta, + validarFecha, }; diff --git a/server/routes/Servicio.js b/server/routes/Servicio.js new file mode 100644 index 0000000..dd851eb --- /dev/null +++ b/server/routes/Servicio.js @@ -0,0 +1,27 @@ +const express = require('express'); +const app = express(); +const route = '/servicio'; +const nuevo = require('../controller/Servicio/nuevo'); +// const login = require('../controller/Usuario/login'); + +app.post(`${route}/nuevo`, (req, res) => { + return nuevo(req.body) + .then((data) => { + res.status(201).json(data); + }) + .catch((err) => { + res.status(400).json({ message: err.message }); + }); +}); + +// app.post(`${route}/login`, (req, res) => { +// return login(req.body) +// .then((data) => { +// res.status(201).json(data); +// }) +// .catch((err) => { +// res.status(400).json({ message: err.message }); +// }); +// }); + +module.exports = app; diff --git a/server/routes/Usuario.js b/server/routes/Usuario.js index 2a1746f..55bf9f5 100644 --- a/server/routes/Usuario.js +++ b/server/routes/Usuario.js @@ -2,11 +2,11 @@ const express = require('express'); const app = express(); const route = '/usuario'; const escolares = require('../controller/Usuario/escolares'); -// const create = require('../controller/Usuario/create'); +// const nuevo = require('../controller/Servicio/ser'); // const login = require('../controller/Usuario/login'); -// app.post(`${route}/create`, (req, res) => { -// return create(req.body) +// app.post(`${route}/nuevo`, (req, res) => { +// return nuevo(req.body) // .then((data) => { // res.status(201).json(data); // }) diff --git a/server/routes/index.js b/server/routes/index.js index 26cc25d..6ac1049 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -4,5 +4,7 @@ const app = express(); // app.use(require('./')) app.use(require('./Status')); app.use(require('./Usuario')); +app.use(require('./Servicio')); + module.exports = app;