150 lines
5 KiB
TypeScript
150 lines
5 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { Configuracion } from './entities/configuracion.entity.js';
|
|
import { ConfiguracionDto } from './dto/configuracion.dto.js';
|
|
import { UpdateConfiguracionDto } from './dto/update-configuracion.dto.js';
|
|
import { defaultServiceResult, ServiceResult } from '../../common/interfaces/service.result.js';
|
|
import { ZONA_HORA_DEFAULT, ZONA_HORA_RE } from '../../common/utils/zona-horaria.js';
|
|
|
|
@Injectable()
|
|
export class ConfiguracionService {
|
|
constructor(
|
|
@InjectRepository(Configuracion)
|
|
private configuracionRepository: Repository<Configuracion>,
|
|
) {}
|
|
|
|
async findUnique(): Promise<ServiceResult> {
|
|
const serviceResult = defaultServiceResult();
|
|
|
|
const result = await this.configuracionRepository.findOneBy({});
|
|
|
|
if (result) {
|
|
serviceResult.boolean = true;
|
|
serviceResult.message = 'Existe una configuración.';
|
|
serviceResult.number = 1;
|
|
serviceResult.object = result;
|
|
} else {
|
|
serviceResult.message = 'No existe la configuración.';
|
|
}
|
|
|
|
return serviceResult;
|
|
}
|
|
|
|
/**
|
|
* Singleton: solo se crea si no existe ninguna fila.
|
|
*/
|
|
async create(configuracionDto: ConfiguracionDto): Promise<ServiceResult> {
|
|
const serviceResult = defaultServiceResult();
|
|
|
|
const exists = await this.configuracionRepository.findOneBy({});
|
|
|
|
if (exists) {
|
|
serviceResult.message = 'Ya existe una configuración. Use PATCH para actualizarla.';
|
|
return serviceResult;
|
|
}
|
|
|
|
if (configuracionDto.distancia <= 0) {
|
|
serviceResult.message = 'La distancia debe ser mayor a 0.';
|
|
return serviceResult;
|
|
}
|
|
if (
|
|
configuracionDto.tolerancia !== undefined &&
|
|
configuracionDto.tolerancia < 0
|
|
) {
|
|
serviceResult.message = 'La tolerancia no puede ser negativa.';
|
|
return serviceResult;
|
|
}
|
|
if (
|
|
configuracionDto.zonaHoraria !== undefined &&
|
|
!ZONA_HORA_RE.test(configuracionDto.zonaHoraria)
|
|
) {
|
|
serviceResult.message =
|
|
'Zona horaria inválida (use ±HH:MM, p. ej. -04:00).';
|
|
return serviceResult;
|
|
}
|
|
|
|
const entity = this.configuracionRepository.create({
|
|
lat: configuracionDto.lat,
|
|
lng: configuracionDto.lng,
|
|
distancia: configuracionDto.distancia,
|
|
tolerancia: configuracionDto.tolerancia ?? 0,
|
|
zonaHoraria: configuracionDto.zonaHoraria ?? ZONA_HORA_DEFAULT,
|
|
openwaSessionId: configuracionDto.openwaSessionId ?? null,
|
|
});
|
|
const result = await this.configuracionRepository.save(entity);
|
|
|
|
serviceResult.boolean = true;
|
|
serviceResult.message = 'Configuración creada correctamente.';
|
|
serviceResult.number = 1;
|
|
serviceResult.object = result;
|
|
|
|
return serviceResult;
|
|
}
|
|
|
|
async update(updateConfiguracionDto: UpdateConfiguracionDto): Promise<ServiceResult> {
|
|
const serviceResult = defaultServiceResult();
|
|
|
|
const exists = await this.configuracionRepository.findOneBy({});
|
|
|
|
if (!exists) {
|
|
serviceResult.message = 'No existe la configuración. Cree la alta inicial primero.';
|
|
return serviceResult;
|
|
}
|
|
|
|
if (
|
|
updateConfiguracionDto.distancia !== undefined &&
|
|
updateConfiguracionDto.distancia <= 0
|
|
) {
|
|
serviceResult.message = 'La distancia debe ser mayor a 0.';
|
|
return serviceResult;
|
|
}
|
|
if (
|
|
updateConfiguracionDto.tolerancia !== undefined &&
|
|
updateConfiguracionDto.tolerancia < 0
|
|
) {
|
|
serviceResult.message = 'La tolerancia no puede ser negativa.';
|
|
return serviceResult;
|
|
}
|
|
if (
|
|
updateConfiguracionDto.zonaHoraria !== undefined &&
|
|
!ZONA_HORA_RE.test(updateConfiguracionDto.zonaHoraria)
|
|
) {
|
|
serviceResult.message =
|
|
'Zona horaria inválida (use ±HH:MM, p. ej. -04:00).';
|
|
return serviceResult;
|
|
}
|
|
|
|
const clean: Partial<Configuracion> = {};
|
|
if (updateConfiguracionDto.lat !== undefined) clean.lat = updateConfiguracionDto.lat;
|
|
if (updateConfiguracionDto.lng !== undefined) clean.lng = updateConfiguracionDto.lng;
|
|
if (updateConfiguracionDto.distancia !== undefined) {
|
|
clean.distancia = updateConfiguracionDto.distancia;
|
|
}
|
|
if (updateConfiguracionDto.tolerancia !== undefined) {
|
|
clean.tolerancia = updateConfiguracionDto.tolerancia;
|
|
}
|
|
if (updateConfiguracionDto.zonaHoraria !== undefined) {
|
|
clean.zonaHoraria = updateConfiguracionDto.zonaHoraria;
|
|
}
|
|
if (updateConfiguracionDto.openwaSessionId !== undefined) {
|
|
clean.openwaSessionId = updateConfiguracionDto.openwaSessionId;
|
|
}
|
|
|
|
if (Object.keys(clean).length === 0) {
|
|
serviceResult.message = 'No hay campos para actualizar.';
|
|
return serviceResult;
|
|
}
|
|
|
|
const result = await this.configuracionRepository.update(exists.idConfig, clean);
|
|
|
|
serviceResult.boolean = result.affected === 1;
|
|
serviceResult.message =
|
|
result.affected === 1
|
|
? 'Configuración actualizada correctamente.'
|
|
: 'No se encontró la configuración.';
|
|
serviceResult.number = result.affected || 0;
|
|
|
|
return serviceResult;
|
|
}
|
|
}
|