Skip to content

Commit

Permalink
[Hubspot Cloud Actions]- Implemented Batching for Upsert Contact (#1514)
Browse files Browse the repository at this point in the history
* Added batching support for Upsert Contact

* added unit test cases for batch upsertContact

* email case insensitivity

* added log and updated Method

* removed logs and unwanted code

* moved the code to seperate methods

* added a test case for handled error

* Added enable_batching to UpsertContact

* worked on debugging batched requrest error

* remove unnecessary

* skipresponseCloning

* updated snapshots

---------

Co-authored-by: Sayan Das <[email protected]>
Co-authored-by: Gaurav Kochar <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2023
1 parent 991c501 commit a1a43b6
Show file tree
Hide file tree
Showing 6 changed files with 1,097 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const destination: DestinationDefinition<Settings> = {
},
extendRequest({ auth }) {
return {
skipResponseCloning: true,
headers: {
authorization: `Bearer ${auth?.accessToken}`
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { createTestEvent } from '@segment/actions-core'
import { ContactBatchResponse } from '../../../upsertContact'

export type BatchContactListItem = {
id?: string
email: string
firstname: string
lastname: string
lifecyclestage?: string | undefined
}

export const createBatchTestEvents = (batchContactList: BatchContactListItem[]) =>
batchContactList.map((contact) =>
createTestEvent({
type: 'identify',
traits: {
email: contact.email,
firstname: contact.firstname,
lastname: contact.lastname,
address: {
city: 'San Francisco',
country: 'USA',
postal_code: '600001',
state: 'California',
street: 'Vancover st'
},
graduation_date: 1664533942262,
lifecyclestage: contact?.lifecyclestage ?? 'subscriber',
company: 'Some Company',
phone: '+13134561129',
website: 'somecompany.com'
}
})
)

export const generateBatchReadResponse = (batchContactList: BatchContactListItem[]) => {
const batchReadResponse: ContactBatchResponse = {
status: 'COMPLETE',
results: [],
numErrors: 0,
errors: []
}

const notFoundEmails: string[] = []

for (const contact of batchContactList) {
// Set success response
if (contact.id) {
batchReadResponse.results.push({
id: contact.id,
properties: {
createdate: '2023-07-06T12:47:47.626Z',
email: contact.email,
hs_object_id: contact.id,
lastmodifieddate: '2023-07-06T12:48:02.784Z'
}
})
} else {
// Push to not found list if id is not present
notFoundEmails.push(contact.email)
}
}

// Set error response
if (notFoundEmails.length > 0) {
batchReadResponse.numErrors = 1
batchReadResponse.errors = [
{
status: 'error',
category: 'OBJECT_NOT_FOUND',
message: 'Could not get some CONTACT objects, they may be deleted or not exist. Check that ids are valid.',
context: {
ids: notFoundEmails
}
}
]
}

return batchReadResponse
}

export const generateBatchCreateResponse = (batchContactList: BatchContactListItem[]) => {
const batchCreateResponse: ContactBatchResponse = {
status: 'COMPLETE',
results: []
}

batchContactList.forEach((contact, index) => {
// Set success response
// if (!contact.id) {
const contactResponse: any = {
id: contact.id ? contact.id : (101 + index).toString(),
properties: {
email: contact.email,
firstname: contact.firstname,
lastname: contact.lastname,
createdate: '2023-07-06T12:47:47.626Z',
lastmodifieddate: '2023-07-06T12:48:02.784Z'
}
}
if (Object.prototype.hasOwnProperty.call(contact, 'lifecyclestage')) {
contactResponse.properties.lifecyclestage = contact.lifecyclestage
}

batchCreateResponse.results.push(contactResponse)
// }
})

return batchCreateResponse
}
Loading

0 comments on commit a1a43b6

Please sign in to comment.