148 lines
5.1 KiB
TypeScript
148 lines
5.1 KiB
TypeScript
import { TarifasConfig, ITarifasConfig } from '../models/TarifasConfig';
|
|
import { whatsAppNotificationService } from './whatsapp-notification.service';
|
|
|
|
export interface UpdateTarifasDTO {
|
|
tarifaPasajeroFija?: number;
|
|
precioBasePaquete?: number;
|
|
precioKmPaquete?: number;
|
|
factorPaquetes?: {
|
|
sobre?: number;
|
|
mediano?: number;
|
|
grande?: number;
|
|
};
|
|
seguroBasePaquete?: number;
|
|
porcentajeComisionAdmin?: number;
|
|
costoPenalidadCancelacion?: number;
|
|
radioMaxPoolingMetros?: number;
|
|
numeroAtencionWhatsApp?: string;
|
|
nombreSoporte?: string;
|
|
grupoWhatsAppPacificTrans?: string;
|
|
grupoWhatsAppDripzone?: string;
|
|
}
|
|
|
|
export class TariffsService {
|
|
private static instance: TariffsService;
|
|
|
|
private constructor() {}
|
|
|
|
public static getInstance(): TariffsService {
|
|
if (!TariffsService.instance) {
|
|
TariffsService.instance = new TariffsService();
|
|
}
|
|
return TariffsService.instance;
|
|
}
|
|
|
|
/**
|
|
* Obtiene la configuración de tarifas activa en MongoDB.
|
|
* Si no existe, inicializa la configuración predeterminada oficial.
|
|
*/
|
|
public async getTarifasActivas(): Promise<ITarifasConfig> {
|
|
let config = await TarifasConfig.findOne({ estaActiva: true });
|
|
if (!config) {
|
|
config = await TarifasConfig.create({
|
|
version: 'v1.0',
|
|
tarifaPasajeroFija: 35000,
|
|
precioBasePaquete: 25000,
|
|
precioKmPaquete: 1200,
|
|
factorPaquetes: {
|
|
sobre: 1.0,
|
|
mediano: 1.6,
|
|
grande: 2.2,
|
|
},
|
|
seguroBasePaquete: 5000,
|
|
porcentajeComisionAdmin: 15,
|
|
costoPenalidadCancelacion: 15000,
|
|
radioMaxPoolingMetros: 4000,
|
|
numeroAtencionWhatsApp: '573167455877',
|
|
nombreSoporte: 'Atención Pacific Trans GO',
|
|
estaActiva: true,
|
|
});
|
|
}
|
|
// Sincronizar número oficial con el servicio de mensajería de WhatsApp
|
|
if (config.numeroAtencionWhatsApp) {
|
|
whatsAppNotificationService.setAdminPhone(config.numeroAtencionWhatsApp);
|
|
}
|
|
return config;
|
|
}
|
|
|
|
/**
|
|
* Actualiza los valores de tarifas en la plataforma y persiste en MongoDB.
|
|
*/
|
|
public async updateTarifas(updates: UpdateTarifasDTO): Promise<ITarifasConfig> {
|
|
const config = await this.getTarifasActivas();
|
|
|
|
if (updates.tarifaPasajeroFija !== undefined && updates.tarifaPasajeroFija > 0) {
|
|
config.tarifaPasajeroFija = updates.tarifaPasajeroFija;
|
|
}
|
|
|
|
if (updates.precioBasePaquete !== undefined && updates.precioBasePaquete >= 0) {
|
|
config.precioBasePaquete = updates.precioBasePaquete;
|
|
}
|
|
|
|
if (updates.precioKmPaquete !== undefined && updates.precioKmPaquete >= 0) {
|
|
config.precioKmPaquete = updates.precioKmPaquete;
|
|
}
|
|
|
|
if (updates.factorPaquetes) {
|
|
if (updates.factorPaquetes.sobre !== undefined) {
|
|
config.factorPaquetes.sobre = updates.factorPaquetes.sobre;
|
|
}
|
|
if (updates.factorPaquetes.mediano !== undefined) {
|
|
config.factorPaquetes.mediano = updates.factorPaquetes.mediano;
|
|
}
|
|
if (updates.factorPaquetes.grande !== undefined) {
|
|
config.factorPaquetes.grande = updates.factorPaquetes.grande;
|
|
}
|
|
}
|
|
|
|
if (updates.seguroBasePaquete !== undefined && updates.seguroBasePaquete >= 0) {
|
|
config.seguroBasePaquete = updates.seguroBasePaquete;
|
|
}
|
|
|
|
if (updates.porcentajeComisionAdmin !== undefined && updates.porcentajeComisionAdmin >= 0 && updates.porcentajeComisionAdmin <= 100) {
|
|
config.porcentajeComisionAdmin = updates.porcentajeComisionAdmin;
|
|
}
|
|
|
|
if (updates.costoPenalidadCancelacion !== undefined && updates.costoPenalidadCancelacion >= 0) {
|
|
config.costoPenalidadCancelacion = updates.costoPenalidadCancelacion;
|
|
}
|
|
|
|
if (updates.radioMaxPoolingMetros !== undefined && updates.radioMaxPoolingMetros > 0) {
|
|
config.radioMaxPoolingMetros = updates.radioMaxPoolingMetros;
|
|
}
|
|
|
|
if (updates.numeroAtencionWhatsApp !== undefined && updates.numeroAtencionWhatsApp.trim()) {
|
|
config.numeroAtencionWhatsApp = updates.numeroAtencionWhatsApp.trim();
|
|
whatsAppNotificationService.setAdminPhone(config.numeroAtencionWhatsApp);
|
|
}
|
|
|
|
if (updates.nombreSoporte !== undefined && updates.nombreSoporte.trim()) {
|
|
config.nombreSoporte = updates.nombreSoporte.trim();
|
|
}
|
|
|
|
if (updates.grupoWhatsAppPacificTrans !== undefined && updates.grupoWhatsAppPacificTrans.trim()) {
|
|
config.grupoWhatsAppPacificTrans = updates.grupoWhatsAppPacificTrans.trim();
|
|
}
|
|
|
|
if (updates.grupoWhatsAppDripzone !== undefined && updates.grupoWhatsAppDripzone.trim()) {
|
|
config.grupoWhatsAppDripzone = updates.grupoWhatsAppDripzone.trim();
|
|
}
|
|
|
|
config.updatedAt = new Date();
|
|
await config.save();
|
|
|
|
console.log('✅ [Tarifas y Configuración Actualizadas]:', {
|
|
tarifaPasajeroFija: config.tarifaPasajeroFija,
|
|
precioBasePaquete: config.precioBasePaquete,
|
|
precioKmPaquete: config.precioKmPaquete,
|
|
porcentajeComisionAdmin: config.porcentajeComisionAdmin,
|
|
numeroAtencionWhatsApp: config.numeroAtencionWhatsApp,
|
|
nombreSoporte: config.nombreSoporte
|
|
});
|
|
|
|
return config;
|
|
}
|
|
}
|
|
|
|
export const tariffsService = TariffsService.getInstance();
|