|
| 1 | +import { ConfigurationError } from "@pipedream/platform"; |
| 2 | +import { |
| 3 | + GENDER_OPTIONS, |
| 4 | + PHOTO_TYPE_OPTIONS, |
| 5 | +} from "../../common/constants.mjs"; |
| 6 | +import { |
| 7 | + cleanObject, |
| 8 | + parseObject, |
| 9 | +} from "../../common/utils.mjs"; |
1 | 10 | import helpScout from "../../help_scout.app.mjs"; |
2 | 11 |
|
3 | 12 | export default { |
4 | 13 | key: "help_scout-create-customer", |
5 | 14 | name: "Create Customer", |
6 | 15 | description: "Creates a new customer record in Help Scout. [See the documentation](https://developer.helpscout.com/mailbox-api/endpoints/customers/create/)", |
7 | | - version: "0.0.{{ts}}", |
| 16 | + version: "0.0.1", |
8 | 17 | type: "action", |
9 | 18 | props: { |
10 | 19 | helpScout, |
11 | | - customerEmail: { |
12 | | - propDefinition: [ |
13 | | - helpScout, |
14 | | - "customerEmail", |
15 | | - ], |
16 | | - }, |
17 | | - customerPhone: { |
18 | | - propDefinition: [ |
19 | | - helpScout, |
20 | | - "customerPhone", |
21 | | - ], |
22 | | - }, |
23 | | - chatHandles: { |
24 | | - propDefinition: [ |
25 | | - helpScout, |
26 | | - "chatHandles", |
27 | | - ], |
| 20 | + firstName: { |
| 21 | + type: "string", |
| 22 | + label: "First Name", |
| 23 | + description: "First name of the customer. When defined it must be between 1 and 40 characters.", |
| 24 | + optional: true, |
| 25 | + }, |
| 26 | + lastName: { |
| 27 | + type: "string", |
| 28 | + label: "Last Name", |
| 29 | + description: "Last name of the customer. When defined it must be between 1 and 40 characters.", |
| 30 | + optional: true, |
| 31 | + }, |
| 32 | + phone: { |
| 33 | + type: "string", |
| 34 | + label: "Phone", |
| 35 | + description: "The phone number that will be used when creating a new customer.", |
| 36 | + optional: true, |
| 37 | + }, |
| 38 | + photoUrl: { |
| 39 | + type: "string", |
| 40 | + label: "Photo URL", |
| 41 | + description: "URL of the customer's photo. Max length 200 characters.", |
| 42 | + optional: true, |
| 43 | + }, |
| 44 | + jobTitle: { |
| 45 | + type: "string", |
| 46 | + label: "Job Title", |
| 47 | + description: "Job title. Max length 60 characters.", |
| 48 | + optional: true, |
| 49 | + }, |
| 50 | + photoType: { |
| 51 | + type: "string", |
| 52 | + label: "Photo Type", |
| 53 | + description: "The type of photo.", |
| 54 | + options: PHOTO_TYPE_OPTIONS, |
| 55 | + optional: true, |
| 56 | + }, |
| 57 | + background: { |
| 58 | + type: "string", |
| 59 | + label: "Background", |
| 60 | + description: "This is the Notes field from the user interface. Max length 200 characters.", |
| 61 | + optional: true, |
| 62 | + }, |
| 63 | + location: { |
| 64 | + type: "string", |
| 65 | + label: "Location", |
| 66 | + description: "Location of the customer. Max length 60 characters.", |
| 67 | + optional: true, |
| 68 | + }, |
| 69 | + organization: { |
| 70 | + type: "string", |
| 71 | + label: "Organization", |
| 72 | + description: "Organization. Max length 60 characters.", |
| 73 | + optional: true, |
| 74 | + }, |
| 75 | + gender: { |
| 76 | + type: "string", |
| 77 | + label: "Gender", |
| 78 | + description: "Gender of this customer.", |
| 79 | + options: GENDER_OPTIONS, |
| 80 | + optional: true, |
| 81 | + }, |
| 82 | + age: { |
| 83 | + type: "string", |
| 84 | + label: "Age", |
| 85 | + description: "Customer's age.", |
| 86 | + optional: true, |
| 87 | + }, |
| 88 | + emails: { |
| 89 | + type: "string[]", |
| 90 | + label: "Emails", |
| 91 | + description: "List of email entries. **Format: {\"type\":\"home\",\"value\":\"[email protected]\"}**. see [Create Email](https://developer.helpscout.com/mailbox-api/endpoints/customers/emails/create) for the object documentation.", |
| 92 | + optional: true, |
| 93 | + }, |
| 94 | + phones: { |
| 95 | + type: "string[]", |
| 96 | + label: "Phones", |
| 97 | + description: "List of phone entries. **Format: {\"type\":\"work\",\"value\":\"222-333-4444\"}**. see [Create Phone](https://developer.helpscout.com/mailbox-api/endpoints/customers/phones/create) for the object documentation.", |
| 98 | + optional: true, |
| 99 | + }, |
| 100 | + chats: { |
| 101 | + type: "string[]", |
| 102 | + label: "Chats", |
| 103 | + description: "List of chat entries. **Format: {\"type\":\"aim\",\"value\":\"jsprout\"}**. see [Create Chat Handle](https://developer.helpscout.com/mailbox-api/endpoints/customers/chat_handles/create) for the object documentation.", |
| 104 | + optional: true, |
28 | 105 | }, |
29 | 106 | socialProfiles: { |
30 | | - propDefinition: [ |
31 | | - helpScout, |
32 | | - "socialProfiles", |
33 | | - ], |
34 | | - }, |
35 | | - customerAddress: { |
36 | | - propDefinition: [ |
37 | | - helpScout, |
38 | | - "customerAddress", |
39 | | - ], |
40 | | - }, |
41 | | - customerDetails: { |
42 | | - propDefinition: [ |
43 | | - helpScout, |
44 | | - "customerDetails", |
45 | | - ], |
| 107 | + type: "string[]", |
| 108 | + label: "Social Profiles", |
| 109 | + description: "List of social profile entries. **Format: {\"type\":\"googleplus\",\"value\":\"https://api.helpscout.net/+HelpscoutNet\"}**. see [Create Social Profile](https://developer.helpscout.com/mailbox-api/endpoints/customers/social_profiles/create) for the object documentation.", |
| 110 | + optional: true, |
| 111 | + }, |
| 112 | + websites: { |
| 113 | + type: "string[]", |
| 114 | + label: "Websites", |
| 115 | + description: "List of websites entries. **Format: {\"value\":\"https://api.helpscout.net/\"}**. see [Create Website](https://developer.helpscout.com/mailbox-api/endpoints/customers/websites/create) for the object documentation.", |
| 116 | + optional: true, |
| 117 | + }, |
| 118 | + addressCity: { |
| 119 | + type: "string", |
| 120 | + label: "Address City", |
| 121 | + description: "The city of the customer.", |
| 122 | + optional: true, |
| 123 | + }, |
| 124 | + addressState: { |
| 125 | + type: "string", |
| 126 | + label: "Address State", |
| 127 | + description: "The state of the customer.", |
| 128 | + optional: true, |
| 129 | + }, |
| 130 | + addressPostalCode: { |
| 131 | + type: "string", |
| 132 | + label: "Address Postal Code", |
| 133 | + description: "The postal code of the customer.", |
| 134 | + optional: true, |
| 135 | + }, |
| 136 | + addressCountry: { |
| 137 | + type: "string", |
| 138 | + label: "Address Country", |
| 139 | + description: "The [ISO 3166 Alpha-2 code](https://www.iban.com/country-codes) country of the customer.", |
| 140 | + optional: true, |
| 141 | + }, |
| 142 | + addressLines: { |
| 143 | + type: "string[]", |
| 144 | + label: "Address Lines", |
| 145 | + description: "A list of address lines.", |
| 146 | + optional: true, |
| 147 | + }, |
| 148 | + properties: { |
| 149 | + type: "string[]", |
| 150 | + label: "Properties", |
| 151 | + description: "List of social profile entries. **Format: {\"type\":\"googleplus\",\"value\":\"https://api.helpscout.net/+HelpscoutNet\"}**. see [Create Social Profile](https://developer.helpscout.com/mailbox-api/endpoints/customers/social_profiles/create) for the object documentation.", |
| 152 | + optional: true, |
46 | 153 | }, |
47 | 154 | }, |
48 | 155 | async run({ $ }) { |
49 | | - const response = await this.helpScout.createCustomer({ |
50 | | - customerEmail: this.customerEmail, |
51 | | - customerPhone: this.customerPhone, |
52 | | - chatHandles: this.chatHandles, |
53 | | - socialProfiles: this.socialProfiles, |
54 | | - customerAddress: this.customerAddress, |
55 | | - ...this.customerDetails, |
56 | | - }); |
| 156 | + try { |
| 157 | + const address = cleanObject({ |
| 158 | + city: this.addressCity, |
| 159 | + state: this.addressState, |
| 160 | + postalCode: this.addressPostalCode, |
| 161 | + country: this.addressCountry, |
| 162 | + lines: parseObject(this.addressLines), |
| 163 | + properties: parseObject(this.properties), |
| 164 | + }); |
| 165 | + |
| 166 | + const data = cleanObject({ |
| 167 | + firstName: this.firstName, |
| 168 | + lastName: this.lastName, |
| 169 | + phone: this.phone, |
| 170 | + photoUrl: this.photoUrl, |
| 171 | + jobTitle: this.jobTitle, |
| 172 | + photoType: this.photoType, |
| 173 | + background: this.background, |
| 174 | + location: this.location, |
| 175 | + organization: this.organization, |
| 176 | + gender: this.gender, |
| 177 | + age: this.age, |
| 178 | + emails: parseObject(this.emails), |
| 179 | + phones: parseObject(this.phones), |
| 180 | + chats: parseObject(this.chats), |
| 181 | + socialProfiles: parseObject(this.socialProfiles), |
| 182 | + websites: parseObject(this.websites), |
| 183 | + }); |
| 184 | + |
| 185 | + if (Object.keys(address).length) data.address = address; |
| 186 | + |
| 187 | + if (!Object.keys(data).length) { |
| 188 | + throw new ConfigurationError("At least one field or customer entry must be defined."); |
| 189 | + } |
| 190 | + |
| 191 | + const response = await this.helpScout.createCustomer({ |
| 192 | + $, |
| 193 | + data, |
| 194 | + }); |
| 195 | + |
| 196 | + $.export("$summary", "Successfully created the new customer."); |
| 197 | + return response; |
57 | 198 |
|
58 | | - $.export("$summary", `Successfully created customer with email: ${this.customerEmail}`); |
59 | | - return response; |
| 199 | + } catch ({ response }) { |
| 200 | + throw new ConfigurationError(response.data.message); |
| 201 | + } |
60 | 202 | }, |
61 | 203 | }; |
0 commit comments