Skip to content

Commit 597a6d5

Browse files
committed
add lineItemsJson prop
1 parent 6366449 commit 597a6d5

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

components/clear_books/actions/create-purchase-document/create-purchase-document.mjs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import clearBooks from "../../clear_books.app.mjs";
2+
import { ConfigurationError } from "@pipedream/platform";
23

34
export default {
45
key: "clear_books-create-purchase-document",
@@ -34,9 +35,16 @@ export default {
3435
numLineItems: {
3536
type: "integer",
3637
label: "Number of Line Items",
37-
description: "The number of line items to create",
38+
description: "The number of line items. Use this to manually enter Unit Price, Quantity, and Description of each line item.",
39+
optional: true,
3840
reloadProps: true,
3941
},
42+
lineItemsJson: {
43+
type: "string",
44+
label: "Line Items JSON",
45+
description: "JSON value containing an array of Line Items. For example: `[{\"description\":\"Line Item 1 Description\",\"unitPrice\":1022,\"quantity\":1,\"accountCode\":\"2001001\"},{\"description\":\"Line Item 2 Description\",\"unitPrice\":1023,\"quantity\":2,\"accountCode\":\"2001001\"}]`. [See documentation](https://u.pcloud.link/publink/show?code=XZkThJ5Z4zKewgCL6VBpfxlPeHPDdXXj0Cc7)",
46+
optional: true,
47+
},
4048
},
4149
additionalProps() {
4250
const props = {};
@@ -59,14 +67,45 @@ export default {
5967
}
6068
return props;
6169
},
70+
methods: {
71+
buildLineItems() {
72+
const lineItems = [];
73+
for (let i = 1; i <= this.numLineItems; i++) {
74+
lineItems.push({
75+
unitPrice: this[`line_item_${i}_unit_price`],
76+
quantity: this[`line_item_${i}_quantity`],
77+
description: this[`line_item_${i}_description`],
78+
});
79+
}
80+
return lineItems;
81+
},
82+
parseLineItemsJson() {
83+
try {
84+
return Array.isArray(this.lineItemsJson)
85+
? this.lineItemsJson.map((item) => typeof item === "string"
86+
? JSON.parse(item)
87+
: item)
88+
: typeof this.lineItemsJson === "string"
89+
? JSON.parse(this.lineItemsJson)
90+
: this.lineItemsJson;
91+
} catch {
92+
throw new ConfigurationError("Could not parse Line Items JSON");
93+
}
94+
},
95+
},
6296
async run({ $ }) {
97+
if (!this.numLineItems && !this.lineItemsJson) {
98+
throw new ConfigurationError("Please enter at least one line item");
99+
}
100+
63101
const lineItems = [];
64-
for (let i = 1; i <= this.numLineItems; i++) {
65-
lineItems.push({
66-
unitPrice: this[`line_item_${i}_unit_price`],
67-
quantity: this[`line_item_${i}_quantity`],
68-
description: this[`line_item_${i}_description`],
69-
});
102+
if (this.numLineItems) {
103+
const lineItemsManual = this.buildLineItems();
104+
lineItems.push(...lineItemsManual);
105+
}
106+
if (this.lineItemsJson) {
107+
const lineItemsJson = this.parseLineItemsJson();
108+
lineItems.push(...lineItemsJson);
70109
}
71110

72111
const response = await this.clearBooks.createPurchaseDocument({

0 commit comments

Comments
 (0)