From 725551007c20b55c5b74d1c37782432dc12a4665 Mon Sep 17 00:00:00 2001 From: Misha Savelyev Date: Wed, 28 Jun 2023 15:23:56 +0200 Subject: [PATCH 1/2] Pass `segmentId` to automation processing --- .../nodejs/automation/workers/newActivityWorker.ts | 4 ++-- .../nodejs/automation/workers/newMemberWorker.ts | 4 ++-- .../serverless/microservices/nodejs/messageTypes.ts | 10 ++++++++-- .../serverless/microservices/nodejs/workerFactory.ts | 12 ++++++++++-- backend/src/serverless/utils/nodeWorkerSQS.ts | 4 ++++ backend/src/services/activityService.ts | 6 +++++- backend/src/services/memberService.ts | 3 ++- 7 files changed, 33 insertions(+), 10 deletions(-) diff --git a/backend/src/serverless/microservices/nodejs/automation/workers/newActivityWorker.ts b/backend/src/serverless/microservices/nodejs/automation/workers/newActivityWorker.ts index ef7aca8b00..11b57e453e 100644 --- a/backend/src/serverless/microservices/nodejs/automation/workers/newActivityWorker.ts +++ b/backend/src/serverless/microservices/nodejs/automation/workers/newActivityWorker.ts @@ -126,8 +126,8 @@ export const prepareActivityPayload = (activity: any): any => { * @param activityId activity unique ID * @param activityData activity data */ -export default async (tenantId: string, activityId: string): Promise => { - const userContext = await getUserContext(tenantId) +export default async (tenantId: string, activityId: string, segmentId: string): Promise => { + const userContext = await getUserContext(tenantId, null, [segmentId]) try { // check if relevant automations exists in this tenant diff --git a/backend/src/serverless/microservices/nodejs/automation/workers/newMemberWorker.ts b/backend/src/serverless/microservices/nodejs/automation/workers/newMemberWorker.ts index a36878fb80..f1c6c38b5e 100644 --- a/backend/src/serverless/microservices/nodejs/automation/workers/newMemberWorker.ts +++ b/backend/src/serverless/microservices/nodejs/automation/workers/newMemberWorker.ts @@ -84,8 +84,8 @@ export const prepareMemberPayload = (member: any): any => { * @param memberId tenant member ID * @param memberData community member data */ -export default async (tenantId: string, memberId?: string): Promise => { - const userContext = await getUserContext(tenantId) +export default async (tenantId: string, memberId: string, segmentId: string): Promise => { + const userContext = await getUserContext(tenantId, null, [segmentId]) try { // check if relevant automation exists in this tenant diff --git a/backend/src/serverless/microservices/nodejs/messageTypes.ts b/backend/src/serverless/microservices/nodejs/messageTypes.ts index e60c5b2b5d..0d5037c2cb 100644 --- a/backend/src/serverless/microservices/nodejs/messageTypes.ts +++ b/backend/src/serverless/microservices/nodejs/messageTypes.ts @@ -29,13 +29,19 @@ export type ActivityAutomationData = { activityId: string } -export type NewActivityAutomationMessage = BaseNodeMicroserviceMessage & ActivityAutomationData +export type NewActivityAutomationMessage = BaseNodeMicroserviceMessage & + ActivityAutomationData & { + segmentId: string + } export type MemberAutomationData = { memberId: string } -export type NewMemberAutomationMessage = BaseNodeMicroserviceMessage & MemberAutomationData +export type NewMemberAutomationMessage = BaseNodeMicroserviceMessage & + MemberAutomationData & { + segmentId: string + } export type ProcessAutomationMessage = BaseNodeMicroserviceMessage & { automationType: AutomationType diff --git a/backend/src/serverless/microservices/nodejs/workerFactory.ts b/backend/src/serverless/microservices/nodejs/workerFactory.ts index f5f3b43f77..9417c31244 100644 --- a/backend/src/serverless/microservices/nodejs/workerFactory.ts +++ b/backend/src/serverless/microservices/nodejs/workerFactory.ts @@ -116,10 +116,18 @@ async function workerFactory(event: NodeMicroserviceMessage): Promise { switch (automationRequest.trigger) { case AutomationTrigger.NEW_ACTIVITY: const newActivityAutomationRequest = event as NewActivityAutomationMessage - return newActivityWorker(tenant, newActivityAutomationRequest.activityId) + return newActivityWorker( + tenant, + newActivityAutomationRequest.activityId, + newActivityAutomationRequest.segmentId, + ) case AutomationTrigger.NEW_MEMBER: const newMemberAutomationRequest = event as NewMemberAutomationMessage - return newMemberWorker(tenant, newMemberAutomationRequest.memberId) + return newMemberWorker( + tenant, + newMemberAutomationRequest.memberId, + newMemberAutomationRequest.segmentId, + ) default: throw new Error(`Invalid automation trigger ${automationRequest.trigger}!`) } diff --git a/backend/src/serverless/utils/nodeWorkerSQS.ts b/backend/src/serverless/utils/nodeWorkerSQS.ts index 3af426338f..f6d151d391 100644 --- a/backend/src/serverless/utils/nodeWorkerSQS.ts +++ b/backend/src/serverless/utils/nodeWorkerSQS.ts @@ -84,11 +84,13 @@ export const sendNodeWorkerMessage = async ( export const sendNewActivityNodeSQSMessage = async ( tenant: string, activityId: string, + segmentId: string, ): Promise => { const payload = { type: NodeWorkerMessageType.NODE_MICROSERVICE, tenant, activityId, + segmentId, trigger: AutomationTrigger.NEW_ACTIVITY, service: 'automation', } @@ -98,11 +100,13 @@ export const sendNewActivityNodeSQSMessage = async ( export const sendNewMemberNodeSQSMessage = async ( tenant: string, memberId: string, + segmentId: string, ): Promise => { const payload = { type: NodeWorkerMessageType.NODE_MICROSERVICE, tenant, memberId, + segmentId, trigger: AutomationTrigger.NEW_MEMBER, service: 'automation', } diff --git a/backend/src/services/activityService.ts b/backend/src/services/activityService.ts index 7b13a79fb3..e222efec89 100644 --- a/backend/src/services/activityService.ts +++ b/backend/src/services/activityService.ts @@ -179,7 +179,11 @@ export default class ActivityService extends LoggerBase { if (!existing && fireCrowdWebhooks) { try { - await sendNewActivityNodeSQSMessage(this.options.currentTenant.id, record.id) + await sendNewActivityNodeSQSMessage( + this.options.currentTenant.id, + record.id, + record.segmentId, + ) } catch (err) { this.log.error( err, diff --git a/backend/src/services/memberService.ts b/backend/src/services/memberService.ts index f6be8c239e..d23348d6a4 100644 --- a/backend/src/services/memberService.ts +++ b/backend/src/services/memberService.ts @@ -390,7 +390,8 @@ export default class MemberService extends LoggerBase { if (!existing && fireCrowdWebhooks) { try { - await sendNewMemberNodeSQSMessage(this.options.currentTenant.id, record.id) + const segment = SequelizeRepository.getStrictlySingleActiveSegment(this.options) + await sendNewMemberNodeSQSMessage(this.options.currentTenant.id, record.id, segment.id) } catch (err) { logger.error(err, `Error triggering new member automation - ${record.id}!`) } From 907368aedfab97cf614ed24f084b75933f152c49 Mon Sep 17 00:00:00 2001 From: Misha Savelyev Date: Wed, 28 Jun 2023 16:05:51 +0200 Subject: [PATCH 2/2] If segments aren't available, don't try to build activity types --- backend/src/services/segmentService.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/src/services/segmentService.ts b/backend/src/services/segmentService.ts index 14c86b6c3f..ff76437eb9 100644 --- a/backend/src/services/segmentService.ts +++ b/backend/src/services/segmentService.ts @@ -384,6 +384,9 @@ export default class SegmentService extends LoggerBase { } static async getTenantActivityTypes(subprojects: any) { + if (!subprojects) { + return { custom: {}, default: {} } + } return subprojects.reduce((acc: any, subproject) => { const activityTypes = SegmentRepository.buildActivityTypes(subproject)