Skip to content

Commit 1009642

Browse files
committed
refactor(quickbooks): Improve code quality and address review feedback - Add shared utilities, ensure optional props, enhance error handling
1 parent c969b5c commit 1009642

File tree

6 files changed

+94
-61
lines changed

6 files changed

+94
-61
lines changed

components/quickbooks/actions/create-estimate/create-estimate.mjs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ConfigurationError } from "@pipedream/platform";
22
import quickbooks from "../../quickbooks.app.mjs";
3-
import { parseLineItems } from "../../common/utils.mjs";
3+
import {
4+
parseLineItems,
5+
buildSalesLineItems,
6+
} from "../../common/utils.mjs";
47

58
export default {
69
key: "quickbooks-create-estimate",
@@ -45,6 +48,7 @@ export default {
4548
quickbooks,
4649
"currency",
4750
],
51+
optional: true,
4852
},
4953
docNumber: {
5054
type: "string",
@@ -135,19 +139,7 @@ export default {
135139
},
136140
methods: {
137141
buildLineItems() {
138-
const lineItems = [];
139-
for (let i = 1; i <= this.numLineItems; i++) {
140-
lineItems.push({
141-
DetailType: "SalesItemLineDetail",
142-
Amount: this[`amount_${i}`],
143-
SalesItemLineDetail: {
144-
ItemRef: {
145-
value: this[`item_${i}`],
146-
},
147-
},
148-
});
149-
}
150-
return lineItems;
142+
return buildSalesLineItems(this.numLineItems, this);
151143
},
152144
},
153145
async run({ $ }) {

components/quickbooks/actions/create-purchase-order/create-purchase-order.mjs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ConfigurationError } from "@pipedream/platform";
22
import quickbooks from "../../quickbooks.app.mjs";
3-
import { parseLineItems } from "../../common/utils.mjs";
3+
import {
4+
parseLineItems,
5+
buildPurchaseLineItems,
6+
} from "../../common/utils.mjs";
47

58
export default {
69
key: "quickbooks-create-purchase-order",
@@ -74,9 +77,18 @@ export default {
7477
return props;
7578
}
7679
for (let i = 1; i <= this.numLineItems; i++) {
80+
props[`detailType_${i}`] = {
81+
type: "string",
82+
label: `Line ${i} - Detail Type`,
83+
options: [
84+
{ label: "Item Based Expense", value: "ItemBasedExpenseLineDetail" },
85+
{ label: "Account Based Expense", value: "AccountBasedExpenseLineDetail" }
86+
],
87+
default: "ItemBasedExpenseLineDetail",
88+
};
7789
props[`item_${i}`] = {
7890
type: "string",
79-
label: `Line ${i} - Item ID`,
91+
label: `Line ${i} - Item/Account ID`,
8092
options: async ({ page }) => {
8193
return this.quickbooks.getPropOptions({
8294
page,
@@ -99,30 +111,26 @@ export default {
99111
},
100112
methods: {
101113
buildLineItems() {
102-
const lineItems = [];
103-
for (let i = 1; i <= this.numLineItems; i++) {
104-
lineItems.push({
105-
DetailType: "ItemBasedExpenseLineDetail",
106-
Amount: this[`amount_${i}`],
107-
ItemBasedExpenseLineDetail: {
108-
ItemRef: {
109-
value: this[`item_${i}`],
110-
},
111-
},
112-
});
113-
}
114-
return lineItems;
114+
return buildPurchaseLineItems(this.numLineItems, this);
115115
},
116116
},
117117
async run({ $ }) {
118-
if ((!this.numLineItems && !this.lineItemsAsObjects) || !this.vendorRefValue) {
119-
throw new ConfigurationError("Must provide lineItems and vendorRefValue parameters.");
118+
if (!this.vendorRefValue) {
119+
throw new ConfigurationError("Vendor is required to create a purchase order.");
120+
}
121+
122+
if (!this.numLineItems && !this.lineItemsAsObjects) {
123+
throw new ConfigurationError("At least one line item is required. Either specify the number of line items or provide line items as objects.");
120124
}
121125

122126
const lines = this.lineItemsAsObjects
123127
? parseLineItems(this.lineItems)
124128
: this.buildLineItems();
125129

130+
if (!lines || lines.length === 0) {
131+
throw new ConfigurationError("No valid line items were provided.");
132+
}
133+
126134
lines.forEach((line) => {
127135
if (line.DetailType !== "ItemBasedExpenseLineDetail" && line.DetailType !== "AccountBasedExpenseLineDetail") {
128136
throw new ConfigurationError("Line Item DetailType must be `ItemBasedExpenseLineDetail` or `AccountBasedExpenseLineDetail`");

components/quickbooks/actions/update-estimate/update-estimate.mjs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ConfigurationError } from "@pipedream/platform";
22
import quickbooks from "../../quickbooks.app.mjs";
3-
import { parseLineItems } from "../../common/utils.mjs";
3+
import {
4+
parseLineItems,
5+
buildSalesLineItems,
6+
} from "../../common/utils.mjs";
47

58
export default {
69
key: "quickbooks-update-estimate",
@@ -52,6 +55,7 @@ export default {
5255
quickbooks,
5356
"currency",
5457
],
58+
optional: true,
5559
},
5660
docNumber: {
5761
type: "string",
@@ -88,6 +92,7 @@ export default {
8892
quickbooks,
8993
"taxCodeId",
9094
],
95+
optional: true,
9196
},
9297
lineItemsAsObjects: {
9398
propDefinition: [
@@ -142,19 +147,7 @@ export default {
142147
},
143148
methods: {
144149
buildLineItems() {
145-
const lineItems = [];
146-
for (let i = 1; i <= this.numLineItems; i++) {
147-
lineItems.push({
148-
DetailType: "SalesItemLineDetail",
149-
Amount: this[`amount_${i}`],
150-
SalesItemLineDetail: {
151-
ItemRef: {
152-
value: this[`item_${i}`],
153-
},
154-
},
155-
});
156-
}
157-
return lineItems;
150+
return buildSalesLineItems(this.numLineItems, this);
158151
},
159152
},
160153
async run({ $ }) {

components/quickbooks/actions/update-invoice/update-invoice.mjs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ConfigurationError } from "@pipedream/platform";
22
import quickbooks from "../../quickbooks.app.mjs";
3-
import { parseLineItems } from "../../common/utils.mjs";
3+
import {
4+
parseLineItems,
5+
buildSalesLineItems,
6+
} from "../../common/utils.mjs";
47

58
export default {
69
key: "quickbooks-update-invoice",
@@ -52,6 +55,7 @@ export default {
5255
quickbooks,
5356
"currency",
5457
],
58+
optional: true,
5559
},
5660
docNumber: {
5761
type: "string",
@@ -94,6 +98,7 @@ export default {
9498
quickbooks,
9599
"taxCodeId",
96100
],
101+
optional: true,
97102
},
98103
lineItemsAsObjects: {
99104
propDefinition: [
@@ -148,19 +153,7 @@ export default {
148153
},
149154
methods: {
150155
buildLineItems() {
151-
const lineItems = [];
152-
for (let i = 1; i <= this.numLineItems; i++) {
153-
lineItems.push({
154-
DetailType: "SalesItemLineDetail",
155-
Amount: this[`amount_${i}`],
156-
SalesItemLineDetail: {
157-
ItemRef: {
158-
value: this[`item_${i}`],
159-
},
160-
},
161-
});
162-
}
163-
return lineItems;
156+
return buildSalesLineItems(this.numLineItems, this);
164157
},
165158
},
166159
async run({ $ }) {

components/quickbooks/actions/void-invoice/void-invoice.mjs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ConfigurationError } from "@pipedream/platform";
12
import quickbooks from "../../quickbooks.app.mjs";
23

34
export default {
@@ -17,11 +18,20 @@ export default {
1718
},
1819
async run({ $ }) {
1920
// Get the current invoice to obtain SyncToken
20-
const { Invoice: invoice } = await this.quickbooks.getInvoice({
21+
const invoiceResponse = await this.quickbooks.getInvoice({
2122
$,
2223
invoiceId: this.invoiceId,
2324
});
2425

26+
if (!invoiceResponse?.Invoice) {
27+
throw new ConfigurationError(`Invoice with ID ${this.invoiceId} not found or could not be retrieved.`);
28+
}
29+
30+
const invoice = invoiceResponse.Invoice;
31+
if (!invoice.SyncToken) {
32+
throw new ConfigurationError(`Invoice ${this.invoiceId} is missing required SyncToken.`);
33+
}
34+
2535
const data = {
2636
Id: this.invoiceId,
2737
SyncToken: invoice.SyncToken,
@@ -33,7 +43,11 @@ export default {
3343
});
3444

3545
if (response) {
36-
$.export("summary", `Successfully voided invoice with ID ${response.Invoice.Id}`);
46+
if (response.Invoice?.Id) {
47+
$.export("summary", `Successfully voided invoice with ID ${response.Invoice.Id}`);
48+
} else {
49+
$.export("summary", `Invoice void operation completed for ID ${this.invoiceId}`);
50+
}
3751
}
3852

3953
return response;

components/quickbooks/common/utils.mjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,36 @@ export function parseLineItems(arr) {
4444
throw new ConfigurationError(`We got an error trying to parse the LineItems. Error: ${error}`);
4545
}
4646
}
47+
48+
export function buildSalesLineItems(numLineItems, context) {
49+
const lineItems = [];
50+
for (let i = 1; i <= numLineItems; i++) {
51+
lineItems.push({
52+
DetailType: "SalesItemLineDetail",
53+
Amount: context[`amount_${i}`],
54+
SalesItemLineDetail: {
55+
ItemRef: {
56+
value: context[`item_${i}`],
57+
},
58+
},
59+
});
60+
}
61+
return lineItems;
62+
}
63+
64+
export function buildPurchaseLineItems(numLineItems, context) {
65+
const lineItems = [];
66+
for (let i = 1; i <= numLineItems; i++) {
67+
const detailType = context[`detailType_${i}`] || "ItemBasedExpenseLineDetail";
68+
lineItems.push({
69+
DetailType: detailType,
70+
Amount: context[`amount_${i}`],
71+
[detailType === "ItemBasedExpenseLineDetail" ? "ItemBasedExpenseLineDetail" : "AccountBasedExpenseLineDetail"]: {
72+
[detailType === "ItemBasedExpenseLineDetail" ? "ItemRef" : "AccountRef"]: {
73+
value: context[`item_${i}`],
74+
},
75+
},
76+
});
77+
}
78+
return lineItems;
79+
}

0 commit comments

Comments
 (0)