diff --git a/components/upviral/actions/add-contact/add-contact.mjs b/components/upviral/actions/add-contact/add-contact.mjs new file mode 100644 index 0000000000000..5e2fff791b713 --- /dev/null +++ b/components/upviral/actions/add-contact/add-contact.mjs @@ -0,0 +1,105 @@ +import { ConfigurationError } from "@pipedream/platform"; +import FormData from "form-data"; +import upviral from "../../upviral.app.mjs"; + +export default { + key: "upviral-add-contact", + name: "Add Contact", + version: "0.0.1", + description: "Add a new contact in your particular campaign. [See the documentation](https://api.upviral.com/#add-contact)", + type: "action", + props: { + upviral, + campaignId: { + propDefinition: [ + upviral, + "campaignId", + ], + reloadProps: true, + }, + email: { + type: "string", + label: "Email", + description: "The user's email address.", + }, + name: { + type: "string", + label: "Name", + description: "The user's name.", + optional: true, + }, + ipAddress: { + type: "string", + label: "IP Address", + description: "The user's IP Address.", + optional: true, + }, + referralCode: { + type: "string", + label: "Referral Code", + description: "The unique referral code (Last part of unique Referral URL). For example - https://upvir.al/ref/XXXX, Referral Code will be XXXX.This will be the referral code of the person who referred this new contact. The original participant will get the credit for this new contact.", + optional: true, + }, + }, + async additionalProps() { + const props = {}; + if (this.campaignId) { + let count = 0; + let data = new FormData(); + data.append("campaign_id", this.campaignId); + + const { custom_fields: customFields } = await this.upviral.listCustomFields({ + data, + headers: data.getHeaders(), + }); + + for (const field of customFields) { + props[`customField-${field}`] = { + type: "string", + label: field, + description: `Custom field ${++count}`, + optional: true, + }; + } + } + return props; + }, + methods: { + parseCustomFields() { + const customFields = Object.entries(this).filter(([ + key, + ]) => key.includes("customField-")) + .map(([ + key, + value, + ]) => ([ + key.split("-")[1], + value, + ])); + + return JSON.stringify(Object.fromEntries(customFields)) || null; + }, + }, + async run({ $ }) { + var bodyFormData = new FormData(); + bodyFormData.append("campaign_id", this.campaignId); + bodyFormData.append("email", this.email); + if (this.name) bodyFormData.append("name", this.name); + if (this.ipAddress) bodyFormData.append("ip_address", this.ipAddress); + if (this.referralCode) bodyFormData.append("referral_code", this.referralCode); + bodyFormData.append("custom_fields", this.parseCustomFields() ); + + const response = await this.upviral.addContact({ + $, + data: bodyFormData, + headers: bodyFormData.getHeaders(), + }); + + if (response.result === "error") { + throw new ConfigurationError(response.message); + } + + $.export("$summary", `A new contact with UID: '${response.uid}' was successfully created!`); + return response; + }, +}; diff --git a/components/upviral/actions/add-points/add-points.mjs b/components/upviral/actions/add-points/add-points.mjs new file mode 100644 index 0000000000000..27dc11b028e60 --- /dev/null +++ b/components/upviral/actions/add-points/add-points.mjs @@ -0,0 +1,51 @@ +import { ConfigurationError } from "@pipedream/platform"; +import FormData from "form-data"; +import upviral from "../../upviral.app.mjs"; + +export default { + key: "upviral-add-points", + name: "Add Points", + version: "0.0.1", + description: "Add points in user profile. [See the documentation](https://api.upviral.com/#add-points)", + type: "action", + props: { + upviral, + campaignId: { + propDefinition: [ + upviral, + "campaignId", + ], + }, + leadId: { + propDefinition: [ + upviral, + "leadId", + ({ campaignId }) => ({ + campaignId, + }), + ], + }, + points: { + type: "string", + label: "Points", + description: "The points to add.", + }, + }, + async run({ $ }) { + var bodyFormData = new FormData(); + bodyFormData.append("campaign_id", this.campaignId); + bodyFormData.append("lead_id", this.leadId); + bodyFormData.append("points", this.points); + + const response = await this.upviral.addPoints({ + $, + data: bodyFormData, + headers: bodyFormData.getHeaders(), + }); + + if (response.result === "error") throw new ConfigurationError(response.message); + + $.export("$summary", `${this.points} points were successfully added!`); + return response; + }, +}; diff --git a/components/upviral/common/constants.mjs b/components/upviral/common/constants.mjs new file mode 100644 index 0000000000000..ea830c15a04cb --- /dev/null +++ b/components/upviral/common/constants.mjs @@ -0,0 +1 @@ +export const LIMIT = 100; diff --git a/components/upviral/package.json b/components/upviral/package.json new file mode 100644 index 0000000000000..acc3df365493d --- /dev/null +++ b/components/upviral/package.json @@ -0,0 +1,19 @@ +{ + "name": "@pipedream/upviral", + "version": "0.1.0", + "description": "Pipedream UpViral Components", + "main": "upviral.app.mjs", + "keywords": [ + "pipedream", + "upviral" + ], + "homepage": "https://pipedream.com/apps/upviral", + "author": "Pipedream (https://pipedream.com/)", + "publishConfig": { + "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^1.5.1", + "form-data": "^4.0.0" + } +} diff --git a/components/upviral/upviral.app.mjs b/components/upviral/upviral.app.mjs index 4e443c7110a33..4698f7acfdf21 100644 --- a/components/upviral/upviral.app.mjs +++ b/components/upviral/upviral.app.mjs @@ -1,11 +1,135 @@ +import { axios } from "@pipedream/platform"; +import FormData from "form-data"; +import { LIMIT } from "./common/constants.mjs"; + export default { type: "app", app: "upviral", - propDefinitions: {}, + propDefinitions: { + campaignId: { + type: "string", + label: "Campaign Id", + description: "The Id of the campaign.", + async options() { + const { data } = await this.listCampaigns({}); + + return data.map(({ + id: value, name: label, + }) => ({ + label, + value, + })); + }, + }, + leadId: { + type: "string", + label: "Lead Id", + description: "The Id of the lead.", + async options({ + page, campaignId, + }) { + var formData = new FormData(); + formData.append("campaign_id", campaignId); + const { data } = await this.listLeads({ + params: { + start: LIMIT * page, + size: LIMIT, + }, + data: formData, + headers: formData.getHeaders(), + }); + + return data.leads.map(({ + id: value, email: label, + }) => ({ + label, + value, + })); + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _apiKey() { + return this.$auth.api_key; + }, + _apiUrl() { + return "https://app.upviral.com/api/v1"; + }, + _getParams(params) { + return { + uvapikey: this._apiKey(), + ...params, + }; + }, + _makeRequest({ + $ = this, params, ...opts + }) { + const config = { + url: `${this._apiUrl()}`, + params: this._getParams(params), + ...opts, + }; + + return axios($, config); + }, + addContact({ + params, ...args + }) { + return this._makeRequest({ + method: "POST", + params: { + uvmethod: "add_contact", + ...params, + }, + ...args, + }); + }, + addPoints({ + params, ...args + }) { + return this._makeRequest({ + method: "POST", + params: { + uvmethod: "add_points", + ...params, + }, + ...args, + }); + }, + listCampaigns({ + params = {}, ...args + }) { + return this._makeRequest({ + params: { + uvmethod: "lists", + ...params, + }, + ...args, + }); + }, + listCustomFields({ + params, ...args + }) { + return this._makeRequest({ + method: "POST", + params: { + uvmethod: "get_custom_fields", + ...params, + }, + ...args, + }); + }, + listLeads({ + params, ...args + }) { + return this._makeRequest({ + method: "POST", + params: { + uvmethod: "get_leads", + ...params, + }, + ...args, + }); }, }, }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 78c4221397897..fd553a249806a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6106,6 +6106,14 @@ importers: components/uptimerobot: specifiers: {} + components/upviral: + specifiers: + '@pipedream/platform': ^1.5.1 + form-data: ^4.0.0 + dependencies: + '@pipedream/platform': 1.5.1 + form-data: 4.0.0 + components/upwave: specifiers: {}