From f4dc25d00a4c46365b504aeb0188a1469b45cb5c Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Mon, 6 May 2024 10:05:33 -0400 Subject: [PATCH] New Components - relavate (#11803) * relavate init * new components * pnpm-lock.yaml * fix typo --- .../create-affiliate-lead.mjs | 100 ++++++++++++++++++ components/relavate/package.json | 7 +- components/relavate/relavate.app.mjs | 98 ++++++++++++++++- components/relavate/sources/common/base.mjs | 57 ++++++++++ .../new-affiliate-campaign.mjs | 22 ++++ .../new-affiliate-campaign/test-event.mjs | 15 +++ .../sources/new-referral/new-referral.mjs | 39 +++++++ .../sources/new-referral/test-event.mjs | 19 ++++ pnpm-lock.yaml | 5 +- 9 files changed, 354 insertions(+), 8 deletions(-) create mode 100644 components/relavate/actions/create-affiliate-lead/create-affiliate-lead.mjs create mode 100644 components/relavate/sources/common/base.mjs create mode 100644 components/relavate/sources/new-affiliate-campaign/new-affiliate-campaign.mjs create mode 100644 components/relavate/sources/new-affiliate-campaign/test-event.mjs create mode 100644 components/relavate/sources/new-referral/new-referral.mjs create mode 100644 components/relavate/sources/new-referral/test-event.mjs diff --git a/components/relavate/actions/create-affiliate-lead/create-affiliate-lead.mjs b/components/relavate/actions/create-affiliate-lead/create-affiliate-lead.mjs new file mode 100644 index 0000000000000..b2cf19aab6b6e --- /dev/null +++ b/components/relavate/actions/create-affiliate-lead/create-affiliate-lead.mjs @@ -0,0 +1,100 @@ +import relavate from "../../relavate.app.mjs"; + +export default { + key: "relavate-create-affiliate-lead", + name: "Create Affiliate Lead", + description: "This component enables you to create a new affiliate lead for marketing. [See the documentation](https://api.relavate.co/#2a307851-9d54-42ea-9f54-3fb600b152a5)", + version: "0.0.1", + type: "action", + props: { + relavate, + campaignId: { + propDefinition: [ + relavate, + "campaignId", + ], + reloadProps: true, + }, + status: { + type: "string", + label: "Status", + description: "The status of the lead being created", + options: [ + "Pending", + "Won", + ], + }, + client: { + type: "string", + label: "Client", + description: "The company or name of the client", + }, + }, + async additionalProps() { + const props = {}; + const partnerType = this.relavate.getPartnerType(); + if (partnerType === "partner") { + props.vendorId = { + type: "string", + label: "Vendor ID", + description: "The ID of the vendor", + options: async () => await this.getVendorIdPropOptions(), + }; + } + if (partnerType === "vendor") { + props.partnerId = { + type: "string", + label: "Partner ID", + description: "The ID of the partner", + options: async () => await this.getPartnerIdPropOptions(), + }; + } + return props; + }, + methods: { + async getVendorIdPropOptions() { + const vendors = await this.relavate.listVendors(); + const vendorIds = vendors?.map(({ vendorID }) => vendorID ); + const options = []; + for (const vendorId of vendorIds) { + const { name } = await this.relavate.getVendor({ + vendorId, + }); + options.push({ + value: vendorId, + label: name, + }); + } + return options; + }, + async getPartnerIdPropOptions() { + const partners = await this.relavate.listPartners(); + const partnerIds = partners?.map(({ partnerID }) => partnerID ); + const options = []; + for (const partnerId of partnerIds) { + const { name } = await this.relavate.getPartner({ + partnerId, + }); + options.push({ + value: partnerId, + label: name, + }); + } + return options; + }, + }, + async run({ $ }) { + const response = await this.relavate.createAffiliateLead({ + $, + params: { + campaignID: this.campaignId, + client: this.client, + status: this.status, + vendorID: this.vendorId, + partnerID: this.partnerId, + }, + }); + $.export("$summary", `Created affiliate lead with ID: ${response.id}`); + return response; + }, +}; diff --git a/components/relavate/package.json b/components/relavate/package.json index 9bbaf81006d6a..c3b9d59277f2b 100644 --- a/components/relavate/package.json +++ b/components/relavate/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/relavate", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Relavate Components", "main": "relavate.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^1.6.5" } -} \ No newline at end of file +} diff --git a/components/relavate/relavate.app.mjs b/components/relavate/relavate.app.mjs index 758274fee0919..3b2d691a52c37 100644 --- a/components/relavate/relavate.app.mjs +++ b/components/relavate/relavate.app.mjs @@ -1,11 +1,99 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "relavate", - propDefinitions: {}, + propDefinitions: { + campaignId: { + type: "string", + label: "Campaign ID", + description: "The ID of the campaign", + async options() { + const campaigns = await this.listCampaigns(); + return campaigns?.map(({ + id: value, name: label, + }) => ({ + value, + label, + })) || []; + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://app.relavate.co/api"; + }, + _makeRequest(opts = {}) { + const { + $ = this, + path, + ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + url: `${this._baseUrl()}${path}`, + headers: { + "X-key": this.$auth.api_key, + "X-secret": this.$auth.api_secret, + "X-partnerType": this.$auth.partner_type, + }, + }); + }, + getPartnerType() { + return this.$auth.partner_type; + }, + getVendor({ + vendorId, ...opts + }) { + return this._makeRequest({ + path: `/vendors/${vendorId}`, + ...opts, + }); + }, + getPartner({ + partnerId, ...opts + }) { + return this._makeRequest({ + path: `/partners/${partnerId}`, + ...opts, + }); + }, + listVendors(opts = {}) { + return this._makeRequest({ + path: "/partners/vendors/all", + ...opts, + }); + }, + listPartners(opts = {}) { + return this._makeRequest({ + path: "/vendors/partners/all", + ...opts, + }); + }, + listCampaigns(opts = {}) { + return this._makeRequest({ + path: "/affiliate-campaigns", + ...opts, + }); + }, + listAffiliateLeads(opts = {}) { + return this._makeRequest({ + path: "/affiliate-leads", + ...opts, + }); + }, + listReferrals(opts = {}) { + return this._makeRequest({ + path: "/referrals", + ...opts, + }); + }, + createAffiliateLead(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/affiliate-leads", + ...opts, + }); }, }, -}; \ No newline at end of file +}; diff --git a/components/relavate/sources/common/base.mjs b/components/relavate/sources/common/base.mjs new file mode 100644 index 0000000000000..ecdedc29dfc1a --- /dev/null +++ b/components/relavate/sources/common/base.mjs @@ -0,0 +1,57 @@ +import relavate from "../../relavate.app.mjs"; +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; + +export default { + props: { + relavate, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + methods: { + _getStartDate() { + return this.db.get("startDate") || this._today(); + }, + _setStartDate(startDate) { + this.db.set("startDate", startDate); + }, + _today() { + return new Date().toISOString() + .split("T")[0]; + }, + generateMeta(item) { + return { + id: item.id, + summary: this.getSummary(item), + ts: Date.parse(item.created_at), + }; + }, + getParams() { + return {}; + }, + getResourceFn() { + throw new Error("getResourceFn is not implemented"); + }, + getSummary() { + throw new Error("getSummary is not implemented"); + }, + }, + async run() { + const resourceFn = this.getResourceFn(); + const params = this.getParams(); + const results = await resourceFn({ + params, + }); + if (!results?.length) { + return; + } + for (const item of results) { + const meta = this.generateMeta(item); + this.$emit(item, meta); + } + }, +}; diff --git a/components/relavate/sources/new-affiliate-campaign/new-affiliate-campaign.mjs b/components/relavate/sources/new-affiliate-campaign/new-affiliate-campaign.mjs new file mode 100644 index 0000000000000..9b8ba0c3e4891 --- /dev/null +++ b/components/relavate/sources/new-affiliate-campaign/new-affiliate-campaign.mjs @@ -0,0 +1,22 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + type: "source", + key: "relavate-new-affiliate-campaign", + name: "New Affiliate Campaign", + description: "Emit new event when a new affiliate campaign is created.", + version: "0.0.1", + dedupe: "unique", + methods: { + ...common.methods, + getResourceFn() { + return this.relavate.listCampaigns; + }, + getSummary(campaign) { + return `New campaign: ${campaign.name}`; + }, + }, + sampleEmit, +}; diff --git a/components/relavate/sources/new-affiliate-campaign/test-event.mjs b/components/relavate/sources/new-affiliate-campaign/test-event.mjs new file mode 100644 index 0000000000000..ed0d08473883d --- /dev/null +++ b/components/relavate/sources/new-affiliate-campaign/test-event.mjs @@ -0,0 +1,15 @@ +export default { + "id": 4, + "partnerID": 2, + "vendorID": 4, + "name": "Tracking link campaign", + "type": "Link", + "created_at": "2023-10-17T10:23:37.000000Z", + "updated_at": "2023-10-17T10:23:37.000000Z", + "status": "Active", + "campaignBaseURL": "https://nichols.com", + "campaignUrlPath": null, + "linkFormat": "vendorWebsite", + "couponCode": null, + "subtitle": null +} \ No newline at end of file diff --git a/components/relavate/sources/new-referral/new-referral.mjs b/components/relavate/sources/new-referral/new-referral.mjs new file mode 100644 index 0000000000000..2e29ae0207d99 --- /dev/null +++ b/components/relavate/sources/new-referral/new-referral.mjs @@ -0,0 +1,39 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "relavate-new-referral", + name: "New Referral", + description: "Emit new event when a new referral is created in Relavate", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + _getStartDate() { + return this.db.get("startDate") || this._today(); + }, + _setStartDate(startDate) { + this.db.set("startDate", startDate); + }, + _today() { + return new Date().toISOString() + .split("T")[0]; + }, + getResourceFn() { + return this.relavate.listReferrals; + }, + getParams() { + const startDate = this._getStartDate(); + this._setStartDate(this._today()); + return { + startDate, + }; + }, + getSummary(referral) { + return `New referral with ID ${referral.id}`; + }, + }, + sampleEmit, +}; diff --git a/components/relavate/sources/new-referral/test-event.mjs b/components/relavate/sources/new-referral/test-event.mjs new file mode 100644 index 0000000000000..9a2b1a30030c5 --- /dev/null +++ b/components/relavate/sources/new-referral/test-event.mjs @@ -0,0 +1,19 @@ +export default { + "id": 4, + "vendorID": 4, + "partnerID": 2, + "company": "Sample Company", + "status": "New", + "revenue": 5000, + "frequency": "Annual", + "dateAdded": "2023-05-20", + "comment": "{\"ops\":[{\"insert\":\"This is a sample comment\"}]}", + "clientContactID": 7, + "created_at": "2023-10-17T09:52:11.000000Z", + "updated_at": "2023-10-17T09:54:28.000000Z", + "wonDate": null, + "lostDate": "2023-10-17", + "annualRevenue": 5000, + "lostID": 10, + "comment_text": "This is a sample comment" +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 49ff92b0cd5e8..95bbf06fae3c0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6882,7 +6882,10 @@ importers: '@pipedream/platform': 1.6.4 components/relavate: - specifiers: {} + specifiers: + '@pipedream/platform': ^1.6.5 + dependencies: + '@pipedream/platform': 1.6.5 components/relevance_ai: specifiers: