|
| 1 | +import { ROLE_OPTIONS } from "../../common/constants.mjs"; |
| 2 | +import intercom from "../../intercom.app.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "intercom-upsert-contact", |
| 6 | + name: "Upsert Contact", |
| 7 | + 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)", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + intercom, |
| 12 | + email: { |
| 13 | + type: "string", |
| 14 | + label: "Email", |
| 15 | + description: "The contact's email.", |
| 16 | + }, |
| 17 | + role: { |
| 18 | + type: "string", |
| 19 | + label: "Role", |
| 20 | + description: "The role of the contact.", |
| 21 | + options: ROLE_OPTIONS, |
| 22 | + optional: true, |
| 23 | + }, |
| 24 | + externalId: { |
| 25 | + type: "string", |
| 26 | + label: "External Id", |
| 27 | + description: "A unique identifier for the contact which is given to Intercom.", |
| 28 | + optional: true, |
| 29 | + }, |
| 30 | + phone: { |
| 31 | + type: "string", |
| 32 | + label: "Phone", |
| 33 | + description: "The contact's email.", |
| 34 | + optional: true, |
| 35 | + }, |
| 36 | + name: { |
| 37 | + type: "string", |
| 38 | + label: "Name", |
| 39 | + description: "The contact's name.", |
| 40 | + optional: true, |
| 41 | + }, |
| 42 | + avatar: { |
| 43 | + type: "string", |
| 44 | + label: "Avatar", |
| 45 | + description: "An image URL containing the avatar of a contact.", |
| 46 | + optional: true, |
| 47 | + }, |
| 48 | + unsubscribedFromEmails: { |
| 49 | + type: "boolean", |
| 50 | + label: "Unsubscribed From Emails", |
| 51 | + description: "Whether the contact is unsubscribed from emails.", |
| 52 | + optional: true, |
| 53 | + }, |
| 54 | + customAttributes: { |
| 55 | + type: "object", |
| 56 | + label: "Custom Attributes", |
| 57 | + description: "The custom attributes which are set for the contact.", |
| 58 | + optional: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + async run({ $ }) { |
| 62 | + let response = {}; |
| 63 | + let requestType = "created"; |
| 64 | + let data = { |
| 65 | + email: this.email, |
| 66 | + role: this.role, |
| 67 | + externalId: this.externalId, |
| 68 | + phone: this.phone, |
| 69 | + name: this.name, |
| 70 | + avatar: this.avatar, |
| 71 | + unsubscribedFromEmails: this.unsubscribedFromEmails, |
| 72 | + customAttributes: this.customAttributes, |
| 73 | + }; |
| 74 | + |
| 75 | + data = Object.entries(data).filter(([ |
| 76 | + , |
| 77 | + value, |
| 78 | + ]) => (value != "" && value != undefined)) |
| 79 | + .reduce((obj, [ |
| 80 | + key, |
| 81 | + value, |
| 82 | + ]) => Object.assign(obj, { |
| 83 | + [key]: value, |
| 84 | + }), {}); |
| 85 | + |
| 86 | + const { |
| 87 | + data: contact, total_count: total, |
| 88 | + } = await this.intercom.searchContact({ |
| 89 | + data: { |
| 90 | + query: { |
| 91 | + operator: "AND", |
| 92 | + value: [ |
| 93 | + { |
| 94 | + field: "email", |
| 95 | + operator: "=", |
| 96 | + value: this.email, |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + pagination: { |
| 101 | + per_page: 5, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + if (total) { |
| 107 | + const { |
| 108 | + id: contactId, |
| 109 | + // eslint-disable-next-line no-unused-vars |
| 110 | + owner_id, |
| 111 | + ...contactInfos |
| 112 | + } = contact[0]; |
| 113 | + response = await this.intercom.updateContact({ |
| 114 | + $, |
| 115 | + contactId, |
| 116 | + data: { |
| 117 | + ...contactInfos, |
| 118 | + ...data, |
| 119 | + }, |
| 120 | + }); |
| 121 | + requestType = "updated"; |
| 122 | + } else { |
| 123 | + response = await this.intercom.createContact({ |
| 124 | + $, |
| 125 | + data, |
| 126 | + }); |
| 127 | + } |
| 128 | + $.export("$summary", `Successfully ${requestType} note with ID ${response.id}`); |
| 129 | + return response; |
| 130 | + }, |
| 131 | +}; |
0 commit comments