Skip to content

Commit 7c31044

Browse files
committed
refactor(salesforce): share payload object type parsing
Remove dead code in the Salesforce provider and move shared object-type extraction into a single helper so trigger matching and input shaping stay in sync. Made-with: Cursor
1 parent 732755a commit 7c31044

File tree

3 files changed

+35
-62
lines changed

3 files changed

+35
-62
lines changed

apps/sim/lib/webhooks/providers/salesforce.ts

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
WebhookProviderHandler,
1010
} from '@/lib/webhooks/providers/types'
1111
import { verifyTokenAuth } from '@/lib/webhooks/providers/utils'
12+
import { extractSalesforceObjectTypeFromPayload } from '@/lib/webhooks/salesforce-payload-utils'
1213

1314
const logger = createLogger('WebhookProvider:Salesforce')
1415

@@ -25,39 +26,6 @@ function asRecord(body: unknown): Record<string, unknown> {
2526
: {}
2627
}
2728

28-
function extractObjectTypeFromPayload(body: Record<string, unknown>): string | undefined {
29-
const direct =
30-
(typeof body.objectType === 'string' && body.objectType) ||
31-
(typeof body.sobjectType === 'string' && body.sobjectType) ||
32-
undefined
33-
if (direct) {
34-
return direct
35-
}
36-
37-
const attrs = body.attributes as Record<string, unknown> | undefined
38-
if (typeof attrs?.type === 'string') {
39-
return attrs.type
40-
}
41-
42-
const record = body.record
43-
if (record && typeof record === 'object' && !Array.isArray(record)) {
44-
const r = record as Record<string, unknown>
45-
if (typeof r.sobjectType === 'string') {
46-
return r.sobjectType
47-
}
48-
const ra = r.attributes as Record<string, unknown> | undefined
49-
if (typeof ra?.type === 'string') {
50-
return ra.type
51-
}
52-
}
53-
54-
return undefined
55-
}
56-
57-
function normalizeSObjectType(t: string): string {
58-
return t.trim().toLowerCase()
59-
}
60-
6129
function extractRecordCore(body: Record<string, unknown>): Record<string, unknown> {
6230
const nested = body.record
6331
if (nested && typeof nested === 'object' && !Array.isArray(nested)) {
@@ -176,7 +144,7 @@ export const salesforceHandler: WebhookProviderHandler = {
176144

177145
const record = extractRecordCore(body)
178146
const objectType =
179-
extractObjectTypeFromPayload(body) ||
147+
extractSalesforceObjectTypeFromPayload(body) ||
180148
(typeof record.attributes === 'object' &&
181149
record.attributes &&
182150
typeof (record.attributes as Record<string, unknown>).type === 'string'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export function extractSalesforceObjectTypeFromPayload(
2+
body: Record<string, unknown>
3+
): string | undefined {
4+
const direct =
5+
(typeof body.objectType === 'string' && body.objectType) ||
6+
(typeof body.sobjectType === 'string' && body.sobjectType) ||
7+
undefined
8+
if (direct) {
9+
return direct
10+
}
11+
12+
const attrs = body.attributes as Record<string, unknown> | undefined
13+
if (typeof attrs?.type === 'string') {
14+
return attrs.type
15+
}
16+
17+
const record = body.record
18+
if (record && typeof record === 'object' && !Array.isArray(record)) {
19+
const r = record as Record<string, unknown>
20+
if (typeof r.sobjectType === 'string') {
21+
return r.sobjectType
22+
}
23+
const ra = r.attributes as Record<string, unknown> | undefined
24+
if (typeof ra?.type === 'string') {
25+
return ra.type
26+
}
27+
}
28+
29+
return undefined
30+
}

apps/sim/triggers/salesforce/utils.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { extractSalesforceObjectTypeFromPayload } from '@/lib/webhooks/salesforce-payload-utils'
12
import type { SubBlockConfig } from '@/blocks/types'
23
import type { TriggerOutput } from '@/triggers/types'
34

@@ -20,32 +21,6 @@ function normalizeToken(s: string): string {
2021
.replace(/[\s-]+/g, '_')
2122
}
2223

23-
function payloadObjectType(body: Record<string, unknown>): string | undefined {
24-
const direct =
25-
(typeof body.objectType === 'string' && body.objectType) ||
26-
(typeof body.sobjectType === 'string' && body.sobjectType) ||
27-
undefined
28-
if (direct) {
29-
return direct
30-
}
31-
const attrs = body.attributes as Record<string, unknown> | undefined
32-
if (typeof attrs?.type === 'string') {
33-
return attrs.type
34-
}
35-
const record = body.record
36-
if (record && typeof record === 'object' && !Array.isArray(record)) {
37-
const r = record as Record<string, unknown>
38-
if (typeof r.sobjectType === 'string') {
39-
return r.sobjectType
40-
}
41-
const ra = r.attributes as Record<string, unknown> | undefined
42-
if (typeof ra?.type === 'string') {
43-
return ra.type
44-
}
45-
}
46-
return undefined
47-
}
48-
4924
const RECORD_CREATED = new Set([
5025
'record_created',
5126
'created',
@@ -115,15 +90,15 @@ export function isSalesforceEventMatch(
11590
if (!want) {
11691
return true
11792
}
118-
const got = payloadObjectType(body)
93+
const got = extractSalesforceObjectTypeFromPayload(body)
11994
if (!got) {
12095
return false
12196
}
12297
return normalizeToken(got) === normalizeToken(want)
12398
}
12499

125100
const wantType = configuredObjectType?.trim()
126-
const gotType = payloadObjectType(body)
101+
const gotType = extractSalesforceObjectTypeFromPayload(body)
127102
if (wantType) {
128103
if (!gotType) {
129104
return false

0 commit comments

Comments
 (0)