-
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
Changes from 1 commit
2b6e0d2
8667c81
224b621
bff82ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 email.", | ||||||||||||||||||||||||||||||||||
| 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, | ||||||||||||||||||||||||||||||||||
| }), {}); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+74
to
+84
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. - data = Object.entries(data).filter(([
- ,
- value,
- ]) => (value != "" && value != undefined))
- .reduce((obj, [
- key,
- value,
- ]) => Object.assign(obj, {
- [key]: value,
- }), {});
+ data = Object.fromEntries(
+ Object.entries(data).filter(([, value]) =>
+ value !== "" && value !== undefined
+ )
+ );📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||||
| data: contact, total_count: total, | ||||||||||||||||||||||||||||||||||
| } = await this.intercom.searchContact({ | ||||||||||||||||||||||||||||||||||
| data: { | ||||||||||||||||||||||||||||||||||
| query: { | ||||||||||||||||||||||||||||||||||
| operator: "AND", | ||||||||||||||||||||||||||||||||||
| value: [ | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| field: "email", | ||||||||||||||||||||||||||||||||||
| operator: "=", | ||||||||||||||||||||||||||||||||||
| value: this.email, | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| pagination: { | ||||||||||||||||||||||||||||||||||
| per_page: 5, | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| 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} note with ID ${response.id}`); | ||||||||||||||||||||||||||||||||||
luancazarine marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| return response; | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| 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", | ||
| }, | ||
| ]; |
Uh oh!
There was an error while loading. Please reload this page.