Skip to content

Commit

Permalink
[STRATCONN-3686] | HubSpot's "Upsert Custom Object Record" mapping re…
Browse files Browse the repository at this point in the history
…quires arrays be flattened into strings (#1984)

* Fixed passing array parameter while updating custom object

* Added a unit test case

---------

Co-authored-by: Gaurav Kochar <gaurav.kochar@segment.com>
  • Loading branch information
Innovative-GauravKochar and Gaurav Kochar committed Apr 23, 2024
1 parent 61aeb8b commit 5c22fd2
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,118 @@ describe('HubSpot.upsertCustomObjectRecord', () => {
})
})

it('Should handle flattening of array while updating a custom object record', async () => {
// Mock: Search Custom Object Record with custom Search Fields
nock(HUBSPOT_BASE_URL)
.post(`/crm/v3/objects/${objectType}/search`)
.reply(200, {
total: 1,
results: [
{
id: hubspotGeneratedCustomObjectRecordId,
properties: {
createdate: '2023-06-01T19:56:33.914Z',
hs_lastmodifieddate: '2023-06-01T13:19:08.067Z',
hs_object_id: hubspotGeneratedCustomObjectRecordId,
test: 'new_test_value',
test_custom_object_type: 'new_test_custom_object_type',
couponCode: 'TEST1234',
discountPercentage: '10%',
customPropertyOne: [1, 2, 3, 4, 5],
customPropertyTwo: {
a: 1,
b: 2,
c: 3
},
customPropertyThree: [1, 'two', true, { four: 4 }]
},
createdAt: '2023-06-01T19:56:33.914Z',
updatedAt: '2023-06-01T13:19:08.067Z',
archived: false
}
]
})

nock(HUBSPOT_BASE_URL)
.patch(`/crm/v3/objects/${objectType}/${hubspotGeneratedCustomObjectRecordId}`)
.reply(200, {
id: hubspotGeneratedCustomObjectRecordId,
properties: {
createdate: '2023-06-01T19:56:33.914Z',
hs_lastmodifieddate: '2023-06-01T13:19:08.067Z',
hs_object_id: hubspotGeneratedCustomObjectRecordId,
test: 'new_test_value',
test_custom_object_type: 'new_test_custom_object_type',
couponCode: 'TEST1234',
discountPercentage: '10%',
customPropertyOne: [1, 2, 3, 4, 5],
customPropertyTwo: {
a: 1,
b: 2,
c: 3
},
customPropertyThree: [1, 'two', true, { four: 4 }]
},
createdAt: '2022-09-25T19:56:33.914Z',
updatedAt: '2022-10-14T13:19:08.067Z',
archived: false
})

const event = createTestEvent({
type: 'track',
event: 'Apply Discount',
properties: {
couponCode: 'TEST1234',
discountPercentage: '10%',
customPropertyOne: [1, 3, 4, 5],
customPropertyTwo: {
a: 1,
b: 2,
c: 3
},
customPropertyThree: [1, 'two', 3, true, { four: 4 }]
}
})

const responses = await testDestination.testAction('upsertCustomObjectRecord', {
event,
mapping: {
objectType: 'p11223344_discount',
createNewCustomRecord: true,
customObjectSearchFields: {
test_custom_object_type: 'new_test_custom_object_type'
},
properties: {
coupon_code: {
'@path': '$.properties.couponCode'
},
discount_percent: {
'@path': '$.properties.couponCode'
},
custom_property_1: {
'@path': '$.properties.customPropertyOne'
},
custom_property_2: {
'@path': '$.properties.customPropertyTwo'
},
custom_property_3: {
'@path': '$.properties.customPropertyThree'
}
}
}
})
expect(responses).toHaveLength(2)
expect(responses[0].status).toBe(200)
expect(responses[1].status).toBe(200)
expect(responses[1].options.json).toMatchObject({
properties: {
custom_property_1: '1;3;4;5',
custom_property_2: '{"a":1,"b":2,"c":3}',
custom_property_3: '1;two;3;true;{"four":4}'
}
})
})

it('should create a custom object record and associate with another record on the basis of provided search field to associate', async () => {
// Mock: Search Custom Object Record with custom Search Fields
nock(HUBSPOT_BASE_URL).post(`/crm/v3/objects/${objectType}/search`).reply(200, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ const action: ActionDefinition<Settings, Payload> = {
let upsertCustomRecordResponse: ModifiedResponse<UpsertRecordResponse>
// Check if any custom object record were found based Custom Search Fields
// If the search was skipped, searchCustomResponse would have a falsy value (null)
const properties = { ...flattenObject(payload.properties) }
if (!searchCustomResponse?.data || searchCustomResponse?.data?.total === 0) {
// No existing custom object record found with search criteria, attempt to create a new custom object record

Expand All @@ -161,18 +162,14 @@ const action: ActionDefinition<Settings, Payload> = {
if (!createNewCustomRecord) {
return 'There was no record found to update. If you want to create a new custom object record in such cases, enable the Create Custom Object Record if Not Found flag'
}
const properties = { ...flattenObject(payload.properties) }
upsertCustomRecordResponse = await hubspotApiClient.create(properties, association ? [association] : [])
} else {
// Throw error if more than one custom object record were found with search criteria
if (searchCustomResponse?.data?.total > 1) {
throw MultipleCustomRecordsInSearchResultThrowableError
}
// An existing Custom object record was identified, attempt to update the same record
upsertCustomRecordResponse = await hubspotApiClient.update(
searchCustomResponse.data.results[0].id,
payload.properties
)
upsertCustomRecordResponse = await hubspotApiClient.update(searchCustomResponse.data.results[0].id, properties)
// If we have custom object record id to associate then associate it else don't associate
if (toCustomObjectId && parsedAssociationType) {
await hubspotApiClient.associate(searchCustomResponse.data.results[0].id, toCustomObjectId, [
Expand Down

0 comments on commit 5c22fd2

Please sign in to comment.