fix(msg): pipedrive updateActivity fix (#38167)

This commit is contained in:
Marcus Hof
2025-09-16 15:44:49 +02:00
committed by GitHub
parent f51500382b
commit c7f9f925d4
2 changed files with 86 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import { DateTime, Settings } from 'luxon'
import { defaultConfig } from '~/config/config'
import { forSnapshot } from '~/tests/helpers/snapshots'
import { parseJSON } from '~/utils/json-parse'
import { createHogFunction } from '../_tests/fixtures'
import {
@@ -26,6 +27,7 @@ describe('SegmentDestinationExecutorService', () => {
const pipedrivePlugin = SEGMENT_DESTINATIONS_BY_ID['segment-actions-pipedrive']
const pipedriveAction = pipedrivePlugin.destination.actions['createUpdatePerson']
const pipedriveActivitiesAction = pipedrivePlugin.destination.actions['createUpdateActivity']
beforeEach(() => {
mockFetch.mockReset()
@@ -46,6 +48,7 @@ describe('SegmentDestinationExecutorService', () => {
jest.spyOn(Date, 'now').mockReturnValue(fixedTime.toMillis())
jest.spyOn(amplitudeAction as any, 'perform')
jest.spyOn(pipedriveAction as any, 'perform')
jest.spyOn(pipedriveActivitiesAction as any, 'perform')
})
afterAll(() => {
@@ -431,5 +434,71 @@ describe('SegmentDestinationExecutorService', () => {
expect(result.finished).toBe(true)
})
it('handles activity_id field correctly for pipedrive activities action', async () => {
jest.spyOn(pipedriveActivitiesAction as any, 'perform')
const testCases = [
{ activity_id: null, shouldHaveId: false },
{ activity_id: '', shouldHaveId: false },
{ activity_id: '15', shouldHaveId: true },
]
for (const testCase of testCases) {
mockFetch.mockReset()
const pipedriveInputs = {
domain: 'posthog-sandbox',
apiToken: 'api-key',
person_match_value: 'e252ca85-9ea2-4d17-9d99-5fda5535995d',
activity_id: testCase.activity_id,
personField: 'id',
organization_match_value: '',
organizationField: 'id',
deal_match_value: null,
dealField: 'id',
subject: null,
type: null,
description: null,
note: null,
due_date: null,
due_time: null,
duration: null,
done: null,
internal_partner_action: 'createUpdateActivity',
debug_mode: true,
}
const fn = createHogFunction({
name: 'Plugin test',
template_id: 'segment-actions-pipedrive',
})
const invocation = createExampleSegmentInvocation(fn, pipedriveInputs)
mockFetch.mockResolvedValue({
status: 200,
json: () => Promise.resolve({ total_count: 1 }),
text: () => Promise.resolve(JSON.stringify(pipedriveResponse)),
headers: {},
})
await service.execute(invocation)
expect(mockFetch).toHaveBeenCalledTimes(2)
const requestBody = parseJSON(mockFetch.mock.calls[1][1].body)
const endpoint = mockFetch.mock.calls[1][0]
if (testCase.shouldHaveId) {
expect(endpoint).toBe(
`https://posthog-sandbox.pipedrive.com/api/v1/activities/${testCase.activity_id}?api_token=api-key`
)
} else {
expect(requestBody).not.toHaveProperty('id')
expect(endpoint).toBe('https://posthog-sandbox.pipedrive.com/api/v1/activities?api_token=api-key')
}
}
})
})
})

View File

@@ -165,7 +165,23 @@ export class SegmentDestinationExecutorService {
let body: string | undefined = undefined
if (options?.json) {
body = JSON.stringify(options.json)
let jsonData = options.json
// For the pipedrive activities endpoint, we need to omit the id field if it's null or an empty string
// https://devcommunity.pipedrive.com/t/getting-bad-request-for-all-post-and-put-at-activities/345
if (
endpoint.endsWith('.pipedrive.com/api/v1/activities') &&
jsonData &&
typeof jsonData === 'object' &&
'id' in jsonData
) {
const { id, ...rest } = jsonData as any
if (id === null || id === '') {
jsonData = rest
}
}
body = JSON.stringify(jsonData)
headers['Content-Type'] = 'application/json'
} else if (options?.body && options.body instanceof URLSearchParams) {
body = options.body.toString()