Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 update existing active contacts #104

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading