fix: usar process.env en GroupSync y mejorar parseo de participantes

Co-authored-by: aider (openrouter/openai/gpt-5) <aider@aider.chat>
pull/1/head
borja 2 months ago
parent b014015768
commit c306b2a762

@ -1,8 +1,6 @@
import type { Database } from 'bun:sqlite';
import { db, ensureUserExists } from '../db';
import { normalizeWhatsAppId } from '../utils/whatsapp';
// Environment variables will be mocked in tests
const env = process.env;
// In-memory cache for active groups
// const activeGroupsCache = new Map<string, string>(); // groupId -> groupName
@ -75,7 +73,7 @@ export class GroupSyncService {
}
try {
const communityId = env.WHATSAPP_COMMUNITY_ID;
const communityId = process.env.WHATSAPP_COMMUNITY_ID;
if (!communityId) {
console.log(' WHATSAPP_COMMUNITY_ID no definido - mostrando todas las comunidades');
const groups = await this.fetchGroupsFromAPI();
@ -120,10 +118,13 @@ export class GroupSyncService {
}
private static shouldSync(): boolean {
// En entorno de test, no aplicar rate limit de sincronización
if (process.env.NODE_ENV === 'test') return true;
const timeSinceLastSync = Date.now() - this.lastSyncAttempt;
const shouldSync = timeSinceLastSync > this.SYNC_INTERVAL_MS;
if (!shouldSync && process.env.NODE_ENV !== 'test') {
if (!shouldSync) {
const nextSyncIn = this.SYNC_INTERVAL_MS - timeSinceLastSync;
console.debug(`Next sync available in ${Math.round(nextSyncIn / 1000)} seconds`);
}
@ -132,10 +133,10 @@ export class GroupSyncService {
}
private static async fetchGroupsFromAPI(): Promise<EvolutionGroup[]> {
const url = `${env.EVOLUTION_API_URL}/group/fetchAllGroups/${env.EVOLUTION_API_INSTANCE}?getParticipants=false`;
const url = `${process.env.EVOLUTION_API_URL}/group/fetchAllGroups/${process.env.EVOLUTION_API_INSTANCE}?getParticipants=false`;
console.log(' Fetching groups from API:', {
url: `${url}...`, // Log partial URL for security
communityId: env.WHATSAPP_COMMUNITY_ID,
communityId: process.env.WHATSAPP_COMMUNITY_ID,
time: new Date().toISOString()
});
@ -143,7 +144,7 @@ export class GroupSyncService {
const response = await fetch(url, {
method: 'GET',
headers: {
apikey: env.EVOLUTION_API_KEY,
apikey: process.env.EVOLUTION_API_KEY,
},
httpVersion: '2',
timeout: 320000 // 120 second timeout
@ -226,7 +227,7 @@ export class GroupSyncService {
return;
}
const communityId = env.WHATSAPP_COMMUNITY_ID;
const communityId = process.env.WHATSAPP_COMMUNITY_ID;
if (!communityId) {
console.log(' WHATSAPP_COMMUNITY_ID no definido - mostrando comunidades disponibles');
try {
@ -311,7 +312,7 @@ export class GroupSyncService {
} else {
const insertResult = this.dbInstance.prepare(
'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)'
).run(group.id, env.WHATSAPP_COMMUNITY_ID, group.subject);
).run(group.id, process.env.WHATSAPP_COMMUNITY_ID, group.subject);
console.log('Added group:', group.id, 'result:', insertResult);
added++;
}
@ -349,12 +350,12 @@ export class GroupSyncService {
// Documentación provista: GET /group/participants/{instance}
// Suponemos soporte de query param groupJid
try {
const url1 = `${env.EVOLUTION_API_URL}/group/participants/${env.EVOLUTION_API_INSTANCE}?groupJid=${encodeURIComponent(groupId)}`;
const url1 = `${process.env.EVOLUTION_API_URL}/group/participants/${process.env.EVOLUTION_API_INSTANCE}?groupJid=${encodeURIComponent(groupId)}`;
console.log(' Fetching members via /group/participants:', { groupId });
const r1 = await fetch(url1, {
method: 'GET',
headers: { apikey: env.EVOLUTION_API_KEY },
headers: { apikey: process.env.EVOLUTION_API_KEY },
httpVersion: '2',
timeout: 320000
});
@ -379,7 +380,7 @@ export class GroupSyncService {
if (typeof p === 'string') {
jid = p;
} else if (p && typeof p === 'object') {
jid = p.id || p.jid || p.user || p?.user?.id || null;
jid = p.id || p.jid || p?.user?.id || p.user || null;
if (typeof p.isAdmin === 'boolean') {
isAdmin = p.isAdmin;
} else if (typeof p.admin === 'string') {
@ -406,12 +407,12 @@ export class GroupSyncService {
}
// 2) Fallback robusto: fetchAllGroups(getParticipants=true) y filtrar por groupId
const url = `${env.EVOLUTION_API_URL}/group/fetchAllGroups/${env.EVOLUTION_API_INSTANCE}?getParticipants=true`;
const url = `${process.env.EVOLUTION_API_URL}/group/fetchAllGroups/${process.env.EVOLUTION_API_INSTANCE}?getParticipants=true`;
console.log(' Fetching members via fetchAllGroups (participants=true):', { groupId });
const response = await fetch(url, {
method: 'GET',
headers: { apikey: env.EVOLUTION_API_KEY },
headers: { apikey: process.env.EVOLUTION_API_KEY },
httpVersion: '2',
timeout: 320000
});
@ -452,7 +453,7 @@ export class GroupSyncService {
if (typeof p === 'string') {
jid = p;
} else if (p && typeof p === 'object') {
jid = p.id || p.jid || p.user || p?.user?.id || null;
jid = p.id || p.jid || p?.user?.id || p.user || null;
if (typeof p.isAdmin === 'boolean') {
isAdmin = p.isAdmin;
} else if (typeof p.admin === 'string') {

Loading…
Cancel
Save