feat: add initial group sync check at startup

pull/1/head
borja (aider) 7 months ago
parent 081ed51bab
commit a2487e5c2f

@ -1,5 +1,6 @@
/// <reference types="bun-types" /> /// <reference types="bun-types" />
import { CommandService } from './services/command'; import { CommandService } from './services/command';
import { GroupSyncService } from './services/group-sync';
import { ResponseQueue } from './services/response-queue'; import { ResponseQueue } from './services/response-queue';
import { WebhookManager } from './services/webhook-manager'; import { WebhookManager } from './services/webhook-manager';
@ -216,6 +217,9 @@ export class WebhookServer {
process.exit(1); process.exit(1);
} }
} }
// Initialize groups - critical for operation
await GroupSyncService.checkInitialGroups();
} catch (error) { } catch (error) {
console.error('❌ Failed to setup webhook:', error instanceof Error ? error.message : error); console.error('❌ Failed to setup webhook:', error instanceof Error ? error.message : error);
process.exit(1); process.exit(1);

@ -2,6 +2,9 @@ import { db } from '../db';
// Environment variables will be mocked in tests // Environment variables will be mocked in tests
const env = process.env; const env = process.env;
// In-memory cache for active groups
const activeGroupsCache = new Map<string, string>(); // groupId -> groupName
type EvolutionGroup = { type EvolutionGroup = {
id: string; id: string;
subject: string; subject: string;
@ -92,6 +95,43 @@ export class GroupSyncService {
return data.response; return data.response;
} }
private static cacheActiveGroups(): void {
const groups = db.prepare('SELECT id, name FROM groups WHERE active = TRUE').all();
activeGroupsCache.clear();
for (const group of groups) {
activeGroupsCache.set(group.id, group.name);
}
console.log(`Cached ${activeGroupsCache.size} active groups`);
}
private static getActiveGroupsCount(): number {
const result = db.prepare('SELECT COUNT(*) as count FROM groups WHERE active = TRUE').get();
return result?.count || 0;
}
static async checkInitialGroups(): Promise<void> {
const count = this.getActiveGroupsCount();
if (count > 0) {
this.cacheActiveGroups();
console.log(`✅ Using ${count} existing groups from database`);
return;
}
console.log('⚠️ No groups found in database - performing initial sync');
try {
const { added } = await this.syncGroups();
if (added === 0) {
throw new Error('Initial group sync completed but no groups were added');
}
this.cacheActiveGroups();
console.log(`✅ Initial group sync completed - added ${added} groups`);
} catch (error) {
console.error('❌ Critical: Initial group sync failed - no groups available');
console.error(error instanceof Error ? error.message : 'Unknown error');
process.exit(1);
}
}
private static async upsertGroups(groups: EvolutionGroup[]): Promise<{ added: number; updated: number }> { private static async upsertGroups(groups: EvolutionGroup[]): Promise<{ added: number; updated: number }> {
let added = 0; let added = 0;
let updated = 0; let updated = 0;

Loading…
Cancel
Save