Skip to content

Commit efd78b3

Browse files
michelle0927jcortes
authored andcommitted
GrowSurf - new components (#19361)
* new components * pnpm-lock.yaml * updates
1 parent 9d610f8 commit efd78b3

File tree

7 files changed

+275
-6
lines changed

7 files changed

+275
-6
lines changed
Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,80 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "growsurf",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
campaignId: {
8+
type: "string",
9+
label: "Campaign ID",
10+
description: "The ID of a campaign",
11+
async options() {
12+
const { campaigns } = await this.listCampaigns();
13+
return campaigns?.map((campaign) => ({
14+
label: campaign.name,
15+
value: campaign.id,
16+
})) || [];
17+
},
18+
},
19+
},
520
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
21+
_baseUrl() {
22+
return "https://api.growsurf.com/v2";
23+
},
24+
_makeRequest({
25+
$ = this, path, ...opts
26+
}) {
27+
return axios($, {
28+
url: `${this._baseUrl()}${path}`,
29+
headers: {
30+
"Authorization": `Bearer ${this.$auth.api_key}`,
31+
},
32+
...opts,
33+
});
34+
},
35+
listCampaigns(opts = {}) {
36+
return this._makeRequest({
37+
path: "/campaigns",
38+
...opts,
39+
});
40+
},
41+
listParticipants({
42+
campaignId, ...opts
43+
}) {
44+
return this._makeRequest({
45+
path: `/campaign/${campaignId}/participants`,
46+
...opts,
47+
});
48+
},
49+
listReferrals({
50+
campaignId, ...opts
51+
}) {
52+
return this._makeRequest({
53+
path: `/campaign/${campaignId}/referrals`,
54+
...opts,
55+
});
56+
},
57+
async *paginate({
58+
fn, args, resourceKey, max,
59+
}) {
60+
args.params = args.params || {};
61+
let hasMore = true;
62+
let count = 0;
63+
do {
64+
const response = await fn(args);
65+
const items = response[resourceKey];
66+
if (!items?.length) {
67+
return;
68+
}
69+
for (const item of items) {
70+
yield item;
71+
if (max && ++count >= max) {
72+
return;
73+
}
74+
}
75+
hasMore = response.nextId;
76+
args.params.nextId = response.nextId;
77+
} while (hasMore);
978
},
1079
},
1180
};

components/growsurf/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/growsurf",
3-
"version": "0.0.4",
3+
"version": "0.1.0",
44
"description": "Pipedream Growsurf Components",
55
"main": "growsurf.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <support@pipedream.com> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.1"
1417
}
1518
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "growsurf-campaign-completed",
6+
name: "Campaign Completed",
7+
description: "Emit new event when a campaign is completed. [See the documentation](https://docs.growsurf.com/developer-tools/rest-api/api-reference#get-campaigns)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
async getResources() {
14+
const { campaigns } = await this.growsurf.listCampaigns();
15+
return campaigns.filter((campaign) => campaign.status === "COMPLETE");
16+
},
17+
generateMeta(campaign) {
18+
return {
19+
id: campaign.id,
20+
summary: `Campaign Completed: ${campaign.name}`,
21+
ts: Date.now(),
22+
};
23+
},
24+
},
25+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import growsurf from "../../growsurf.app.mjs";
2+
import {
3+
DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, ConfigurationError,
4+
} from "@pipedream/platform";
5+
6+
export default {
7+
props: {
8+
growsurf,
9+
db: "$.service.db",
10+
timer: {
11+
type: "$.interface.timer",
12+
default: {
13+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
14+
},
15+
},
16+
},
17+
methods: {
18+
_getLastTs() {
19+
return this.db.get("lastTs") || 0;
20+
},
21+
_setLastTs(lastTs) {
22+
this.db.set("lastTs", lastTs);
23+
},
24+
getResources() {
25+
throw new ConfigurationError("getResources is not implemented");
26+
},
27+
generateMeta() {
28+
throw new ConfigurationError("generateMeta is not implemented");
29+
},
30+
async processEvent(max) {
31+
const results = await this.getResources(max);
32+
if (!results.length) {
33+
return;
34+
}
35+
for (const result of results) {
36+
const meta = this.generateMeta(result);
37+
this.$emit(result, meta);
38+
}
39+
},
40+
},
41+
hooks: {
42+
async deploy() {
43+
await this.processEvent(10);
44+
},
45+
},
46+
async run() {
47+
await this.processEvent();
48+
},
49+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "growsurf-new-campaign-referral-made",
6+
name: "New Campaign Referral Made",
7+
description: "Emit new event when a new referral is made for a campaign. [See the documentation](https://docs.growsurf.com/developer-tools/rest-api/api-reference#get-referrals-and-invites)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
...common.props,
13+
campaignId: {
14+
propDefinition: [
15+
common.props.growsurf,
16+
"campaignId",
17+
],
18+
},
19+
},
20+
methods: {
21+
...common.methods,
22+
async getResources(max) {
23+
const results = this.growsurf.paginate({
24+
fn: this.growsurf.listReferrals,
25+
args: {
26+
campaignId: this.campaignId,
27+
params: {
28+
sortBy: "createdAt",
29+
desc: true,
30+
},
31+
},
32+
resourceKey: "referrals",
33+
max,
34+
});
35+
const referrals = [];
36+
const lastTs = this._getLastTs();
37+
let maxTs = lastTs;
38+
for await (const referral of results) {
39+
const ts = referral.createdAt;
40+
if (ts > lastTs) {
41+
referrals.push(referral);
42+
maxTs = Math.max(ts, maxTs);
43+
} else {
44+
break;
45+
}
46+
}
47+
this._setLastTs(maxTs);
48+
return referrals;
49+
},
50+
generateMeta(referral) {
51+
return {
52+
id: referral.id,
53+
summary: `New Campaign Referral Made: ${referral.email || referral.id}`,
54+
ts: referral.createdAt,
55+
};
56+
},
57+
},
58+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import common from "../common/base.mjs";
2+
3+
export default {
4+
...common,
5+
key: "growsurf-new-participant-added",
6+
name: "New Participant Added",
7+
description: "Emit new event when a new participant is added to a campaign. [See the documentation](https://docs.growsurf.com/developer-tools/rest-api/api-reference#get-participants)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
...common.props,
13+
campaignId: {
14+
propDefinition: [
15+
common.props.growsurf,
16+
"campaignId",
17+
],
18+
},
19+
},
20+
methods: {
21+
...common.methods,
22+
async getResources(max) {
23+
const results = this.growsurf.paginate({
24+
fn: this.growsurf.listParticipants,
25+
args: {
26+
campaignId: this.campaignId,
27+
params: {
28+
limit: 100,
29+
},
30+
},
31+
resourceKey: "participants",
32+
});
33+
const participants = [];
34+
const lastTs = this._getLastTs();
35+
let maxTs = lastTs;
36+
for await (const participant of results) {
37+
const ts = participant.createdAt;
38+
if (ts > lastTs) {
39+
participants.push(participant);
40+
maxTs = Math.max(ts, maxTs);
41+
}
42+
}
43+
this._setLastTs(maxTs);
44+
if (max && participants.length > max) {
45+
participants.length = max;
46+
}
47+
return participants;
48+
},
49+
generateMeta(participant) {
50+
const name = [
51+
participant.firstName,
52+
participant.lastName,
53+
].filter(Boolean).join(" ") || participant.email || participant.id;
54+
return {
55+
id: participant.id,
56+
summary: `New Participant Added: ${name}`,
57+
ts: participant.createdAt,
58+
};
59+
},
60+
},
61+
};

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)