diff --git a/src/modules/workspaces/workspaces.usecase.spec.ts b/src/modules/workspaces/workspaces.usecase.spec.ts index 901120f1..79d054bf 100644 --- a/src/modules/workspaces/workspaces.usecase.spec.ts +++ b/src/modules/workspaces/workspaces.usecase.spec.ts @@ -1931,8 +1931,37 @@ describe('WorkspacesUsecases', () => { ).rejects.toThrow(BadRequestException); }); + it('When new space to be assigned is higher than the max assignable space per user in the workspace, then it should throw', async () => { + const workspace = newWorkspace({ attributes: { numberOfSeats: 11 } }); + const member = newWorkspaceUser({ attributes: { spaceLimit: 500 } }); + const workspaceUser = newUser(); + const mockedUsedSpace = 400; + + jest.spyOn(workspaceRepository, 'findById').mockResolvedValue(workspace); + jest + .spyOn(workspaceRepository, 'findWorkspaceUser') + .mockResolvedValue(member); + jest.spyOn(userRepository, 'findByUuid').mockResolvedValue(workspaceUser); + jest + .spyOn(service, 'getAssignableSpaceInWorkspace') + .mockResolvedValue(2000); + jest.spyOn(member, 'getUsedSpace').mockReturnValue(mockedUsedSpace); + jest.spyOn(service, 'adjustOwnerStorage').mockResolvedValue(); + jest + .spyOn(service, 'getWorkspaceNetworkLimit') + .mockResolvedValueOnce(10000); + + await expect( + service.changeUserAssignedSpace( + workspaceId, + memberId, + changeAssignedSpace, + ), + ).rejects.toThrow(BadRequestException); + }); + it('When new space to be assigned is valid, then it should update the space', async () => { - const workspace = newWorkspace(); + const workspace = newWorkspace({ attributes: { numberOfSeats: 5 } }); const member = newWorkspaceUser({ attributes: { spaceLimit: 500 } }); const workspaceUser = newUser(); const mockedUsedSpace = 400; @@ -1947,6 +1976,7 @@ describe('WorkspacesUsecases', () => { .mockResolvedValue(2000); jest.spyOn(member, 'getUsedSpace').mockReturnValue(mockedUsedSpace); jest.spyOn(service, 'adjustOwnerStorage').mockResolvedValue(); + jest.spyOn(service, 'getWorkspaceNetworkLimit').mockResolvedValue(10000); const updatedMember = await service.changeUserAssignedSpace( workspaceId, diff --git a/src/modules/workspaces/workspaces.usecase.ts b/src/modules/workspaces/workspaces.usecase.ts index 324604d8..603f7c71 100644 --- a/src/modules/workspaces/workspaces.usecase.ts +++ b/src/modules/workspaces/workspaces.usecase.ts @@ -574,7 +574,7 @@ export class WorkspacesUsecases { if (newSpaceLimit > spaceLeftWithoutUser) { throw new BadRequestException( - `Space limit set for the invitation is superior to the space assignable in workspace. Assignable space: ${spaceLeftWithoutUser}`, + `Space limit set for the user is superior to the space assignable in workspace. Assignable space: ${spaceLeftWithoutUser}`, ); } @@ -584,6 +584,15 @@ export class WorkspacesUsecases { ); } + const workspaceLimit = await this.getWorkspaceNetworkLimit(workspace); + const maxSpacePerUser = workspaceLimit / workspace.numberOfSeats; + + if (newSpaceLimit > maxSpacePerUser) { + throw new BadRequestException( + `Space limit set for the user is superior to the space assignable per user in workspace. Max space per user: ${maxSpacePerUser}`, + ); + } + await this.adjustOwnerStorage( workspaceId, Math.abs(limitDifference),