146 lines
4.0 KiB
TypeScript
146 lines
4.0 KiB
TypeScript
import { Usuario } from '@src/entity/Usuario'
|
|
import { Request, Response } from 'express'
|
|
import { validate } from 'class-validator'
|
|
import { getRepository } from 'typeorm'
|
|
import { AuthController } from './Auth'
|
|
export class UsuarioController {
|
|
static async create (req: Request, res: Response) {
|
|
console.log(req.file)
|
|
const {
|
|
id,
|
|
nombre,
|
|
apellidoPaterno,
|
|
apellidoMaterno,
|
|
correoAlternativo,
|
|
numeroTelefonico,
|
|
institucion,
|
|
secreto,
|
|
interno
|
|
} = JSON.parse(req.body.usuario)
|
|
|
|
const identificacion = req.file
|
|
if (!interno && !identificacion) {
|
|
res.status(400).send({
|
|
status: 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
|
|
usuario.identificacion = interno ? 'null' : JSON.stringify(identificacion)
|
|
usuario.institucion = institucion
|
|
usuario.secreto = secreto
|
|
usuario.interno = interno
|
|
|
|
usuario.hashPassword()
|
|
|
|
const errors = await validate(usuario)
|
|
if (errors.length > 0) {
|
|
res.status(400).send(errors)
|
|
}
|
|
|
|
const repository = getRepository(Usuario)
|
|
try {
|
|
await repository.save(usuario)
|
|
} catch (e) {
|
|
res.status(409).send(e.message)
|
|
return
|
|
}
|
|
res.status(201).send('User create')
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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)
|
|
} catch (e) {
|
|
res.status(404).send('Usuario no encontrado')
|
|
}
|
|
}
|
|
|
|
static async login (req: Request, res: Response) {
|
|
const repository = getRepository(Usuario)
|
|
const { id, secreto } = req.body
|
|
try {
|
|
const usuario = await repository.findOne({
|
|
where: {
|
|
id
|
|
}
|
|
})
|
|
if (!usuario) {
|
|
throw new Error('*Usuario o contraseña invalidos')
|
|
}
|
|
if (!usuario.checkIfUnencryptedPasswordIsValid(secreto)) {
|
|
throw new Error('Usuario o *contraseña invalidos')
|
|
}
|
|
const token = await AuthController.signUsuario(
|
|
usuario.interno ? 'usuarioInterno' : 'usuarioExterno',
|
|
usuario.id
|
|
)
|
|
console.log(token)
|
|
res.status(200).json({
|
|
nombre: usuario.nombre,
|
|
apellidoPaterno: usuario.apellidoPaterno,
|
|
apellidoMaterno: usuario.apellidoMaterno,
|
|
interno: usuario.interno,
|
|
token
|
|
})
|
|
} catch (err) {
|
|
res.status(400).send(err.message)
|
|
}
|
|
}
|
|
|
|
static async verificar (req: Request, res: Response) {
|
|
const usuarios = ['415112132', '41511216']
|
|
const externos = ['315112132', '31511216']
|
|
|
|
const usuarioId = req.params.id
|
|
|
|
const usuarioInterno = {
|
|
nombre: 'Arturo',
|
|
apellidoPaterno: 'holdda',
|
|
apellidoMaterno: 'holdda',
|
|
institucion: 'Fes acatlan',
|
|
carrera: 'Matematicas aplicadas',
|
|
interno: true
|
|
}
|
|
const usuarioExterno = {
|
|
nombre: 'Paulina',
|
|
apellidoPaterno: 'Guerrero',
|
|
apellidoMaterno: 'Lopez',
|
|
institucion: 'FES Iztacala',
|
|
carrera: 'Biologia',
|
|
interno: false
|
|
}
|
|
|
|
if (usuarios.includes(usuarioId)) {
|
|
res.status(200).json(usuarioInterno)
|
|
return
|
|
}
|
|
if (externos.includes(usuarioId)) {
|
|
res.status(200).json(usuarioExterno)
|
|
return
|
|
}
|
|
res.status(400).json({ err: 'no se encontro un usuario' })
|
|
}
|
|
}
|