13vo Commit
This commit is contained in:
parent
cf671a683e
commit
c8c754e796
6 changed files with 37 additions and 20 deletions
|
|
@ -19,7 +19,8 @@ HASH_SALT_ROUNDS=10
|
|||
OPENWA_URL=https://tu-servidor-openwa
|
||||
OPENWA_API_KEY=tu_api_key
|
||||
|
||||
# Fallback si configuracion.openwaSessionId está vacío en BD
|
||||
# Sesión(s) OpenWA para el PIN de login — ÚNICA FUENTE (la BD no participa).
|
||||
# Un id se usa siempre; varios ids coma-separados rotan round-robin en cada envío.
|
||||
OPENWA_SESSION_ID=
|
||||
|
||||
# Path del envío; {sessionId} se sustituye en runtime
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Configuracion } from '../../modules/configuracion/entities/configuracion.entity.js';
|
||||
|
||||
export type OpenwaSessionInfo = {
|
||||
id: string;
|
||||
|
|
@ -12,6 +11,9 @@ export type OpenwaSessionInfo = {
|
|||
export class OpenwaService {
|
||||
private readonly logger = new Logger(OpenwaService.name);
|
||||
|
||||
/** Índice de la rotación round-robin del pool OPENWA_SESSION_ID. */
|
||||
private indiceRotacion = 0;
|
||||
|
||||
private get baseUrl(): string {
|
||||
return (process.env.OPENWA_URL || '').replace(/\/$/, '');
|
||||
}
|
||||
|
|
@ -35,15 +37,26 @@ export class OpenwaService {
|
|||
}
|
||||
|
||||
/**
|
||||
* SessionId: DB (configuracion.openwaSessionId) → fallback .env OPENWA_SESSION_ID
|
||||
* Única fuente: `OPENWA_SESSION_ID` del .env (también sirve como pool
|
||||
* **coma-separado**). El PIN de login rota round-robin entre los ids
|
||||
* para no concentrar todo el volumen en un solo número:
|
||||
* - un id → siempre ese;
|
||||
* - varios → siguiente del pool en cada envío;
|
||||
* - vacío → `null` (el caller responde que no hay sesión).
|
||||
*
|
||||
* `configuracion.openwaSessionId` ya **no** se lee (fuente única .env).
|
||||
*/
|
||||
resolveSessionId(config?: Configuracion | null): string | null {
|
||||
const fromDb = config?.openwaSessionId?.trim();
|
||||
if (fromDb) {
|
||||
return fromDb;
|
||||
}
|
||||
const fromEnv = process.env.OPENWA_SESSION_ID?.trim();
|
||||
return fromEnv || null;
|
||||
resolveSessionId(): string | null {
|
||||
const sesiones = (process.env.OPENWA_SESSION_ID ?? '')
|
||||
.split(',')
|
||||
.map((id) => id.trim())
|
||||
.filter((id) => id.length > 0);
|
||||
if (sesiones.length === 0) return null;
|
||||
if (sesiones.length === 1) return sesiones[0];
|
||||
|
||||
const elegida = sesiones[this.indiceRotacion % sesiones.length];
|
||||
this.indiceRotacion = (this.indiceRotacion + 1) % sesiones.length;
|
||||
return elegida;
|
||||
}
|
||||
|
||||
async listSessions(): Promise<OpenwaSessionInfo[]> {
|
||||
|
|
|
|||
|
|
@ -4,13 +4,12 @@ import { AuthController } from './auth.controller.js';
|
|||
import { AuthService } from './auth.service.js';
|
||||
import { Persona } from '../persona/entities/persona.entity.js';
|
||||
import { CodigoOtp } from '../codigo-otp/entities/codigo-otp.entity.js';
|
||||
import { Configuracion } from '../configuracion/entities/configuracion.entity.js';
|
||||
import { HashService } from '../../common/services/hash.service.js';
|
||||
import { TokenService } from '../../common/services/token.service.js';
|
||||
import { OpenwaService } from '../../common/services/openwa.service.js';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Persona, CodigoOtp, Configuracion])],
|
||||
imports: [TypeOrmModule.forFeature([Persona, CodigoOtp])],
|
||||
controllers: [AuthController],
|
||||
providers: [AuthService, HashService, TokenService, OpenwaService],
|
||||
exports: [TokenService],
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import { InjectRepository } from '@nestjs/typeorm';
|
|||
import { Repository } from 'typeorm';
|
||||
import { Persona } from '../persona/entities/persona.entity.js';
|
||||
import { CodigoOtp } from '../codigo-otp/entities/codigo-otp.entity.js';
|
||||
import { Configuracion } from '../configuracion/entities/configuracion.entity.js';
|
||||
import { HashService } from '../../common/services/hash.service.js';
|
||||
import { TokenService } from '../../common/services/token.service.js';
|
||||
import { OpenwaService } from '../../common/services/openwa.service.js';
|
||||
|
|
@ -33,8 +32,6 @@ export class AuthService {
|
|||
private personaRepository: Repository<Persona>,
|
||||
@InjectRepository(CodigoOtp)
|
||||
private codigoOtpRepository: Repository<CodigoOtp>,
|
||||
@InjectRepository(Configuracion)
|
||||
private configuracionRepository: Repository<Configuracion>,
|
||||
private hashService: HashService,
|
||||
private tokenService: TokenService,
|
||||
private openwaService: OpenwaService,
|
||||
|
|
@ -362,12 +359,13 @@ export class AuthService {
|
|||
persona: Persona,
|
||||
codigo: string,
|
||||
): Promise<void> {
|
||||
const config = await this.configuracionRepository.findOneBy({});
|
||||
const sessionId = this.openwaService.resolveSessionId(config);
|
||||
// Fuente única: OPENWA_SESSION_ID del .env (pool coma-separado con
|
||||
// rotación round-robin; la BD ya no participa).
|
||||
const sessionId = this.openwaService.resolveSessionId();
|
||||
|
||||
if (!sessionId) {
|
||||
throw new Error(
|
||||
'No hay sessionId de OpenWA (configuracion.openwaSessionId ni OPENWA_SESSION_ID).',
|
||||
'No hay sessionId de OpenWA: definí OPENWA_SESSION_ID en .env (un id o una lista coma-separada).',
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,13 @@ export class Configuracion {
|
|||
@Column({ type: 'varchar', default: '-04:00' })
|
||||
zonaHoraria: string;
|
||||
|
||||
/** SessionId de OpenWA (Easy API). Si es null se usa OPENWA_SESSION_ID de .env */
|
||||
/**
|
||||
* SessionId de OpenWA (Easy API).
|
||||
*
|
||||
* **Sin uso en el envío de PIN** desde la tanda de rotación: la fuente
|
||||
* única es `OPENWA_SESSION_ID` del .env (pool coma-separado). Se conserva
|
||||
* el campo por compatibilidad con datos/configuración existentes.
|
||||
*/
|
||||
@Column({ type: 'varchar', nullable: true })
|
||||
openwaSessionId?: string | null;
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue