Merge pull request #2943 from specklesystems/fabians/accessreq-ioc-6
chore(server): accessrequests IoC 6 - initializeEventListenerFactory
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { moduleLogger } from '@/logging/logging'
|
||||
import { initializeEventListener } from '@/modules/accessrequests/services/eventListener'
|
||||
import { AccessRequestsEmitter } from '@/modules/accessrequests/events/emitter'
|
||||
import { initializeEventListenerFactory } from '@/modules/accessrequests/services/eventListener'
|
||||
import { getStreamCollaborators } from '@/modules/core/repositories/streams'
|
||||
import { publishNotification } from '@/modules/notifications/services/publication'
|
||||
import { Optional, SpeckleModule } from '@/modules/shared/helpers/typeHelper'
|
||||
|
||||
let quitListeners: Optional<() => void> = undefined
|
||||
@@ -9,6 +12,11 @@ const ServerAccessRequestsModule: SpeckleModule = {
|
||||
moduleLogger.info('🔐 Init access request module')
|
||||
|
||||
if (isInitial) {
|
||||
const initializeEventListener = initializeEventListenerFactory({
|
||||
getStreamCollaborators,
|
||||
publishNotification,
|
||||
accessRequestsEventListener: AccessRequestsEmitter.listen
|
||||
})
|
||||
quitListeners = initializeEventListener()
|
||||
}
|
||||
},
|
||||
|
||||
@@ -6,61 +6,85 @@ import {
|
||||
import { isStreamAccessRequest } from '@/modules/accessrequests/repositories'
|
||||
import { Roles } from '@/modules/core/helpers/mainConstants'
|
||||
import { getStreamCollaborators } from '@/modules/core/repositories/streams'
|
||||
import { NotificationType } from '@/modules/notifications/helpers/types'
|
||||
import { publishNotification } from '@/modules/notifications/services/publication'
|
||||
import {
|
||||
NotificationPublisher,
|
||||
NotificationType
|
||||
} from '@/modules/notifications/helpers/types'
|
||||
|
||||
async function onServerAccessRequestCreated(
|
||||
payload: AccessRequestsEventsPayloads[AccessRequestsEvents.Created]
|
||||
) {
|
||||
const { request } = payload
|
||||
type OnServerAccessRequestCreatedDeps = {
|
||||
getStreamCollaborators: typeof getStreamCollaborators
|
||||
publishNotification: NotificationPublisher
|
||||
}
|
||||
|
||||
// Send out email to all owners of the stream
|
||||
if (isStreamAccessRequest(request)) {
|
||||
const owners = await getStreamCollaborators(request.resourceId, Roles.Stream.Owner)
|
||||
await Promise.all(
|
||||
owners.map((o) =>
|
||||
publishNotification(NotificationType.NewStreamAccessRequest, {
|
||||
targetUserId: o.id,
|
||||
data: {
|
||||
requestId: request.id
|
||||
}
|
||||
})
|
||||
const onServerAccessRequestCreatedFactory =
|
||||
(deps: OnServerAccessRequestCreatedDeps) =>
|
||||
async (payload: AccessRequestsEventsPayloads[AccessRequestsEvents.Created]) => {
|
||||
const { request } = payload
|
||||
|
||||
// Send out email to all owners of the stream
|
||||
if (isStreamAccessRequest(request)) {
|
||||
const owners = await deps.getStreamCollaborators(
|
||||
request.resourceId,
|
||||
Roles.Stream.Owner
|
||||
)
|
||||
)
|
||||
await Promise.all(
|
||||
owners.map((o) =>
|
||||
deps.publishNotification(NotificationType.NewStreamAccessRequest, {
|
||||
targetUserId: o.id,
|
||||
data: {
|
||||
requestId: request.id
|
||||
}
|
||||
})
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
type OnServerAccessRequestFinalizedDeps = {
|
||||
publishNotification: NotificationPublisher
|
||||
}
|
||||
|
||||
async function onServerAccessRequestFinalized(
|
||||
payload: AccessRequestsEventsPayloads[AccessRequestsEvents.Finalized]
|
||||
) {
|
||||
const { approved, request, finalizedBy } = payload
|
||||
const onServerAccessRequestFinalizedFactory =
|
||||
(deps: OnServerAccessRequestFinalizedDeps) =>
|
||||
async (payload: AccessRequestsEventsPayloads[AccessRequestsEvents.Finalized]) => {
|
||||
const { approved, request, finalizedBy } = payload
|
||||
|
||||
// Send out email to requester, if accepted
|
||||
if (approved && isStreamAccessRequest(request)) {
|
||||
await publishNotification(NotificationType.StreamAccessRequestApproved, {
|
||||
targetUserId: request.requesterId,
|
||||
data: {
|
||||
request,
|
||||
finalizedBy
|
||||
}
|
||||
})
|
||||
// Send out email to requester, if accepted
|
||||
if (approved && isStreamAccessRequest(request)) {
|
||||
await deps.publishNotification(NotificationType.StreamAccessRequestApproved, {
|
||||
targetUserId: request.requesterId,
|
||||
data: {
|
||||
request,
|
||||
finalizedBy
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize event listener for tracking various Speckle events
|
||||
*/
|
||||
export function initializeEventListener() {
|
||||
const quitCbs = [
|
||||
AccessRequestsEmitter.listen(
|
||||
AccessRequestsEvents.Created,
|
||||
onServerAccessRequestCreated
|
||||
),
|
||||
AccessRequestsEmitter.listen(
|
||||
AccessRequestsEvents.Finalized,
|
||||
onServerAccessRequestFinalized
|
||||
)
|
||||
]
|
||||
export const initializeEventListenerFactory =
|
||||
(
|
||||
deps: {
|
||||
accessRequestsEventListener: (typeof AccessRequestsEmitter)['listen']
|
||||
} & OnServerAccessRequestCreatedDeps &
|
||||
OnServerAccessRequestFinalizedDeps
|
||||
) =>
|
||||
() => {
|
||||
const onServerAccessRequestCreated = onServerAccessRequestCreatedFactory(deps)
|
||||
const onServerAccessRequestFinalized = onServerAccessRequestFinalizedFactory(deps)
|
||||
|
||||
return () => quitCbs.forEach((quit) => quit())
|
||||
}
|
||||
const quitCbs = [
|
||||
deps.accessRequestsEventListener(
|
||||
AccessRequestsEvents.Created,
|
||||
onServerAccessRequestCreated
|
||||
),
|
||||
deps.accessRequestsEventListener(
|
||||
AccessRequestsEvents.Finalized,
|
||||
onServerAccessRequestFinalized
|
||||
)
|
||||
]
|
||||
|
||||
return () => quitCbs.forEach((quit) => quit())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user