From 227094dafd02ed7db1022bb70694762dbd882d54 Mon Sep 17 00:00:00 2001 From: Dmytro Svyrydenko Date: Fri, 15 Nov 2024 22:44:59 +0100 Subject: [PATCH] fix: accounts linking to groups --- .../add-account-to-group.e2e.ts | 32 +++++++++++-------- .../account-groups/add-account-to-group.ts | 13 +++----- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/services/account-groups/add-account-to-group.e2e.ts b/src/services/account-groups/add-account-to-group.e2e.ts index 23993ef..f625d3b 100644 --- a/src/services/account-groups/add-account-to-group.e2e.ts +++ b/src/services/account-groups/add-account-to-group.e2e.ts @@ -18,35 +18,39 @@ describe('Add account to group', () => { expect(result.statusCode).toBe(201); }); - it('fails when account does not exist', async () => { + it('successfully adds account to group_1 -> group_2 -> group_1', async () => { + const group_2 = await helpers.createAccountGroup({ name: 'test-1', raw: true }); + await helpers.addAccountToGroup({ + accountId: account.id, + groupId: group.id, + }); + await helpers.addAccountToGroup({ + accountId: account.id, + groupId: group_2.id, + }); const result = await helpers.addAccountToGroup({ - accountId: 9999, + accountId: account.id, groupId: group.id, }); - expect(result.statusCode).toBe(404); + expect(result.statusCode).toBe(201); }); - it('fails when group does not exist', async () => { + it('fails when account does not exist', async () => { const result = await helpers.addAccountToGroup({ - accountId: account.id, - groupId: 9999, + accountId: 9999, + groupId: group.id, }); expect(result.statusCode).toBe(404); }); - it('does not allow duplicate entries', async () => { - await helpers.addAccountToGroup({ - accountId: account.id, - groupId: group.id, - }); - + it('fails when group does not exist', async () => { const result = await helpers.addAccountToGroup({ accountId: account.id, - groupId: group.id, + groupId: 9999, }); - expect(result.statusCode).toBe(409); + expect(result.statusCode).toBe(404); }); }); diff --git a/src/services/account-groups/add-account-to-group.ts b/src/services/account-groups/add-account-to-group.ts index 76bb359..8e3f8dc 100644 --- a/src/services/account-groups/add-account-to-group.ts +++ b/src/services/account-groups/add-account-to-group.ts @@ -1,7 +1,7 @@ import AccountGrouping from '@models/accounts-groups/AccountGrouping.model'; import { withTransaction } from '../common'; import Accounts from '@models/Accounts.model'; -import { ConflictError, NotAllowedError, NotFoundError } from '@js/errors'; +import { NotAllowedError, NotFoundError } from '@js/errors'; import AccountGroup from '@models/accounts-groups/AccountGroups.model'; import { logger } from '@js/utils'; @@ -23,13 +23,6 @@ export const addAccountToGroup = withTransaction( throw new NotFoundError({ message: 'Account group with such id is not found.' }); } - const existingGrouping = await AccountGrouping.findOne({ - where: { accountId, groupId }, - }); - if (existingGrouping) { - throw new ConflictError({ message: 'Account is already in this group' }); - } - if (existingAccount.userId !== existingGroup.userId) { logger.error('Tried to add account to a group with different userId in both.', { accountId, @@ -37,6 +30,10 @@ export const addAccountToGroup = withTransaction( }); throw new NotAllowedError({ message: 'Operation is not allowed' }); } + + // Remove all other account linkings + await AccountGrouping.destroy({ where: { accountId } }); + return AccountGrouping.create({ accountId, groupId }); }, );