2025-09-16 22:37:25 -06:00
|
|
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
2025-09-17 16:35:52 -06:00
|
|
|
import { AlumnoService } from 'src/alumno/student.service';
|
2025-09-16 22:37:25 -06:00
|
|
|
import { DetalleServicioService } from 'src/detalle_servicio/detalle_servicio.service';
|
|
|
|
|
import { DataSource } from 'typeorm';
|
|
|
|
|
import { chargePrintDto } from './dto/operations.dto';
|
|
|
|
|
import { CreateDetalleServicioDto } from 'src/detalle_servicio/dto/create-detalle_servicio.dto';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class OperationsService {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly dataSource: DataSource,
|
2025-09-17 16:35:52 -06:00
|
|
|
private readonly alumnoService: AlumnoService,
|
2025-09-16 22:37:25 -06:00
|
|
|
private readonly detalleServicioService: DetalleServicioService,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
async chargePrint(data: chargePrintDto, userId: number) {
|
|
|
|
|
const { monto, id_cuenta } = data;
|
|
|
|
|
|
|
|
|
|
const queryRunner = this.dataSource.createQueryRunner();
|
|
|
|
|
await queryRunner.connect();
|
|
|
|
|
await queryRunner.startTransaction();
|
|
|
|
|
|
|
|
|
|
try {
|
2025-09-17 16:35:52 -06:00
|
|
|
const credit = await this.alumnoService.GetCredit(id_cuenta);
|
2025-09-16 22:37:25 -06:00
|
|
|
|
|
|
|
|
if (credit < monto) {
|
|
|
|
|
throw new BadRequestException('Crédito insuficiente');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const detalleData: CreateDetalleServicioDto = {
|
|
|
|
|
...data,
|
|
|
|
|
id_servicio: 1,
|
|
|
|
|
id_usuario: userId,
|
|
|
|
|
fecha_operacion: new Date(),
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-17 16:35:52 -06:00
|
|
|
await this.detalleServicioService.Create(
|
|
|
|
|
detalleData,
|
|
|
|
|
queryRunner.manager,
|
|
|
|
|
);
|
2025-09-16 22:37:25 -06:00
|
|
|
|
2025-09-17 16:35:52 -06:00
|
|
|
await this.alumnoService.UpdateCredit(
|
2025-09-16 22:37:25 -06:00
|
|
|
id_cuenta,
|
|
|
|
|
monto,
|
|
|
|
|
queryRunner.manager,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await queryRunner.commitTransaction();
|
|
|
|
|
return { message: 'correct' };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await queryRunner.rollbackTransaction();
|
|
|
|
|
return { message: 'error', error: error.message };
|
|
|
|
|
} finally {
|
|
|
|
|
await queryRunner.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|