From a8d45f8b26bcb200fe675d67de525ec18a5019e9 Mon Sep 17 00:00:00 2001 From: jzunigax2 <125698953+jzunigax2@users.noreply.github.com> Date: Thu, 18 Jul 2024 09:42:48 -0600 Subject: [PATCH 1/5] feat: add endpoint to edit workspace billing address --- .../dto/edit-billing-address.dto.ts | 15 +++ .../workspaces/workspaces.controller.spec.ts | 42 ++++++++ .../workspaces/workspaces.controller.ts | 44 ++++++++ .../workspaces/workspaces.usecase.spec.ts | 100 ++++++++++++++++++ src/modules/workspaces/workspaces.usecase.ts | 36 +++++++ 5 files changed, 237 insertions(+) create mode 100644 src/modules/workspaces/dto/edit-billing-address.dto.ts diff --git a/src/modules/workspaces/dto/edit-billing-address.dto.ts b/src/modules/workspaces/dto/edit-billing-address.dto.ts new file mode 100644 index 000000000..2e5107a64 --- /dev/null +++ b/src/modules/workspaces/dto/edit-billing-address.dto.ts @@ -0,0 +1,15 @@ +import { IsNotEmpty, IsString, Length } from 'class-validator'; +import { ApiProperty } from '@nestjs/swagger'; +import { WorkspaceAttributes } from '../attributes/workspace.attributes'; + +export class EditBillingAddressDto { + @ApiProperty({ + example: + 'La marina de Valencia, Muelle de la Aduana s/n, 46024 Valencia, Spain', + description: 'Workspace billing address', + }) + @IsNotEmpty() + @IsString() + @Length(5, 255) + address: WorkspaceAttributes['address']; +} diff --git a/src/modules/workspaces/workspaces.controller.spec.ts b/src/modules/workspaces/workspaces.controller.spec.ts index ad11c5c81..68502ccaf 100644 --- a/src/modules/workspaces/workspaces.controller.spec.ts +++ b/src/modules/workspaces/workspaces.controller.spec.ts @@ -15,6 +15,7 @@ import { WorkspaceUserMemberDto } from './dto/workspace-user-member.dto'; import { SharingService } from '../sharing/sharing.service'; import { CreateWorkspaceFolderDto } from './dto/create-workspace-folder.dto'; import { WorkspaceItemType } from './attributes/workspace-items-users.attributes'; +import { EditBillingAddressDto } from './dto/edit-billing-address.dto'; describe('Workspace Controller', () => { let workspacesController: WorkspacesController; @@ -590,4 +591,45 @@ describe('Workspace Controller', () => { ); }); }); + + describe('PATCH /:workspaceId/billing-address', () => { + it('When workspace billing address is updated successfully, then it should return', async () => { + const user = newUser(); + const workspace = newWorkspace({ owner: user }); + const address: EditBillingAddressDto = { + address: 'Test Address', + }; + + jest + .spyOn(workspacesUsecases, 'editWorkspaceBillingAddress') + .mockResolvedValueOnce(Promise.resolve()); + + await expect( + workspacesController.changeBillingAddress(workspace.id, user, address), + ).resolves.toBeUndefined(); + + expect( + workspacesUsecases.editWorkspaceBillingAddress, + ).toHaveBeenCalledWith(user, workspace.id, address); + }); + }); + + describe('GET /:workspaceId/billing-address', () => { + it('When workspace billing address is requested, then it should return', async () => { + const user = newUser(); + const workspace = newWorkspace({ owner: user }); + + jest + .spyOn(workspacesUsecases, 'getWorkspaceBillingAddress') + .mockResolvedValueOnce('Test Address'); + + await expect( + workspacesController.getBillingAddress(workspace.id, user), + ).resolves.toBe('Test Address'); + + expect( + workspacesUsecases.getWorkspaceBillingAddress, + ).toHaveBeenCalledWith(user, workspace.id); + }); + }); }); diff --git a/src/modules/workspaces/workspaces.controller.ts b/src/modules/workspaces/workspaces.controller.ts index a511bd956..48e2aa64c 100644 --- a/src/modules/workspaces/workspaces.controller.ts +++ b/src/modules/workspaces/workspaces.controller.ts @@ -70,6 +70,7 @@ import { GetItemsInsideSharedFolderDtoQuery } from './dto/get-items-inside-share import { WorkspaceUserAttributes } from './attributes/workspace-users.attributes'; import { ChangeUserAssignedSpaceDto } from './dto/change-user-assigned-space.dto'; import { Public } from '../auth/decorators/public.decorator'; +import { EditBillingAddressDto } from './dto/edit-billing-address.dto'; @ApiTags('Workspaces') @Controller('workspaces') @@ -958,4 +959,47 @@ export class WorkspacesController { workspaceId, ); } + + @Patch(':workspaceId/billing-address') + @ApiBearerAuth() + @ApiOperation({ + summary: 'Change workspace billing address', + }) + @ApiParam({ name: 'workspaceId', type: String, required: true }) + @ApiOkResponse({ + description: 'Workspace billing address changed', + }) + @UseGuards(WorkspaceGuard) + @WorkspaceRequiredAccess(AccessContext.WORKSPACE, WorkspaceRole.OWNER) + async changeBillingAddress( + @Param('workspaceId', ValidateUUIDPipe) + workspaceId: WorkspaceAttributes['id'], + @UserDecorator() user: User, + @Body() editBillingAddressDto: EditBillingAddressDto, + ) { + return this.workspaceUseCases.editWorkspaceBillingAddress( + user, + workspaceId, + editBillingAddressDto, + ); + } + + @Get(':workspaceId/billing-address') + @ApiBearerAuth() + @ApiOperation({ + summary: 'Get workspace billing address', + }) + @ApiParam({ name: 'workspaceId', type: String, required: true }) + @ApiOkResponse({ + description: 'Workspace billing address', + }) + @UseGuards(WorkspaceGuard) + @WorkspaceRequiredAccess(AccessContext.WORKSPACE, WorkspaceRole.OWNER) + async getBillingAddress( + @Param('workspaceId', ValidateUUIDPipe) + workspaceId: WorkspaceAttributes['id'], + @UserDecorator() user: User, + ) { + return this.workspaceUseCases.getWorkspaceBillingAddress(user, workspaceId); + } } diff --git a/src/modules/workspaces/workspaces.usecase.spec.ts b/src/modules/workspaces/workspaces.usecase.spec.ts index e374e1ec3..345962ccf 100644 --- a/src/modules/workspaces/workspaces.usecase.spec.ts +++ b/src/modules/workspaces/workspaces.usecase.spec.ts @@ -383,6 +383,106 @@ describe('WorkspacesUsecases', () => { }); }); + describe('getWokspaceBillingAddress', () => { + it('When workspace does not exist, then it should throw', async () => { + jest.spyOn(workspaceRepository, 'findById').mockResolvedValueOnce(null); + + await expect( + service.getWorkspaceBillingAddress(newUser(), v4()), + ).rejects.toThrow(NotFoundException); + }); + + it('When user is not the owner of the workspace, then it should throw', async () => { + const user = newUser(); + const workspace = newWorkspace(); + + jest + .spyOn(workspaceRepository, 'findById') + .mockResolvedValueOnce(workspace); + + await expect( + service.getWorkspaceBillingAddress(user, workspace.id), + ).rejects.toThrow(ForbiddenException); + }); + + it('When the user is the owner of the workspace, then it should return the billing address', async () => { + const user = newUser(); + const workspace = newWorkspace({ + owner: user, + attributes: { + address: 'Test Address', + }, + }); + + jest + .spyOn(workspaceRepository, 'findById') + .mockResolvedValueOnce(workspace); + + const billingAddress = 'Test Address'; + + const result = await service.getWorkspaceBillingAddress( + user, + workspace.id, + ); + + expect(result).toBe(billingAddress); + }); + }); + + describe('editWorkspaceBillingAddress', () => { + it('When workspace does not exist, then it should throw', async () => { + jest.spyOn(workspaceRepository, 'findById').mockResolvedValueOnce(null); + + await expect( + service.editWorkspaceBillingAddress(newUser(), v4(), { + address: 'Test Address', + }), + ).rejects.toThrow(NotFoundException); + }); + + it('When user is not the owner of the workspace, then it should throw', async () => { + const user = newUser(); + const workspace = newWorkspace(); + + jest + .spyOn(workspaceRepository, 'findById') + .mockResolvedValueOnce(workspace); + + await expect( + service.editWorkspaceBillingAddress(user, workspace.id, { + address: 'Test Address', + }), + ).rejects.toThrow(ForbiddenException); + }); + + it('When the user is the owner of the workspace, then it should update the address', async () => { + const user = newUser(); + const workspace = newWorkspace({ owner: user }); + const editBillingAddressDto = { + address: 'Test Address', + }; + + jest + .spyOn(workspaceRepository, 'findById') + .mockResolvedValueOnce(workspace); + + await expect( + service.editWorkspaceBillingAddress( + user, + workspace.id, + editBillingAddressDto, + ), + ).resolves.not.toThrow(); + + expect(workspaceRepository.updateBy).toHaveBeenCalledWith( + { + id: workspace.id, + }, + editBillingAddressDto, + ); + }); + }); + describe('setupWorkspace', () => { const user = newUser(); diff --git a/src/modules/workspaces/workspaces.usecase.ts b/src/modules/workspaces/workspaces.usecase.ts index 2c5b17b34..e109105ac 100644 --- a/src/modules/workspaces/workspaces.usecase.ts +++ b/src/modules/workspaces/workspaces.usecase.ts @@ -64,6 +64,7 @@ import { import { WorkspaceItemUser } from './domains/workspace-item-user.domain'; import { SharingService } from '../sharing/sharing.service'; import { ChangeUserAssignedSpaceDto } from './dto/change-user-assigned-space.dto'; +import { EditBillingAddressDto } from './dto/edit-billing-address.dto'; @Injectable() export class WorkspacesUsecases { @@ -169,6 +170,41 @@ export class WorkspacesUsecases { } } + async getWorkspaceBillingAddress(user: User, workspaceId: Workspace['id']) { + const workspace = await this.workspaceRepository.findById(workspaceId); + + if (!workspace) { + throw new NotFoundException('Workspace not found'); + } + + if (!workspace.isUserOwner(user)) { + throw new ForbiddenException('You are not the owner of this workspace'); + } + + return workspace.address; + } + + async editWorkspaceBillingAddress( + user: User, + workspaceId: Workspace['id'], + editBillingAddressDto: EditBillingAddressDto, + ) { + const workspace = await this.workspaceRepository.findById(workspaceId); + + if (!workspace) { + throw new NotFoundException('Workspace not found'); + } + + if (!workspace.isUserOwner(user)) { + throw new ForbiddenException('You are not the owner of this workspace'); + } + + await this.workspaceRepository.updateBy( + { id: workspaceId }, + { address: editBillingAddressDto.address }, + ); + } + async setupWorkspace( user: User, workspaceId: WorkspaceAttributes['id'], From e9bc67749a441fa9d4c4aaa51ec69eb8ba026906 Mon Sep 17 00:00:00 2001 From: jzunigax2 <125698953+jzunigax2@users.noreply.github.com> Date: Fri, 19 Jul 2024 10:25:52 -0600 Subject: [PATCH 2/5] fix: update workspace details endpoint to include address field --- .../dto/edit-billing-address.dto.ts | 15 ------ .../dto/edit-workspace-details-dto.ts | 4 ++ .../workspaces/workspaces.controller.spec.ts | 48 +++++-------------- .../workspaces/workspaces.controller.ts | 37 +++----------- src/modules/workspaces/workspaces.usecase.ts | 30 +----------- 5 files changed, 25 insertions(+), 109 deletions(-) delete mode 100644 src/modules/workspaces/dto/edit-billing-address.dto.ts diff --git a/src/modules/workspaces/dto/edit-billing-address.dto.ts b/src/modules/workspaces/dto/edit-billing-address.dto.ts deleted file mode 100644 index 2e5107a64..000000000 --- a/src/modules/workspaces/dto/edit-billing-address.dto.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { IsNotEmpty, IsString, Length } from 'class-validator'; -import { ApiProperty } from '@nestjs/swagger'; -import { WorkspaceAttributes } from '../attributes/workspace.attributes'; - -export class EditBillingAddressDto { - @ApiProperty({ - example: - 'La marina de Valencia, Muelle de la Aduana s/n, 46024 Valencia, Spain', - description: 'Workspace billing address', - }) - @IsNotEmpty() - @IsString() - @Length(5, 255) - address: WorkspaceAttributes['address']; -} diff --git a/src/modules/workspaces/dto/edit-workspace-details-dto.ts b/src/modules/workspaces/dto/edit-workspace-details-dto.ts index d967fe4e7..b56bc5726 100644 --- a/src/modules/workspaces/dto/edit-workspace-details-dto.ts +++ b/src/modules/workspaces/dto/edit-workspace-details-dto.ts @@ -21,4 +21,8 @@ export class EditWorkspaceDetailsDto { @IsString() @Length(0, 150) description?: Workspace['description']; + @IsOptional() + @IsString() + @Length(5, 255) + address?: Workspace['address']; } diff --git a/src/modules/workspaces/workspaces.controller.spec.ts b/src/modules/workspaces/workspaces.controller.spec.ts index 68502ccaf..b6023ecc8 100644 --- a/src/modules/workspaces/workspaces.controller.spec.ts +++ b/src/modules/workspaces/workspaces.controller.spec.ts @@ -15,7 +15,6 @@ import { WorkspaceUserMemberDto } from './dto/workspace-user-member.dto'; import { SharingService } from '../sharing/sharing.service'; import { CreateWorkspaceFolderDto } from './dto/create-workspace-folder.dto'; import { WorkspaceItemType } from './attributes/workspace-items-users.attributes'; -import { EditBillingAddressDto } from './dto/edit-billing-address.dto'; describe('Workspace Controller', () => { let workspacesController: WorkspacesController; @@ -257,13 +256,18 @@ describe('Workspace Controller', () => { workspacesController.editWorkspaceDetails(workspace.id, user, { name: 'new name', description: 'new description', + address: 'new address', }), ).resolves.toBeUndefined(); expect(workspacesUsecases.editWorkspaceDetails).toHaveBeenCalledWith( workspace.id, user, - { name: 'new name' }, + { + name: 'new name', + description: 'new description', + address: 'new address', + }, ); }); }); @@ -592,44 +596,18 @@ describe('Workspace Controller', () => { }); }); - describe('PATCH /:workspaceId/billing-address', () => { - it('When workspace billing address is updated successfully, then it should return', async () => { + describe('GET /:workspaceId', () => { + it('When a workspace is requested, then it should return the workspace data', async () => { const user = newUser(); - const workspace = newWorkspace({ owner: user }); - const address: EditBillingAddressDto = { - address: 'Test Address', - }; - - jest - .spyOn(workspacesUsecases, 'editWorkspaceBillingAddress') - .mockResolvedValueOnce(Promise.resolve()); - - await expect( - workspacesController.changeBillingAddress(workspace.id, user, address), - ).resolves.toBeUndefined(); - - expect( - workspacesUsecases.editWorkspaceBillingAddress, - ).toHaveBeenCalledWith(user, workspace.id, address); - }); - }); - - describe('GET /:workspaceId/billing-address', () => { - it('When workspace billing address is requested, then it should return', async () => { - const user = newUser(); - const workspace = newWorkspace({ owner: user }); + const workspace = newWorkspace(); jest - .spyOn(workspacesUsecases, 'getWorkspaceBillingAddress') - .mockResolvedValueOnce('Test Address'); + .spyOn(workspacesUsecases, 'getWorkspaceDetails') + .mockResolvedValueOnce(workspace); await expect( - workspacesController.getBillingAddress(workspace.id, user), - ).resolves.toBe('Test Address'); - - expect( - workspacesUsecases.getWorkspaceBillingAddress, - ).toHaveBeenCalledWith(user, workspace.id); + workspacesController.getWorkspaceDetails(workspace.id, user), + ).resolves.toEqual(workspace); }); }); }); diff --git a/src/modules/workspaces/workspaces.controller.ts b/src/modules/workspaces/workspaces.controller.ts index 48e2aa64c..9bbf5f1ec 100644 --- a/src/modules/workspaces/workspaces.controller.ts +++ b/src/modules/workspaces/workspaces.controller.ts @@ -70,7 +70,6 @@ import { GetItemsInsideSharedFolderDtoQuery } from './dto/get-items-inside-share import { WorkspaceUserAttributes } from './attributes/workspace-users.attributes'; import { ChangeUserAssignedSpaceDto } from './dto/change-user-assigned-space.dto'; import { Public } from '../auth/decorators/public.decorator'; -import { EditBillingAddressDto } from './dto/edit-billing-address.dto'; @ApiTags('Workspaces') @Controller('workspaces') @@ -960,46 +959,22 @@ export class WorkspacesController { ); } - @Patch(':workspaceId/billing-address') + @Get(':workspaceId') @ApiBearerAuth() @ApiOperation({ - summary: 'Change workspace billing address', + summary: 'Get workspace details', }) @ApiParam({ name: 'workspaceId', type: String, required: true }) @ApiOkResponse({ - description: 'Workspace billing address changed', + description: 'Workspace details', }) @UseGuards(WorkspaceGuard) - @WorkspaceRequiredAccess(AccessContext.WORKSPACE, WorkspaceRole.OWNER) - async changeBillingAddress( - @Param('workspaceId', ValidateUUIDPipe) - workspaceId: WorkspaceAttributes['id'], - @UserDecorator() user: User, - @Body() editBillingAddressDto: EditBillingAddressDto, - ) { - return this.workspaceUseCases.editWorkspaceBillingAddress( - user, - workspaceId, - editBillingAddressDto, - ); - } - - @Get(':workspaceId/billing-address') - @ApiBearerAuth() - @ApiOperation({ - summary: 'Get workspace billing address', - }) - @ApiParam({ name: 'workspaceId', type: String, required: true }) - @ApiOkResponse({ - description: 'Workspace billing address', - }) - @UseGuards(WorkspaceGuard) - @WorkspaceRequiredAccess(AccessContext.WORKSPACE, WorkspaceRole.OWNER) - async getBillingAddress( + @WorkspaceRequiredAccess(AccessContext.WORKSPACE, WorkspaceRole.MEMBER) + async getWorkspaceDetails( @Param('workspaceId', ValidateUUIDPipe) workspaceId: WorkspaceAttributes['id'], @UserDecorator() user: User, ) { - return this.workspaceUseCases.getWorkspaceBillingAddress(user, workspaceId); + return this.workspaceUseCases.getWorkspaceDetails(user, workspaceId); } } diff --git a/src/modules/workspaces/workspaces.usecase.ts b/src/modules/workspaces/workspaces.usecase.ts index e109105ac..d6dc28057 100644 --- a/src/modules/workspaces/workspaces.usecase.ts +++ b/src/modules/workspaces/workspaces.usecase.ts @@ -64,7 +64,6 @@ import { import { WorkspaceItemUser } from './domains/workspace-item-user.domain'; import { SharingService } from '../sharing/sharing.service'; import { ChangeUserAssignedSpaceDto } from './dto/change-user-assigned-space.dto'; -import { EditBillingAddressDto } from './dto/edit-billing-address.dto'; @Injectable() export class WorkspacesUsecases { @@ -170,39 +169,14 @@ export class WorkspacesUsecases { } } - async getWorkspaceBillingAddress(user: User, workspaceId: Workspace['id']) { + async getWorkspaceDetails(user: User, workspaceId: Workspace['id']) { const workspace = await this.workspaceRepository.findById(workspaceId); if (!workspace) { throw new NotFoundException('Workspace not found'); } - if (!workspace.isUserOwner(user)) { - throw new ForbiddenException('You are not the owner of this workspace'); - } - - return workspace.address; - } - - async editWorkspaceBillingAddress( - user: User, - workspaceId: Workspace['id'], - editBillingAddressDto: EditBillingAddressDto, - ) { - const workspace = await this.workspaceRepository.findById(workspaceId); - - if (!workspace) { - throw new NotFoundException('Workspace not found'); - } - - if (!workspace.isUserOwner(user)) { - throw new ForbiddenException('You are not the owner of this workspace'); - } - - await this.workspaceRepository.updateBy( - { id: workspaceId }, - { address: editBillingAddressDto.address }, - ); + return workspace; } async setupWorkspace( From 1a7e9d3d06aba4f9801154db2b210757d309935e Mon Sep 17 00:00:00 2001 From: jzunigax2 <125698953+jzunigax2@users.noreply.github.com> Date: Fri, 19 Jul 2024 10:49:54 -0600 Subject: [PATCH 3/5] fix: remove billing tests from useCases --- .../workspaces/workspaces.controller.ts | 2 +- .../workspaces/workspaces.usecase.spec.ts | 116 +++--------------- src/modules/workspaces/workspaces.usecase.ts | 2 +- 3 files changed, 21 insertions(+), 99 deletions(-) diff --git a/src/modules/workspaces/workspaces.controller.ts b/src/modules/workspaces/workspaces.controller.ts index 9bbf5f1ec..8b3bafdc0 100644 --- a/src/modules/workspaces/workspaces.controller.ts +++ b/src/modules/workspaces/workspaces.controller.ts @@ -975,6 +975,6 @@ export class WorkspacesController { workspaceId: WorkspaceAttributes['id'], @UserDecorator() user: User, ) { - return this.workspaceUseCases.getWorkspaceDetails(user, workspaceId); + return this.workspaceUseCases.getWorkspaceDetails(workspaceId); } } diff --git a/src/modules/workspaces/workspaces.usecase.spec.ts b/src/modules/workspaces/workspaces.usecase.spec.ts index 345962ccf..7d60982d0 100644 --- a/src/modules/workspaces/workspaces.usecase.spec.ts +++ b/src/modules/workspaces/workspaces.usecase.spec.ts @@ -340,145 +340,67 @@ describe('WorkspacesUsecases', () => { }); }); - describe('editWorkspaceDetails', () => { - const user = newUser(); - const workspace = newWorkspace({ owner: user }); - const editWorkspaceDto: EditWorkspaceDetailsDto = { - name: 'Test Workspace', - description: 'Workspace description', - }; + describe('getWorkspaceDetails', () => { it('When workspace does not exist, then it should throw', async () => { jest.spyOn(workspaceRepository, 'findById').mockResolvedValueOnce(null); - await expect( - service.editWorkspaceDetails(workspace.id, user, editWorkspaceDto), - ).rejects.toThrow(NotFoundException); - }); - - it('When user is not the owner of the workspace, then it should throw', async () => { - jest - .spyOn(workspaceRepository, 'findById') - .mockResolvedValueOnce(workspace); - - await expect( - service.editWorkspaceDetails(workspace.id, newUser(), editWorkspaceDto), - ).rejects.toThrow(ForbiddenException); - }); - - it('When the user is the owner of the workspace, then it should update the workspace details', async () => { - jest - .spyOn(workspaceRepository, 'findById') - .mockResolvedValueOnce(workspace); - - await expect( - service.editWorkspaceDetails(workspace.id, user, editWorkspaceDto), - ).resolves.not.toThrow(); - - expect(workspaceRepository.updateBy).toHaveBeenCalledWith( - { - id: workspace.id, - }, - editWorkspaceDto, + await expect(service.getWorkspaceDetails(v4())).rejects.toThrow( + NotFoundException, ); }); - }); - - describe('getWokspaceBillingAddress', () => { - it('When workspace does not exist, then it should throw', async () => { - jest.spyOn(workspaceRepository, 'findById').mockResolvedValueOnce(null); - await expect( - service.getWorkspaceBillingAddress(newUser(), v4()), - ).rejects.toThrow(NotFoundException); - }); - - it('When user is not the owner of the workspace, then it should throw', async () => { - const user = newUser(); + it('When workspace exists, then it should return the workspace details', async () => { const workspace = newWorkspace(); - - jest - .spyOn(workspaceRepository, 'findById') - .mockResolvedValueOnce(workspace); - - await expect( - service.getWorkspaceBillingAddress(user, workspace.id), - ).rejects.toThrow(ForbiddenException); - }); - - it('When the user is the owner of the workspace, then it should return the billing address', async () => { - const user = newUser(); - const workspace = newWorkspace({ - owner: user, - attributes: { - address: 'Test Address', - }, - }); - jest .spyOn(workspaceRepository, 'findById') .mockResolvedValueOnce(workspace); - const billingAddress = 'Test Address'; + const result = await service.getWorkspaceDetails(workspace.id); - const result = await service.getWorkspaceBillingAddress( - user, - workspace.id, - ); - - expect(result).toBe(billingAddress); + expect(result).toEqual(workspace.toJSON()); }); }); - describe('editWorkspaceBillingAddress', () => { + describe('editWorkspaceDetails', () => { + const user = newUser(); + const workspace = newWorkspace({ owner: user }); + const editWorkspaceDto: EditWorkspaceDetailsDto = { + name: 'Test Workspace', + description: 'Workspace description', + address: 'Workspace Address', + }; it('When workspace does not exist, then it should throw', async () => { jest.spyOn(workspaceRepository, 'findById').mockResolvedValueOnce(null); await expect( - service.editWorkspaceBillingAddress(newUser(), v4(), { - address: 'Test Address', - }), + service.editWorkspaceDetails(workspace.id, user, editWorkspaceDto), ).rejects.toThrow(NotFoundException); }); it('When user is not the owner of the workspace, then it should throw', async () => { - const user = newUser(); - const workspace = newWorkspace(); - jest .spyOn(workspaceRepository, 'findById') .mockResolvedValueOnce(workspace); await expect( - service.editWorkspaceBillingAddress(user, workspace.id, { - address: 'Test Address', - }), + service.editWorkspaceDetails(workspace.id, newUser(), editWorkspaceDto), ).rejects.toThrow(ForbiddenException); }); - it('When the user is the owner of the workspace, then it should update the address', async () => { - const user = newUser(); - const workspace = newWorkspace({ owner: user }); - const editBillingAddressDto = { - address: 'Test Address', - }; - + it('When the user is the owner of the workspace, then it should update the workspace details', async () => { jest .spyOn(workspaceRepository, 'findById') .mockResolvedValueOnce(workspace); await expect( - service.editWorkspaceBillingAddress( - user, - workspace.id, - editBillingAddressDto, - ), + service.editWorkspaceDetails(workspace.id, user, editWorkspaceDto), ).resolves.not.toThrow(); expect(workspaceRepository.updateBy).toHaveBeenCalledWith( { id: workspace.id, }, - editBillingAddressDto, + editWorkspaceDto, ); }); }); diff --git a/src/modules/workspaces/workspaces.usecase.ts b/src/modules/workspaces/workspaces.usecase.ts index d6dc28057..0513c185d 100644 --- a/src/modules/workspaces/workspaces.usecase.ts +++ b/src/modules/workspaces/workspaces.usecase.ts @@ -169,7 +169,7 @@ export class WorkspacesUsecases { } } - async getWorkspaceDetails(user: User, workspaceId: Workspace['id']) { + async getWorkspaceDetails(workspaceId: Workspace['id']) { const workspace = await this.workspaceRepository.findById(workspaceId); if (!workspace) { From 7ad361c9b9e9a0756cefd0d95713c92453b42a3d Mon Sep 17 00:00:00 2001 From: jzunigax2 <125698953+jzunigax2@users.noreply.github.com> Date: Fri, 19 Jul 2024 10:54:07 -0600 Subject: [PATCH 4/5] fix: test case --- src/modules/workspaces/domains/workspaces.domain.ts | 1 + src/modules/workspaces/workspaces.usecase.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/workspaces/domains/workspaces.domain.ts b/src/modules/workspaces/domains/workspaces.domain.ts index 41cbae0b6..624708aae 100644 --- a/src/modules/workspaces/domains/workspaces.domain.ts +++ b/src/modules/workspaces/domains/workspaces.domain.ts @@ -78,6 +78,7 @@ export class Workspace implements WorkspaceAttributes { defaultTeamId: this.defaultTeamId, avatar: this.avatar, workspaceUserId: this.workspaceUserId, + numberOfSeats: this.numberOfSeats, createdAt: this.createdAt, updatedAt: this.updatedAt, }; diff --git a/src/modules/workspaces/workspaces.usecase.ts b/src/modules/workspaces/workspaces.usecase.ts index 0513c185d..ee7498117 100644 --- a/src/modules/workspaces/workspaces.usecase.ts +++ b/src/modules/workspaces/workspaces.usecase.ts @@ -176,7 +176,7 @@ export class WorkspacesUsecases { throw new NotFoundException('Workspace not found'); } - return workspace; + return workspace.toJSON(); } async setupWorkspace( From 6a842cde1c9f9ad342d46e5802e121d61e3142c5 Mon Sep 17 00:00:00 2001 From: jzunigax2 <125698953+jzunigax2@users.noreply.github.com> Date: Fri, 19 Jul 2024 11:03:16 -0600 Subject: [PATCH 5/5] fix: test case --- src/modules/workspaces/domains/workspaces.domain.ts | 1 + src/modules/workspaces/workspaces.controller.spec.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/modules/workspaces/domains/workspaces.domain.ts b/src/modules/workspaces/domains/workspaces.domain.ts index 624708aae..a53675f90 100644 --- a/src/modules/workspaces/domains/workspaces.domain.ts +++ b/src/modules/workspaces/domains/workspaces.domain.ts @@ -79,6 +79,7 @@ export class Workspace implements WorkspaceAttributes { avatar: this.avatar, workspaceUserId: this.workspaceUserId, numberOfSeats: this.numberOfSeats, + setupCompleted: this.setupCompleted, createdAt: this.createdAt, updatedAt: this.updatedAt, }; diff --git a/src/modules/workspaces/workspaces.controller.spec.ts b/src/modules/workspaces/workspaces.controller.spec.ts index b6023ecc8..7c9274146 100644 --- a/src/modules/workspaces/workspaces.controller.spec.ts +++ b/src/modules/workspaces/workspaces.controller.spec.ts @@ -603,11 +603,11 @@ describe('Workspace Controller', () => { jest .spyOn(workspacesUsecases, 'getWorkspaceDetails') - .mockResolvedValueOnce(workspace); + .mockResolvedValueOnce(workspace.toJSON()); await expect( workspacesController.getWorkspaceDetails(workspace.id, user), - ).resolves.toEqual(workspace); + ).resolves.toEqual(workspace.toJSON()); }); }); });