Skip to content

Commit

Permalink
Merge pull request #139 from letehaha/fix/account-group-linking
Browse files Browse the repository at this point in the history
fix: accounts linking to groups
  • Loading branch information
letehaha authored Nov 15, 2024
2 parents 4f807c4 + 227094d commit e1ab2db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
32 changes: 18 additions & 14 deletions src/services/account-groups/add-account-to-group.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
13 changes: 5 additions & 8 deletions src/services/account-groups/add-account-to-group.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -23,20 +23,17 @@ 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,
groupId,
});
throw new NotAllowedError({ message: 'Operation is not allowed' });
}

// Remove all other account linkings
await AccountGrouping.destroy({ where: { accountId } });

return AccountGrouping.create({ accountId, groupId });
},
);

0 comments on commit e1ab2db

Please sign in to comment.