: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
feat(cdp): key a code send on the sender the caller chose (#127129)
Michael Reichenbach172c74045747
diff --git a/nodejs/src/cdp/cdp-api.serial.test.ts b/nodejs/src/cdp/cdp-api.serial.test.tsindex eb8786683717..1c9d3271da02 100644--- a/nodejs/src/cdp/cdp-api.serial.test.ts+++ b/nodejs/src/cdp/cdp-api.serial.test.ts@@ -2313,31 +2313,45 @@ describe('CDP API', () => {.set({ Authorization: `Bearer ${tokenFor(route, team.id, hogFlow.id)}` }).send({ ...payload, idempotency_key: keyed.idempotency_key })const changedContent = { ...codeSend, content: { ...codeSend.content, subject: 'Your new code' } }+ const otherSenderId = codeSend.from.integrationId + 3+ const addresslessSend = { ...codeSend, from: { integrationId: codeSend.from.integrationId } }beforeEach(() => {keyed = { ...body, idempotency_key: `key-${new UUIDT().toString()}` }})it.each([- ['a template send', 'transactional_email', body],- ['a code send', 'transactional_email/send', { ...body, send: codeSend }],+ ['a template send', 'transactional_email', body, body],+ ['a code send', 'transactional_email/send', { ...body, send: codeSend }, { ...body, send: codeSend }],+ [+ 'a code send after its named address resolved to another sender',+ 'transactional_email/send',+ { ...body, send: codeSend },+ { ...body, send: { ...codeSend, from: { ...codeSend.from, integrationId: otherSenderId } } },+ ],+ [+ 'a code send from a sender without an address',+ 'transactional_email/send',+ { ...body, send: addresslessSend },+ { ...body, send: addresslessSend },+ ],] as const)('returns the first send for a retry of %s and queues nothing new',- async (_desc, route, payload) => {- const first = await sendKeyedOn(route, payload)- const retry = await sendKeyedOn(route, payload)-- expect(first.status).toEqual(202)- expect(first.body).toMatchObject({ replayed: false })- expect(retry.status).toEqual(202)- expect(retry.body).toEqual({+ async (_desc, route, first, retry) => {+ const sent = await sendKeyedOn(route, first)+ const replayed = await sendKeyedOn(route, retry)++ expect(sent.status).toEqual(202)+ expect(sent.body).toMatchObject({ replayed: false })+ expect(replayed.status).toEqual(202)+ expect(replayed.body).toEqual({status: 'queued',- invocation_id: first.body.invocation_id,+ invocation_id: sent.body.invocation_id,replayed: true,})expect(mockQueueInvocations).toHaveBeenCalledTimes(1)expect(emailStatusRows()).toEqual([- expect.objectContaining({ message_id: first.body.invocation_id, status: 'queued' }),+ expect.objectContaining({ message_id: sent.body.invocation_id, status: 'queued' }),])})@@ -2358,6 +2372,22 @@ describe('CDP API', () => {['transactional_email', body],['transactional_email/send', { ...body, send: codeSend }],],+ [+ 'another named sender',+ ['transactional_email/send', { ...body, send: codeSend }],+ [+ 'transactional_email/send',+ { ...body, send: { ...codeSend, from: { ...codeSend.from, email: '[email protected]' } } },+ ],+ ],+ [+ 'another sender without an address',+ ['transactional_email/send', { ...body, send: addresslessSend }],+ [+ 'transactional_email/send',+ { ...body, send: { ...addresslessSend, from: { integrationId: otherSenderId } } },+ ],+ ],['a changed cc',['transactional_email/send', { ...body, send: { ...codeSend, cc: ['[email protected]'] } }],diff --git a/nodejs/src/cdp/cdp-api.ts b/nodejs/src/cdp/cdp-api.tsindex b21392779e99..2071adfc65f1 100644--- a/nodejs/src/cdp/cdp-api.ts+++ b/nodejs/src/cdp/cdp-api.ts@@ -54,6 +54,7 @@ import { matchesWaitUntilCondition } from './services/hogflows/hogflow-utils'import { IdempotencyKeys } from './services/hogflows/idempotency-keys'import { WorkflowStepResumeSchema } from './services/hogflows/step-resume.service'import {+ AcceptedSend,AcceptedSendRequestSchema,CODE_SEND_TOKEN_CLAIMS,TEMPLATE_SEND_TOKEN_CLAIMS,@@ -181,10 +182,27 @@ const transactionalEmailFingerprint = (): string => {const templateRequest = [hogFlowId, to, distinct_id, variables]return IdempotencyKeys.fingerprint(- send ? [...templateRequest, ACCEPTED_SEND_FINGERPRINT_MARKER, send] : templateRequest+ send ? [...templateRequest, ACCEPTED_SEND_FINGERPRINT_MARKER, withSenderTheCallerChose(send)] : templateRequest)}+type CallerChosenSender = Partial<NonNullable<AcceptedSend['from']>>++const withSenderTheCallerChose = ({+ from,+ ...send+}: AcceptedSend): Omit<AcceptedSend, 'from'> & { from?: CallerChosenSender } => ({+ ...send,+ from: senderTheCallerChose(from),+})++const senderTheCallerChose = (from: AcceptedSend['from']): CallerChosenSender | undefined => {+ if (!from) {+ return undefined+ }+ return from.email ? { email: from.email, name: from.name } : { integrationId: from.integrationId, name: from.name }+}+export class CdpApi {private hogExecutorAsync: HogExecutorAsyncServiceprivate hogInputsService: HogInputsService