add toggleActivo

This commit is contained in:
2026-02-05 16:15:05 -06:00
parent a1b810fe60
commit dfc223f3f8
2 changed files with 43 additions and 4 deletions
+6 -1
View File
@@ -4,7 +4,7 @@ import { UpdateMesaDto } from './dto/update-mesa.dto';
@Controller('mesa')
export class MesaController {
constructor(private readonly mesaService: MesaService) {}
constructor(private readonly mesaService: MesaService) { }
@Get()
async findAll() {
@@ -23,4 +23,9 @@ export class MesaController {
) {
return this.mesaService.updateActivo(id, updateMesaDto);
}
@Patch(':id/activo')
toggleActivo(@Param('id') id: number) {
return this.mesaService.toggleActivo(+id);
}
}
+37 -3
View File
@@ -1,4 +1,4 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { UpdateMesaDto } from './dto/update-mesa.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
@@ -9,14 +9,14 @@ export class MesaService {
constructor(
@InjectRepository(Mesa)
private readonly mesaRepository: Repository<Mesa>,
) {}
) { }
async findAll(): Promise<Mesa[]> {
return await this.mesaRepository.find();
}
async findAllActivo(): Promise<Mesa[]> {
return await this.mesaRepository.find({where:{activo:true}});
return await this.mesaRepository.find({ where: { activo: true } });
}
async updateActivo(id: number, updateMesaDto: UpdateMesaDto) {
@@ -28,4 +28,38 @@ export class MesaService {
mesa.activo = updateMesaDto.activo;
return this.mesaRepository.save(mesa);
}
async toggleActivo(id_mesa: number) {
if (Number.isNaN(id_mesa)) {
throw new BadRequestException('ID de mesa inválido');
}
const mesa = await this.mesaRepository.findOne({
where: { id_mesa },
});
if (!mesa) {
throw new NotFoundException('Mesa no encontrada');
}
let activoActual: boolean;
if (Buffer.isBuffer(mesa.activo)) {
activoActual = mesa.activo[0] === 1;
} else {
activoActual = Boolean(mesa.activo);
}
const nuevoActivo = !activoActual;
await this.mesaRepository.update(id_mesa, {
activo: nuevoActivo,
});
return {
message: 'Estado de mesa actualizado',
id_mesa,
activo: nuevoActivo,
};
}
}