feat(api): add inviting to workspace
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
export enum WorkspaceApiOperations {
|
export enum WorkspaceApiOperations {
|
||||||
Creating,
|
Creating,
|
||||||
|
Inviting,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { WorkspaceType } from '@standardnotes/common'
|
import { WorkspaceType } from '@standardnotes/common'
|
||||||
|
import { WorkspaceInvitationResponse } from '../../Response'
|
||||||
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
||||||
import { WorkspaceServerInterface } from '../../Server/Workspace/WorkspaceServerInterface'
|
import { WorkspaceServerInterface } from '../../Server/Workspace/WorkspaceServerInterface'
|
||||||
|
|
||||||
@@ -15,6 +16,9 @@ describe('WorkspaceApiService', () => {
|
|||||||
workspaceServer.createWorkspace = jest.fn().mockReturnValue({
|
workspaceServer.createWorkspace = jest.fn().mockReturnValue({
|
||||||
data: { uuid: '1-2-3' },
|
data: { uuid: '1-2-3' },
|
||||||
} as jest.Mocked<WorkspaceCreationResponse>)
|
} as jest.Mocked<WorkspaceCreationResponse>)
|
||||||
|
workspaceServer.inviteToWorkspace = jest.fn().mockReturnValue({
|
||||||
|
data: { uuid: 'i-1-2-3' },
|
||||||
|
} as jest.Mocked<WorkspaceInvitationResponse>)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should create a workspace', async () => {
|
it('should create a workspace', async () => {
|
||||||
@@ -77,4 +81,58 @@ describe('WorkspaceApiService', () => {
|
|||||||
|
|
||||||
expect(error).not.toBeNull()
|
expect(error).not.toBeNull()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should invite to a workspace', async () => {
|
||||||
|
const response = await createService().inviteToWorkspace({
|
||||||
|
workspaceUuid: 'w-1-2-3',
|
||||||
|
inviteeEmail: 'test@test.te'
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(response).toEqual({
|
||||||
|
data: {
|
||||||
|
uuid: 'i-1-2-3',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
expect(workspaceServer.inviteToWorkspace).toHaveBeenCalledWith({
|
||||||
|
workspaceUuid: 'w-1-2-3',
|
||||||
|
inviteeEmail: 'test@test.te'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not invite to a workspace if it is already inviting', async () => {
|
||||||
|
const service = createService()
|
||||||
|
Object.defineProperty(service, 'operationsInProgress', {
|
||||||
|
get: () => new Map([[WorkspaceApiOperations.Inviting, true]]),
|
||||||
|
})
|
||||||
|
|
||||||
|
let error = null
|
||||||
|
try {
|
||||||
|
await service.inviteToWorkspace({
|
||||||
|
workspaceUuid: 'w-1-2-3',
|
||||||
|
inviteeEmail: 'test@test.te'
|
||||||
|
})
|
||||||
|
} catch (caughtError) {
|
||||||
|
error = caughtError
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(error).not.toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not invite to a workspace if the server fails', async () => {
|
||||||
|
workspaceServer.inviteToWorkspace = jest.fn().mockImplementation(() => {
|
||||||
|
throw new Error('Oops')
|
||||||
|
})
|
||||||
|
|
||||||
|
let error = null
|
||||||
|
try {
|
||||||
|
await createService().inviteToWorkspace({
|
||||||
|
workspaceUuid: 'w-1-2-3',
|
||||||
|
inviteeEmail: 'test@test.te'
|
||||||
|
})
|
||||||
|
} catch (caughtError) {
|
||||||
|
error = caughtError
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(error).not.toBeNull()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { Uuid, WorkspaceType } from '@standardnotes/common'
|
||||||
|
|
||||||
import { ErrorMessage } from '../../Error/ErrorMessage'
|
import { ErrorMessage } from '../../Error/ErrorMessage'
|
||||||
import { ApiCallError } from '../../Error/ApiCallError'
|
import { ApiCallError } from '../../Error/ApiCallError'
|
||||||
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
||||||
@@ -6,7 +8,7 @@ import { WorkspaceServerInterface } from '../../Server/Workspace/WorkspaceServer
|
|||||||
import { WorkspaceApiServiceInterface } from './WorkspaceApiServiceInterface'
|
import { WorkspaceApiServiceInterface } from './WorkspaceApiServiceInterface'
|
||||||
import { WorkspaceApiOperations } from './WorkspaceApiOperations'
|
import { WorkspaceApiOperations } from './WorkspaceApiOperations'
|
||||||
|
|
||||||
import { WorkspaceType } from '@standardnotes/common'
|
import { WorkspaceInvitationResponse } from '../../Response'
|
||||||
|
|
||||||
export class WorkspaceApiService implements WorkspaceApiServiceInterface {
|
export class WorkspaceApiService implements WorkspaceApiServiceInterface {
|
||||||
private operationsInProgress: Map<WorkspaceApiOperations, boolean>
|
private operationsInProgress: Map<WorkspaceApiOperations, boolean>
|
||||||
@@ -15,6 +17,27 @@ export class WorkspaceApiService implements WorkspaceApiServiceInterface {
|
|||||||
this.operationsInProgress = new Map()
|
this.operationsInProgress = new Map()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async inviteToWorkspace(dto: { inviteeEmail: string; workspaceUuid: Uuid }): Promise<WorkspaceInvitationResponse> {
|
||||||
|
if (this.operationsInProgress.get(WorkspaceApiOperations.Inviting)) {
|
||||||
|
throw new ApiCallError(ErrorMessage.GenericInProgress)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.operationsInProgress.set(WorkspaceApiOperations.Inviting, true)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await this.workspaceServer.inviteToWorkspace({
|
||||||
|
inviteeEmail: dto.inviteeEmail,
|
||||||
|
workspaceUuid: dto.workspaceUuid,
|
||||||
|
})
|
||||||
|
|
||||||
|
this.operationsInProgress.set(WorkspaceApiOperations.Inviting, false)
|
||||||
|
|
||||||
|
return response
|
||||||
|
} catch (error) {
|
||||||
|
throw new ApiCallError(ErrorMessage.GenericFail)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async createWorkspace(dto: {
|
async createWorkspace(dto: {
|
||||||
workspaceType: WorkspaceType,
|
workspaceType: WorkspaceType,
|
||||||
encryptedWorkspaceKey?: string
|
encryptedWorkspaceKey?: string
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { WorkspaceType } from '@standardnotes/common'
|
import { Uuid, WorkspaceType } from '@standardnotes/common'
|
||||||
|
|
||||||
import { WorkspaceCreationResponse } from '../../Response'
|
import { WorkspaceCreationResponse, WorkspaceInvitationResponse } from '../../Response'
|
||||||
|
|
||||||
export interface WorkspaceApiServiceInterface {
|
export interface WorkspaceApiServiceInterface {
|
||||||
createWorkspace(dto: {
|
createWorkspace(dto: {
|
||||||
@@ -10,4 +10,8 @@ export interface WorkspaceApiServiceInterface {
|
|||||||
publicKey?: string
|
publicKey?: string
|
||||||
workspaceName?: string
|
workspaceName?: string
|
||||||
}): Promise<WorkspaceCreationResponse>
|
}): Promise<WorkspaceCreationResponse>
|
||||||
|
inviteToWorkspace(dto: {
|
||||||
|
inviteeEmail: string
|
||||||
|
workspaceUuid: Uuid
|
||||||
|
}): Promise<WorkspaceInvitationResponse>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { Uuid } from "@standardnotes/common"
|
||||||
|
|
||||||
|
export type WorkspaceInvitationRequestParams = {
|
||||||
|
workspaceUuid: Uuid
|
||||||
|
inviteeEmail: string
|
||||||
|
[additionalParam: string]: unknown
|
||||||
|
}
|
||||||
@@ -7,3 +7,4 @@ export * from './Subscription/SubscriptionInviteRequestParams'
|
|||||||
export * from './User/UserRegistrationRequestParams'
|
export * from './User/UserRegistrationRequestParams'
|
||||||
export * from './WebSocket/WebSocketConnectionTokenRequestParams'
|
export * from './WebSocket/WebSocketConnectionTokenRequestParams'
|
||||||
export * from './Workspace/WorkspaceCreationRequestParams'
|
export * from './Workspace/WorkspaceCreationRequestParams'
|
||||||
|
export * from './Workspace/WorkspaceInvitationRequestParams'
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import { Either } from '@standardnotes/common'
|
||||||
|
|
||||||
|
import { HttpErrorResponseBody } from '../../Http/HttpErrorResponseBody'
|
||||||
|
import { HttpResponse } from '../../Http/HttpResponse'
|
||||||
|
import { WorkspaceInvitationResponseBody } from './WorkspaceInvitationResponseBody'
|
||||||
|
|
||||||
|
export interface WorkspaceInvitationResponse extends HttpResponse {
|
||||||
|
data: Either<WorkspaceInvitationResponseBody, HttpErrorResponseBody>
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export type WorkspaceInvitationResponseBody = {
|
||||||
|
uuid: string
|
||||||
|
}
|
||||||
@@ -14,3 +14,5 @@ export * from './WebSocket/WebSocketConnectionTokenResponse'
|
|||||||
export * from './WebSocket/WebSocketConnectionTokenResponseBody'
|
export * from './WebSocket/WebSocketConnectionTokenResponseBody'
|
||||||
export * from './Workspace/WorkspaceCreationResponse'
|
export * from './Workspace/WorkspaceCreationResponse'
|
||||||
export * from './Workspace/WorkspaceCreationResponseBody'
|
export * from './Workspace/WorkspaceCreationResponseBody'
|
||||||
|
export * from './Workspace/WorkspaceInvitationResponse'
|
||||||
|
export * from './Workspace/WorkspaceInvitationResponseBody'
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
|
import { Uuid } from '@standardnotes/common'
|
||||||
|
|
||||||
const WorkspacePaths = {
|
const WorkspacePaths = {
|
||||||
createWorkspace: '/v1/workspaces',
|
createWorkspace: '/v1/workspaces',
|
||||||
|
inviteToWorkspace: (uuid: Uuid) => `/v1/workspaces/${uuid}/invites`,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Paths = {
|
export const Paths = {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
import { WorkspaceType } from '@standardnotes/common'
|
||||||
import { HttpServiceInterface } from '../../Http'
|
import { HttpServiceInterface } from '../../Http'
|
||||||
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
||||||
|
import { WorkspaceInvitationResponse } from '../../Response/Workspace/WorkspaceInvitationResponse'
|
||||||
|
|
||||||
import { WorkspaceServer } from './WorkspaceServer'
|
import { WorkspaceServer } from './WorkspaceServer'
|
||||||
|
|
||||||
@@ -17,6 +19,7 @@ describe('WorkspaceServer', () => {
|
|||||||
|
|
||||||
it('should create a workspace', async () => {
|
it('should create a workspace', async () => {
|
||||||
const response = await createServer().createWorkspace({
|
const response = await createServer().createWorkspace({
|
||||||
|
workspaceType: WorkspaceType.Private,
|
||||||
encryptedPrivateKey: 'foo',
|
encryptedPrivateKey: 'foo',
|
||||||
encryptedWorkspaceKey: 'bar',
|
encryptedWorkspaceKey: 'bar',
|
||||||
publicKey: 'buzz',
|
publicKey: 'buzz',
|
||||||
@@ -28,4 +31,21 @@ describe('WorkspaceServer', () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should inivte to a workspace', async () => {
|
||||||
|
httpService.post = jest.fn().mockReturnValue({
|
||||||
|
data: { uuid: 'i-1-2-3' },
|
||||||
|
} as jest.Mocked<WorkspaceInvitationResponse>)
|
||||||
|
|
||||||
|
const response = await createServer().inviteToWorkspace({
|
||||||
|
inviteeEmail: 'test@test.te',
|
||||||
|
workspaceUuid: 'w-1-2-3',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(response).toEqual({
|
||||||
|
data: {
|
||||||
|
uuid: 'i-1-2-3',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { HttpServiceInterface } from '../../Http/HttpServiceInterface'
|
import { HttpServiceInterface } from '../../Http/HttpServiceInterface'
|
||||||
|
import { WorkspaceInvitationRequestParams } from '../../Request/Workspace/WorkspaceInvitationRequestParams'
|
||||||
import { WorkspaceCreationRequestParams } from '../../Request/Workspace/WorkspaceCreationRequestParams'
|
import { WorkspaceCreationRequestParams } from '../../Request/Workspace/WorkspaceCreationRequestParams'
|
||||||
|
import { WorkspaceInvitationResponse } from '../../Response/Workspace/WorkspaceInvitationResponse'
|
||||||
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
||||||
|
|
||||||
import { Paths } from './Paths'
|
import { Paths } from './Paths'
|
||||||
@@ -8,6 +10,12 @@ import { WorkspaceServerInterface } from './WorkspaceServerInterface'
|
|||||||
export class WorkspaceServer implements WorkspaceServerInterface {
|
export class WorkspaceServer implements WorkspaceServerInterface {
|
||||||
constructor(private httpService: HttpServiceInterface) {}
|
constructor(private httpService: HttpServiceInterface) {}
|
||||||
|
|
||||||
|
async inviteToWorkspace(params: WorkspaceInvitationRequestParams): Promise<WorkspaceInvitationResponse> {
|
||||||
|
const response = await this.httpService.post(Paths.v1.inviteToWorkspace(params.workspaceUuid), params)
|
||||||
|
|
||||||
|
return response as WorkspaceInvitationResponse
|
||||||
|
}
|
||||||
|
|
||||||
async createWorkspace(params: WorkspaceCreationRequestParams): Promise<WorkspaceCreationResponse> {
|
async createWorkspace(params: WorkspaceCreationRequestParams): Promise<WorkspaceCreationResponse> {
|
||||||
const response = await this.httpService.post(Paths.v1.createWorkspace, params)
|
const response = await this.httpService.post(Paths.v1.createWorkspace, params)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
import { WorkspaceInvitationRequestParams } from '../../Request/Workspace/WorkspaceInvitationRequestParams'
|
||||||
import { WorkspaceCreationRequestParams } from '../../Request/Workspace/WorkspaceCreationRequestParams'
|
import { WorkspaceCreationRequestParams } from '../../Request/Workspace/WorkspaceCreationRequestParams'
|
||||||
|
import { WorkspaceInvitationResponse } from '../../Response/Workspace/WorkspaceInvitationResponse'
|
||||||
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
import { WorkspaceCreationResponse } from '../../Response/Workspace/WorkspaceCreationResponse'
|
||||||
|
|
||||||
export interface WorkspaceServerInterface {
|
export interface WorkspaceServerInterface {
|
||||||
createWorkspace(params: WorkspaceCreationRequestParams): Promise<WorkspaceCreationResponse>
|
createWorkspace(params: WorkspaceCreationRequestParams): Promise<WorkspaceCreationResponse>
|
||||||
|
inviteToWorkspace(params: WorkspaceInvitationRequestParams): Promise<WorkspaceInvitationResponse>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { WorkspaceType } from '@standardnotes/common'
|
import { Uuid, WorkspaceType } from '@standardnotes/common'
|
||||||
|
|
||||||
export interface WorkspaceClientInterface {
|
export interface WorkspaceClientInterface {
|
||||||
createWorkspace(dto: {
|
createWorkspace(dto: {
|
||||||
@@ -8,4 +8,8 @@ export interface WorkspaceClientInterface {
|
|||||||
publicKey?: string
|
publicKey?: string
|
||||||
workspaceName?: string
|
workspaceName?: string
|
||||||
}): Promise<{ uuid: string } | null>
|
}): Promise<{ uuid: string } | null>
|
||||||
|
inviteToWorkspace(dto: {
|
||||||
|
inviteeEmail: string
|
||||||
|
workspaceUuid: Uuid
|
||||||
|
}): Promise<{ uuid: string } | null>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { WorkspaceApiServiceInterface } from '@standardnotes/api'
|
import { WorkspaceApiServiceInterface } from '@standardnotes/api'
|
||||||
import { WorkspaceType } from '@standardnotes/common'
|
import { Uuid, WorkspaceType } from '@standardnotes/common'
|
||||||
|
|
||||||
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
import { InternalEventBusInterface } from '../Internal/InternalEventBusInterface'
|
||||||
import { AbstractService } from '../Service/AbstractService'
|
import { AbstractService } from '../Service/AbstractService'
|
||||||
@@ -13,6 +13,20 @@ export class WorkspaceManager extends AbstractService implements WorkspaceClient
|
|||||||
super(internalEventBus)
|
super(internalEventBus)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async inviteToWorkspace(dto: { inviteeEmail: string; workspaceUuid: Uuid }): Promise<{ uuid: string } | null> {
|
||||||
|
try {
|
||||||
|
const result = await this.workspaceApiService.inviteToWorkspace(dto)
|
||||||
|
|
||||||
|
if (result.data.error !== undefined) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.data
|
||||||
|
} catch (error) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async createWorkspace(dto: {
|
async createWorkspace(dto: {
|
||||||
workspaceType: WorkspaceType,
|
workspaceType: WorkspaceType,
|
||||||
encryptedWorkspaceKey?: string
|
encryptedWorkspaceKey?: string
|
||||||
|
|||||||
Reference in New Issue
Block a user