|  | 
|  | 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"; | 
|  | 10 | +import helpScout from "../../help_scout.app.mjs"; | 
|  | 11 | + | 
|  | 12 | +export default { | 
|  | 13 | +  key: "help_scout-create-customer", | 
|  | 14 | +  name: "Create Customer", | 
|  | 15 | +  description: "Creates a new customer record in Help Scout. [See the documentation](https://developer.helpscout.com/mailbox-api/endpoints/customers/create/)", | 
|  | 16 | +  version: "0.0.1", | 
|  | 17 | +  type: "action", | 
|  | 18 | +  props: { | 
|  | 19 | +    helpScout, | 
|  | 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, | 
|  | 105 | +    }, | 
|  | 106 | +    socialProfiles: { | 
|  | 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, | 
|  | 153 | +    }, | 
|  | 154 | +  }, | 
|  | 155 | +  async run({ $ }) { | 
|  | 156 | +    const address = cleanObject({ | 
|  | 157 | +      city: this.addressCity, | 
|  | 158 | +      state: this.addressState, | 
|  | 159 | +      postalCode: this.addressPostalCode, | 
|  | 160 | +      country: this.addressCountry, | 
|  | 161 | +      lines: parseObject(this.addressLines), | 
|  | 162 | +      properties: parseObject(this.properties), | 
|  | 163 | +    }); | 
|  | 164 | + | 
|  | 165 | +    let data = {}; | 
|  | 166 | + | 
|  | 167 | +    data = cleanObject({ | 
|  | 168 | +      firstName: this.firstName, | 
|  | 169 | +      lastName: this.lastName, | 
|  | 170 | +      phone: this.phone, | 
|  | 171 | +      photoUrl: this.photoUrl, | 
|  | 172 | +      jobTitle: this.jobTitle, | 
|  | 173 | +      photoType: this.photoType, | 
|  | 174 | +      background: this.background, | 
|  | 175 | +      location: this.location, | 
|  | 176 | +      organization: this.organization, | 
|  | 177 | +      gender: this.gender, | 
|  | 178 | +      age: this.age, | 
|  | 179 | +      emails: parseObject(this.emails), | 
|  | 180 | +      phones: parseObject(this.phones), | 
|  | 181 | +      chats: parseObject(this.chats), | 
|  | 182 | +      socialProfiles: parseObject(this.socialProfiles), | 
|  | 183 | +      websites: parseObject(this.websites), | 
|  | 184 | +    }); | 
|  | 185 | + | 
|  | 186 | +    if (Object.keys(address).length) data.address = address; | 
|  | 187 | + | 
|  | 188 | +    if (!Object.keys(data).length) { | 
|  | 189 | +      throw new ConfigurationError("At least one field or customer entry must be defined."); | 
|  | 190 | +    } | 
|  | 191 | + | 
|  | 192 | +    try { | 
|  | 193 | +      const response = await this.helpScout.createCustomer({ | 
|  | 194 | +        $, | 
|  | 195 | +        data, | 
|  | 196 | +      }); | 
|  | 197 | + | 
|  | 198 | +      $.export("$summary", "Successfully created the new customer."); | 
|  | 199 | +      return response; | 
|  | 200 | +    } catch ({ message }) { | 
|  | 201 | +      const error = JSON.parse(message)._embedded.errors[0]; | 
|  | 202 | +      throw new ConfigurationError(`Path: ${error.path} - ${error.message}`); | 
|  | 203 | +    } | 
|  | 204 | +  }, | 
|  | 205 | +}; | 
0 commit comments