Skip to content

Commit f40f212

Browse files
authored
Merging pull request #18476
* Implement Afosto cart actions and update component structure - Added new actions for managing carts: create, add item, add information, add note, and confirm cart. - Introduced a new utility function for parsing objects. - Updated the Afosto component to include a Cart ID property definition. - Bumped version to 0.1.0 and added dependencies for the platform. * pnpm update * pnpm update * Remove optional flag from billing address fields and correct parameter order in pickup points query * Add Afosto cart mutations and refactor actions to utilize them - Introduced new mutations for adding information, items, notes, confirming, and creating carts. - Refactored existing actions to leverage the new mutations for improved clarity and maintainability. - Updated the shipping and billing address fields to accept structured data objects. - Added error handling for API responses in actions.
1 parent 8fb1a6b commit f40f212

File tree

10 files changed

+625
-7
lines changed

10 files changed

+625
-7
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import app from "../../afosto.app.mjs";
3+
import { parseObject } from "../../common/utils.mjs";
4+
5+
export default {
6+
key: "afosto-add-information-to-cart",
7+
name: "Add Information to Cart",
8+
description: "Add customer information to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/collecting-customer-data/)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
app,
13+
cartId: {
14+
propDefinition: [
15+
app,
16+
"cartId",
17+
],
18+
},
19+
email: {
20+
type: "string",
21+
label: "Email",
22+
description: "The email of the customer",
23+
optional: true,
24+
},
25+
isGuest: {
26+
type: "boolean",
27+
label: "Is Guest",
28+
description: "Whether the customer is a guest",
29+
optional: true,
30+
},
31+
givenName: {
32+
type: "string",
33+
label: "Given Name",
34+
description: "The given name of the customer",
35+
optional: true,
36+
},
37+
additionalName: {
38+
type: "string",
39+
label: "Additional Name",
40+
description: "The additional name of the customer",
41+
optional: true,
42+
},
43+
familyName: {
44+
type: "string",
45+
label: "Family Name",
46+
description: "The family name of the customer",
47+
optional: true,
48+
},
49+
organisationName: {
50+
type: "string",
51+
label: "Organisation Name",
52+
description: "The name of the organisation",
53+
optional: true,
54+
},
55+
organisationIsGuest: {
56+
type: "boolean",
57+
label: "Organisation Is Guest",
58+
description: "Whether the organisation is a guest",
59+
optional: true,
60+
},
61+
organisationEmail: {
62+
type: "string",
63+
label: "Organisation Email",
64+
description: "The email of the organisation",
65+
optional: true,
66+
},
67+
organisationCountryCode: {
68+
type: "string",
69+
label: "Organisation Country Code",
70+
description: "The country code of the organisation",
71+
optional: true,
72+
},
73+
organisationNumber: {
74+
type: "string",
75+
label: "Organisation Number",
76+
description: "The number of the organisation",
77+
optional: true,
78+
},
79+
countryCode: {
80+
type: "string",
81+
label: "Phone Number Country Code",
82+
description: "The country code of the phone number",
83+
optional: true,
84+
},
85+
number: {
86+
type: "string",
87+
label: "Phone Number",
88+
description: "The number of the phone number",
89+
optional: true,
90+
},
91+
shippingAddressData: {
92+
type: "object",
93+
label: "Shipping Address Data",
94+
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/)",
95+
},
96+
billingAddressData: {
97+
type: "object",
98+
label: "Billing Address Data",
99+
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/)",
100+
},
101+
},
102+
async run({ $ }) {
103+
const cartId = this.cartId;
104+
105+
const phoneNumberData = {
106+
country_code: this.countryCode,
107+
number: this.number,
108+
};
109+
110+
const parsedBillingAddressData = parseObject(this.billingAddressData);
111+
112+
const variables = {
113+
customerInput: {
114+
cart_id: cartId,
115+
customer: {
116+
contact: {
117+
email: this.email,
118+
is_guest: this.isGuest,
119+
given_name: this.givenName,
120+
additional_name: this.additionalName,
121+
family_name: this.familyName,
122+
phone_numbers: [
123+
phoneNumberData,
124+
],
125+
},
126+
organisation: {
127+
name: this.organisationName,
128+
is_guest: this.organisationIsGuest,
129+
administration: {
130+
email: this.organisationEmail,
131+
},
132+
registration: {
133+
country_code: this.organisationCountryCode,
134+
number: this.organisationNumber,
135+
},
136+
},
137+
},
138+
},
139+
phoneNumberInput: {
140+
cart_id: cartId,
141+
phone_number: phoneNumberData,
142+
},
143+
shippingAddressInput: {
144+
address: parseObject(this.shippingAddressData),
145+
cart_id: cartId,
146+
type: "ADDRESS",
147+
},
148+
billingAddressInput: {
149+
address: parsedBillingAddressData,
150+
cart_id: cartId,
151+
},
152+
countryCode: parsedBillingAddressData?.country_code,
153+
postalCode: parsedBillingAddressData?.postal_code,
154+
};
155+
156+
const response = await this.app.addInformationToCart({
157+
$,
158+
variables,
159+
});
160+
161+
if (response.errors) {
162+
throw new ConfigurationError(JSON.stringify(response.errors[0]));
163+
}
164+
165+
$.export("$summary", `Successfully added information to cart with ID: ${this.cartId}`);
166+
return response.data;
167+
},
168+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import app from "../../afosto.app.mjs";
3+
4+
export default {
5+
key: "afosto-add-item-to-cart",
6+
name: "Add Item to Cart",
7+
description: "Add an item to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/integration/add-and-remove-items/)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
sku: {
13+
type: "string",
14+
label: "SKU",
15+
description: "The SKU of the item to add to the cart.",
16+
},
17+
quantity: {
18+
type: "integer",
19+
label: "Quantity",
20+
description: "The quantity of the item to add to the cart.",
21+
},
22+
cartId: {
23+
propDefinition: [
24+
app,
25+
"cartId",
26+
],
27+
},
28+
},
29+
async run({ $ }) {
30+
const response = await this.app.addItemToCart({
31+
$,
32+
variables: {
33+
input: {
34+
cart_id: this.cartId,
35+
items: [
36+
{
37+
sku: this.sku,
38+
quantity: this.quantity,
39+
},
40+
],
41+
},
42+
},
43+
});
44+
45+
if (response.errors) {
46+
throw new ConfigurationError(JSON.stringify(response.errors[0]));
47+
}
48+
$.export("$summary", `Successfully added item to cart with SKU: ${response.data.addItemsToCart.cart.items[0].sku}`);
49+
return response.data.addItemsToCart.cart;
50+
},
51+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import app from "../../afosto.app.mjs";
3+
4+
export default {
5+
key: "afosto-add-note-to-cart",
6+
name: "Add Note to Cart",
7+
description: "Add a note to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/checkout-summary/)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
cartId: {
13+
propDefinition: [
14+
app,
15+
"cartId",
16+
],
17+
},
18+
note: {
19+
type: "string",
20+
label: "Note",
21+
description: "The note to add to the cart",
22+
},
23+
},
24+
async run({ $ }) {
25+
const variables = {
26+
cartId: this.cartId,
27+
note: this.note,
28+
};
29+
30+
const response = await this.app.addNoteToCart({
31+
$,
32+
variables,
33+
});
34+
35+
if (response.errors) {
36+
throw new ConfigurationError(JSON.stringify(response.errors[0]));
37+
}
38+
39+
$.export("$summary", `Successfully added note to cart with ID: ${this.cartId}`);
40+
return response.data.setNoteForCart.cart;
41+
},
42+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import app from "../../afosto.app.mjs";
3+
4+
export default {
5+
key: "afosto-confirm-cart",
6+
name: "Confirm Cart",
7+
description: "Confirm a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/payment-process/)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
cartId: {
13+
propDefinition: [
14+
app,
15+
"cartId",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const response = await this.app.confirmCart({
21+
$,
22+
variables: {
23+
cartId: this.cartId,
24+
},
25+
});
26+
27+
if (response.errors) {
28+
throw new ConfigurationError(JSON.stringify(response.errors[0]));
29+
}
30+
31+
$.export("$summary", `Successfully confirmed cart with ID: ${response.data.confirmCart.order.id}`);
32+
return response.data.confirmCart.order;
33+
},
34+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
import app from "../../afosto.app.mjs";
3+
4+
export default {
5+
key: "afosto-create-cart",
6+
name: "Create Cart",
7+
description: "Create a new cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/integration/create-a-cart/)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
},
13+
async run({ $ }) {
14+
const response = await this.app.createCart({
15+
$,
16+
});
17+
18+
if (response.errors) {
19+
throw new ConfigurationError(JSON.stringify(response.errors[0]));
20+
}
21+
22+
$.export("$summary", `Successfully created cart with ID: ${response.data.createCart.cart.id}`);
23+
return response.data.createCart.cart;
24+
},
25+
};

0 commit comments

Comments
 (0)