-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathnew-automation-webhook.mjs
91 lines (81 loc) · 2.2 KB
/
new-automation-webhook.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
import activecampaign from "../../activecampaign.app.mjs";
import common from "../common/base.mjs";
export default {
...common,
name: "New Automation Webhook",
key: "activecampaign-new-automation-webhook",
description: "Emit new event each time an automation sends out webhook data.",
version: "0.0.7",
type: "source",
dedupe: "unique",
props: {
...common.props,
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
automations: {
propDefinition: [
activecampaign,
"automations",
],
},
},
methods: {
isWatchedAutomation(automation) {
return (
this.automations?.length === 0 ||
this.automations?.includes(automation.id)
);
},
isAutomationRelevant(automation) {
if (!this.isWatchedAutomation(automation)) {
return false;
}
const entered = this.db.get(automation.id) || 0; // number of times automation has run
if (parseInt(automation.entered) <= entered) {
return false;
}
this.db.set(automation.id, parseInt(automation.entered));
return true;
},
getMeta(automation) {
return {
id: `${automation.id}${automation.entered}`,
summary: automation.name,
ts: Date.now(),
};
},
},
async run() {
let resources = [];
let offset = 0;
let total = 1;
do {
const response =
await this.activecampaign.paginateResources({
requestFn: this.activecampaign.listAutomations,
requestArgs: {
params: {
offset,
},
},
resourceName: "automations",
mapper: (resource) => resource,
});
const { options: nextResources } = response;
({
offset, total,
} = response.context);
resources = resources.concat(nextResources);
resources.forEach((resource) => {
if (this.isAutomationRelevant(resource)) {
this.$emit(resource, this.getMeta(resource));
}
});
} while (resources.length < total);
},
};