feat: Add error handling and logging to group sync service

main
borja (aider) 3 months ago
parent 7778a5f366
commit 1ade146a3b

@ -66,28 +66,37 @@ export class GroupSyncService {
let added = 0; let added = 0;
let updated = 0; let updated = 0;
try {
db.transaction(() => { db.transaction(() => {
// First mark all groups as inactive // First mark all groups as inactive
db.exec('UPDATE groups SET active = FALSE'); const inactiveResult = db.exec('UPDATE groups SET active = FALSE');
console.log('Marked groups inactive:', inactiveResult);
for (const group of groups) { for (const group of groups) {
const existing = db.query('SELECT 1 FROM groups WHERE id = ?').get(group.id); const existing = db.query('SELECT 1 FROM groups WHERE id = ?').get(group.id);
console.log('Checking group:', group.id, 'exists:', !!existing);
if (existing) { if (existing) {
db.exec( const updateResult = db.exec(
'UPDATE groups SET name = ?, active = TRUE, last_verified = CURRENT_TIMESTAMP WHERE id = ?', 'UPDATE groups SET name = ?, active = TRUE, last_verified = CURRENT_TIMESTAMP WHERE id = ?',
[group.subject, group.id] [group.subject, group.id]
); );
console.log('Updated group:', group.id, 'result:', updateResult);
updated++; updated++;
} else { } else {
db.exec( const insertResult = db.exec(
'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)', 'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)',
[group.id, env.WHATSAPP_COMMUNITY_ID, group.subject] [group.id, env.WHATSAPP_COMMUNITY_ID, group.subject]
); );
console.log('Added group:', group.id, 'result:', insertResult);
added++; added++;
} }
} }
}); });
} catch (error) {
console.error('Error in upsertGroups:', error);
throw error;
}
console.log(`Group sync completed: ${added} added, ${updated} updated`); console.log(`Group sync completed: ${added} added, ${updated} updated`);
return { added, updated }; return { added, updated };

Loading…
Cancel
Save