forked from keyshade-xyz/keyshade
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(API): Refactor workspace-membership into a separate module (k…
…eyshade-xyz#421) Co-authored-by: rajdip-b <agentR47@gmail.com>
- Loading branch information
Showing
14 changed files
with
2,442 additions
and
2,063 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
apps/api/src/workspace-membership/controller/workspace-membership.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { WorkspaceMembershipController } from './workspace-membership.controller' | ||
import { WorkspaceMembershipService } from '../service/workspace-membership.service' | ||
import { PrismaService } from '@/prisma/prisma.service' | ||
import { MAIL_SERVICE } from '@/mail/services/interface.service' | ||
import { MockMailService } from '@/mail/services/mock.service' | ||
import { JwtService } from '@nestjs/jwt' | ||
import { AuthorityCheckerService } from '@/common/authority-checker.service' | ||
import { CommonModule } from '@/common/common.module' | ||
|
||
describe('WorkspaceMembershipController', () => { | ||
let controller: WorkspaceMembershipController | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [CommonModule], | ||
providers: [ | ||
WorkspaceMembershipService, | ||
PrismaService, | ||
{ | ||
provide: MAIL_SERVICE, | ||
useClass: MockMailService | ||
}, | ||
JwtService, | ||
AuthorityCheckerService | ||
], | ||
controllers: [WorkspaceMembershipController] | ||
}).compile() | ||
|
||
controller = module.get<WorkspaceMembershipController>( | ||
WorkspaceMembershipController | ||
) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined() | ||
}) | ||
}) |
163 changes: 163 additions & 0 deletions
163
apps/api/src/workspace-membership/controller/workspace-membership.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { RequiredApiKeyAuthorities } from '@/decorators/required-api-key-authorities.decorator' | ||
import { CurrentUser } from '@/decorators/user.decorator' | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
Post, | ||
Put, | ||
Query | ||
} from '@nestjs/common' | ||
import { Authority, User, Workspace, WorkspaceRole } from '@prisma/client' | ||
import { CreateWorkspaceMember } from '../dto/create.workspace/create.workspace-membership' | ||
import { WorkspaceMembershipService } from '../service/workspace-membership.service' | ||
|
||
@Controller('workspace-membership') | ||
export class WorkspaceMembershipController { | ||
constructor( | ||
private readonly workspaceMembershipService: WorkspaceMembershipService | ||
) {} | ||
|
||
@Put(':workspaceSlug/transfer-ownership/:userEmail') | ||
@RequiredApiKeyAuthorities(Authority.WORKSPACE_ADMIN) | ||
async transferOwnership( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'], | ||
@Param('userEmail') userEmail: User['email'] | ||
) { | ||
return this.workspaceMembershipService.transferOwnership( | ||
user, | ||
workspaceSlug, | ||
userEmail | ||
) | ||
} | ||
|
||
@Post(':workspaceSlug/invite-users') | ||
@RequiredApiKeyAuthorities(Authority.ADD_USER, Authority.READ_WORKSPACE) | ||
async addUsers( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'], | ||
@Body() members: CreateWorkspaceMember[] | ||
) { | ||
return this.workspaceMembershipService.inviteUsersToWorkspace( | ||
user, | ||
workspaceSlug, | ||
members | ||
) | ||
} | ||
|
||
@Delete(':workspaceSlug/remove-users') | ||
@RequiredApiKeyAuthorities(Authority.REMOVE_USER, Authority.READ_WORKSPACE) | ||
async removeUsers( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'], | ||
@Body() userEmails: User['email'][] | ||
) { | ||
return this.workspaceMembershipService.removeUsersFromWorkspace( | ||
user, | ||
workspaceSlug, | ||
userEmails | ||
) | ||
} | ||
|
||
@Put(':workspaceSlug/update-member-role/:userEmail') | ||
@RequiredApiKeyAuthorities( | ||
Authority.UPDATE_USER_ROLE, | ||
Authority.READ_WORKSPACE | ||
) | ||
async updateMemberRoles( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'], | ||
@Param('userEmail') userEmail: User['email'], | ||
@Body() roleSlugs: WorkspaceRole['slug'][] | ||
) { | ||
return this.workspaceMembershipService.updateMemberRoles( | ||
user, | ||
workspaceSlug, | ||
userEmail, | ||
roleSlugs | ||
) | ||
} | ||
|
||
@Post(':workspaceSlug/accept-invitation') | ||
@RequiredApiKeyAuthorities(Authority.READ_WORKSPACE) | ||
async acceptInvitation( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'] | ||
) { | ||
return this.workspaceMembershipService.acceptInvitation(user, workspaceSlug) | ||
} | ||
|
||
@Delete(':workspaceSlug/decline-invitation') | ||
@RequiredApiKeyAuthorities(Authority.READ_WORKSPACE) | ||
async declineInvitation( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'] | ||
) { | ||
return this.workspaceMembershipService.declineInvitation( | ||
user, | ||
workspaceSlug | ||
) | ||
} | ||
|
||
@Delete(':workspaceSlug/cancel-invitation/:userEmail') | ||
@RequiredApiKeyAuthorities(Authority.REMOVE_USER) | ||
async cancelInvitation( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'], | ||
@Param('userEmail') userEmail: User['email'] | ||
) { | ||
return this.workspaceMembershipService.cancelInvitation( | ||
user, | ||
workspaceSlug, | ||
userEmail | ||
) | ||
} | ||
|
||
@Delete(':workspaceSlug/leave') | ||
@RequiredApiKeyAuthorities(Authority.READ_WORKSPACE) | ||
async leave( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'] | ||
) { | ||
return this.workspaceMembershipService.leaveWorkspace(user, workspaceSlug) | ||
} | ||
|
||
@Get(':workspaceSlug/is-member/:userEmail') | ||
@RequiredApiKeyAuthorities(Authority.READ_WORKSPACE) | ||
async isMember( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'], | ||
@Param('userEmail') userEmail: User['email'] | ||
) { | ||
return this.workspaceMembershipService.isUserMemberOfWorkspace( | ||
user, | ||
workspaceSlug, | ||
userEmail | ||
) | ||
} | ||
|
||
@Get(':workspaceSlug/members') | ||
@RequiredApiKeyAuthorities(Authority.READ_WORKSPACE) | ||
async getMembers( | ||
@CurrentUser() user: User, | ||
@Param('workspaceSlug') workspaceSlug: Workspace['slug'], | ||
@Query('page') page: number = 0, | ||
@Query('limit') limit: number = 10, | ||
@Query('sort') sort: string = 'name', | ||
@Query('order') order: string = 'asc', | ||
@Query('search') search: string = '' | ||
) { | ||
return this.workspaceMembershipService.getAllMembersOfWorkspace( | ||
user, | ||
workspaceSlug, | ||
page, | ||
limit, | ||
sort, | ||
order, | ||
search | ||
) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
apps/api/src/workspace-membership/dto/create.workspace/create.workspace-membership.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { CreateWorkspaceMember } from './create.workspace-membership' | ||
|
||
describe('CreateWorkspaceMember', () => { | ||
it('should be defined', () => { | ||
expect(new CreateWorkspaceMember()).toBeDefined() | ||
}) | ||
}) |
12 changes: 12 additions & 0 deletions
12
apps/api/src/workspace-membership/dto/create.workspace/create.workspace-membership.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { WorkspaceRole } from '@prisma/client' | ||
import { IsArray, IsNotEmpty, IsString } from 'class-validator' | ||
|
||
export class CreateWorkspaceMember { | ||
@IsString() | ||
@IsNotEmpty() | ||
email: string | ||
|
||
@IsArray() | ||
@IsString() | ||
roleSlugs: WorkspaceRole['slug'][] | ||
} |
34 changes: 34 additions & 0 deletions
34
apps/api/src/workspace-membership/service/workspace-membership.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { WorkspaceMembershipService } from './workspace-membership.service' | ||
import { PrismaService } from '@/prisma/prisma.service' | ||
import { MAIL_SERVICE } from '@/mail/services/interface.service' | ||
import { MockMailService } from '@/mail/services/mock.service' | ||
import { JwtService } from '@nestjs/jwt' | ||
import { AuthorityCheckerService } from '@/common/authority-checker.service' | ||
import { CommonModule } from '@/common/common.module' | ||
|
||
describe('WorkspaceMembershipService', () => { | ||
let service: WorkspaceMembershipService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [CommonModule], | ||
providers: [ | ||
WorkspaceMembershipService, | ||
PrismaService, | ||
{ | ||
provide: MAIL_SERVICE, | ||
useClass: MockMailService | ||
}, | ||
JwtService, | ||
AuthorityCheckerService | ||
] | ||
}).compile() | ||
|
||
service = module.get<WorkspaceMembershipService>(WorkspaceMembershipService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
}) |
Oops, something went wrong.