:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(workflows): send transactional emails past the email quota (#123849)
Meikel Ratz3bd70068a391
diff --git a/nodejs/src/cdp/cdp-api.serial.test.ts b/nodejs/src/cdp/cdp-api.serial.test.tsindex 64baeb284314..2132938a4b3c 100644--- a/nodejs/src/cdp/cdp-api.serial.test.ts+++ b/nodejs/src/cdp/cdp-api.serial.test.ts@@ -2117,7 +2117,11 @@ describe('CDP API', () => {} = {}) => jwt.sign({ team_id: teamId, hog_flow_id: hogFlowId }, secret, { audience, expiresIn: '2m' })- const transactionalFlow = (trigger: 'api' | 'schedule', status: HogFlow['status']): HogFlow =>+ const transactionalFlow = (+ trigger: 'api' | 'schedule',+ status: HogFlow['status'],+ category: 'transactional' | 'marketing' = 'transactional'+ ): HogFlow =>new FixtureHogFlowBuilder().withTeamId(team.id).withStatus(status)@@ -2125,7 +2129,10 @@ describe('CDP API', () => {.withWorkflow({actions: {trigger: { type: 'trigger', config: { type: trigger } },- email: { type: 'function_email', config: { template_id: 'template-email', inputs: {} } },+ email: {+ type: 'function_email',+ config: { template_id: 'template-email', inputs: {}, message_category_type: category },+ },},edges: [{ from: 'trigger', to: 'email', type: 'continue' }],})@@ -2257,12 +2264,26 @@ describe('CDP API', () => {})})- it('refuses a send when the team is over its email quota', async () => {+ it('queues a send when the team is over its email quota', async () => {jest.spyOn(api['deps'].quotaLimiting, 'isTeamQuotaLimited').mockResolvedValue(true)- const limitedBefore = await outcomeCount('quota_limited')+ const queuedBefore = await outcomeCount('queued')const res = await send(hogFlow.id, { Authorization: `Bearer ${mintToken(team.id, hogFlow.id)}` })+ expect(res.status).toEqual(202)+ expect(await outcomeCount('queued')).toEqual(queuedBefore + 1)+ expect(mockQueueInvocations).toHaveBeenCalledTimes(1)+ })++ it('refuses a send of a marketing email when the team is over its email quota', async () => {+ jest.spyOn(api['deps'].quotaLimiting, 'isTeamQuotaLimited').mockResolvedValue(true)+ const marketingFlow = await insertHogFlow(transactionalFlow('api', 'active', 'marketing'))+ const limitedBefore = await outcomeCount('quota_limited')++ const res = await send(marketingFlow.id, {+ Authorization: `Bearer ${mintToken(team.id, marketingFlow.id)}`,+ })+expect(res.status).toEqual(429)expect(await outcomeCount('quota_limited')).toEqual(limitedBefore + 1)expect(mockQueueInvocations).not.toHaveBeenCalled()diff --git a/nodejs/src/cdp/services/hog-flow-invocation-pipeline.service.test.ts b/nodejs/src/cdp/services/hog-flow-invocation-pipeline.service.test.tsindex 6e4ae9c5d948..0e3a67ebe439 100644--- a/nodejs/src/cdp/services/hog-flow-invocation-pipeline.service.test.ts+++ b/nodejs/src/cdp/services/hog-flow-invocation-pipeline.service.test.ts@@ -31,6 +31,7 @@ function makeHogFlowInvocation(hogFlowId = 'flow-1', overrides: { billable_actioname: 'test flow',version: 5,billable_action_types: overrides.billable_action_types ?? [],+ actions: (overrides.billable_action_types ?? []).map((type) => ({ type, config: {} })),},state: { event: { uuid: 'evt-1' } },person: undefined,diff --git a/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.test.ts b/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.test.tsindex 91295126201e..ed38724827a8 100644--- a/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.test.ts+++ b/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.test.ts@@ -67,6 +67,55 @@ describe('HogFlow Quota Limiting', () => {expect(result.isLimited).toBe(true)})+ const transactionalEmail = { type: 'function_email', config: { message_category_type: 'transactional' } }+ const marketingEmail = { type: 'function_email', config: { message_category_type: 'marketing' } }++ it.each([+ {+ name: 'only transactional emails are not limited',+ trigger: { type: 'event' },+ actions: [transactionalEmail, transactionalEmail],+ limitedResources: ['workflow_emails'],+ expected: false,+ },+ {+ name: 'an api trigger alone does not exempt an uncategorized email',+ trigger: { type: 'api' },+ actions: [{ type: 'function_email', config: {} }],+ limitedResources: ['workflow_emails'],+ expected: true,+ },+ {+ name: 'a marketing email next to a transactional one is limited',+ trigger: { type: 'event' },+ actions: [transactionalEmail, marketingEmail],+ limitedResources: ['workflow_emails'],+ expected: true,+ },+ {+ name: 'a destination next to transactional emails is still limited by the destination quota',+ trigger: { type: 'event' },+ actions: [transactionalEmail, { type: 'function', config: {} }],+ limitedResources: ['workflow_emails', 'workflow_destinations_dispatched'],+ expected: true,+ },+ ])('$name', async ({ trigger, actions, limitedResources, expected }) => {+ mockQuotaLimiting.isTeamQuotaLimited.mockImplementation((_teamId, resource) =>+ Promise.resolve(limitedResources.includes(resource))+ )++ const hogFlow: HogFlow = {+ ...baseHogFlow,+ trigger,+ actions,+ billable_action_types: [...new Set(actions.map((action) => action.type))],+ } as unknown as HogFlow++ const result = await checkHogFlowQuotaLimits(hogFlow, teamId, mockQuotaLimiting)++ expect(result.isLimited).toBe(expected)+ })+it('should limit workflow with push action when team has destination quota limit', async () => {mockQuotaLimiting.isTeamQuotaLimited.mockImplementation((_teamId, resource) => {return Promise.resolve(resource === 'workflow_destinations_dispatched')diff --git a/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.ts b/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.tsindex 76f83523e74f..849173a89e80 100644--- a/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.ts+++ b/nodejs/src/cdp/services/hogflows/hogflow-quota-limiting.ts@@ -1,6 +1,6 @@import { Counter } from 'prom-client'-import { HogFlow } from '~/cdp/schema/hogflow'+import { HogFlow, HogFlowAction } from '~/cdp/schema/hogflow'import { QuotaLimiting } from '../../../common/services/quota-limiting.service'import { CyclotronJobInvocationHogFlow } from '../../types'@@ -16,6 +16,17 @@ export interface HogFlowQuotaLimitResult {isLimited: boolean}+// Transactional emails bypass the email quota. Each send still reports billable usage, so the team pays for it.+function sendsOnlyTransactionalEmail(hogFlow: HogFlow): boolean {+ const emailActions = hogFlow.actions.filter(+ (action): action is Extract<HogFlowAction, { type: 'function_email' }> => action.type === 'function_email'+ )+ return (+ emailActions.length > 0 &&+ emailActions.every((action) => action.config?.message_category_type === 'transactional')+ )+}+/*** Checks if a hogflow is quota limited based on its billable action types.* Uses the pre-computed billable_action_types field for efficient quota checking.@@ -40,7 +51,11 @@ export async function checkHogFlowQuotaLimits(])// Check if any billable action type is quota limited- if (isEmailQuotaLimited && billableActionTypes.includes('function_email')) {+ if (+ isEmailQuotaLimited &&+ billableActionTypes.includes('function_email') &&+ !sendsOnlyTransactionalEmail(hogFlow)+ ) {return { isLimited: true }}