diff --git a/components/afosto/actions/add-information-to-cart/add-information-to-cart.mjs b/components/afosto/actions/add-information-to-cart/add-information-to-cart.mjs new file mode 100644 index 0000000000000..8e56270b0a870 --- /dev/null +++ b/components/afosto/actions/add-information-to-cart/add-information-to-cart.mjs @@ -0,0 +1,168 @@ +import { ConfigurationError } from "@pipedream/platform"; +import app from "../../afosto.app.mjs"; +import { parseObject } from "../../common/utils.mjs"; + +export default { + key: "afosto-add-information-to-cart", + name: "Add Information to Cart", + description: "Add customer information to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/collecting-customer-data/)", + version: "0.0.1", + type: "action", + props: { + app, + cartId: { + propDefinition: [ + app, + "cartId", + ], + }, + email: { + type: "string", + label: "Email", + description: "The email of the customer", + optional: true, + }, + isGuest: { + type: "boolean", + label: "Is Guest", + description: "Whether the customer is a guest", + optional: true, + }, + givenName: { + type: "string", + label: "Given Name", + description: "The given name of the customer", + optional: true, + }, + additionalName: { + type: "string", + label: "Additional Name", + description: "The additional name of the customer", + optional: true, + }, + familyName: { + type: "string", + label: "Family Name", + description: "The family name of the customer", + optional: true, + }, + organisationName: { + type: "string", + label: "Organisation Name", + description: "The name of the organisation", + optional: true, + }, + organisationIsGuest: { + type: "boolean", + label: "Organisation Is Guest", + description: "Whether the organisation is a guest", + optional: true, + }, + organisationEmail: { + type: "string", + label: "Organisation Email", + description: "The email of the organisation", + optional: true, + }, + organisationCountryCode: { + type: "string", + label: "Organisation Country Code", + description: "The country code of the organisation", + optional: true, + }, + organisationNumber: { + type: "string", + label: "Organisation Number", + description: "The number of the organisation", + optional: true, + }, + countryCode: { + type: "string", + label: "Phone Number Country Code", + description: "The country code of the phone number", + optional: true, + }, + number: { + type: "string", + label: "Phone Number", + description: "The number of the phone number", + optional: true, + }, + shippingAddressData: { + type: "object", + label: "Shipping Address Data", + description: "The shipping address data. **E.g. {\"country_code\": \"US\", \"postal_code\": \"10001\", \"address_line_1\": \"123 Main St\", \"given_name\": \"John\", \"family_name\": \"Smith\", \"organisation\": \"Organization Name\"}** [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/collecting-customer-data/)", + }, + billingAddressData: { + type: "object", + label: "Billing Address Data", + description: "The billing address data. **E.g. {\"country_code\": \"US\", \"postal_code\": \"10001\", \"address_line_1\": \"123 Main St\", \"given_name\": \"John\", \"family_name\": \"Smith\", \"organisation\": \"Organization Name\"}** [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/collecting-customer-data/)", + }, + }, + async run({ $ }) { + const cartId = this.cartId; + + const phoneNumberData = { + country_code: this.countryCode, + number: this.number, + }; + + const parsedBillingAddressData = parseObject(this.billingAddressData); + + const variables = { + customerInput: { + cart_id: cartId, + customer: { + contact: { + email: this.email, + is_guest: this.isGuest, + given_name: this.givenName, + additional_name: this.additionalName, + family_name: this.familyName, + phone_numbers: [ + phoneNumberData, + ], + }, + organisation: { + name: this.organisationName, + is_guest: this.organisationIsGuest, + administration: { + email: this.organisationEmail, + }, + registration: { + country_code: this.organisationCountryCode, + number: this.organisationNumber, + }, + }, + }, + }, + phoneNumberInput: { + cart_id: cartId, + phone_number: phoneNumberData, + }, + shippingAddressInput: { + address: parseObject(this.shippingAddressData), + cart_id: cartId, + type: "ADDRESS", + }, + billingAddressInput: { + address: parsedBillingAddressData, + cart_id: cartId, + }, + countryCode: parsedBillingAddressData?.country_code, + postalCode: parsedBillingAddressData?.postal_code, + }; + + const response = await this.app.addInformationToCart({ + $, + variables, + }); + + if (response.errors) { + throw new ConfigurationError(JSON.stringify(response.errors[0])); + } + + $.export("$summary", `Successfully added information to cart with ID: ${this.cartId}`); + return response.data; + }, +}; diff --git a/components/afosto/actions/add-item-to-cart/add-item-to-cart.mjs b/components/afosto/actions/add-item-to-cart/add-item-to-cart.mjs new file mode 100644 index 0000000000000..ca78b9a65a12d --- /dev/null +++ b/components/afosto/actions/add-item-to-cart/add-item-to-cart.mjs @@ -0,0 +1,51 @@ +import { ConfigurationError } from "@pipedream/platform"; +import app from "../../afosto.app.mjs"; + +export default { + key: "afosto-add-item-to-cart", + name: "Add Item to Cart", + description: "Add an item to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/integration/add-and-remove-items/)", + version: "0.0.1", + type: "action", + props: { + app, + sku: { + type: "string", + label: "SKU", + description: "The SKU of the item to add to the cart.", + }, + quantity: { + type: "integer", + label: "Quantity", + description: "The quantity of the item to add to the cart.", + }, + cartId: { + propDefinition: [ + app, + "cartId", + ], + }, + }, + async run({ $ }) { + const response = await this.app.addItemToCart({ + $, + variables: { + input: { + cart_id: this.cartId, + items: [ + { + sku: this.sku, + quantity: this.quantity, + }, + ], + }, + }, + }); + + if (response.errors) { + throw new ConfigurationError(JSON.stringify(response.errors[0])); + } + $.export("$summary", `Successfully added item to cart with SKU: ${response.data.addItemsToCart.cart.items[0].sku}`); + return response.data.addItemsToCart.cart; + }, +}; diff --git a/components/afosto/actions/add-note-to-cart/add-note-to-cart.mjs b/components/afosto/actions/add-note-to-cart/add-note-to-cart.mjs new file mode 100644 index 0000000000000..02e40b2061f41 --- /dev/null +++ b/components/afosto/actions/add-note-to-cart/add-note-to-cart.mjs @@ -0,0 +1,42 @@ +import { ConfigurationError } from "@pipedream/platform"; +import app from "../../afosto.app.mjs"; + +export default { + key: "afosto-add-note-to-cart", + name: "Add Note to Cart", + description: "Add a note to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/checkout-summary/)", + version: "0.0.1", + type: "action", + props: { + app, + cartId: { + propDefinition: [ + app, + "cartId", + ], + }, + note: { + type: "string", + label: "Note", + description: "The note to add to the cart", + }, + }, + async run({ $ }) { + const variables = { + cartId: this.cartId, + note: this.note, + }; + + const response = await this.app.addNoteToCart({ + $, + variables, + }); + + if (response.errors) { + throw new ConfigurationError(JSON.stringify(response.errors[0])); + } + + $.export("$summary", `Successfully added note to cart with ID: ${this.cartId}`); + return response.data.setNoteForCart.cart; + }, +}; diff --git a/components/afosto/actions/confirm-cart/confirm-cart.mjs b/components/afosto/actions/confirm-cart/confirm-cart.mjs new file mode 100644 index 0000000000000..91d2499f9f0cd --- /dev/null +++ b/components/afosto/actions/confirm-cart/confirm-cart.mjs @@ -0,0 +1,34 @@ +import { ConfigurationError } from "@pipedream/platform"; +import app from "../../afosto.app.mjs"; + +export default { + key: "afosto-confirm-cart", + name: "Confirm Cart", + description: "Confirm a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/payment-process/)", + version: "0.0.1", + type: "action", + props: { + app, + cartId: { + propDefinition: [ + app, + "cartId", + ], + }, + }, + async run({ $ }) { + const response = await this.app.confirmCart({ + $, + variables: { + cartId: this.cartId, + }, + }); + + if (response.errors) { + throw new ConfigurationError(JSON.stringify(response.errors[0])); + } + + $.export("$summary", `Successfully confirmed cart with ID: ${response.data.confirmCart.order.id}`); + return response.data.confirmCart.order; + }, +}; diff --git a/components/afosto/actions/create-cart/create-cart.mjs b/components/afosto/actions/create-cart/create-cart.mjs new file mode 100644 index 0000000000000..863d19b156fce --- /dev/null +++ b/components/afosto/actions/create-cart/create-cart.mjs @@ -0,0 +1,25 @@ +import { ConfigurationError } from "@pipedream/platform"; +import app from "../../afosto.app.mjs"; + +export default { + key: "afosto-create-cart", + name: "Create Cart", + description: "Create a new cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/integration/create-a-cart/)", + version: "0.0.1", + type: "action", + props: { + app, + }, + async run({ $ }) { + const response = await this.app.createCart({ + $, + }); + + if (response.errors) { + throw new ConfigurationError(JSON.stringify(response.errors[0])); + } + + $.export("$summary", `Successfully created cart with ID: ${response.data.createCart.cart.id}`); + return response.data.createCart.cart; + }, +}; diff --git a/components/afosto/afosto.app.mjs b/components/afosto/afosto.app.mjs index caddad113b4f7..46d2574c3f3e1 100644 --- a/components/afosto/afosto.app.mjs +++ b/components/afosto/afosto.app.mjs @@ -1,11 +1,94 @@ +import { axios } from "@pipedream/platform"; +import mutations from "./common/mutations.mjs"; + export default { type: "app", app: "afosto", - propDefinitions: {}, + propDefinitions: { + cartId: { + type: "string", + label: "Cart ID", + description: "The ID of the cart", + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _headers(headers = {}) { + return { + "Authorization": `Bearer ${this.$auth.auth_token}`, + "Content-Type": "application/json", + ...headers, + }; + }, + _baseUrl() { + return "https://afosto.app/graphql"; + }, + _makeRequest({ + $ = this, path = "", headers, ...opts + }) { + return axios($, { + method: "POST", + url: this._baseUrl() + path, + headers: this._headers(headers), + ...opts, + }); + }, + query(opts = {}) { + return this._makeRequest({ + method: "POST", + ...opts, + }); + }, + addInformationToCart({ + variables, ...opts + }) { + return this._makeRequest({ + data: JSON.stringify({ + query: mutations.addInformationToCart, + variables, + }), + ...opts, + }); + }, + addItemToCart({ + variables, ...opts + }) { + return this._makeRequest({ + data: JSON.stringify({ + query: mutations.addItemToCart, + variables, + }), + ...opts, + }); + }, + addNoteToCart({ + variables, ...opts + }) { + return this._makeRequest({ + data: JSON.stringify({ + query: mutations.addNoteToCart, + variables, + }), + ...opts, + }); + }, + confirmCart({ + variables, ...opts + }) { + return this._makeRequest({ + data: JSON.stringify({ + query: mutations.confirmCart, + variables, + }), + ...opts, + }); + }, + createCart(opts = {}) { + return this._makeRequest({ + data: JSON.stringify({ + query: mutations.createCart, + }), + ...opts, + }); }, }, }; diff --git a/components/afosto/common/mutations.mjs b/components/afosto/common/mutations.mjs new file mode 100644 index 0000000000000..6cb4d258e0544 --- /dev/null +++ b/components/afosto/common/mutations.mjs @@ -0,0 +1,184 @@ +const addInformationToCart = `mutation AddInformationToCart( + $customerInput: AddCustomerToCartInput! + $phoneNumberInput: AddPhoneNumberToCartInput! + $shippingAddressInput: AddShippingAddressToCartInput! + $billingAddressInput: AddBillingAddressToCartInput! + $countryCode: String! + $postalCode: String! + ) { + addCustomerToCart(input: $customerInput) { + cart { + customer { + contact { + id + number + email + is_guest + name + given_name + additional_name + family_name + created_at + } + } + } + } + addPhoneNumberToCart(input: $phoneNumberInput) { + cart { + phone_number { + id + country_code + number + national + created_at + } + } + } + addShippingAddressToCart(input: $shippingAddressInput) { + cart { + delivery { + address { + id + country_code + administrative_area + locality + postal_code + address_line_1 + address_line_2 + thoroughfare + premise_number + premise_number_suffix + given_name + additional_name + family_name + created_at + } + } + } + } + addBillingAddressToCart(input: $billingAddressInput) { + cart { + billing { + address { + id + country_code + administrative_area + locality + postal_code + address_line_1 + address_line_2 + thoroughfare + premise_number + premise_number_suffix + given_name + additional_name + family_name + created_at + } + } + options { + shipping { + methods { + pickup_points(postal_code: $postalCode, country_code: $countryCode) { + id + name + carrier + latitude + longitude + distance + address { + country_code + id + postal_code + } + } + } + } + } + } + } + } +`; + +const addItemToCart = `mutation AddItemToCart( + $input: AddItemsToCartInput! + ) { + addItemsToCart(input: $input) { + cart { + id + items { + ids + label + brand + mpn + gtin + image + hs_code + country_of_origin + url + sku + quantity + subtotal + total + parent_id + } + } + } + } +`; + +const addNoteToCart = `mutation SetNoteForCart ( + $cartId: String! + $note: String! + ) { + setNoteForCart(input: { cart_id: $cartId, note: $note }) { + cart { + id + number + total + subtotal + total_excluding_vat + currency + is_including_vat + is_vat_shifted + created_at + updated_at + } + } + } +`; + +const confirmCart = `mutation ConfirmCart ( + $cartId: String! + ) { + confirmCart(input: { cart_id: $cartId }) { + order { + id + } + } + } +`; + +const createCart = `mutation createCart { + createCart(input: {}) { + cart { + id + number + total + subtotal + total_excluding_vat + currency + is_including_vat + is_vat_shifted + created_at + } + } +}`; + +export default { + addInformationToCart, + addItemToCart, + addNoteToCart, + confirmCart, + createCart, +}; diff --git a/components/afosto/common/utils.mjs b/components/afosto/common/utils.mjs new file mode 100644 index 0000000000000..dcc9cc61f6f41 --- /dev/null +++ b/components/afosto/common/utils.mjs @@ -0,0 +1,24 @@ +export const parseObject = (obj) => { + if (!obj) return undefined; + + if (Array.isArray(obj)) { + return obj.map((item) => { + if (typeof item === "string") { + try { + return JSON.parse(item); + } catch (e) { + return item; + } + } + return item; + }); + } + if (typeof obj === "string") { + try { + return JSON.parse(obj); + } catch (e) { + return obj; + } + } + return obj; +}; diff --git a/components/afosto/package.json b/components/afosto/package.json index b7ade1930d03e..a14e51213c302 100644 --- a/components/afosto/package.json +++ b/components/afosto/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/afosto", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Afosto Components", "main": "afosto.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.1.0" } -} \ No newline at end of file +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 939d6c9a66e09..e24b3065aac4e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -439,7 +439,11 @@ importers: specifier: ^3.0.0 version: 3.0.3 - components/afosto: {} + components/afosto: + dependencies: + '@pipedream/platform': + specifier: ^3.1.0 + version: 3.1.0 components/aftership: dependencies: @@ -12247,7 +12251,11 @@ importers: specifier: ^3.0.0 version: 3.0.3 - components/rosette_text_analytics: {} + components/rosette_text_analytics: + dependencies: + '@pipedream/platform': + specifier: ^3.1.0 + version: 3.1.0 components/route4me: {} @@ -30984,22 +30992,22 @@ packages: superagent@3.8.1: resolution: {integrity: sha512-VMBFLYgFuRdfeNQSMLbxGSLfmXL/xc+OO+BZp41Za/NRDBet/BNbkRJrYzCUu0u4GU0i/ml2dtT8b9qgkw9z6Q==} engines: {node: '>= 4.0'} - deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net superagent@4.1.0: resolution: {integrity: sha512-FT3QLMasz0YyCd4uIi5HNe+3t/onxMyEho7C3PSqmti3Twgy2rXT4fmkTz6wRL6bTF4uzPcfkUCa8u4JWHw8Ag==} engines: {node: '>= 6.0'} - deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net superagent@5.3.1: resolution: {integrity: sha512-wjJ/MoTid2/RuGCOFtlacyGNxN9QLMgcpYLDQlWFIhhdJ93kNscFonGvrpAHSCVjRVj++DGCglocF7Aej1KHvQ==} engines: {node: '>= 7.0.0'} - deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net superagent@7.1.6: resolution: {integrity: sha512-gZkVCQR1gy/oUXr+kxJMLDjla434KmSOKbx5iGD30Ql+AkJQ/YlPKECJy2nhqOsHLjGHzoDTXNSjhnvWhzKk7g==} engines: {node: '>=6.4.0 <13 || >=14'} - deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net supports-color@10.0.0: resolution: {integrity: sha512-HRVVSbCCMbj7/kdWF9Q+bbckjBHLtHMEoJWlkmYzzdwhYMkjkOwubLM6t7NbWKjgKamGDrWL1++KrjUO1t9oAQ==}