2020-08-18 11:13:12 -05:00
|
|
|
import { Usuario } from '@src/entity/Usuario'
|
|
|
|
|
import { Request, Response } from 'express'
|
|
|
|
|
import { validate } from 'class-validator'
|
|
|
|
|
import { getRepository } from 'typeorm'
|
|
|
|
|
export class UsuarioController {
|
|
|
|
|
static async create (req: Request, res: Response) {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
nombre,
|
|
|
|
|
apellidoPaterno,
|
|
|
|
|
apellidoMaterno,
|
|
|
|
|
correoAlternativo,
|
|
|
|
|
numeroTelefonico,
|
|
|
|
|
identificacion,
|
2020-08-19 09:34:11 -05:00
|
|
|
institucion,
|
|
|
|
|
secreto
|
2020-08-18 11:13:12 -05:00
|
|
|
} = req.body
|
|
|
|
|
|
|
|
|
|
const usuario = new Usuario()
|
|
|
|
|
usuario.id = id
|
|
|
|
|
usuario.nombre = nombre
|
|
|
|
|
usuario.apellidoPaterno = apellidoPaterno
|
|
|
|
|
usuario.apellidoMaterno = apellidoMaterno
|
|
|
|
|
usuario.correoAlternativo = correoAlternativo
|
|
|
|
|
usuario.numeroTelefonico = numeroTelefonico
|
|
|
|
|
usuario.identificacion = identificacion
|
|
|
|
|
usuario.institucion = institucion
|
2020-08-19 09:34:11 -05:00
|
|
|
usuario.secreto = secreto
|
|
|
|
|
|
|
|
|
|
usuario.hashPassword()
|
2020-08-18 11:13:12 -05:00
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-19 09:34:11 -05:00
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
}
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
nombre: usuario.nombre,
|
|
|
|
|
apellidoPaterno: usuario.apellidoPaterno,
|
|
|
|
|
apellidoMaterno: usuario.apellidoMaterno
|
|
|
|
|
})
|
|
|
|
|
} catch (err) {
|
|
|
|
|
res.status(400).send(err.message)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-18 11:13:12 -05:00
|
|
|
}
|