This commit is contained in:
2022-11-03 02:48:47 -06:00
parent f9a13021fc
commit aef87ffe94
14 changed files with 106 additions and 94 deletions
+4
View File
@@ -31,6 +31,7 @@ import { ModuloModule } from './modulo/modulo.module';
import { MultaModule } from './multa/multa.module';
import { NodemailerModule } from './nodemailer/nodemailer.module';
import { OperadorModule } from './operador/operador.module';
import { PasscodeModule } from './passcode/passcode.module';
import { PrestamoModule } from './prestamo/prestamo.module';
import { StatusModule } from './status/status.module';
import { TipoUsuarioModule } from './tipo-usuario/tipo-usuario.module';
@@ -63,6 +64,7 @@ import { Modulo } from './modulo/entity/modulo.entity';
import { Multa } from './multa/entity/multa.entity';
import { Nivel } from './institucion-carrera/entity/nivel.entity';
import { Operador } from './operador/entity/operador.entity';
import { Passcode } from './passcode/entity/passcode.entity';
import { Prestamo } from './prestamo/entity/prestamo.entity';
import { Programa } from './institucion-programa/entity/programa.entity';
import { Status } from './status/entity/status.entity';
@@ -143,6 +145,7 @@ import { FullInformacionUsuarioView } from './usuario/entity/views/full-informac
Multa,
Nivel,
Operador,
Passcode,
Prestamo,
Programa,
Status,
@@ -199,6 +202,7 @@ import { FullInformacionUsuarioView } from './usuario/entity/views/full-informac
MultaModule,
NodemailerModule,
OperadorModule,
PasscodeModule,
PrestamoModule,
StatusModule,
TipoUsuarioModule,
@@ -63,6 +63,7 @@ export class JwtStrategyService extends PassportStrategy(Strategy) {
);
}
user.operador = operador;
console.log(user.operador);
});
// Este objeto retornado se encuentra en el objeto request
// (El ojbeto que recibe toda la información de la petición)
+3 -3
View File
@@ -1,6 +1,6 @@
import { IsInt } from 'class-validator';
import { IsString } from 'class-validator';
export class DevolucionDto {
@IsInt()
passcode: number;
@IsString()
passcode: string;
}
+4 -4
View File
@@ -1,9 +1,9 @@
import { IsInt } from 'class-validator';
import { IsInt, IsString } from 'class-validator';
export class NotificacionDto {
@IsInt()
passcode: number;
@IsInt()
estatus: number;
@IsString()
passcode: string;
}
+2 -2
View File
@@ -2,8 +2,8 @@ import { IsString } from 'class-validator';
export class PrestamoDto {
@IsString()
passcode: string;
id_locker: string;
@IsString()
id_locker: string;
passcode: string;
}
@@ -1,6 +1,6 @@
import { Expose } from 'class-transformer';
export class DevolucionOutputDto {
export class CodeOutputDto {
@Expose()
code;
}
@@ -1,6 +0,0 @@
import { Expose } from 'class-transformer';
export class NotificacionOutputDto {
@Expose()
code;
}
+2 -2
View File
@@ -5,11 +5,11 @@ export class PasscodeOutputDto {
code;
@Expose()
passcode;
door;
@Expose()
id_locker;
@Expose()
door;
passcode;
}
+3 -3
View File
@@ -12,12 +12,12 @@ export class Passcode {
@PrimaryGeneratedColumn()
id_passcode: number;
@Column({ type: String, nullable: false })
passcode: string;
@Column({ type: Boolean, nullable: true, default: false })
abrio: boolean;
@Column({ type: String, nullable: true, default: null })
passcode: string;
@OneToOne(() => Prestamo, (Prestamo) => Prestamo.id_prestamo, {
eager: true,
})
+16 -13
View File
@@ -7,6 +7,7 @@ import {
Request,
UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import {
ApiBearerAuth,
ApiBody,
@@ -14,16 +15,15 @@ import {
ApiQuery,
ApiTags,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { Serealize } from '../interceptors/serialize.interceptor';
import { ValidarUsuarioService } from '../validar-usuario/validar-usuario.service';
import { DevolucionDto } from './dto/input/devolucion.dto';
import { DevolucionOutputDto } from './dto/output/devolucion.dto';
import { PasscodeService } from './passcode.service';
import { NotificacionDto } from './dto/input/notificacion.dto';
import { NotificacionOutputDto } from './dto/output/notificacion.dto';
import { ValidarUsuarioService } from '../validar-usuario/validar-usuario.service';
import { Modulo } from '../modulo/entity/modulo.entity';
import { Operador } from '../operador/entity/operador.entity';
import { DevolucionDto } from './dto/input/devolucion.dto';
import { NotificacionDto } from './dto/input/notificacion.dto';
import { PrestamoDto } from './dto/input/prestamo.dto';
import { CodeOutputDto } from './dto/output/code.dto';
import { PasscodeOutputDto } from './dto/output/passcode.dto';
@Controller('passcode')
@@ -54,7 +54,7 @@ export class PasscodeController {
prestamo(@Request() req, @Query() query: PrestamoDto) {
const operador: Operador = req.user.operador;
this.validarUsuarioService.validarAdminOperador(operador);
this.validarUsuarioService.validarSoloOperador(operador);
return this.passcodeService.findByPasscode(
query.passcode,
false,
@@ -62,7 +62,7 @@ export class PasscodeController {
);
}
@Serealize(NotificacionOutputDto)
@Serealize(CodeOutputDto)
@Put('notificacion')
@UseGuards(AuthGuard('jwt'))
@ApiOperation({
@@ -76,16 +76,18 @@ export class PasscodeController {
})
autoprestamo(@Request() req, @Body() body: NotificacionDto) {
const operador: Operador = req.user.operador;
this.validarUsuarioService.validarAdminOperador(operador);
const modulo: Modulo = req.user.modulo;
this.validarUsuarioService.validarSoloOperador(operador);
return this.passcodeService.entregarEquipo(
operador,
modulo,
body.passcode,
body.estatus,
operador,
);
}
@Serealize(DevolucionOutputDto)
@Serealize(CodeOutputDto)
@Put('devolucion')
@UseGuards(AuthGuard('jwt'))
@ApiOperation({ description: 'Endpoint que da por terminado un préstamo.' })
@@ -102,8 +104,9 @@ export class PasscodeController {
})
devolucion(@Request() req, @Body() body: DevolucionDto) {
const operador: Operador = req.user.operador;
const modulo: Modulo = req.user.modulo;
this.validarUsuarioService.validarAdminOperador(operador);
return this.passcodeService.devolverEquipo(body.passcode, operador);
this.validarUsuarioService.validarSoloOperador(operador);
return this.passcodeService.devolverEquipo(operador, modulo, body.passcode);
}
}
+52 -47
View File
@@ -6,7 +6,10 @@ import {
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Modulo } from '../modulo/entity/modulo.entity';
import { Operador } from '../operador/entity/operador.entity';
import { Passcode } from './entity/passcode.entity';
import { Prestamo } from '../prestamo/entity/prestamo.entity';
import { PrestamoService } from '../prestamo/prestamo.service';
@Injectable()
@@ -17,54 +20,56 @@ export class PasscodeService {
private prestamoService: PrestamoService,
) {}
crearPasscode(id_carrito, id_prestamo) {
const prestamo = {
crearPasscode(prestamo: Prestamo) {
const aux = {
1: '00000',
2: '0000',
3: '000',
4: '00',
5: '0',
6: '',
};
const passcode =
id_carrito +
prestamo[id_prestamo.length] +
id_prestamo +
Math.floor(Math.random() * 99999 + 10000).toString();
this.repository.save(
this.repository.create({
passcode,
prestamo: { id_prestamo },
}),
);
return passcode;
return this.repository
.save(this.repository.create({ prestamo }))
.then((passcodeInterno) => {
passcodeInterno.passcode = `${prestamo.equipo.carrito.id_carrito}${
aux[passcodeInterno.id_passcode.toString().length]
}${passcodeInterno.id_passcode}${Math.floor(
Math.random() * 99999 + 10000,
)}`;
return this.repository.save(passcodeInterno);
});
}
async devolverEquipo(passcode, operador) {
const passcodeInterno = await this.findByPasscode(passcode, true);
// return this.prestamoService
// .regresarIdPrestamo(
// operador.id_operador,
// passcodeInterno.prestamo.id_prestamo,
// )
// .then((_) => {
// return { code: 1 };
// });
devolverEquipo(operador: Operador, modulo: Modulo, passcode: string) {
return this.findByPasscode(passcode, true)
.then((passcodeInterno) =>
this.prestamoService.regresar(
operador,
modulo,
passcodeInterno.prestamo,
1,
),
)
.then((_) => ({ code: 1 }));
}
async entregarEquipo(passcode, estatus, operador) {
async entregarEquipo(
operador: Operador,
modulo: Modulo,
passcode: string,
estatus: number,
) {
const passcodeInterno = await this.findByPasscode(passcode, false);
if (estatus) {
if (passcodeInterno.abrio != estatus)
passcodeInterno.abrio = estatus === 1 ? true : false;
// return this.prestamoService
// .entregar(passcodeInterno.prestamo.id_prestamo, operador.id_operador)
// .then((_) => {
// return this.repository.save(passcodeInterno).then((_) => {
// return { code: 1 };
// });
// });
if (passcodeInterno.abrio != (estatus === 1))
passcodeInterno.abrio = estatus === 1;
return this.prestamoService
.entregar(operador, modulo, passcodeInterno.prestamo.id_prestamo)
.then((_) => this.repository.save(passcodeInterno))
.then((_) => ({ code: 1 }));
}
}
@@ -96,16 +101,16 @@ export class PasscodeService {
});
}
findPasscode(id_prestamo: number) {
return this.repository
.findOne({ where: { prestamo: { id_prestamo } } })
.then(async (passcode) => {
if (!passcode)
throw new BadRequestException({
message: `Este passcode no existe`,
code: 6,
});
return passcode.passcode;
});
}
// findPasscode(id_prestamo: number) {
// return this.repository
// .findOne({ where: { prestamo: { id_prestamo } } })
// .then(async (passcode) => {
// if (!passcode)
// throw new BadRequestException({
// message: `Este passcode no existe`,
// code: 6,
// });
// return passcode.passcode;
// });
// }
}
+7 -7
View File
@@ -178,7 +178,7 @@ export class PrestamoController {
const modulo: Modulo = req.user.modulo;
this.validarUsuarioService.validarAdminOperador(operador);
return this.prestamoService.entregar(operador, body.id_prestamo, modulo);
return this.prestamoService.entregar(operador, modulo, body.id_prestamo);
}
// @Serealize(PrestamosOutputDto)
@@ -505,17 +505,17 @@ export class PrestamoController {
})
regresarIdPrestamo(@Request() req, @Body() body: RegresarIdPrestamoDto) {
const operador: Operador = req.user.operador;
const modulo: Modulo = req.user.modulo || null;
const modulo: Modulo = req.user.modulo;
this.validarUsuarioService.validarAdminOperador(operador);
this.validarUsuarioService.validarSoloOperador(operador);
return this.prestamoService.regresarIdPrestamo(
operador,
modulo,
body.id_prestamo,
body.id_status,
body.motivo,
body.descripcion,
body.id_institucion_infraccion,
modulo,
);
}
@@ -543,17 +543,17 @@ export class PrestamoController {
@Body() body: RegresarNumeroInventarioDto,
) {
const operador: Operador = req.user.operador;
const modulo: Modulo = req.user.modulo || null;
const modulo: Modulo = req.user.modulo;
this.validarUsuarioService.validarAdminOperador(operador);
this.validarUsuarioService.validarSoloOperador(operador);
return this.prestamoService.regresarNumeroInventario(
operador,
modulo,
body.numero_inventario,
body.id_status,
body.motivo,
body.descripcion,
body.id_institucion_infraccion,
modulo,
);
}
}
+6 -6
View File
@@ -281,8 +281,8 @@ export class PrestamoService {
async entregar(
operadorEntrega: Operador,
id_prestamo: number,
modulo: Modulo,
id_prestamo: number,
) {
const ahora = moment();
const ahoraStr = ahora.format('YYYY-MM-DD');
@@ -742,12 +742,12 @@ export class PrestamoService {
async regresar(
operadorRegreso: Operador,
modulo: Modulo,
prestamo: Prestamo,
id_status: number,
motivo?: string,
descripcion?: string,
id_institucion_infraccion?: number,
modulo?: Modulo,
) {
const ahora = moment();
// La resta ahora - hora_fin nos da la cantidad de tiempo que se paso el usuario
@@ -821,35 +821,35 @@ export class PrestamoService {
regresarIdPrestamo(
operador: Operador,
modulo: Modulo,
id_prestamo: number,
id_status: number,
motivo: string,
descripcion?: string,
id_institucion_infraccion?: number,
modulo?: Modulo,
) {
// Busco el prestamo por id prestamo y luego regreso el equipo
return this.findById(id_prestamo).then((prestamo) =>
this.regresar(
operador,
modulo,
prestamo,
id_status,
motivo,
descripcion,
id_institucion_infraccion,
modulo,
),
);
}
regresarNumeroInventario(
operador: Operador,
modulo: Modulo,
numero_inventario: string,
id_status: number,
motivo: string,
descripcion?: string,
id_institucion_infraccion?: number,
modulo?: Modulo,
) {
// Busco el prestamo por número de inventario y la institución del operador
// luego regreso el equipo
@@ -859,12 +859,12 @@ export class PrestamoService {
).then((prestamo) =>
this.regresar(
operador,
modulo,
prestamo,
id_status,
motivo,
descripcion,
id_institucion_infraccion,
modulo,
),
);
}
@@ -31,6 +31,11 @@ export class ValidarUsuarioService {
this.sinPermisos();
}
validarSoloOperador(operador: Operador) {
if (!operador || operador.tipoUsuario.id_tipo_usuario != 4)
this.sinPermisos();
}
validarSuperAdmin(superAdmin: Operador) {
if (!superAdmin || superAdmin.tipoUsuario.id_tipo_usuario != 2)
this.sinPermisos();