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;
db.transaction(() => { try {
// First mark all groups as inactive db.transaction(() => {
db.exec('UPDATE groups SET active = FALSE'); // First mark all groups as inactive
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) {
db.exec( if (existing) {
'UPDATE groups SET name = ?, active = TRUE, last_verified = CURRENT_TIMESTAMP WHERE id = ?', const updateResult = db.exec(
[group.subject, group.id] 'UPDATE groups SET name = ?, active = TRUE, last_verified = CURRENT_TIMESTAMP WHERE id = ?',
); [group.subject, group.id]
updated++; );
} else { console.log('Updated group:', group.id, 'result:', updateResult);
db.exec( updated++;
'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)', } else {
[group.id, env.WHATSAPP_COMMUNITY_ID, group.subject] const insertResult = db.exec(
); 'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)',
added++; [group.id, env.WHATSAPP_COMMUNITY_ID, group.subject]
);
console.log('Added group:', group.id, 'result:', insertResult);
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