Skip to content

Commit 424a86c

Browse files
committed
change polling to webhooks
1 parent 21b5042 commit 424a86c

File tree

13 files changed

+424
-186
lines changed

13 files changed

+424
-186
lines changed
Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,45 @@
1-
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
21
import zohoBooks from "../../zoho_books.app.mjs";
32

43
export default {
54
props: {
65
zohoBooks,
6+
http: "$.interface.http",
77
db: "$.service.db",
8-
timer: {
9-
type: "$.interface.timer",
10-
default: {
11-
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12-
},
8+
webhookName: {
9+
type: "string",
10+
label: "Webhook Name",
11+
description: "A name to identify the webhook.",
1312
},
1413
},
1514
methods: {
16-
_getLastDate() {
17-
return this.db.get("lastDate") || 0;
15+
_getHookId() {
16+
return this.db.get("hookId");
1817
},
19-
_setLastDate(lastDate) {
20-
this.db.set("lastDate", lastDate);
21-
},
22-
async emitEvent(maxResults = false) {
23-
const lastDate = this._getLastDate();
24-
25-
const response = this.zohoBooks.paginate({
26-
fn: this.getFunction(),
27-
fieldName: this.getFieldName(),
28-
});
29-
30-
let responseArray = [];
31-
for await (const item of response) {
32-
if (item.created_time <= lastDate) break;
33-
responseArray.push(item);
34-
}
35-
36-
if (responseArray.length) {
37-
if (maxResults && (responseArray.length > maxResults)) {
38-
responseArray.length = maxResults;
39-
}
40-
this._setLastDate(Date.parse(responseArray[0].created_time));
41-
}
42-
43-
for (const item of responseArray.reverse()) {
44-
this.$emit(item, {
45-
id: item[this.getFieldId()],
46-
summary: this.getSummary(item),
47-
ts: Date.parse(item.created_time),
48-
});
49-
}
18+
_setHookId(hookId) {
19+
this.db.set("hookId", hookId);
5020
},
5121
},
5222
hooks: {
53-
async deploy() {
54-
await this.emitEvent(25);
23+
async activate() {
24+
const { webhook } = await this.zohoBooks.createWebhook({
25+
data: {
26+
webhook_name: this.webhookName,
27+
url: this.http.endpoint,
28+
entity: this.getEntity(),
29+
method: "POST",
30+
body_type: "application/json",
31+
raw_data: "${JSONString}",
32+
},
33+
});
34+
this._setHookId(webhook.webhook_id);
35+
},
36+
async deactivate() {
37+
const webhookId = this._getHookId();
38+
await this.zohoBooks.deleteWebhook(webhookId);
5539
},
5640
},
57-
async run() {
58-
await this.emitEvent();
41+
async run({ body }) {
42+
if (body.payload === "") return true;
43+
this.$emit(body, this.generateMeta(body));
5944
},
6045
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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-customer-instant",
7+
name: "New Customer (Instant)",
8+
description: "Emit new event when a new customer is created.",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
getEntity() {
15+
return "customer";
16+
},
17+
generateMeta({ contact }) {
18+
return {
19+
id: contact.contact_id,
20+
summary: `New Customer: ${contact.contact_name}`,
21+
ts: Date.parse(contact.created_time),
22+
};
23+
},
24+
},
25+
sampleEmit,
26+
};
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
export default {
2+
"contact": {
3+
"contact_id": 460000000026049,
4+
"contact_name": "Bowman and Co",
5+
"company_name": "Bowman and Co",
6+
"has_transaction": true,
7+
"contact_type": "customer",
8+
"customer_sub_type": "business",
9+
"credit_limit": 1000,
10+
"is_taxable": true,
11+
"tax_id": 11149000000061058,
12+
"tax_name": "CGST",
13+
"tax_percentage": 12,
14+
"tax_authority_id": 11149000000061052,
15+
"tax_exemption_id": 11149000000061054,
16+
"tax_authority_name": "string",
17+
"tax_exemption_code": "string",
18+
"place_of_contact": "TN",
19+
"gst_no": "22AAAAA0000A1Z5",
20+
"tax_treatment": "string",
21+
"tax_regime": "general_legal_person",
22+
"legal_name": "ESCUELA KEMPER URGATE",
23+
"is_tds_registered": true,
24+
"vat_treatment": "string",
25+
"gst_treatment": "business_gst",
26+
"is_linked_with_zohocrm": false,
27+
"website": "www.bowmanfurniture.com",
28+
"owner_id": 460000000016051,
29+
"primary_contact_id": 460000000026051,
30+
"payment_terms": 15,
31+
"payment_terms_label": "Net 15",
32+
"currency_id": 460000000000097,
33+
"currency_code": "USD",
34+
"currency_symbol": "$",
35+
"opening_balance_amount": 1200,
36+
"exchange_rate": 1,
37+
"outstanding_receivable_amount": 250,
38+
"outstanding_receivable_amount_bcy": 250,
39+
"unused_credits_receivable_amount": 1369.66,
40+
"unused_credits_receivable_amount_bcy": 1369.66,
41+
"status": "active",
42+
"facebook": "zoho",
43+
"twitter": "zoho",
44+
"payment_reminder_enabled": true,
45+
"custom_fields": [
46+
{
47+
"index": 1,
48+
"value": "GBGD078",
49+
"label": "VAT ID"
50+
}
51+
],
52+
"billing_address": {
53+
"attention": "Mr.John",
54+
"address": "4900 Hopyard Rd",
55+
"street2": "Suite 310",
56+
"state_code": "CA",
57+
"city": "Pleasanton",
58+
"state": "CA",
59+
"zip": 94588,
60+
"country": "U.S.A",
61+
"fax": "+1-925-924-9600",
62+
"phone": "+1-925-921-9201"
63+
},
64+
"shipping_address": {
65+
"attention": "Mr.John",
66+
"address": "4900 Hopyard Rd",
67+
"street2": "Suite 310",
68+
"state_code": "CA",
69+
"city": "Pleasanton",
70+
"state": "CA",
71+
"zip": 94588,
72+
"country": "U.S.A",
73+
"fax": "+1-925-924-9600",
74+
"phone": "+1-925-921-9201"
75+
},
76+
"contact_persons": [
77+
{
78+
"contact_person_id": 460000000026051,
79+
"salutation": "Mr",
80+
"first_name": "Will",
81+
"last_name": "Smith",
82+
"email": "[email protected]",
83+
"phone": "+1-925-921-9201",
84+
"mobile": "+1-4054439562",
85+
"designation": "Sales Executive",
86+
"department": "Sales and Marketing",
87+
"skype": "Zoho",
88+
"is_primary_contact": true,
89+
"enable_portal": true
90+
}
91+
],
92+
"default_templates": {
93+
"invoice_template_id": 460000000052069,
94+
"estimate_template_id": 460000000000179,
95+
"creditnote_template_id": 460000000000211,
96+
"purchaseorder_template_id": 460000000000213,
97+
"salesorder_template_id": 460000000000214,
98+
"retainerinvoice_template_id": 460000000000215,
99+
"paymentthankyou_template_id": 460000000000216,
100+
"retainerinvoice_paymentthankyou_template_id": 460000000000217,
101+
"invoice_email_template_id": 460000000052071,
102+
"estimate_email_template_id": 460000000052073,
103+
"creditnote_email_template_id": 460000000052075,
104+
"purchaseorder_email_template_id": 460000000000218,
105+
"salesorder_email_template_id": 460000000000219,
106+
"retainerinvoice_email_template_id": 460000000000220,
107+
"paymentthankyou_email_template_id": 460000000000221,
108+
"retainerinvoice_paymentthankyou_email_template_id": 460000000000222
109+
},
110+
"notes": "Payment option : Through check",
111+
"created_time": "2013-08-05T12:06:10+0530",
112+
"last_modified_time": "2013-10-07T18:24:51+0530"
113+
}
114+
}

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

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 0 additions & 20 deletions
This file was deleted.

components/zoho_books/sources/new-expense/new-expense.mjs renamed to components/zoho_books/sources/new-expense-instant/new-expense-instant.mjs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@ import sampleEmit from "./test-event.mjs";
33

44
export default {
55
...common,
6-
key: "zoho_books-new-expense",
7-
name: "New Expense",
6+
key: "zoho_books-new-expense-instant",
7+
name: "New Expense (Instant)",
88
description: "Emit new event when a new expense is created.",
99
version: "0.0.1",
1010
type: "source",
1111
dedupe: "unique",
1212
methods: {
1313
...common.methods,
14-
getFunction() {
15-
return this.zohoBooks.listExpenses;
14+
getEntity() {
15+
return "expense";
1616
},
17-
getFieldName() {
18-
return "expenses";
19-
},
20-
getFieldId() {
21-
return "expense_id";
22-
},
23-
getSummary(item) {
24-
return `New Expense: ${item.expense_id}`;
17+
generateMeta({ expense }) {
18+
return {
19+
id: expense.expense_id,
20+
summary: `New Expense: ${expense.expense_id}`,
21+
ts: Date.parse(expense.created_time),
22+
};
2523
},
2624
},
2725
sampleEmit,

0 commit comments

Comments
 (0)