Skip to content

Commit fd15883

Browse files
committed
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.
1 parent 946cafd commit fd15883

File tree

8 files changed

+744
-6
lines changed

8 files changed

+744
-6
lines changed

components/afosto/actions/add-information-to-cart/add-information-to-cart.mjs

Lines changed: 452 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import app from "../../afosto.app.mjs";
2+
3+
export default {
4+
key: "afosto-add-item-to-cart",
5+
name: "Add Item to Cart",
6+
description: "Add an item to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/integration/add-and-remove-items/)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
sku: {
12+
type: "string",
13+
label: "SKU",
14+
description: "The SKU of the item to add to the cart.",
15+
},
16+
quantity: {
17+
type: "integer",
18+
label: "Quantity",
19+
description: "The quantity of the item to add to the cart.",
20+
},
21+
cartId: {
22+
propDefinition: [
23+
app,
24+
"cartId",
25+
],
26+
},
27+
},
28+
async run({ $ }) {
29+
try {
30+
const response = await this.app.query({
31+
$,
32+
data: JSON.stringify({
33+
query: `mutation AddItemToCart(
34+
$input: AddItemsToCartInput!
35+
) {
36+
addItemsToCart(input: $input) {
37+
cart {
38+
id
39+
items {
40+
ids
41+
label
42+
brand
43+
mpn
44+
gtin
45+
image
46+
hs_code
47+
country_of_origin
48+
url
49+
sku
50+
quantity
51+
subtotal
52+
total
53+
parent_id
54+
}
55+
}
56+
}
57+
}`,
58+
variables: {
59+
input: {
60+
cart_id: this.cartId,
61+
items: [
62+
{
63+
sku: this.sku,
64+
quantity: this.quantity,
65+
},
66+
],
67+
},
68+
},
69+
}),
70+
});
71+
72+
$.export("$summary", `Successfully added item to cart with SKU: ${response.data.addItemsToCart.cart.items[0].sku}`);
73+
return response.data.addItemsToCart.cart;
74+
} catch ({ response }) {
75+
throw new Error(`${response.data?.errors?.[0]?.message}`);
76+
}
77+
},
78+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import app from "../../afosto.app.mjs";
2+
3+
export default {
4+
key: "afosto-add-note-to-cart",
5+
name: "Add Note to Cart",
6+
description: "Add a note to a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/checkout-summary/)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
cartId: {
12+
propDefinition: [
13+
app,
14+
"cartId",
15+
],
16+
},
17+
note: {
18+
type: "string",
19+
label: "Note",
20+
description: "The note to add to the cart",
21+
},
22+
},
23+
async run({ $ }) {
24+
try {
25+
const variables = {
26+
cartId: this.cartId,
27+
note: this.note,
28+
};
29+
30+
const response = await this.app.query({
31+
$,
32+
maxBodyLength: Infinity,
33+
headers: {
34+
"DisablePreParseMultipartForm": "true",
35+
},
36+
data: JSON.stringify({
37+
query: `mutation SetNoteForCart (
38+
$cartId: String!
39+
$note: String!
40+
) {
41+
setNoteForCart(input: { cart_id: $cartId, note: $note }) {
42+
cart {
43+
id
44+
number
45+
total
46+
subtotal
47+
total_excluding_vat
48+
currency
49+
is_including_vat
50+
is_vat_shifted
51+
created_at
52+
updated_at
53+
}
54+
}
55+
}`,
56+
variables,
57+
}),
58+
});
59+
60+
$.export("$summary", `Successfully added note to cart with ID: ${this.cartId}`);
61+
return response.data.setNoteForCart.cart;
62+
} catch (error) {
63+
throw new Error(`${error.errors?.[0]?.message || "An unknown error occurred"}`);
64+
}
65+
},
66+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import app from "../../afosto.app.mjs";
2+
3+
export default {
4+
key: "afosto-confirm-cart",
5+
name: "Confirm Cart",
6+
description: "Confirm a cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/custom-checkout/payment-process/)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
cartId: {
12+
propDefinition: [
13+
app,
14+
"cartId",
15+
],
16+
},
17+
},
18+
async run({ $ }) {
19+
try {
20+
const response = await this.app.query({
21+
$,
22+
data: JSON.stringify({
23+
query: `mutation ConfirmCart (
24+
$cartId: String!
25+
) {
26+
confirmCart(input: { cart_id: $cartId }) {
27+
order {
28+
id
29+
}
30+
}
31+
}
32+
`,
33+
variables: {
34+
cartId: this.cartId,
35+
},
36+
}),
37+
});
38+
39+
$.export("$summary", `Successfully confirmed cart with ID: ${response.data.confirmCart.order.id}`);
40+
return response.data.confirmCart.order;
41+
} catch ({ response }) {
42+
throw new Error(`${response.data?.errors?.[0]?.message}`);
43+
}
44+
},
45+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import app from "../../afosto.app.mjs";
2+
3+
export default {
4+
key: "afosto-create-cart",
5+
name: "Create Cart",
6+
description: "Create a new cart. [See the documentation](https://afosto.com/docs/developers/storefront-js-client/integration/create-a-cart/)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
},
12+
async run({ $ }) {
13+
try {
14+
const response = await this.app.query({
15+
$,
16+
data: JSON.stringify({
17+
query: `mutation createCart {
18+
createCart(input: {}) {
19+
cart {
20+
id
21+
number
22+
total
23+
subtotal
24+
total_excluding_vat
25+
currency
26+
is_including_vat
27+
is_vat_shifted
28+
created_at
29+
}
30+
}
31+
}`,
32+
}),
33+
});
34+
35+
$.export("$summary", `Successfully created cart with ID: ${response.data.createCart.cart.id}`);
36+
return response.data.createCart.cart;
37+
} catch ({ response }) {
38+
throw new Error(`${response.data?.errors?.[0]?.message}`);
39+
}
40+
},
41+
};

components/afosto/afosto.app.mjs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "afosto",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
cartId: {
8+
type: "string",
9+
label: "Cart ID",
10+
description: "The ID of the cart",
11+
},
12+
},
513
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
14+
_headers(headers = {}) {
15+
return {
16+
"Authorization": `Bearer ${this.$auth.auth_token}`,
17+
"Content-Type": "application/json",
18+
...headers,
19+
};
20+
},
21+
_baseUrl() {
22+
return "https://afosto.app/graphql";
23+
},
24+
_makeRequest({
25+
$ = this, path = "", headers, ...opts
26+
}) {
27+
return axios($, {
28+
url: this._baseUrl() + path,
29+
headers: this._headers(headers),
30+
...opts,
31+
});
32+
},
33+
query(opts = {}) {
34+
return this._makeRequest({
35+
method: "POST",
36+
...opts,
37+
});
938
},
1039
},
1140
};

components/afosto/common/utils.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) return undefined;
3+
4+
if (Array.isArray(obj)) {
5+
return obj.map((item) => {
6+
if (typeof item === "string") {
7+
try {
8+
return JSON.parse(item);
9+
} catch (e) {
10+
return item;
11+
}
12+
}
13+
return item;
14+
});
15+
}
16+
if (typeof obj === "string") {
17+
try {
18+
return JSON.parse(obj);
19+
} catch (e) {
20+
return obj;
21+
}
22+
}
23+
return obj;
24+
};

components/afosto/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/afosto",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Afosto Components",
55
"main": "afosto.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.0"
1417
}
15-
}
18+
}

0 commit comments

Comments
 (0)