From c2c7e4fa494c82e4b53e0a6a62d4a6c0e7c9ca07 Mon Sep 17 00:00:00 2001 From: Khaled FERJANI Date: Tue, 9 Jul 2024 14:20:16 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix:=20update=20existing=20a?= =?UTF-8?q?ctive=20contacts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/active-contacts-api/services/index.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/tom-server/src/active-contacts-api/services/index.ts b/packages/tom-server/src/active-contacts-api/services/index.ts index 5b3dd48f..78472e76 100644 --- a/packages/tom-server/src/active-contacts-api/services/index.ts +++ b/packages/tom-server/src/active-contacts-api/services/index.ts @@ -55,6 +55,23 @@ class ActiveContactsService implements IActiveContactsService { */ save = async (userId: string, contacts: string): Promise => { try { + const existing = await this.db.get( + 'activeContacts' as Collections, + ['contacts'], + { userId } + ) + + if (existing.length > 0) { + await this.db.update( + 'activeContacts' as Collections, + { contacts }, + 'userId', + userId + ) + this.logger.info('active contacts updated successfully') + return + } + await this.db.insert('activeContacts' as Collections, { userId, contacts From 8f87f87aa840c7bde4f04d2f25333d8c21380426 Mon Sep 17 00:00:00 2001 From: Khaled FERJANI Date: Tue, 9 Jul 2024 14:20:36 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A7=AA=20chore:=20update=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/controllers.test.ts | 1 + .../active-contacts-api/tests/service.test.ts | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/tom-server/src/active-contacts-api/tests/controllers.test.ts b/packages/tom-server/src/active-contacts-api/tests/controllers.test.ts index 9dce895a..d792706f 100644 --- a/packages/tom-server/src/active-contacts-api/tests/controllers.test.ts +++ b/packages/tom-server/src/active-contacts-api/tests/controllers.test.ts @@ -91,6 +91,7 @@ describe('the active contacts API controller', () => { describe('active contacts save', () => { it('should try to save active contacts', async () => { + dbMock.get.mockResolvedValue([]) dbMock.insert.mockResolvedValue([]) const response = await supertest(app) diff --git a/packages/tom-server/src/active-contacts-api/tests/service.test.ts b/packages/tom-server/src/active-contacts-api/tests/service.test.ts index 33741258..264b9265 100644 --- a/packages/tom-server/src/active-contacts-api/tests/service.test.ts +++ b/packages/tom-server/src/active-contacts-api/tests/service.test.ts @@ -6,7 +6,8 @@ describe('The active contacts service', () => { const dbMock = { get: jest.fn(), insert: jest.fn(), - deleteEqual: jest.fn() + deleteEqual: jest.fn(), + update: jest.fn() } const loggerMock = { @@ -22,6 +23,7 @@ describe('The active contacts service', () => { it('should save active contacts for a user', async () => { dbMock.insert.mockResolvedValue(undefined) + dbMock.get.mockResolvedValue([]) await expect( activeContactsService.save('test', 'contact') @@ -33,6 +35,22 @@ describe('The active contacts service', () => { }) }) + it('should update active contacts for a user if there are existing ones', async () => { + dbMock.get.mockResolvedValue([{ userId: 'test', contacts: 'test contact' }]) + dbMock.update.mockResolvedValue(undefined) + + await expect( + activeContactsService.save('test', 'contact') + ).resolves.not.toThrow() + + expect(dbMock.update).toHaveBeenCalledWith( + 'activeContacts', + { contacts: 'contact' }, + 'userId', + 'test' + ) + }) + it('should fetch active contacts for a user', async () => { dbMock.get.mockResolvedValue([{ userId: 'test', contacts: 'contact' }])