diff --git a/src/services/group-sync.ts b/src/services/group-sync.ts index 4f2af8e..0b65d81 100644 --- a/src/services/group-sync.ts +++ b/src/services/group-sync.ts @@ -78,21 +78,49 @@ export class GroupSyncService { private static async fetchGroupsFromAPI(): Promise { const url = `${env.EVOLUTION_API_URL}/group/fetchAllGroups/${env.EVOLUTION_API_INSTANCE}?getParticipants=false`; - const response = await fetch(url, { - headers: { - apikey: env.EVOLUTION_API_KEY, - }, + console.log('ℹ️ Fetching groups from API:', { + url: `${url.substring(0, 50)}...`, // Log partial URL for security + communityId: env.WHATSAPP_COMMUNITY_ID, + time: new Date().toISOString() }); - if (!response.ok) { - throw new Error(`API request failed: ${response.statusText}`); - } + try { + const response = await fetch(url, { + headers: { + apikey: env.EVOLUTION_API_KEY, + }, + timeout: 120000 // 120 second timeout + }); + + if (!response.ok) { + const errorBody = await response.text().catch(() => 'Unable to read error body'); + console.error('❌ API request failed:', { + status: response.status, + statusText: response.statusText, + headers: Object.fromEntries(response.headers.entries()), + body: errorBody + }); + throw new Error(`API request failed: ${response.status} ${response.statusText}`); + } + + const data = await response.json(); + console.log('ℹ️ API response:', { + status: data.status, + message: data.message, + responseLength: data.response?.length || 0 + }); - const data = await response.json(); - if (data.status !== 'success') { - throw new Error(`API error: ${data.message || 'Unknown error'}`); + if (data.status !== 'success') { + throw new Error(`API error: ${data.message || 'Unknown error'}`); + } + return data.response; + } catch (error) { + console.error('❌ Failed to fetch groups:', { + error: error instanceof Error ? error.message : String(error), + stack: error instanceof Error ? error.stack : undefined + }); + throw error; } - return data.response; } private static cacheActiveGroups(): void {