diff --git a/components/growsurf/growsurf.app.mjs b/components/growsurf/growsurf.app.mjs index 6e22d5edc8535..7bdd3b9c87a99 100644 --- a/components/growsurf/growsurf.app.mjs +++ b/components/growsurf/growsurf.app.mjs @@ -1,11 +1,80 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "growsurf", - propDefinitions: {}, + propDefinitions: { + campaignId: { + type: "string", + label: "Campaign ID", + description: "The ID of a campaign", + async options() { + const { campaigns } = await this.listCampaigns(); + return campaigns?.map((campaign) => ({ + label: campaign.name, + value: campaign.id, + })) || []; + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://api.growsurf.com/v2"; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: `${this._baseUrl()}${path}`, + headers: { + "Authorization": `Bearer ${this.$auth.api_key}`, + }, + ...opts, + }); + }, + listCampaigns(opts = {}) { + return this._makeRequest({ + path: "/campaigns", + ...opts, + }); + }, + listParticipants({ + campaignId, ...opts + }) { + return this._makeRequest({ + path: `/campaign/${campaignId}/participants`, + ...opts, + }); + }, + listReferrals({ + campaignId, ...opts + }) { + return this._makeRequest({ + path: `/campaign/${campaignId}/referrals`, + ...opts, + }); + }, + async *paginate({ + fn, args, resourceKey, max, + }) { + args.params = args.params || {}; + let hasMore = true; + let count = 0; + do { + const response = await fn(args); + const items = response[resourceKey]; + if (!items?.length) { + return; + } + for (const item of items) { + yield item; + if (max && ++count >= max) { + return; + } + } + hasMore = response.nextId; + args.params.nextId = response.nextId; + } while (hasMore); }, }, }; diff --git a/components/growsurf/package.json b/components/growsurf/package.json index 48ea78cea06ae..de7b83539c58b 100644 --- a/components/growsurf/package.json +++ b/components/growsurf/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/growsurf", - "version": "0.0.4", + "version": "0.1.0", "description": "Pipedream Growsurf Components", "main": "growsurf.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.1.1" } } diff --git a/components/growsurf/sources/campaign-completed/campaign-completed.mjs b/components/growsurf/sources/campaign-completed/campaign-completed.mjs new file mode 100644 index 0000000000000..0959fedef4ae6 --- /dev/null +++ b/components/growsurf/sources/campaign-completed/campaign-completed.mjs @@ -0,0 +1,25 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "growsurf-campaign-completed", + name: "Campaign Completed", + description: "Emit new event when a campaign is completed. [See the documentation](https://docs.growsurf.com/developer-tools/rest-api/api-reference#get-campaigns)", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + async getResources() { + const { campaigns } = await this.growsurf.listCampaigns(); + return campaigns.filter((campaign) => campaign.status === "COMPLETE"); + }, + generateMeta(campaign) { + return { + id: campaign.id, + summary: `Campaign Completed: ${campaign.name}`, + ts: Date.now(), + }; + }, + }, +}; diff --git a/components/growsurf/sources/common/base.mjs b/components/growsurf/sources/common/base.mjs new file mode 100644 index 0000000000000..1b48eb133c2e7 --- /dev/null +++ b/components/growsurf/sources/common/base.mjs @@ -0,0 +1,49 @@ +import growsurf from "../../growsurf.app.mjs"; +import { + DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, ConfigurationError, +} from "@pipedream/platform"; + +export default { + props: { + growsurf, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + methods: { + _getLastTs() { + return this.db.get("lastTs") || 0; + }, + _setLastTs(lastTs) { + this.db.set("lastTs", lastTs); + }, + getResources() { + throw new ConfigurationError("getResources is not implemented"); + }, + generateMeta() { + throw new ConfigurationError("generateMeta is not implemented"); + }, + async processEvent(max) { + const results = await this.getResources(max); + if (!results.length) { + return; + } + for (const result of results) { + const meta = this.generateMeta(result); + this.$emit(result, meta); + } + }, + }, + hooks: { + async deploy() { + await this.processEvent(10); + }, + }, + async run() { + await this.processEvent(); + }, +}; diff --git a/components/growsurf/sources/new-campaign-referral-made/new-campaign-referral-made.mjs b/components/growsurf/sources/new-campaign-referral-made/new-campaign-referral-made.mjs new file mode 100644 index 0000000000000..5b7002477de22 --- /dev/null +++ b/components/growsurf/sources/new-campaign-referral-made/new-campaign-referral-made.mjs @@ -0,0 +1,58 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "growsurf-new-campaign-referral-made", + name: "New Campaign Referral Made", + 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)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + campaignId: { + propDefinition: [ + common.props.growsurf, + "campaignId", + ], + }, + }, + methods: { + ...common.methods, + async getResources(max) { + const results = this.growsurf.paginate({ + fn: this.growsurf.listReferrals, + args: { + campaignId: this.campaignId, + params: { + sortBy: "createdAt", + desc: true, + }, + }, + resourceKey: "referrals", + max, + }); + const referrals = []; + const lastTs = this._getLastTs(); + let maxTs = lastTs; + for await (const referral of results) { + const ts = referral.createdAt; + if (ts > lastTs) { + referrals.push(referral); + maxTs = Math.max(ts, maxTs); + } else { + break; + } + } + this._setLastTs(maxTs); + return referrals; + }, + generateMeta(referral) { + return { + id: referral.id, + summary: `New Campaign Referral Made: ${referral.email || referral.id}`, + ts: referral.createdAt, + }; + }, + }, +}; diff --git a/components/growsurf/sources/new-participant-added/new-participant-added.mjs b/components/growsurf/sources/new-participant-added/new-participant-added.mjs new file mode 100644 index 0000000000000..f00a576ef6132 --- /dev/null +++ b/components/growsurf/sources/new-participant-added/new-participant-added.mjs @@ -0,0 +1,61 @@ +import common from "../common/base.mjs"; + +export default { + ...common, + key: "growsurf-new-participant-added", + name: "New Participant Added", + 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)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + ...common.props, + campaignId: { + propDefinition: [ + common.props.growsurf, + "campaignId", + ], + }, + }, + methods: { + ...common.methods, + async getResources(max) { + const results = this.growsurf.paginate({ + fn: this.growsurf.listParticipants, + args: { + campaignId: this.campaignId, + params: { + limit: 100, + }, + }, + resourceKey: "participants", + }); + const participants = []; + const lastTs = this._getLastTs(); + let maxTs = lastTs; + for await (const participant of results) { + const ts = participant.createdAt; + if (ts > lastTs) { + participants.push(participant); + maxTs = Math.max(ts, maxTs); + } + } + this._setLastTs(maxTs); + if (max && participants.length > max) { + participants.length = max; + } + return participants; + }, + generateMeta(participant) { + const name = [ + participant.firstName, + participant.lastName, + ].filter(Boolean).join(" ") || participant.email || participant.id; + return { + id: participant.id, + summary: `New Participant Added: ${name}`, + ts: participant.createdAt, + }; + }, + }, +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f4b073c41f50d..a10f1bd3bcc9e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6589,7 +6589,11 @@ importers: components/groundhogg: {} - components/growsurf: {} + components/growsurf: + dependencies: + '@pipedream/platform': + specifier: ^3.1.1 + version: 3.1.1 components/gryd: dependencies: @@ -11156,8 +11160,7 @@ importers: components/pingrabbit: {} - components/pinpoint: - specifiers: {} + components/pinpoint: {} components/pinterest: dependencies: @@ -31715,17 +31718,17 @@ packages: superagent@3.8.1: resolution: {integrity: sha512-VMBFLYgFuRdfeNQSMLbxGSLfmXL/xc+OO+BZp41Za/NRDBet/BNbkRJrYzCUu0u4GU0i/ml2dtT8b9qgkw9z6Q==} engines: {node: '>= 4.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net superagent@4.1.0: resolution: {integrity: sha512-FT3QLMasz0YyCd4uIi5HNe+3t/onxMyEho7C3PSqmti3Twgy2rXT4fmkTz6wRL6bTF4uzPcfkUCa8u4JWHw8Ag==} engines: {node: '>= 6.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net superagent@5.3.1: resolution: {integrity: sha512-wjJ/MoTid2/RuGCOFtlacyGNxN9QLMgcpYLDQlWFIhhdJ93kNscFonGvrpAHSCVjRVj++DGCglocF7Aej1KHvQ==} engines: {node: '>= 7.0.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net supports-color@10.2.2: resolution: {integrity: sha512-SS+jx45GF1QjgEXQx4NJZV9ImqmO2NPz5FNsIHrsDjh2YsHnawpan7SNQ1o8NuhrbHZy9AZhIoCUiCeaW/C80g==}