-
Notifications
You must be signed in to change notification settings - Fork 5.6k
[ACTION] Intercom Upsert Contact #14268 #14479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2b6e0d2
[ACTION] Intercom Upsert Contact #14268
luancazarine 8667c81
Update components/intercom/actions/upsert-contact/upsert-contact.mjs
luancazarine 224b621
Update components/intercom/actions/upsert-contact/upsert-contact.mjs
luancazarine bff82ae
Update components/intercom/actions/upsert-contact/upsert-contact.mjs
luancazarine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
components/intercom/actions/upsert-contact/upsert-contact.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { ROLE_OPTIONS } from "../../common/constants.mjs"; | ||
| import intercom from "../../intercom.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "intercom-upsert-contact", | ||
| name: "Upsert Contact", | ||
| description: "Create a new contact. If there is already a contact with the email provided, the existing contact will be updated. [See the docs here](https://developers.intercom.com/docs/references/rest-api/api.intercom.io/contacts/createcontact)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| intercom, | ||
| email: { | ||
| type: "string", | ||
| label: "Email", | ||
| description: "The contact's email.", | ||
| }, | ||
| role: { | ||
| type: "string", | ||
| label: "Role", | ||
| description: "The role of the contact.", | ||
| options: ROLE_OPTIONS, | ||
| optional: true, | ||
| }, | ||
| externalId: { | ||
| type: "string", | ||
| label: "External Id", | ||
| description: "A unique identifier for the contact which is given to Intercom.", | ||
| optional: true, | ||
| }, | ||
| phone: { | ||
| type: "string", | ||
| label: "Phone", | ||
| description: "The contact's phone number.", | ||
| optional: true, | ||
| }, | ||
| name: { | ||
| type: "string", | ||
| label: "Name", | ||
| description: "The contact's name.", | ||
| optional: true, | ||
| }, | ||
| avatar: { | ||
| type: "string", | ||
| label: "Avatar", | ||
| description: "An image URL containing the avatar of a contact.", | ||
| optional: true, | ||
| }, | ||
| unsubscribedFromEmails: { | ||
| type: "boolean", | ||
| label: "Unsubscribed From Emails", | ||
| description: "Whether the contact is unsubscribed from emails.", | ||
| optional: true, | ||
| }, | ||
| customAttributes: { | ||
| type: "object", | ||
| label: "Custom Attributes", | ||
| description: "The custom attributes which are set for the contact.", | ||
| optional: true, | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| let response = {}; | ||
| let requestType = "created"; | ||
| let data = { | ||
| email: this.email, | ||
| role: this.role, | ||
| externalId: this.externalId, | ||
| phone: this.phone, | ||
| name: this.name, | ||
| avatar: this.avatar, | ||
| unsubscribedFromEmails: this.unsubscribedFromEmails, | ||
| customAttributes: this.customAttributes, | ||
| }; | ||
|
|
||
| data = Object.entries(data).filter(([ | ||
| , | ||
| value, | ||
| ]) => (value != "" && value != undefined)) | ||
| .reduce((obj, [ | ||
| key, | ||
| value, | ||
| ]) => Object.assign(obj, { | ||
| [key]: value, | ||
| }), {}); | ||
|
|
||
| const { | ||
| data: contact, total_count: total, | ||
| } = await this.intercom.searchContact({ | ||
| data: { | ||
| query: { | ||
| operator: "AND", | ||
| value: [ | ||
| { | ||
| field: "email", | ||
| operator: "=", | ||
| value: this.email, | ||
| }, | ||
| ], | ||
| }, | ||
| pagination: { | ||
| per_page: 1, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (total) { | ||
| const { | ||
| id: contactId, | ||
| // eslint-disable-next-line no-unused-vars | ||
| owner_id, | ||
| ...contactInfos | ||
| } = contact[0]; | ||
| response = await this.intercom.updateContact({ | ||
| $, | ||
| contactId, | ||
| data: { | ||
| ...contactInfos, | ||
| ...data, | ||
| }, | ||
| }); | ||
| requestType = "updated"; | ||
| } else { | ||
| response = await this.intercom.createContact({ | ||
| $, | ||
| data, | ||
| }); | ||
| } | ||
| $.export("$summary", `Successfully ${requestType} contact with ID ${response.id}`); | ||
| return response; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export const ROLE_OPTIONS = [ | ||
| { | ||
| label: "User", | ||
| value: "user", | ||
| }, | ||
| { | ||
| label: "Lead", | ||
| value: "lead", | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Simplify object filtering
The current object filtering logic could be simplified using Object.fromEntries for better readability.
📝 Committable suggestion