gergo/eventBus (#2498)
* feat(eventBus): WIP event bus typescript wizardy * feat(eventBus): final eventbus setup with all the typescript foo * fix(workspaces): fix workspace core imports * test(workspaces): fix expected events name * test(workspaces): fix tests
This commit is contained in:
@@ -2,7 +2,7 @@ import {
|
||||
WorkspaceEvents,
|
||||
WorkspaceEventsPayloads
|
||||
} from '@/modules/workspacesCore/domain/events'
|
||||
import { Workspace, WorkspaceAcl } from '@/modules/workspaces/domain/types'
|
||||
import { Workspace, WorkspaceAcl } from '@/modules/workspacesCore/domain/types'
|
||||
|
||||
/** Workspace */
|
||||
|
||||
@@ -70,6 +70,6 @@ export type StoreBlob = (args: string) => Promise<string>
|
||||
/** Events */
|
||||
|
||||
export type EmitWorkspaceEvent = <TEvent extends WorkspaceEvents>(args: {
|
||||
event: TEvent
|
||||
eventName: TEvent
|
||||
payload: WorkspaceEventsPayloads[TEvent]
|
||||
}) => Promise<unknown[]>
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { WorkspaceRoles } from '@speckle/shared'
|
||||
|
||||
export type Workspace = {
|
||||
id: string
|
||||
name: string
|
||||
description: string | null
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
logoUrl: string | null
|
||||
}
|
||||
|
||||
export type WorkspaceAcl = { userId: string; role: WorkspaceRoles; workspaceId: string }
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Workspace, WorkspaceAcl } from '@/modules/workspaces/domain/types'
|
||||
import { Workspace, WorkspaceAcl } from '@/modules/workspacesCore/domain/types'
|
||||
import {
|
||||
DeleteWorkspaceRole,
|
||||
GetWorkspace,
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
UpsertWorkspace,
|
||||
UpsertWorkspaceRole
|
||||
} from '@/modules/workspaces/domain/operations'
|
||||
import { Workspace } from '@/modules/workspaces/domain/types'
|
||||
import { Workspace } from '@/modules/workspacesCore/domain/types'
|
||||
import { Roles } from '@speckle/shared'
|
||||
import cryptoRandomString from 'crypto-random-string'
|
||||
|
||||
@@ -47,7 +47,10 @@ export const createWorkspaceFactory =
|
||||
workspaceId: workspace.id
|
||||
})
|
||||
|
||||
await emitWorkspaceEvent({ event: WorkspaceEvents.Created, payload: workspace })
|
||||
await emitWorkspaceEvent({
|
||||
eventName: WorkspaceEvents.Created,
|
||||
payload: { ...workspace, createdByUserId: userId }
|
||||
})
|
||||
// emit a workspace created event
|
||||
|
||||
return workspace
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
GetWorkspaceRoles,
|
||||
UpsertWorkspaceRole
|
||||
} from '@/modules/workspaces/domain/operations'
|
||||
import { WorkspaceAcl } from '@/modules/workspaces/domain/types'
|
||||
import { WorkspaceAcl } from '@/modules/workspacesCore/domain/types'
|
||||
import { WorkspaceAdminRequiredError } from '@/modules/workspaces/errors/workspace'
|
||||
import { isUserLastWorkspaceAdmin } from '@/modules/workspaces/utils/isUserLastWorkspaceAdmin'
|
||||
import { WorkspaceEvents } from '@/modules/workspacesCore/domain/events'
|
||||
@@ -41,7 +41,7 @@ export const deleteWorkspaceRoleFactory =
|
||||
return null
|
||||
}
|
||||
|
||||
emitWorkspaceEvent({ event: WorkspaceEvents.RoleDeleted, payload: deletedRole })
|
||||
emitWorkspaceEvent({ eventName: WorkspaceEvents.RoleDeleted, payload: deletedRole })
|
||||
|
||||
return deletedRole
|
||||
}
|
||||
@@ -83,7 +83,7 @@ export const setWorkspaceRoleFactory =
|
||||
await upsertWorkspaceRole({ userId, workspaceId, role })
|
||||
|
||||
await emitWorkspaceEvent({
|
||||
event: WorkspaceEvents.RoleUpdated,
|
||||
eventName: WorkspaceEvents.RoleUpdated,
|
||||
payload: { userId, workspaceId, role }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
import db from '@/db/knex'
|
||||
import cryptoRandomString from 'crypto-random-string'
|
||||
import { expect } from 'chai'
|
||||
import { Workspace, WorkspaceAcl } from '@/modules/workspaces/domain/types'
|
||||
import { Workspace, WorkspaceAcl } from '@/modules/workspacesCore/domain/types'
|
||||
import { expectToThrow } from '@/test/assertionHelper'
|
||||
import { BasicTestUser, createTestUser } from '@/test/authHelper'
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Workspace, WorkspaceAcl } from '@/modules/workspaces/domain/types'
|
||||
import { Workspace, WorkspaceAcl } from '@/modules/workspacesCore/domain/types'
|
||||
import { createWorkspaceFactory } from '@/modules/workspaces/services/workspaceCreation'
|
||||
import { Roles } from '@speckle/shared'
|
||||
import { expect } from 'chai'
|
||||
import cryptoRandomString from 'crypto-random-string'
|
||||
import { WorkspaceEvents } from '@/modules/workspacesCore/domain/events'
|
||||
|
||||
describe('Workspace services', () => {
|
||||
describe('createWorkspaceFactory creates a function, that', () => {
|
||||
@@ -60,15 +61,15 @@ describe('Workspace services', () => {
|
||||
it('emits a workspace created event', async () => {
|
||||
const eventData = {
|
||||
isCalled: false,
|
||||
event: '',
|
||||
eventName: '',
|
||||
payload: {}
|
||||
}
|
||||
const createWorkspace = createWorkspaceFactory({
|
||||
upsertWorkspace: async () => {},
|
||||
upsertWorkspaceRole: async () => {},
|
||||
emitWorkspaceEvent: async ({ event, payload }) => {
|
||||
emitWorkspaceEvent: async ({ eventName, payload }) => {
|
||||
eventData.isCalled = true
|
||||
eventData.event = event
|
||||
eventData.eventName = eventName
|
||||
eventData.payload = payload
|
||||
return []
|
||||
},
|
||||
@@ -88,8 +89,8 @@ describe('Workspace services', () => {
|
||||
})
|
||||
|
||||
expect(eventData.isCalled).to.equal(true)
|
||||
expect(eventData.event).to.equal('created')
|
||||
expect(eventData.payload).to.deep.equal(workspace)
|
||||
expect(eventData.eventName).to.equal(WorkspaceEvents.Created)
|
||||
expect(eventData.payload).to.deep.equal({ ...workspace, createdByUserId: userId })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+9
-9
@@ -1,4 +1,4 @@
|
||||
import { WorkspaceAcl } from '@/modules/workspaces/domain/types'
|
||||
import { WorkspaceAcl } from '@/modules/workspacesCore/domain/types'
|
||||
import {
|
||||
deleteWorkspaceRoleFactory,
|
||||
setWorkspaceRoleFactory
|
||||
@@ -41,7 +41,7 @@ describe('Workspace role services', () => {
|
||||
it('emits a role-deleted event', async () => {
|
||||
const eventData = {
|
||||
isCalled: false,
|
||||
event: '',
|
||||
eventName: '',
|
||||
payload: {}
|
||||
}
|
||||
|
||||
@@ -57,9 +57,9 @@ describe('Workspace role services', () => {
|
||||
deleteWorkspaceRole: async () => {
|
||||
return storedRoles[0]
|
||||
},
|
||||
emitWorkspaceEvent: async ({ event, payload }) => {
|
||||
emitWorkspaceEvent: async ({ eventName, payload }) => {
|
||||
eventData.isCalled = true
|
||||
eventData.event = event
|
||||
eventData.eventName = eventName
|
||||
eventData.payload = payload
|
||||
|
||||
return []
|
||||
@@ -69,7 +69,7 @@ describe('Workspace role services', () => {
|
||||
await deleteWorkspaceRole({ userId, workspaceId })
|
||||
|
||||
expect(eventData.isCalled).to.be.true
|
||||
expect(eventData.event).to.equal(WorkspaceEvents.RoleDeleted)
|
||||
expect(eventData.eventName).to.equal(WorkspaceEvents.RoleDeleted)
|
||||
expect(eventData.payload).to.deep.equal(role)
|
||||
})
|
||||
it('throws if attempting to delete the last admin from a workspace', async () => {
|
||||
@@ -123,7 +123,7 @@ describe('Workspace role services', () => {
|
||||
it('emits a role-updated event', async () => {
|
||||
const eventData = {
|
||||
isCalled: false,
|
||||
event: '',
|
||||
eventName: '',
|
||||
payload: {}
|
||||
}
|
||||
|
||||
@@ -135,9 +135,9 @@ describe('Workspace role services', () => {
|
||||
const setWorkspaceRole = setWorkspaceRoleFactory({
|
||||
getWorkspaceRoles: async () => [],
|
||||
upsertWorkspaceRole: async () => {},
|
||||
emitWorkspaceEvent: async ({ event, payload }) => {
|
||||
emitWorkspaceEvent: async ({ eventName, payload }) => {
|
||||
eventData.isCalled = true
|
||||
eventData.event = event
|
||||
eventData.eventName = eventName
|
||||
eventData.payload = payload
|
||||
|
||||
return []
|
||||
@@ -147,7 +147,7 @@ describe('Workspace role services', () => {
|
||||
await setWorkspaceRole(role)
|
||||
|
||||
expect(eventData.isCalled).to.be.true
|
||||
expect(eventData.event).to.equal(WorkspaceEvents.RoleUpdated)
|
||||
expect(eventData.eventName).to.equal(WorkspaceEvents.RoleUpdated)
|
||||
expect(eventData.payload).to.deep.equal(role)
|
||||
})
|
||||
it('throws if attempting to remove the last admin in a workspace', async () => {
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
import { isUserLastWorkspaceAdmin } from '@/modules/workspaces/utils/isUserLastWorkspaceAdmin'
|
||||
import { WorkspaceAcl } from '@/modules/workspaces/domain/types'
|
||||
import { WorkspaceAcl } from '@/modules/workspacesCore/domain/types'
|
||||
import { expect } from 'chai'
|
||||
import { Roles } from '@speckle/shared'
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { WorkspaceAcl } from '@/modules/workspaces/domain/types'
|
||||
import { WorkspaceAcl } from '@/modules/workspacesCore/domain/types'
|
||||
|
||||
export const isUserLastWorkspaceAdmin = (
|
||||
workspaceRoles: WorkspaceAcl[],
|
||||
|
||||
Reference in New Issue
Block a user