Skip to content

Commit abd2ffe

Browse files
committed
Creating polling sources
1 parent 88f157c commit abd2ffe

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

components/sage_accounting/sage_accounting.app.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,5 +456,23 @@ export default {
456456
...args,
457457
});
458458
},
459+
async listProducts(args) {
460+
return this._paginatedRequest({
461+
path: "/products",
462+
...args,
463+
});
464+
},
465+
async listServices(args) {
466+
return this._paginatedRequest({
467+
path: "/services",
468+
...args,
469+
});
470+
},
471+
async listContactPayments(args) {
472+
return this._paginatedRequest({
473+
path: "/contact_payments",
474+
...args,
475+
});
476+
},
459477
},
460478
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import sageAccounting from "../../sage_accounting.app.mjs";
3+
4+
export default {
5+
props: {
6+
sageAccounting,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
},
15+
methods: {
16+
_getSavedIds() {
17+
return this.db.get("savedIds") || [];
18+
},
19+
_setSavedIds(ids) {
20+
this.db.set("savedIds", ids);
21+
},
22+
async startEvent(maxItems) {
23+
const savedIds = this._getSavedIds();
24+
const items = await this.getItems();
25+
26+
const newIds = [];
27+
for (const item of items) {
28+
const id = this.getItemId(item);
29+
if (!savedIds.includes(id)) {
30+
const meta = this.generateMeta(item);
31+
if (maxItems === undefined || (typeof maxItems === "number" && --maxItems >= 0)) {
32+
this.$emit(item, meta);
33+
}
34+
newIds.push(id);
35+
}
36+
}
37+
38+
if (newIds.length > 0) {
39+
const ids = [
40+
...savedIds,
41+
...newIds,
42+
].slice(-100);
43+
this._setSavedIds(ids);
44+
}
45+
},
46+
},
47+
async run() {
48+
await this.startEvent();
49+
},
50+
hooks: {
51+
async deploy() {
52+
await this.startEvent(5);
53+
},
54+
},
55+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import common from "../common/polling.mjs";
2+
3+
export default {
4+
...common,
5+
key: "sage_accounting-new-contact-payment-created",
6+
name: "New Contact Payment Created",
7+
description: "Emit new event when a contact payment is created in Sage Accounting. [See the documentation](https://developer.sage.com/accounting/reference/payments/#tag/Contact-Payments/operation/getContactPayments)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
generateMeta(payment) {
14+
const id = this.getItemId(payment);
15+
const summary = this.getItemSummary(payment);
16+
return {
17+
id,
18+
summary: `New Contact Payment Created: ${summary}`,
19+
ts: Date.parse(payment.created_at) || Date.now(),
20+
};
21+
},
22+
getItemId(payment) {
23+
return payment.id;
24+
},
25+
getItemSummary(payment) {
26+
const contactName = payment.contact?.displayed_as || payment.contact?.name || "Unknown Contact";
27+
const amount = payment.total_amount || payment.net_amount || "Unknown Amount";
28+
return `${contactName} - ${amount}`;
29+
},
30+
async getItems() {
31+
const payments = await this.sageAccounting.listContactPayments({
32+
params: {
33+
items_per_page: 100,
34+
},
35+
});
36+
return payments || [];
37+
},
38+
},
39+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import common from "../common/polling.mjs";
2+
3+
export default {
4+
...common,
5+
key: "sage_accounting-new-product-created",
6+
name: "New Product Created",
7+
description: "Emit new event when a product is created in Sage Accounting. [See the documentation](https://developer.sage.com/accounting/reference/products-services/#tag/Products/operation/getProducts)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
generateMeta(product) {
14+
const id = this.getItemId(product);
15+
const summary = this.getItemSummary(product);
16+
return {
17+
id,
18+
summary: `New Product Created: ${summary}`,
19+
ts: Date.parse(product.created_at) || Date.now(),
20+
};
21+
},
22+
getItemId(product) {
23+
return product.id;
24+
},
25+
getItemSummary(product) {
26+
return product.description || product.displayed_as || product.id;
27+
},
28+
async getItems() {
29+
const products = await this.sageAccounting.listProducts({
30+
params: {
31+
items_per_page: 100,
32+
},
33+
});
34+
return products || [];
35+
},
36+
},
37+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import common from "../common/polling.mjs";
2+
3+
export default {
4+
...common,
5+
key: "sage_accounting-new-service-created",
6+
name: "New Service Created",
7+
description: "Emit new event when a service is created in Sage Accounting. [See the documentation](https://developer.sage.com/accounting/reference/products-services/#tag/Services/operation/getServices)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
generateMeta(service) {
14+
const id = this.getItemId(service);
15+
const summary = this.getItemSummary(service);
16+
return {
17+
id,
18+
summary: `New Service Created: ${summary}`,
19+
ts: Date.parse(service.created_at) || Date.now(),
20+
};
21+
},
22+
getItemId(service) {
23+
return service.id;
24+
},
25+
getItemSummary(service) {
26+
return service.description || service.displayed_as || service.id;
27+
},
28+
async getItems() {
29+
const services = await this.sageAccounting.listServices({
30+
params: {
31+
items_per_page: 100,
32+
},
33+
});
34+
return services || [];
35+
},
36+
},
37+
};

0 commit comments

Comments
 (0)