Skip to content

Commit 8e36f6a

Browse files
committed
new-or-updated-invoice source
1 parent 60a9be6 commit 8e36f6a

File tree

7 files changed

+164
-9
lines changed

7 files changed

+164
-9
lines changed

components/zoho_books/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/zoho_books",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Pipedream Zoho Books Components",
55
"main": "zoho_books.app.mjs",
66
"keywords": [

components/zoho_books/sources/common/base.mjs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,60 @@ export default {
1919
_setLastDate(lastDate) {
2020
this.db.set("lastDate", lastDate);
2121
},
22+
getTsField() {
23+
return "created_time";
24+
},
25+
getParams() {
26+
return {};
27+
},
28+
isSorted() {
29+
return true;
30+
},
31+
getItemId(item) {
32+
return item[this.getFieldId()];
33+
},
2234
async emitEvent(maxResults = false) {
2335
const lastDate = this._getLastDate();
36+
let maxDate = lastDate;
2437

2538
const response = this.zohoBooks.paginate({
2639
fn: this.getFunction(),
2740
fieldName: this.getFieldName(),
41+
params: this.getParams(lastDate),
2842
});
2943

3044
let responseArray = [];
45+
const tsField = this.getTsField();
46+
const isSorted = this.isSorted();
47+
3148
for await (const item of response) {
32-
if (item.created_time <= lastDate) break;
33-
responseArray.push(item);
49+
const ts = Date.parse(item[tsField]);
50+
if (ts > lastDate) {
51+
responseArray.push(item);
52+
if (!isSorted) {
53+
maxDate = Math.max(maxDate, ts);
54+
}
55+
} else {
56+
if (isSorted) {
57+
break;
58+
}
59+
}
3460
}
3561

3662
if (responseArray.length) {
3763
if (maxResults && (responseArray.length > maxResults)) {
3864
responseArray.length = maxResults;
3965
}
40-
this._setLastDate(Date.parse(responseArray[0].created_time));
66+
this._setLastDate(isSorted
67+
? Date.parse(responseArray[0][tsField])
68+
: maxDate);
4169
}
4270

4371
for (const item of responseArray.reverse()) {
4472
this.$emit(item, {
45-
id: item[this.getFieldId()],
73+
id: this.getItemId(item),
4674
summary: this.getSummary(item),
47-
ts: Date.parse(item.created_time),
75+
ts: Date.parse(item[tsField]),
4876
});
4977
}
5078
},

components/zoho_books/sources/new-customer/new-customer.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "zoho_books-new-customer",
77
name: "New Customer",
88
description: "Emit new event when a new customer is created.",
9-
version: "0.0.1",
9+
version: "0.0.2",
1010
type: "source",
1111
dedupe: "unique",
1212
methods: {

components/zoho_books/sources/new-expense/new-expense.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "zoho_books-new-expense",
77
name: "New Expense",
88
description: "Emit new event when a new expense is created.",
9-
version: "0.0.1",
9+
version: "0.0.2",
1010
type: "source",
1111
dedupe: "unique",
1212
methods: {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "zoho_books-new-or-updated-invoice",
7+
name: "New or Updated Invoice",
8+
description: "Emit new event when a new invoice is created or an existing invoice is updated.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
getFunction() {
15+
return this.zohoBooks.listInvoices;
16+
},
17+
getFieldName() {
18+
return "invoices";
19+
},
20+
getFieldId() {
21+
return "invoice_id";
22+
},
23+
getTsField() {
24+
return "last_modified_time";
25+
},
26+
getParams(lastDate) {
27+
return {
28+
last_modifed_time: this.formatTimestamp(lastDate),
29+
};
30+
},
31+
formatTimestamp(ts) {
32+
if (!ts) {
33+
return undefined;
34+
}
35+
const date = new Date(ts);
36+
const pad = (n) => n.toString().padStart(2, "0");
37+
const formattedUTC = `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())}T${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())}:${pad(date.getUTCSeconds())}`;
38+
return formattedUTC;
39+
},
40+
getItemId(item) {
41+
return `${item.invoice_id}${Date.parse(item[this.getTsField()])}`;
42+
},
43+
getSummary(item) {
44+
const status = item.created_time === item.last_modified_time
45+
? "New"
46+
: "Updated";
47+
return `${status} Invoice: ${item.invoice_id}`;
48+
},
49+
},
50+
sampleEmit,
51+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
export default {
2+
"ach_payment_initiated": false,
3+
"invoice_id": "4372460000000650034",
4+
"zcrm_potential_id": "",
5+
"customer_id": "4372460000000084001",
6+
"zcrm_potential_name": "",
7+
"customer_name": "",
8+
"company_name": "",
9+
"status": "draft",
10+
"invoice_number": "INV-000002",
11+
"reference_number": "",
12+
"date": "2025-04-09",
13+
"due_date": "2025-04-09",
14+
"due_days": "",
15+
"email": "",
16+
"project_name": "",
17+
"billing_address": {
18+
"address": "",
19+
"street2": "",
20+
"city": "",
21+
"state": "",
22+
"zipcode": "",
23+
"country": "",
24+
"phone": "",
25+
"fax": "",
26+
"attention": ""
27+
},
28+
"shipping_address": {
29+
"address": "",
30+
"street2": "",
31+
"city": "",
32+
"state": "",
33+
"zipcode": "",
34+
"country": "",
35+
"phone": "",
36+
"fax": "",
37+
"attention": ""
38+
},
39+
"country": "",
40+
"phone": "",
41+
"created_by": "",
42+
"total": 0,
43+
"balance": 0,
44+
"payment_expected_date": "",
45+
"custom_fields": [],
46+
"custom_field_hash": {},
47+
"tags": [],
48+
"salesperson_name": "",
49+
"shipping_charge": 0,
50+
"adjustment": 0,
51+
"created_time": "2025-04-09T11:25:54-0400",
52+
"last_modified_time": "2025-04-09T11:25:54-0400",
53+
"updated_time": "2025-04-09T11:25:54-0400",
54+
"is_viewed_by_client": false,
55+
"has_attachment": false,
56+
"client_viewed_time": "",
57+
"is_emailed": false,
58+
"color_code": "",
59+
"current_sub_status_id": "",
60+
"current_sub_status": "draft",
61+
"currency_id": "4372460000000000097",
62+
"schedule_time": "",
63+
"currency_code": "USD",
64+
"currency_symbol": "$",
65+
"template_type": "standard",
66+
"transaction_type": "renewal",
67+
"reminders_sent": 0,
68+
"last_reminder_sent_date": "",
69+
"last_payment_date": "",
70+
"template_id": "4372460000000017001",
71+
"documents": "",
72+
"salesperson_id": "",
73+
"write_off_amount": 0,
74+
"exchange_rate": 1,
75+
"unprocessed_payment_amount": 0
76+
}

components/zoho_books/sources/new-sales-order/new-sales-order.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "zoho_books-new-sales-order",
77
name: "New Sales Order",
88
description: "Emit new event when a new sales order is created.",
9-
version: "0.0.1",
9+
version: "0.0.2",
1010
type: "source",
1111
dedupe: "unique",
1212
methods: {

0 commit comments

Comments
 (0)