diff --git a/apps/server/src/modules/account/domain/services/account-db.service.spec.ts b/apps/server/src/modules/account/domain/services/account-db.service.spec.ts index 9ab458a441..9334a0ca93 100644 --- a/apps/server/src/modules/account/domain/services/account-db.service.spec.ts +++ b/apps/server/src/modules/account/domain/services/account-db.service.spec.ts @@ -691,6 +691,57 @@ describe('AccountDbService', () => { }); }); + describe('saveAll', () => { + describe('when given account that does not exist', () => { + const setup = () => { + const account = accountDoFactory.build({ + id: undefined, + }); + const savedAccount = accountDoFactory.build({ + ...account, + id: new ObjectId().toHexString(), + }); + + accountRepo.saveAll.mockResolvedValueOnce([savedAccount]); + + return { account, savedAccount }; + }; + + it('should save it', async () => { + const { account, savedAccount } = setup(); + + const result = await accountService.saveAll([account]); + + expect(result.length).toBe(1); + expect(result[0]).toStrictEqual(savedAccount); + expect(accountRepo.saveAll).toHaveBeenCalledTimes(1); + }); + }); + + describe('when given account that exist', () => { + const setup = () => { + const account = accountDoFactory.build(); + const foundAccount = accountDoFactory.build(); + const updateSpy = jest.spyOn(foundAccount, 'update'); + + accountRepo.findById.mockResolvedValueOnce(foundAccount); + accountRepo.saveAll.mockResolvedValueOnce([foundAccount]); + + return { account, foundAccount, updateSpy }; + }; + + it('should update it', async () => { + const { account, foundAccount, updateSpy } = setup(); + + const result = await accountService.saveAll([account]); + + expect(updateSpy).toHaveBeenCalledTimes(1); + expect(result.length).toBe(1); + expect(result[0].id).toBe(foundAccount.id); + }); + }); + }); + describe('updateUsername', () => { describe('when updating username', () => { const setup = () => { diff --git a/apps/server/src/modules/account/domain/services/account.service.spec.ts b/apps/server/src/modules/account/domain/services/account.service.spec.ts index 595dd31ee5..63d56f28f0 100644 --- a/apps/server/src/modules/account/domain/services/account.service.spec.ts +++ b/apps/server/src/modules/account/domain/services/account.service.spec.ts @@ -380,6 +380,30 @@ describe('AccountService', () => { }); }); + describe('saveAll', () => { + describe('when saving accounts', () => { + const setup = () => { + const accounts = accountDoFactory.buildList(1); + + configService.get.mockReturnValue(true); + accountServiceDb.saveAll.mockResolvedValueOnce(accounts); + accountServiceIdm.saveAll.mockResolvedValueOnce(accounts); + + return { accounts, sut: newAccountService() }; + }; + + it('should delegate to db and idm service', async () => { + const { accounts, sut } = setup(); + + const result = await sut.saveAll(accounts); + + expect(result).toBeDefined(); + expect(accountServiceDb.saveAll).toHaveBeenCalledTimes(1); + expect(accountServiceIdm.saveAll).toHaveBeenCalledTimes(1); + }); + }); + }); + describe('saveWithValidation', () => { describe('When calling with an empty username', () => { it('should throw an ValidationError', async () => { diff --git a/apps/server/src/modules/account/repo/micro-orm/account.repo.integration.spec.ts b/apps/server/src/modules/account/repo/micro-orm/account.repo.integration.spec.ts index 76aff5bad1..a20ee96bf1 100644 --- a/apps/server/src/modules/account/repo/micro-orm/account.repo.integration.spec.ts +++ b/apps/server/src/modules/account/repo/micro-orm/account.repo.integration.spec.ts @@ -68,6 +68,26 @@ describe('account repo', () => { }); }); + describe('saveAll', () => { + describe('When multiple accounts are given', () => { + const setup = () => { + const accounts = accountDoFactory.buildList(3); + const accountIds = accounts.map((account) => account.id); + + return { accounts, accountIds }; + }; + + it('should save all accounts', async () => { + const { accounts, accountIds } = setup(); + + await repo.saveAll(accounts); + + const foundAccounts = await em.find(AccountEntity, { id: { $in: accountIds } }); + expect(foundAccounts.length).toBe(accounts.length); + }); + }); + }); + describe('findById', () => { describe('When the account exists', () => { const setup = async () => { diff --git a/apps/server/src/modules/account/repo/micro-orm/account.repo.ts b/apps/server/src/modules/account/repo/micro-orm/account.repo.ts index d7179c5fe6..54641b9153 100644 --- a/apps/server/src/modules/account/repo/micro-orm/account.repo.ts +++ b/apps/server/src/modules/account/repo/micro-orm/account.repo.ts @@ -13,7 +13,7 @@ import { AccountScope } from './scope/account-scope'; export class AccountRepo { constructor(private readonly em: EntityManager) {} - get entityName() { + get entityName(): typeof AccountEntity { return AccountEntity; } @@ -93,7 +93,7 @@ export class AccountRepo { return AccountEntityToDoMapper.mapToDo(entity); } - getObjectReference>( + public getObjectReference>( entityName: EntityName, id: Primary | Primary[] ): Entity { @@ -186,7 +186,7 @@ export class AccountRepo { return AccountEntityToDoMapper.mapEntitiesToDos(result); } - async findByUserIdsAndSystemId(userIds: string[], systemId: string): Promise { + public async findByUserIdsAndSystemId(userIds: string[], systemId: string): Promise { const scope = new AccountScope(); const userIdScope = new AccountScope();