|
| 1 | +import { ConfigurationError } from "@pipedream/platform"; |
| 2 | +import allocadence from "../../allocadence.app.mjs"; |
| 3 | +import { POSTAGE_ACCOUNT_OPTIONS } from "../../common/constants.mjs"; |
| 4 | +import { parseObject } from "../../common/utils.mjs"; |
| 5 | + |
| 6 | +export default { |
| 7 | + key: "allocadence-create-customer-order", |
| 8 | + name: "Create Customer Order", |
| 9 | + description: "Creates a new customer order. [See the documentation](https://docs.allocadence.com/#tag/customer_order/paths/~1customer-orders/post)", |
| 10 | + version: "0.0.1", |
| 11 | + type: "action", |
| 12 | + props: { |
| 13 | + allocadence, |
| 14 | + orderNumber: { |
| 15 | + type: "string", |
| 16 | + label: "Order Number", |
| 17 | + description: "Order number for the customer order.", |
| 18 | + optional: true, |
| 19 | + }, |
| 20 | + clientId: { |
| 21 | + type: "integer", |
| 22 | + label: "Client Id", |
| 23 | + description: "Id of the client that the customer order is for.", |
| 24 | + optional: true, |
| 25 | + }, |
| 26 | + clientName: { |
| 27 | + type: "string", |
| 28 | + label: "Client Name", |
| 29 | + description: "Name of the client that the customer order is for. **Ignored if clientId is provided and is nonzero.**", |
| 30 | + optional: true, |
| 31 | + }, |
| 32 | + customerId: { |
| 33 | + type: "integer", |
| 34 | + label: "Customer Id", |
| 35 | + description: "Id of the customer. If none is provided, will attempt to find an existing customer based on the other customer fields and shipping address. Other fields not used if id is provided.", |
| 36 | + optional: true, |
| 37 | + }, |
| 38 | + title: { |
| 39 | + type: "string", |
| 40 | + label: "Customer Title", |
| 41 | + description: "The title of the customer.", |
| 42 | + optional: true, |
| 43 | + }, |
| 44 | + name: { |
| 45 | + type: "string", |
| 46 | + label: "Customer Name", |
| 47 | + description: "A combination of name and surname is required if the customer is new.", |
| 48 | + optional: true, |
| 49 | + }, |
| 50 | + surname: { |
| 51 | + type: "string", |
| 52 | + label: "Customer Surname", |
| 53 | + description: "A combination of name and surname is required if the customer is new.", |
| 54 | + optional: true, |
| 55 | + }, |
| 56 | + email: { |
| 57 | + type: "string", |
| 58 | + label: "Customer Email", |
| 59 | + description: "The email of the customer.", |
| 60 | + optional: true, |
| 61 | + }, |
| 62 | + customerCompany: { |
| 63 | + type: "string", |
| 64 | + label: "Customer Company", |
| 65 | + description: "The company of the customer.", |
| 66 | + optional: true, |
| 67 | + }, |
| 68 | + accountNumber: { |
| 69 | + type: "string", |
| 70 | + label: "Customer Account Number", |
| 71 | + description: "Will only match a customer on this field if provided.", |
| 72 | + optional: true, |
| 73 | + }, |
| 74 | + shippingAddressId: { |
| 75 | + type: "integer", |
| 76 | + label: "Shipping Address Id", |
| 77 | + description: "Id of the shipping address. **Other Shipping fields will not be used if provided.**", |
| 78 | + optional: true, |
| 79 | + }, |
| 80 | + shippingAddressCode: { |
| 81 | + type: "string", |
| 82 | + label: "Shipping Address Code", |
| 83 | + description: "Used to find an existing address. Other fields not used if an address is successfully found.", |
| 84 | + optional: true, |
| 85 | + }, |
| 86 | + shippingAddressCompany: { |
| 87 | + type: "string", |
| 88 | + label: "Shipping Address Company", |
| 89 | + description: "The company of the shipping address.", |
| 90 | + optional: true, |
| 91 | + }, |
| 92 | + shippingAddressName: { |
| 93 | + type: "string", |
| 94 | + label: "Shipping Address Name", |
| 95 | + description: "The name of the shipping address.", |
| 96 | + optional: true, |
| 97 | + }, |
| 98 | + shippingAddressline1: { |
| 99 | + type: "string", |
| 100 | + label: "Shipping Address Line 1", |
| 101 | + description: "The shipping address line 1.", |
| 102 | + optional: true, |
| 103 | + }, |
| 104 | + shippingAddressline2: { |
| 105 | + type: "string", |
| 106 | + label: "Shipping Address Line 2", |
| 107 | + description: "The shipping address line 2.", |
| 108 | + optional: true, |
| 109 | + }, |
| 110 | + shippingAddressline3: { |
| 111 | + type: "string", |
| 112 | + label: "Shipping Address Line 3", |
| 113 | + description: "The shipping address line 3.", |
| 114 | + optional: true, |
| 115 | + }, |
| 116 | + shippingAddressCity: { |
| 117 | + type: "string", |
| 118 | + label: "Shipping Address City", |
| 119 | + description: "The city of the shipping address.", |
| 120 | + optional: true, |
| 121 | + }, |
| 122 | + shippingAddressState: { |
| 123 | + type: "string", |
| 124 | + label: "Shipping Address State", |
| 125 | + description: "The state of the shipping address.", |
| 126 | + optional: true, |
| 127 | + }, |
| 128 | + shippingAddressZip: { |
| 129 | + type: "string", |
| 130 | + label: "Shipping Address Zip", |
| 131 | + description: "The zip of the shipping address.", |
| 132 | + optional: true, |
| 133 | + }, |
| 134 | + shippingAddressCountryCode: { |
| 135 | + type: "string", |
| 136 | + label: "Shipping Address Country Code", |
| 137 | + description: "The country code of the shipping address. [See the ISO 3166 codes](https://www.iban.com/country-codes).", |
| 138 | + optional: true, |
| 139 | + }, |
| 140 | + shippingAddressPhone: { |
| 141 | + type: "string", |
| 142 | + label: "Shipping Address Phone", |
| 143 | + description: "The phone of the shipping address.", |
| 144 | + optional: true, |
| 145 | + }, |
| 146 | + sameAsShipping: { |
| 147 | + type: "boolean", |
| 148 | + label: "Same As Shipping", |
| 149 | + description: "True if the billing address is the same as the shipping address.", |
| 150 | + reloadProps: true, |
| 151 | + optional: true, |
| 152 | + }, |
| 153 | + shipFromWarehouseId: { |
| 154 | + type: "string", |
| 155 | + label: "Ship From Warehouse Id", |
| 156 | + description: "Id of the warehouse the ordered items will be allocated from. If no warehouse parameters are given, then the user's current warehouse will be used.", |
| 157 | + optional: true, |
| 158 | + }, |
| 159 | + shipFromWarehouseName: { |
| 160 | + type: "string", |
| 161 | + label: "Ship From Warehouse Name", |
| 162 | + description: "Name of the warehouse the ordered items will be allocated from. Ignored if warehouseId is provided.", |
| 163 | + optional: true, |
| 164 | + }, |
| 165 | + shipVia: { |
| 166 | + type: "string", |
| 167 | + label: "Ship Via", |
| 168 | + description: "Code of the carrier or service to use for shipping.", |
| 169 | + optional: true, |
| 170 | + }, |
| 171 | + postageAccount: { |
| 172 | + type: "string", |
| 173 | + label: "Postage Account", |
| 174 | + description: "Who to bill for shipping.", |
| 175 | + options: POSTAGE_ACCOUNT_OPTIONS, |
| 176 | + optional: true, |
| 177 | + }, |
| 178 | + items: { |
| 179 | + type: "string[]", |
| 180 | + label: "Items", |
| 181 | + description: "An array of objects of ordered items. **Example: {\"itemId\": \"123\", \"sku\": \"SKU123\", \"quantity\": 1}** [See the documentation](https://docs.allocadence.com/#tag/customer_order/paths/~1customer-orders/post) fro further information.", |
| 182 | + optional: true, |
| 183 | + }, |
| 184 | + }, |
| 185 | + async additionalProps() { |
| 186 | + const props = {}; |
| 187 | + |
| 188 | + if (!this.sameAsShipping) { |
| 189 | + props.billingAddressId = { |
| 190 | + type: "integer", |
| 191 | + label: "Billing Address Id", |
| 192 | + description: "Id of the billing address. **Other Billing fields will not be used if provided.**", |
| 193 | + optional: true, |
| 194 | + }; |
| 195 | + props.billingAddressCode = { |
| 196 | + type: "string", |
| 197 | + label: "Billing Address Code", |
| 198 | + description: "Used to find an existing address. Other fields not used if an address is successfully found.", |
| 199 | + optional: true, |
| 200 | + }; |
| 201 | + props.billingAddressCompany = { |
| 202 | + type: "string", |
| 203 | + label: "Billing Address Company", |
| 204 | + description: "The company of the billing address.", |
| 205 | + optional: true, |
| 206 | + }; |
| 207 | + props.billingAddressName = { |
| 208 | + type: "string", |
| 209 | + label: "Billing Address Name", |
| 210 | + description: "The name of the billing address.", |
| 211 | + optional: true, |
| 212 | + }; |
| 213 | + props.billingAddressline1 = { |
| 214 | + type: "string", |
| 215 | + label: "Billing Address Line 1", |
| 216 | + description: "The billing address line 1.", |
| 217 | + optional: true, |
| 218 | + }; |
| 219 | + props.billingAddressline2 = { |
| 220 | + type: "string", |
| 221 | + label: "Billing Address Line 2", |
| 222 | + description: "The billing address line 2.", |
| 223 | + optional: true, |
| 224 | + }; |
| 225 | + props.billingAddressline3 = { |
| 226 | + type: "string", |
| 227 | + label: "Billing Address Line 3", |
| 228 | + description: "The billing address line 3.", |
| 229 | + optional: true, |
| 230 | + }; |
| 231 | + props.billingAddressCity = { |
| 232 | + type: "string", |
| 233 | + label: "Billing Address City", |
| 234 | + description: "The city of the billing address.", |
| 235 | + optional: true, |
| 236 | + }; |
| 237 | + props.billingAddressState = { |
| 238 | + type: "string", |
| 239 | + label: "Billing Address State", |
| 240 | + description: "The state of the billing address.", |
| 241 | + optional: true, |
| 242 | + }; |
| 243 | + props.billingAddressZip = { |
| 244 | + type: "string", |
| 245 | + label: "Billing Address Zip", |
| 246 | + description: "The zip of the billing address.", |
| 247 | + optional: true, |
| 248 | + }; |
| 249 | + props.billingAddressCountryCode = { |
| 250 | + type: "string", |
| 251 | + label: "Billing Address Country Code", |
| 252 | + description: "The country code of the billing address. [See the ISO 3166 codes](https://www.iban.com/country-codes).", |
| 253 | + optional: true, |
| 254 | + }; |
| 255 | + props.billingAddressPhone = { |
| 256 | + type: "string", |
| 257 | + label: "Billing Address Phone", |
| 258 | + description: "The phone of the billing address.", |
| 259 | + optional: true, |
| 260 | + }; |
| 261 | + } |
| 262 | + return props; |
| 263 | + }, |
| 264 | + async run({ $ }) { |
| 265 | + if (!this.customerId && |
| 266 | + !this.title && |
| 267 | + !this.name && |
| 268 | + !this.surname && |
| 269 | + !this.email && |
| 270 | + !this.customerCompany && |
| 271 | + !this.accountNumber) { |
| 272 | + throw new ConfigurationError("You must provide at least 'Customer Id', 'Customer Title', 'Customer Name', 'Customer Surname', 'Customer Email', 'Customer Company' or 'Customer Account Number'."); |
| 273 | + } |
| 274 | + |
| 275 | + if (!this.shippingAddressId && |
| 276 | + !this.shippingAddressline1 && |
| 277 | + !this.shippingAddressCountryCode) { |
| 278 | + throw new ConfigurationError("You must provide at least 'Shipping Address Id' or 'Shipping Address Line1' and 'Shipping Address Country Code'."); |
| 279 | + } |
| 280 | + |
| 281 | + if (!this.sameAsShipping && |
| 282 | + !this.billingAddressId && |
| 283 | + !this.billingAddressline1 && |
| 284 | + !this.billingAddressCountryCode) { |
| 285 | + throw new ConfigurationError("When 'Same As Shipping' is set **False** you must provide at least 'Billing Address Id' or 'Billing Address Line1' and 'Billing Address Country Code'."); |
| 286 | + } |
| 287 | + |
| 288 | + const response = await this.allocadence.createCustomerOrder({ |
| 289 | + $, |
| 290 | + data: { |
| 291 | + orderNumber: this.orderNumber, |
| 292 | + clientId: this.clientId, |
| 293 | + clientName: this.clientName, |
| 294 | + customer: { |
| 295 | + id: this.customerId, |
| 296 | + title: this.title, |
| 297 | + name: this.name, |
| 298 | + surname: this.surname, |
| 299 | + email: this.email, |
| 300 | + company: this.customerCompany, |
| 301 | + accountNumber: this.accountNumber, |
| 302 | + }, |
| 303 | + shippingAddress: { |
| 304 | + id: this.shippingAddressId, |
| 305 | + code: this.shippingAddressCode, |
| 306 | + company: this.shippingAddressCompany, |
| 307 | + name: this.shippingAddressName, |
| 308 | + line1: this.shippingAddressline1, |
| 309 | + line2: this.shippingAddressline2, |
| 310 | + line3: this.shippingAddressline3, |
| 311 | + city: this.shippingAddressCity, |
| 312 | + state: this.shippingAddressState, |
| 313 | + zip: this.shippingAddressZip, |
| 314 | + countryCode: this.shippingAddressCountryCode, |
| 315 | + phone: this.shippingAddressPhone, |
| 316 | + }, |
| 317 | + billingAddress: { |
| 318 | + sameAsShipping: this.sameAsShipping, |
| 319 | + id: this.billingAddressId, |
| 320 | + code: this.billingAddressCode, |
| 321 | + company: this.billingAddressCompany, |
| 322 | + name: this.billingAddressName, |
| 323 | + line1: this.billingAddressline1, |
| 324 | + line2: this.billingAddressline2, |
| 325 | + line3: this.billingAddressline3, |
| 326 | + city: this.billingAddressCity, |
| 327 | + state: this.billingAddressState, |
| 328 | + zip: this.billingAddressZip, |
| 329 | + countryCode: this.billingAddressCountryCode, |
| 330 | + phone: this.billingAddressPhone, |
| 331 | + }, |
| 332 | + shipFromWarehouseId: this.shipFromWarehouseId, |
| 333 | + shipFromWarehouseName: this.shipFromWarehouseName, |
| 334 | + shipVia: this.shipVia, |
| 335 | + postageAccount: this.postageAccount, |
| 336 | + items: parseObject(this.items), |
| 337 | + }, |
| 338 | + }); |
| 339 | + |
| 340 | + $.export("$summary", `Successfully created customer order with ID ${response.id}`); |
| 341 | + return response; |
| 342 | + }, |
| 343 | +}; |
0 commit comments