Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(API-CLIENT): Added workspace-membership and it's tests #452

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions packages/api-client/src/controllers/workspace-membership.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { APIClient } from '@api-client/core/client'
import { parseResponse } from '@api-client/core/response-parser'
import { parsePaginationUrl } from '@api-client/core/pagination-parser'
import {
TransferOwnershipRequest,
TransferOwnershipResponse,
InviteUsersRequest,
InviteUsersResponse,
RemoveUsersRequest,
RemoveUsersResponse,
UpdateMemberRoleRequest,
UpdateMemberRoleResponse,
AcceptInvitationRequest,
AcceptInvitationResponse,
DeclineInvitationRequest,
DeclineInvitationResponse,
CancelInvitationRequest,
CancelInvitationResponse,
LeaveWorkspaceRequest,
LeaveWorkspaceResponse,
IsMemberRequest,
IsMemberResponse,
GetMembersRequest,
GetMembersResponse
} from '@api-client/types/workspace-membership.types'
import { ClientResponse } from '@api-client/types/index.types'

export default class WorkspaceMembershipController {
private apiClient: APIClient

constructor(private readonly backendUrl: string) {
this.apiClient = new APIClient(this.backendUrl)
}

async transferOwnership(
request: TransferOwnershipRequest,
headers?: Record<string, string>
): Promise<ClientResponse<TransferOwnershipResponse>> {
const response = await this.apiClient.put(
`/api/workspace-membership/${request.workspaceSlug}/transfer-ownership/${request.userEmail}`,
request,
headers
)
return await parseResponse<TransferOwnershipResponse>(response)
}

async inviteUsers(
request: InviteUsersRequest,
headers?: Record<string, string>
): Promise<ClientResponse<InviteUsersResponse>> {
const response = await this.apiClient.post(
`/api/workspace-membership/${request.workspaceSlug}/invite-users`,
request,
headers
)
return await parseResponse<InviteUsersResponse>(response)
}

async removeUsers(
request: RemoveUsersRequest,
headers?: Record<string, string>
): Promise<ClientResponse<RemoveUsersResponse>> {
const response = await this.apiClient.delete(
`/api/workspace-membership/${request.workspaceSlug}/remove-users`,
headers
)
return await parseResponse<RemoveUsersResponse>(response)
}

async updateMemberRoles(
request: UpdateMemberRoleRequest,
headers?: Record<string, string>
): Promise<ClientResponse<UpdateMemberRoleResponse>> {
const response = await this.apiClient.put(
`/api/workspace-membership/${request.workspaceSlug}/update-member-role/${request.userEmail}`,
request,
headers
)
return await parseResponse<UpdateMemberRoleResponse>(response)
}

async acceptInvitation(
request: AcceptInvitationRequest,
headers?: Record<string, string>
): Promise<ClientResponse<AcceptInvitationResponse>> {
const response = await this.apiClient.post(
`/api/workspace-membership/${request.workspaceSlug}/accept-invitation`,
request,
headers
)
return await parseResponse<AcceptInvitationResponse>(response)
}

async declineInvitation(
request: DeclineInvitationRequest,
headers?: Record<string, string>
): Promise<ClientResponse<DeclineInvitationResponse>> {
const response = await this.apiClient.delete(
`/api/workspace-membership/${request.workspaceSlug}/decline-invitation`,
headers
)
return await parseResponse<DeclineInvitationResponse>(response)
}

async cancelInvitation(
request: CancelInvitationRequest,
headers?: Record<string, string>
): Promise<ClientResponse<CancelInvitationResponse>> {
const response = await this.apiClient.delete(
`/api/workspace-membership/${request.workspaceSlug}/cancel-invitation/${request.userEmail}`,
headers
)
return await parseResponse<CancelInvitationResponse>(response)
}

async leaveWorkspace(
request: LeaveWorkspaceRequest,
headers?: Record<string, string>
): Promise<ClientResponse<LeaveWorkspaceResponse>> {
const response = await this.apiClient.delete(
`/api/workspace-membership/${request.workspaceSlug}/leave`,
headers
)
return await parseResponse<LeaveWorkspaceResponse>(response)
}

async isMember(
request: IsMemberRequest,
headers?: Record<string, string>
): Promise<ClientResponse<IsMemberResponse>> {
const response = await this.apiClient.get(
`/api/workspace-membership/${request.workspaceSlug}/is-member/${request.userEmail}`,
headers
)
return await parseResponse<IsMemberResponse>(response)
}

async getMembers(
request: GetMembersRequest,
headers?: Record<string, string>
): Promise<ClientResponse<GetMembersResponse>> {
let url = parsePaginationUrl(
`/api/workspace-membership/${request.workspaceSlug}/members`,
request
)
const response = await this.apiClient.get(url, headers)
return await parseResponse<GetMembersResponse>(response)
}
}
4 changes: 3 additions & 1 deletion packages/api-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ProjectController from '@api-client/controllers/project'
import VariableController from '@api-client/controllers/variable'
import WorkspaceController from '@api-client/controllers/workspace'
import WorkspaceRoleController from '@api-client/controllers/workspace-role'
import WorkspaceMembershipController from '@api-client/controllers/workspace-membership'

export {
EnvironmentController,
Expand All @@ -15,5 +16,6 @@ export {
ProjectController,
VariableController,
WorkspaceController,
WorkspaceRoleController
WorkspaceRoleController,
WorkspaceMembershipController
}
145 changes: 145 additions & 0 deletions packages/api-client/src/types/workspace-membership.types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { Page } from '../../../../apps/cli/src/types/index.types'

enum Authority {
CREATE_PROJECT,
READ_USERS,
ADD_USER,
REMOVE_USER,
UPDATE_USER_ROLE,
READ_WORKSPACE,
UPDATE_WORKSPACE,
DELETE_WORKSPACE,
CREATE_WORKSPACE_ROLE,
READ_WORKSPACE_ROLE,
UPDATE_WORKSPACE_ROLE,
DELETE_WORKSPACE_ROLE,
WORKSPACE_ADMIN,
READ_PROJECT,
UPDATE_PROJECT,
DELETE_PROJECT,
CREATE_SECRET,
READ_SECRET,
UPDATE_SECRET,
DELETE_SECRET,
CREATE_ENVIRONMENT,
READ_ENVIRONMENT,
UPDATE_ENVIRONMENT,
DELETE_ENVIRONMENT,
CREATE_VARIABLE,
READ_VARIABLE,
UPDATE_VARIABLE,
DELETE_VARIABLE,
CREATE_INTEGRATION,
READ_INTEGRATION,
UPDATE_INTEGRATION,
DELETE_INTEGRATION,
CREATE_WORKSPACE,
CREATE_API_KEY,
READ_API_KEY,
UPDATE_API_KEY,
DELETE_API_KEY,
UPDATE_PROFILE,
READ_SELF,
UPDATE_SELF,
READ_EVENT
}

export interface CreateWorkspaceMember {
email: string
roleSlugs: string[]
}

export interface TransferOwnershipRequest {
workspaceSlug: string
userEmail: string
}

export interface TransferOwnershipResponse {}

export interface InviteUsersRequest {
emails: string[]
workspaceSlug: string
members: CreateWorkspaceMember[]
}

export interface InviteUsersResponse {}

export interface RemoveUsersRequest {
workspaceSlug: string
userEmails: string[]
}

export interface RemoveUsersResponse {}

export interface UpdateMemberRoleRequest {
workspaceSlug: string
userEmail: string
roleSlugs: string[]
}

export interface UpdateMemberRoleResponse {}

export interface AcceptInvitationRequest {
workspaceSlug: string
}

export interface AcceptInvitationResponse {}

export interface DeclineInvitationRequest {
workspaceSlug: string
}

export interface DeclineInvitationResponse {}

export interface CancelInvitationRequest {
workspaceSlug: string
userEmail: string
}

export interface CancelInvitationResponse {}

export interface LeaveWorkspaceRequest {
workspaceSlug: string
}

export interface LeaveWorkspaceResponse {}

export interface IsMemberRequest {
workspaceSlug: string
userEmail: string
}

export type IsMemberResponse = boolean

export interface GetMembersRequest extends Page {
workspaceSlug: string
page?: number
limit?: number
sort?: string
order?: string
search?: string
}

export interface GetMembersResponse
extends Page<{
metadata: Record<string, string>
id: string
user: {
id: string
email: string
name: string | null
}
roles: {
id: string
role: {
id: string
name: string
description: string | null
colorCode: string | null
authorities: Authority[]
projects: {
id: string
}[]
}
}[]
}> {}
Loading
Loading