cambios para completar user

This commit is contained in:
arturo
2020-09-08 11:29:11 -05:00
parent eda0dc7d81
commit 0b4c3d56d3
10 changed files with 179 additions and 7 deletions
+8
View File
@@ -1,9 +1,11 @@
import { createConnection, getRepository } from 'typeorm'
import { Horario } from './entity/Horario'
import { Mesa } from './entity/Mesa'
createConnection()
.then(async (connection) => {
const horarioRepository = getRepository(Horario)
const mesaRepository = getRepository(Mesa)
const horarios = [
'8:00-10:00',
'10:00-12:00',
@@ -18,6 +20,12 @@ createConnection()
console.log(horario)
await horarioRepository.save(horario)
}
for (let i = 1; i < 30; i++) {
const mesa = new Mesa()
mesa.nombre = String(i)
await mesaRepository.save(mesa)
}
process.exit()
})
.catch((error) => console.log('TypeORM connection error: ', error))
+8
View File
@@ -0,0 +1,8 @@
import * as jwt from 'jsonwebtoken'
export class AuthController {
static async signUsuario (type: string, usuarioId: string) {
const secret = 'Holabeb'
const token = jwt.sign({ type, usuarioId }, secret, { expiresIn: '1h' })
return token
}
}
+17 -4
View File
@@ -1,5 +1,5 @@
import { Request, Response } from 'express'
import { getRepository } from 'typeorm'
import { getRepository, QueryBuilder } from 'typeorm'
import { Equipo } from '@src/entity/Equipo'
import { Prestamo } from '@src/entity/Prestamo'
import { Usuario } from '@src/entity/Usuario'
@@ -133,12 +133,25 @@ export class PrestamoController {
static async estatusPrestamo (req: Request, res: Response) {
const repository = getRepository(Prestamo)
const { prestamoId } = req.query
const { usuarioId } = req.query
console.log(usuarioId)
const prestamo = await repository.findOne(+prestamoId)
const prestamo = await repository
.createQueryBuilder('prestamo')
.leftJoinAndSelect('prestamo.usuario', 'usuario')
.where('usuario.id = :usuarioId', { usuarioId })
.andWhere('prestamo.activo = :activo', { activo: true })
.getOne()
// const prestamo = await repository.find({
// relations: ['usuario'],
// where: {
// usuarioId,
// activo: true
// }
// })
if (!prestamo) {
res.status(400).json({ err: 'El prestamo no existe' })
res.status(400).json({ err: 'El usuario no tiene prestamos activos' })
return
}
+8 -1
View File
@@ -2,6 +2,7 @@ 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)
@@ -91,11 +92,17 @@ export class UsuarioController {
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
interno: usuario.interno,
token
})
} catch (err) {
res.status(400).send(err.message)
+16 -1
View File
@@ -1,9 +1,24 @@
import { Entity, OneToMany } from 'typeorm'
import {
Entity,
OneToMany,
Column,
UpdateDateColumn,
CreateDateColumn
} from 'typeorm'
import { Catalogo } from './Catalogo'
import { Prestamo } from './Prestamo'
@Entity()
export class Mesa extends Catalogo {
@Column({ default: false })
activo: boolean
@UpdateDateColumn()
updatedAt: Date
@CreateDateColumn()
createdAt: Date
@OneToMany(
(type) => Prestamo,
(prestamo) => prestamo.mesa
+2
View File
@@ -86,6 +86,8 @@ export class Usuario {
@UpdateDateColumn()
updatedAt: Date
token: string
@OneToMany(
(type) => Reservacion,
(reservacion) => reservacion.usuario
+17
View File
@@ -0,0 +1,17 @@
import * as jwt from 'jsonwebtoken'
import { Request, Response, NextFunction } from 'express'
const verificaToken = (req: Request, res: Response, next: NextFunction) => {
const token = req.get('authorization')
jwt.verify(token.split(' ')[1], 'Holabeb', (err, decoded) => {
if (err) {
return res.status(401).json({
status: false,
err: 'jsonwebtoken error'
})
}
next()
})
}
export default verificaToken
+2 -1
View File
@@ -1,6 +1,7 @@
import { Router } from 'express'
import { UsuarioController } from '@src/controller/Usuario'
import * as multer from 'multer'
import verificaToken from '@src/middleware/auth'
var upload = multer({ dest: 'uploads/identifiaciones' })
const router = Router()
@@ -9,7 +10,7 @@ router.post('/', upload.single('identificacion'), UsuarioController.create)
router.post('/login', UsuarioController.login)
router.get('/', UsuarioController.getAll)
router.get('/', [verificaToken], UsuarioController.getAll)
router.get('/:id([0-9]+)', UsuarioController.getOne)