From 5efcbbc98b8381880db7da85c84d2f927da51013 Mon Sep 17 00:00:00 2001 From: brobert Date: Tue, 21 Oct 2025 19:14:42 +0200 Subject: [PATCH] refactor: usar getLastChangedActive y eliminar changedActive de syncGroups Co-authored-by: aider (openrouter/openai/gpt-5) --- src/server.ts | 7 +++++-- src/services/group-sync.ts | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/server.ts b/src/server.ts index 48ecb29..a7a4fc6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -211,8 +211,11 @@ export class WebhookServer { try { const res = await GroupSyncService.syncGroups(); GroupSyncService.refreshActiveGroupsCache(); - if (Array.isArray((res as any).changedActive) && (res as any).changedActive.length > 0) { - await GroupSyncService.syncMembersForGroups((res as any).changedActive); + const changed = GroupSyncService.getLastChangedActive(); + if (changed.length > 0) { + await GroupSyncService.syncMembersForGroups(changed); + } else { + await GroupSyncService.syncMembersForActiveGroups(); } } catch (e) { console.error('❌ Error handling groups.upsert:', e); diff --git a/src/services/group-sync.ts b/src/services/group-sync.ts index caaf5df..bb5c9be 100644 --- a/src/services/group-sync.ts +++ b/src/services/group-sync.ts @@ -75,8 +75,9 @@ export class GroupSyncService { private static _groupsIntervalMs: number | null = null; private static _groupsNextTickAt: number | null = null; private static _membersGlobalCooldownUntil: number = 0; + private static _lastChangedActive: string[] = []; - static async syncGroups(force: boolean = false): Promise<{ added: number; updated: number; changedActive: string[] }> { + static async syncGroups(force: boolean = false): Promise<{ added: number; updated: number }> { if (!this.shouldSync(force)) { return { added: 0, updated: 0 }; } @@ -189,7 +190,9 @@ export class GroupSyncService { // Duración opcional Metrics.set('last_sync_duration_ms', Date.now() - (typeof startedAt !== 'undefined' ? startedAt : Date.now())); - return { ...result, changedActive: newlyActivatedIds }; + // Guardar lista de grupos que han pasado a activos para consumo externo + this._lastChangedActive = Array.isArray(newlyActivatedIds) ? newlyActivatedIds : []; + return result; } catch (error) { console.error('Group sync failed:', error); Metrics.inc('sync_errors_total'); @@ -1010,6 +1013,14 @@ export class GroupSyncService { this.cacheActiveGroups(); } + public static getLastChangedActive(): string[] { + try { + return Array.from(this._lastChangedActive || []); + } catch { + return []; + } + } + public static startGroupsScheduler(): void { if (process.env.NODE_ENV === 'test') return; if (this._groupsSchedulerRunning) return;