usuarios
This commit is contained in:
+2
-2
@@ -2,8 +2,8 @@
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"username": "local",
|
||||
"password": "123",
|
||||
"username": "root",
|
||||
"password": "hola",
|
||||
"database": "pcpuma",
|
||||
"synchronize": false,
|
||||
"logging": false,
|
||||
|
||||
+160
-136
@@ -1,13 +1,13 @@
|
||||
import { Usuario } from '@src/entity/Usuario'
|
||||
import { Request, Response } from 'express'
|
||||
import { validate } from 'class-validator'
|
||||
import { getRepository } from 'typeorm'
|
||||
import { AuthController } from './Auth'
|
||||
import { TipoUsuario } from '@src/entity/TipoUsuario'
|
||||
import { Usuario } from "@src/entity/Usuario";
|
||||
import { Request, Response } from "express";
|
||||
import { validate } from "class-validator";
|
||||
import { getRepository } from "typeorm";
|
||||
import { AuthController } from "./Auth";
|
||||
import { TipoUsuario } from "@src/entity/TipoUsuario";
|
||||
export class UsuarioController {
|
||||
static async create (req: Request, res: Response) {
|
||||
const tipoUsuarioRepository = getRepository(TipoUsuario)
|
||||
console.log(req.file)
|
||||
static async create(req: Request, res: Response) {
|
||||
const tipoUsuarioRepository = getRepository(TipoUsuario);
|
||||
console.log(req.file);
|
||||
const {
|
||||
id,
|
||||
nombre,
|
||||
@@ -17,231 +17,255 @@ export class UsuarioController {
|
||||
numeroTelefonico,
|
||||
institucion,
|
||||
secreto,
|
||||
tipoUsuarioId
|
||||
} = JSON.parse(req.body.usuario)
|
||||
tipoUsuarioId,
|
||||
} = JSON.parse(req.body.usuario);
|
||||
|
||||
const tipoUsuario = await tipoUsuarioRepository.findOne(tipoUsuarioId)
|
||||
const tipoUsuario = await tipoUsuarioRepository.findOne(tipoUsuarioId);
|
||||
if (!tipoUsuario) {
|
||||
res.status(400).send({
|
||||
status: false,
|
||||
data: 'Tipo de usuario no encointrado'
|
||||
})
|
||||
return false
|
||||
data: "Tipo de usuario no encointrado",
|
||||
});
|
||||
return false;
|
||||
}
|
||||
const identificacion = req.file
|
||||
if (tipoUsuario.nombre === 'alumno_externo' && !identificacion) {
|
||||
const identificacion = req.file;
|
||||
if (tipoUsuario.nombre === "alumno_externo" && !identificacion) {
|
||||
res.status(400).send({
|
||||
status: false,
|
||||
data: 'Un usuario externo tiene que mandar fotoo de su identificacion'
|
||||
})
|
||||
return false
|
||||
data: "Un usuario externo tiene que mandar fotoo de su identificacion",
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
const usuario = new Usuario()
|
||||
usuario.id = id
|
||||
usuario.nombre = nombre
|
||||
usuario.apellidoPaterno = apellidoPaterno
|
||||
usuario.apellidoMaterno = apellidoMaterno
|
||||
usuario.correoAlternativo = correoAlternativo
|
||||
usuario.numeroTelefonico = numeroTelefonico
|
||||
const usuario = new Usuario();
|
||||
usuario.id = id;
|
||||
usuario.nombre = nombre;
|
||||
usuario.apellidoPaterno = apellidoPaterno;
|
||||
usuario.apellidoMaterno = apellidoMaterno;
|
||||
usuario.correoAlternativo = correoAlternativo;
|
||||
usuario.numeroTelefonico = numeroTelefonico;
|
||||
usuario.identificacion =
|
||||
tipoUsuario.nombre === 'alumno_externo'
|
||||
? 'null'
|
||||
: JSON.stringify(identificacion)
|
||||
usuario.institucion = institucion
|
||||
usuario.secreto = secreto
|
||||
usuario.tipoUsuario = tipoUsuario
|
||||
tipoUsuario.nombre === "alumno_externo"
|
||||
? "null"
|
||||
: JSON.stringify(identificacion);
|
||||
usuario.institucion = institucion;
|
||||
usuario.secreto = secreto;
|
||||
usuario.tipoUsuario = tipoUsuario;
|
||||
|
||||
usuario.hashPassword()
|
||||
usuario.hashPassword();
|
||||
|
||||
const errors = await validate(usuario)
|
||||
const errors = await validate(usuario);
|
||||
if (errors.length > 0) {
|
||||
res.status(400).send(errors)
|
||||
res.status(400).send(errors);
|
||||
}
|
||||
|
||||
const repository = getRepository(Usuario)
|
||||
const repository = getRepository(Usuario);
|
||||
try {
|
||||
await repository.save(usuario)
|
||||
await repository.save(usuario);
|
||||
} catch (e) {
|
||||
res.status(409).send(e.message)
|
||||
return
|
||||
res.status(409).send(e.message);
|
||||
return;
|
||||
}
|
||||
res.status(201).send('User create')
|
||||
res.status(201).send("User create");
|
||||
}
|
||||
|
||||
static async getAll (req: Request, res: Response) {
|
||||
const respository = getRepository(Usuario)
|
||||
static async getAll(req: Request, res: Response) {
|
||||
const respository = getRepository(Usuario);
|
||||
const result = await respository.find({
|
||||
select: ['id', 'nombre', 'apellidoMaterno', 'apellidoPaterno']
|
||||
})
|
||||
res.status(200).json(result)
|
||||
select: ["id", "nombre", "apellidoMaterno", "apellidoPaterno"],
|
||||
});
|
||||
res.status(200).json(result);
|
||||
}
|
||||
|
||||
static async getOne (req: Request, res: Response) {
|
||||
const respository = getRepository(Usuario)
|
||||
const id: number = +req.params.id
|
||||
static async getOne(req: Request, res: Response) {
|
||||
const respository = getRepository(Usuario);
|
||||
const id: number = +req.params.id;
|
||||
try {
|
||||
const result = await respository.findOneOrFail(id, {
|
||||
select: ['id', 'nombre', 'apellidoMaterno', 'apellidoPaterno']
|
||||
})
|
||||
res.status(200).json(result)
|
||||
select: ["id", "nombre", "apellidoMaterno", "apellidoPaterno"],
|
||||
});
|
||||
res.status(200).json(result);
|
||||
} catch (e) {
|
||||
res.status(404).send('Usuario no encontrado')
|
||||
res.status(404).send("Usuario no encontrado");
|
||||
}
|
||||
}
|
||||
|
||||
static async login (req: Request, res: Response) {
|
||||
const repository = getRepository(Usuario)
|
||||
const { id, secreto } = req.body
|
||||
static async login(req: Request, res: Response) {
|
||||
const repository = getRepository(Usuario);
|
||||
const { id, secreto } = req.body;
|
||||
try {
|
||||
const usuario = await repository.findOne({
|
||||
relations: ['tipoUsuario'],
|
||||
relations: ["tipoUsuario"],
|
||||
where: {
|
||||
id
|
||||
}
|
||||
})
|
||||
id,
|
||||
},
|
||||
});
|
||||
if (!usuario) {
|
||||
throw new Error('*Usuario o contraseña invalidos')
|
||||
throw new Error("*Usuario o contraseña invalidos");
|
||||
}
|
||||
if (!usuario.checkIfUnencryptedPasswordIsValid(secreto)) {
|
||||
throw new Error('Usuario o *contraseña invalidos')
|
||||
throw new Error("Usuario o *contraseña invalidos");
|
||||
}
|
||||
const token = await AuthController.signUsuario(
|
||||
usuario.tipoUsuario.nombre,
|
||||
usuario.id
|
||||
)
|
||||
console.log(token)
|
||||
);
|
||||
console.log(token);
|
||||
res.status(200).json({
|
||||
nombre: usuario.nombre,
|
||||
apellidoPaterno: usuario.apellidoPaterno,
|
||||
apellidoMaterno: usuario.apellidoMaterno,
|
||||
tipoUsuario: usuario.tipoUsuario,
|
||||
token
|
||||
})
|
||||
token,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(400).send(err.message)
|
||||
res.status(400).send(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
static async verificar (req: Request, res: Response) {
|
||||
const usuarioId = req.params.id
|
||||
static async verificar(req: Request, res: Response) {
|
||||
const usuarioId = req.params.id;
|
||||
|
||||
const alumnos = [
|
||||
{
|
||||
cuenta: 124493,
|
||||
nombre: 'Nora',
|
||||
apellidoPaterno: 'Goris',
|
||||
apellidoMaterno: 'Mayans',
|
||||
institucion: 'FES Acatlan',
|
||||
carrera: 'Sociologia',
|
||||
nombre: "Nora",
|
||||
apellidoPaterno: "Goris",
|
||||
apellidoMaterno: "Mayans",
|
||||
institucion: "FES Acatlan",
|
||||
carrera: "Sociologia",
|
||||
tipoUsuario: {
|
||||
id: 1,
|
||||
nombre: 'alumno_interno'
|
||||
}
|
||||
nombre: "alumno_interno",
|
||||
},
|
||||
},
|
||||
{
|
||||
cuenta: 91770,
|
||||
nombre: 'Manuel',
|
||||
apellidoPaterno: 'Martinez',
|
||||
apellidoMaterno: 'Justo',
|
||||
institucion: 'FES Acatlan',
|
||||
carrera: 'Derecho',
|
||||
nombre: "Manuel",
|
||||
apellidoPaterno: "Martinez",
|
||||
apellidoMaterno: "Justo",
|
||||
institucion: "FES Acatlan",
|
||||
carrera: "Derecho",
|
||||
tipoUsuario: {
|
||||
id: 1,
|
||||
nombre: 'alumno_interno'
|
||||
}
|
||||
nombre: "alumno_interno",
|
||||
},
|
||||
},
|
||||
{
|
||||
cuenta: 123456789,
|
||||
nombre: 'Enrique',
|
||||
apellidoPaterno: 'Graue',
|
||||
apellidoMaterno: 'Wiechers',
|
||||
institucion: 'Facultad de Mediciona',
|
||||
carrera: 'Medicina',
|
||||
nombre: "Enrique",
|
||||
apellidoPaterno: "Graue",
|
||||
apellidoMaterno: "Wiechers",
|
||||
institucion: "Facultad de Mediciona",
|
||||
carrera: "Medicina",
|
||||
tipoUsuario: {
|
||||
id: 2,
|
||||
nombre: 'alumno_externo'
|
||||
}
|
||||
nombre: "alumno_externo",
|
||||
},
|
||||
},
|
||||
{
|
||||
cuenta: 40108,
|
||||
nombre: 'Fernando',
|
||||
apellidoPaterno: 'Martinez',
|
||||
apellidoMaterno: 'Ramirez',
|
||||
institucion: 'Facultad de Ciencias',
|
||||
carrera: 'Matematicas',
|
||||
nombre: "Fernando",
|
||||
apellidoPaterno: "Martinez",
|
||||
apellidoMaterno: "Ramirez",
|
||||
institucion: "Facultad de Ciencias",
|
||||
carrera: "Matematicas",
|
||||
tipoUsuario: {
|
||||
id: 1,
|
||||
nombre: 'alumno_interno'
|
||||
}
|
||||
nombre: "alumno_interno",
|
||||
},
|
||||
},
|
||||
{
|
||||
cuenta: 415112132,
|
||||
nombre: 'arturo',
|
||||
apellidoPaterno: 'Guerrero',
|
||||
apellidoMaterno: 'Lopez',
|
||||
institucion: 'FES Acatlan',
|
||||
carrera: 'M.A.C',
|
||||
nombre: "arturo",
|
||||
apellidoPaterno: "Guerrero",
|
||||
apellidoMaterno: "Lopez",
|
||||
institucion: "FES Acatlan",
|
||||
carrera: "M.A.C",
|
||||
tipoUsuario: {
|
||||
id: 1,
|
||||
nombre: 'alumno_interno'
|
||||
}
|
||||
nombre: "alumno_interno",
|
||||
},
|
||||
},
|
||||
{
|
||||
cuenta: 1,
|
||||
nombre: 'Fernando',
|
||||
apellidoPaterno: 'Martinez',
|
||||
apellidoMaterno: 'Ramirez',
|
||||
institucion: 'Facultad de Ciencias',
|
||||
carrera: 'Matematicas',
|
||||
nombre: "Fernando",
|
||||
apellidoPaterno: "Martinez",
|
||||
apellidoMaterno: "Ramirez",
|
||||
institucion: "Facultad de Ciencias",
|
||||
carrera: "Matematicas",
|
||||
tipoUsuario: {
|
||||
id: 1,
|
||||
nombre: 'alumno_interno'
|
||||
}
|
||||
nombre: "alumno_interno",
|
||||
},
|
||||
},
|
||||
{
|
||||
cuenta: 12,
|
||||
nombre: 'Fernando',
|
||||
apellidoPaterno: 'Martinez',
|
||||
apellidoMaterno: 'Ramirez',
|
||||
institucion: 'Facultad de Ciencias',
|
||||
carrera: 'Matematicas',
|
||||
nombre: "Fernando",
|
||||
apellidoPaterno: "Martinez",
|
||||
apellidoMaterno: "Ramirez",
|
||||
institucion: "Facultad de Ciencias",
|
||||
carrera: "Matematicas",
|
||||
tipoUsuario: {
|
||||
id: 2,
|
||||
nombre: 'alumno_externo'
|
||||
}
|
||||
nombre: "alumno_externo",
|
||||
},
|
||||
},
|
||||
{
|
||||
cuenta: 123,
|
||||
nombre: 'Fernando',
|
||||
apellidoPaterno: 'Martinez',
|
||||
apellidoMaterno: 'Ramirez',
|
||||
institucion: 'Facultad de Ciencias',
|
||||
carrera: 'Matematicas',
|
||||
nombre: "Fernando",
|
||||
apellidoPaterno: "Martinez",
|
||||
apellidoMaterno: "Ramirez",
|
||||
institucion: "Facultad de Ciencias",
|
||||
carrera: "Matematicas",
|
||||
tipoUsuario: {
|
||||
id: 3,
|
||||
nombre: 'profesor_interno'
|
||||
}
|
||||
nombre: "profesor_interno",
|
||||
},
|
||||
},
|
||||
{
|
||||
cuenta: 812917,
|
||||
nombre: 'Araceli',
|
||||
apellidoPaterno: 'Pérez',
|
||||
apellidoMaterno: 'Palma',
|
||||
institucion: 'FES Acatlan',
|
||||
carrera: 'MAC',
|
||||
nombre: "Araceli",
|
||||
apellidoPaterno: "Pérez",
|
||||
apellidoMaterno: "Palma",
|
||||
institucion: "FES Acatlan",
|
||||
carrera: "MAC",
|
||||
tipoUsuario: {
|
||||
id: 3,
|
||||
nombre: 'profesor_interno'
|
||||
}
|
||||
nombre: "profesor_interno",
|
||||
},
|
||||
},
|
||||
]
|
||||
{
|
||||
cuenta: 313109593,
|
||||
nombre: "Saul",
|
||||
apellidoPaterno: "Hernandez",
|
||||
apellidoMaterno: "Juarez",
|
||||
institucion: "FES Acatlan",
|
||||
carrera: "Actuario",
|
||||
tipoUsuario: {
|
||||
id: 1,
|
||||
nombre: "alumno_interno",
|
||||
},
|
||||
},
|
||||
{
|
||||
cuenta: 316313528,
|
||||
nombre: "Lemuel Helon",
|
||||
apellidoPaterno: "Márquez",
|
||||
apellidoMaterno: "Rosas",
|
||||
institucion: "FES Acatlan",
|
||||
carrera: "MAC",
|
||||
tipoUsuario: {
|
||||
id: 2,
|
||||
nombre: "alumno_externo",
|
||||
},
|
||||
},
|
||||
];
|
||||
for (let index = 0; index < alumnos.length; index++) {
|
||||
if (alumnos[index].cuenta === +usuarioId) {
|
||||
res.status(200).json(alumnos[index])
|
||||
return
|
||||
res.status(200).json(alumnos[index]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
res.status(400).json({ err: 'no se encontro un usuario' })
|
||||
res.status(400).json({ err: "no se encontro un usuario" });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user