Files
speckle-server/packages/server/modules/auth/services/postAuth.ts
T
Kristaps Fabians Geikins aee14edc5b chore(server): getting rid of module-scoped event emitters - batch #1 (#3766)
* got rid of models emitter + added tests

* got rid of projects emitter + added tests

* replaced user event bus

* test migrate
2025-01-13 11:39:46 +02:00

65 lines
1.9 KiB
TypeScript

import { Logger } from '@/logging/logging'
import {
addToMailchimpAudience,
triggerMailchimpCustomerJourney
} from '@/modules/auth/services/mailchimp'
import { UserEvents } from '@/modules/core/domain/users/events'
import {
enableMixpanel,
getMailchimpNewsletterIds,
getMailchimpOnboardingIds,
getMailchimpStatus
} from '@/modules/shared/helpers/envHelper'
import { EventBus, EventPayload } from '@/modules/shared/services/eventBus'
import { mixpanel } from '@/modules/shared/utils/mixpanel'
const onUserCreatedFactory =
(deps: { logger: Logger }) =>
async (payload: EventPayload<typeof UserEvents.Created>) => {
const { user, signUpCtx } = payload.payload
try {
// Send event to MP
const userEmail = user.email
const newsletterConsent = signUpCtx?.newsletterConsent
if (userEmail && enableMixpanel()) {
const isInvite = !!signUpCtx?.isInvite
await mixpanel({ userEmail, req: signUpCtx?.req }).track('Sign Up', {
isInvite
})
}
// Set up mailchimp
if (getMailchimpStatus()) {
try {
const onboardingIds = getMailchimpOnboardingIds()
await triggerMailchimpCustomerJourney(user, onboardingIds)
if (newsletterConsent) {
const { listId } = getMailchimpNewsletterIds()
await addToMailchimpAudience(user, listId)
}
} catch (error) {
deps.logger.warn(error, 'Failed to sign up user to mailchimp lists')
}
}
} catch (e) {
deps.logger.error(
{
error: e,
userId: user.id
},
'Post sign up tracking failed'
)
}
}
export const initializeEventListenerFactory =
(deps: { eventBus: EventBus; logger: Logger }) => () => {
const onUserCreated = onUserCreatedFactory(deps)
const cbs = [deps.eventBus.listen(UserEvents.Created, onUserCreated)]
return () => cbs.forEach((cb) => cb())
}