Skip to content

Commit

Permalink
πŸ› fix: update existing active contacts (#104)
Browse files Browse the repository at this point in the history
* πŸ› fix: update existing active contacts

* πŸ§ͺ chore: update tests
  • Loading branch information
rezk2ll authored Jul 9, 2024
1 parent 3af3865 commit b38abac
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/tom-server/src/active-contacts-api/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ class ActiveContactsService implements IActiveContactsService {
*/
save = async (userId: string, contacts: string): Promise<void> => {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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')
Expand All @@ -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' }])

Expand Down

0 comments on commit b38abac

Please sign in to comment.