Skip to content

Commit

Permalink
feat(api-client): Added workspace-membership and related tests (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil2000 authored Sep 20, 2024
1 parent fb0d6f7 commit 6a1c091
Show file tree
Hide file tree
Showing 4 changed files with 455 additions and 1 deletion.
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

0 comments on commit 6a1c091

Please sign in to comment.