Skip to content

Commit

Permalink
fix: update workspace details endpoint to include address field
Browse files Browse the repository at this point in the history
  • Loading branch information
jzunigax2 committed Jul 19, 2024
1 parent a8d45f8 commit e9bc677
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 109 deletions.
15 changes: 0 additions & 15 deletions src/modules/workspaces/dto/edit-billing-address.dto.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/modules/workspaces/dto/edit-workspace-details-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ export class EditWorkspaceDetailsDto {
@IsString()
@Length(0, 150)
description?: Workspace['description'];
@IsOptional()
@IsString()
@Length(5, 255)
address?: Workspace['address'];
}
48 changes: 13 additions & 35 deletions src/modules/workspaces/workspaces.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
},
);
});
});
Expand Down Expand Up @@ -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);
});
});
});
37 changes: 6 additions & 31 deletions src/modules/workspaces/workspaces.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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);
}
}
30 changes: 2 additions & 28 deletions src/modules/workspaces/workspaces.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit e9bc677

Please sign in to comment.