diff --git a/src/services/group-sync.ts b/src/services/group-sync.ts index 3919159..0f00de0 100644 --- a/src/services/group-sync.ts +++ b/src/services/group-sync.ts @@ -66,39 +66,40 @@ export class GroupSyncService { let added = 0; let updated = 0; - try { - db.transaction(() => { - // First mark all groups as inactive - const inactiveResult = db.exec('UPDATE groups SET active = FALSE'); - console.log('Marked groups inactive:', inactiveResult); + const transactionResult = db.transaction(() => { + // First mark all groups as inactive + const inactiveResult = db.prepare('UPDATE groups SET active = FALSE').run(); + console.log('Marked groups inactive:', inactiveResult); - for (const group of groups) { - const existing = db.query('SELECT 1 FROM groups WHERE id = ?').get(group.id); - console.log('Checking group:', group.id, 'exists:', !!existing); - - if (existing) { - const updateResult = db.exec( - 'UPDATE groups SET name = ?, active = TRUE, last_verified = CURRENT_TIMESTAMP WHERE id = ?', - [group.subject, group.id] - ); - console.log('Updated group:', group.id, 'result:', updateResult); - updated++; - } else { - const insertResult = db.exec( - 'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)', - [group.id, env.WHATSAPP_COMMUNITY_ID, group.subject] - ); - console.log('Added group:', group.id, 'result:', insertResult); - added++; - } + for (const group of groups) { + const existing = db.prepare('SELECT 1 FROM groups WHERE id = ?').get(group.id); + console.log('Checking group:', group.id, 'exists:', !!existing); + + if (existing) { + const updateResult = db.prepare( + 'UPDATE groups SET name = ?, active = TRUE, last_verified = CURRENT_TIMESTAMP WHERE id = ?' + ).run(group.subject, group.id); + console.log('Updated group:', group.id, 'result:', updateResult); + updated++; + } else { + const insertResult = db.prepare( + 'INSERT INTO groups (id, community_id, name, active) VALUES (?, ?, ?, TRUE)' + ).run(group.id, env.WHATSAPP_COMMUNITY_ID, group.subject); + console.log('Added group:', group.id, 'result:', insertResult); + added++; } - }); + } + + return { added, updated }; + }); + + try { + const result = transactionResult(); + console.log(`Group sync completed: ${result.added} added, ${result.updated} updated`); + return result; } catch (error) { console.error('Error in upsertGroups:', error); throw error; } - - console.log(`Group sync completed: ${added} added, ${updated} updated`); - return { added, updated }; } } diff --git a/tests/unit/services/group-sync.test.ts b/tests/unit/services/group-sync.test.ts index d9521b5..0c3cbaf 100644 --- a/tests/unit/services/group-sync.test.ts +++ b/tests/unit/services/group-sync.test.ts @@ -134,12 +134,18 @@ describe('GroupSyncService', () => { }); it('should handle API errors', async () => { + const consoleErrorMock = mock(() => {}); + console.error = consoleErrorMock; + globalThis.fetch = mock(async () => ({ ok: false, - statusText: 'Not Found', - json: async () => ({ status: 'error', message: 'Not Found' }) + statusText: 'Not Found' })); + await expect(GroupSyncService.syncGroups()).rejects.toThrow('API request failed: Not Found'); + expect(consoleErrorMock).toHaveBeenCalled(); + + console.error = originalConsoleError; }); }); });