|
|
@ -78,21 +78,49 @@ export class GroupSyncService {
|
|
|
|
|
|
|
|
|
|
|
|
private static async fetchGroupsFromAPI(): Promise<EvolutionGroup[]> {
|
|
|
|
private static async fetchGroupsFromAPI(): Promise<EvolutionGroup[]> {
|
|
|
|
const url = `${env.EVOLUTION_API_URL}/group/fetchAllGroups/${env.EVOLUTION_API_INSTANCE}?getParticipants=false`;
|
|
|
|
const url = `${env.EVOLUTION_API_URL}/group/fetchAllGroups/${env.EVOLUTION_API_INSTANCE}?getParticipants=false`;
|
|
|
|
const response = await fetch(url, {
|
|
|
|
console.log('ℹ️ Fetching groups from API:', {
|
|
|
|
headers: {
|
|
|
|
url: `${url.substring(0, 50)}...`, // Log partial URL for security
|
|
|
|
apikey: env.EVOLUTION_API_KEY,
|
|
|
|
communityId: env.WHATSAPP_COMMUNITY_ID,
|
|
|
|
},
|
|
|
|
time: new Date().toISOString()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
try {
|
|
|
|
throw new Error(`API request failed: ${response.statusText}`);
|
|
|
|
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') {
|
|
|
|
if (data.status !== 'success') {
|
|
|
|
throw new Error(`API error: ${data.message || 'Unknown error'}`);
|
|
|
|
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 {
|
|
|
|
private static cacheActiveGroups(): void {
|
|
|
|