fix: properly commit group sync transactions and update counts

main
borja (aider) 3 months ago
parent 1ade146a3b
commit 98a01dad19

@ -66,39 +66,40 @@ export class GroupSyncService {
let added = 0; let added = 0;
let updated = 0; let updated = 0;
try { const transactionResult = db.transaction(() => {
db.transaction(() => {
// First mark all groups as inactive // First mark all groups as inactive
const inactiveResult = db.exec('UPDATE groups SET active = FALSE'); const inactiveResult = db.prepare('UPDATE groups SET active = FALSE').run();
console.log('Marked groups inactive:', inactiveResult); 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.prepare('SELECT 1 FROM groups WHERE id = ?').get(group.id);
console.log('Checking group:', group.id, 'exists:', !!existing); console.log('Checking group:', group.id, 'exists:', !!existing);
if (existing) { if (existing) {
const updateResult = db.exec( const updateResult = db.prepare(
'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] ).run(group.subject, group.id);
);
console.log('Updated group:', group.id, 'result:', updateResult); console.log('Updated group:', group.id, 'result:', updateResult);
updated++; updated++;
} else { } else {
const insertResult = db.exec( const insertResult = db.prepare(
'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] ).run(group.id, env.WHATSAPP_COMMUNITY_ID, group.subject);
);
console.log('Added group:', group.id, 'result:', insertResult); console.log('Added group:', group.id, 'result:', insertResult);
added++; added++;
} }
} }
return { added, updated };
}); });
try {
const result = transactionResult();
console.log(`Group sync completed: ${result.added} added, ${result.updated} updated`);
return result;
} catch (error) { } catch (error) {
console.error('Error in upsertGroups:', error); console.error('Error in upsertGroups:', error);
throw error; throw error;
} }
console.log(`Group sync completed: ${added} added, ${updated} updated`);
return { added, updated };
} }
} }

@ -134,12 +134,18 @@ describe('GroupSyncService', () => {
}); });
it('should handle API errors', async () => { it('should handle API errors', async () => {
const consoleErrorMock = mock(() => {});
console.error = consoleErrorMock;
globalThis.fetch = mock(async () => ({ globalThis.fetch = mock(async () => ({
ok: false, ok: false,
statusText: 'Not Found', statusText: 'Not Found'
json: async () => ({ status: 'error', message: 'Not Found' })
})); }));
await expect(GroupSyncService.syncGroups()).rejects.toThrow('API request failed: Not Found'); await expect(GroupSyncService.syncGroups()).rejects.toThrow('API request failed: Not Found');
expect(consoleErrorMock).toHaveBeenCalled();
console.error = originalConsoleError;
}); });
}); });
}); });

Loading…
Cancel
Save