fix: usar getDb() en IdentityService y quitar AllowedGroups.dbInstance

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
main
brobert 1 month ago
parent e415a26442
commit 6cb6c31d8d

@ -190,7 +190,7 @@ export class GroupSyncService {
} }
// Completar labels faltantes en allowed_groups usando todos los grupos devueltos por la API // Completar labels faltantes en allowed_groups usando todos los grupos devueltos por la API
try { AllowedGroups.dbInstance = this.dbInstance; this.fillMissingAllowedGroupLabels(groups); } catch {} try { this.fillMissingAllowedGroupLabels(groups); } catch {}
// Actualizar métricas // Actualizar métricas
this.cacheActiveGroups(); this.cacheActiveGroups();
@ -270,7 +270,7 @@ export class GroupSyncService {
if (!id) continue; if (!id) continue;
const label = nameById.get(id); const label = nameById.get(id);
if (label) { if (label) {
try { (AllowedGroups as any).dbInstance = this.dbInstance; AllowedGroups.upsertPending(id, label, null); } catch {} try { AllowedGroups.upsertPending(id, label, null); } catch {}
filled++; filled++;
} }
} }
@ -459,7 +459,7 @@ export class GroupSyncService {
const mode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase(); const mode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase();
const enforce = mode === 'enforce'; const enforce = mode === 'enforce';
if (enforce) { if (enforce) {
try { AllowedGroups.dbInstance = this.dbInstance; } catch {} // no-op
} }
let groups = 0, added = 0, updated = 0, deactivated = 0; let groups = 0, added = 0, updated = 0, deactivated = 0;
@ -502,7 +502,7 @@ export class GroupSyncService {
const mode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase(); const mode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase();
const enforce = mode === 'enforce'; const enforce = mode === 'enforce';
if (enforce) { if (enforce) {
try { AllowedGroups.dbInstance = this.dbInstance; } catch {} // no-op
} }
let groups = 0, added = 0, updated = 0, deactivated = 0; let groups = 0, added = 0, updated = 0, deactivated = 0;
@ -734,7 +734,7 @@ export class GroupSyncService {
const cached = this.activeGroupsCache.get(groupId); const cached = this.activeGroupsCache.get(groupId);
if (cached && cached.trim()) { if (cached && cached.trim()) {
try { this.ensureGroupExists(groupId, cached); } catch {} try { this.ensureGroupExists(groupId, cached); } catch {}
try { AllowedGroups.dbInstance = this.dbInstance; AllowedGroups.upsertPending(groupId, cached, null); } catch {} try { AllowedGroups.upsertPending(groupId, cached, null); } catch {}
this.cacheActiveGroups(); this.cacheActiveGroups();
return cached; return cached;
} }
@ -745,7 +745,7 @@ export class GroupSyncService {
const name = row?.name ? String(row.name).trim() : ''; const name = row?.name ? String(row.name).trim() : '';
if (name) { if (name) {
try { this.ensureGroupExists(groupId, name); } catch {} try { this.ensureGroupExists(groupId, name); } catch {}
try { AllowedGroups.dbInstance = this.dbInstance; AllowedGroups.upsertPending(groupId, name, null); } catch {} try { AllowedGroups.upsertPending(groupId, name, null); } catch {}
this.cacheActiveGroups(); this.cacheActiveGroups();
return name; return name;
} }
@ -758,7 +758,7 @@ export class GroupSyncService {
const subject = g?.subject ? String(g.subject).trim() : ''; const subject = g?.subject ? String(g.subject).trim() : '';
if (subject) { if (subject) {
try { this.ensureGroupExists(groupId, subject); } catch {} try { this.ensureGroupExists(groupId, subject); } catch {}
try { AllowedGroups.dbInstance = this.dbInstance; AllowedGroups.upsertPending(groupId, subject, null); } catch {} try { AllowedGroups.upsertPending(groupId, subject, null); } catch {}
this.cacheActiveGroups(); this.cacheActiveGroups();
return subject; return subject;
} }
@ -779,7 +779,7 @@ export class GroupSyncService {
const mode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase(); const mode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase();
if (mode === 'enforce') { if (mode === 'enforce') {
try { try {
(AllowedGroups as any).dbInstance = this.dbInstance; // no-op
if (!AllowedGroups.isAllowed(groupId)) { if (!AllowedGroups.isAllowed(groupId)) {
try { Metrics.inc('sync_skipped_group_total'); } catch {} try { Metrics.inc('sync_skipped_group_total'); } catch {}
return { added: 0, updated: 0, deactivated: 0 }; return { added: 0, updated: 0, deactivated: 0 };

@ -16,7 +16,7 @@ export class IdentityService {
const u = normalizeWhatsAppId(userId || ''); const u = normalizeWhatsAppId(userId || '');
if (!a || !u || a === u) return false; if (!a || !u || a === u) return false;
// Asegurar que el user_id numérico exista para no violar la FK (user_aliases.user_id -> users.id) // Asegurar que el user_id numérico exista para no violar la FK (user_aliases.user_id -> users.id)
try { ensureUserExists(u, this.dbInstance); } catch {} try { ensureUserExists(u, getDb()); } catch {}
try { try {
getDb().prepare(` getDb().prepare(`
INSERT INTO user_aliases (alias, user_id, source, created_at, updated_at) INSERT INTO user_aliases (alias, user_id, source, created_at, updated_at)

Loading…
Cancel
Save