From 11973195e081b20338e5bfed55e3fb28ce82ef18 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Fri, 5 Jul 2024 12:24:20 -0300 Subject: [PATCH 1/4] poper init --- components/poper/package.json | 2 +- components/poper/poper.app.mjs | 69 +++++++++++++++++- .../poper/sources/new-lead/new-lead.mjs | 72 +++++++++++++++++++ 3 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 components/poper/sources/new-lead/new-lead.mjs diff --git a/components/poper/package.json b/components/poper/package.json index 28cd5ea9e7bc7..23751a27a1eae 100644 --- a/components/poper/package.json +++ b/components/poper/package.json @@ -12,4 +12,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +} diff --git a/components/poper/poper.app.mjs b/components/poper/poper.app.mjs index 9d746afab567c..6a381e5734b42 100644 --- a/components/poper/poper.app.mjs +++ b/components/poper/poper.app.mjs @@ -1,11 +1,76 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "poper", - propDefinitions: {}, + propDefinitions: { + poperId: { + type: "string", + label: "Poper ID", + description: "The ID of the Poper popup", + async options() { + const popups = await this.listPopups(); + return popups.map((popup) => ({ + label: popup.name, + value: popup.id, + })); + }, + }, + apiKey: { + type: "string", + label: "API Key", + description: "Your Poper API Key", + secret: true, + }, + }, methods: { // this.$auth contains connected account data authKeys() { console.log(Object.keys(this.$auth)); }, + _baseUrl() { + return "https://api.poper.ai/general/v1"; + }, + async _makeRequest(opts = {}) { + const { + $ = this, + method = "POST", + path = "/", + headers, + ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + method, + url: this._baseUrl() + path, + headers: { + ...headers, + "Content-Type": "application/x-www-form-urlencoded", + }, + }); + }, + async listPopups() { + return this._makeRequest({ + path: "/popup/list", + data: `api_key=${this.$auth.api_key}`, + }); + }, + async getPopupResponses({ poperId }) { + return this._makeRequest({ + path: "/popup/responses", + data: `api_key=${this.$auth.api_key}&popup_id=${poperId}`, + }); + }, + async emitNewLeadEvent({ poperId }) { + const responses = await this.getPopupResponses({ + poperId, + }); + responses.responses.forEach((response) => { + this.$emit(response, { + summary: `New lead from Poper ID: ${poperId}`, + id: response.id, + }); + }); + }, }, -}; \ No newline at end of file +}; diff --git a/components/poper/sources/new-lead/new-lead.mjs b/components/poper/sources/new-lead/new-lead.mjs new file mode 100644 index 0000000000000..8e947826723af --- /dev/null +++ b/components/poper/sources/new-lead/new-lead.mjs @@ -0,0 +1,72 @@ +import { axios } from "@pipedream/platform"; +import poper from "../../poper.app.mjs"; + +export default { + key: "poper-new-lead", + name: "New Lead from Poper Popup", + description: "Emit new event when a new lead is obtained from Poper popups. [See the documentation](https://help.poper.ai/portal/en/kb/articles/view-popup-responses)", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + poper, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: 60, + }, + }, + poperId: { + propDefinition: [ + poper, + "poperId", + ], + }, + }, + methods: { + _getLastLeadId() { + return this.db.get("lastLeadId"); + }, + _setLastLeadId(id) { + this.db.set("lastLeadId", id); + }, + }, + hooks: { + async deploy() { + await this.emitNewLeads(); + }, + async activate() { + await this.emitNewLeads(); + }, + async deactivate() { + // No specific deactivation logic required + }, + }, + async run() { + await this.emitNewLeads(); + }, + async emitNewLeads() { + const poperId = this.poperId; + const responses = await this.poper.getPopupResponses({ + poperId, + }); + const lastLeadId = this._getLastLeadId(); + let newLastLeadId = lastLeadId; + + for (const response of responses.responses) { + if (!lastLeadId || response.id > lastLeadId) { + this.$emit(response, { + summary: `New lead from Poper ID: ${poperId}`, + id: response.id, + ts: Date.now(), + }); + newLastLeadId = response.id; + } + } + + if (newLastLeadId !== lastLeadId) { + this._setLastLeadId(newLastLeadId); + } + }, +}; From ff349c40d3410b734666e4de8518a3d685a7272f Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Fri, 5 Jul 2024 14:36:26 -0300 Subject: [PATCH 2/4] [Components] poper #12738 Sources - New Lead --- components/poper/package.json | 6 +- components/poper/poper.app.mjs | 61 +++++--------- .../poper/sources/new-lead/new-lead.mjs | 83 ++++++++++--------- .../poper/sources/new-lead/test-event.mjs | 24 ++++++ 4 files changed, 93 insertions(+), 81 deletions(-) create mode 100644 components/poper/sources/new-lead/test-event.mjs diff --git a/components/poper/package.json b/components/poper/package.json index 23751a27a1eae..ef2ebcffcb8e4 100644 --- a/components/poper/package.json +++ b/components/poper/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/poper", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Poper Components", "main": "poper.app.mjs", "keywords": [ @@ -11,5 +11,9 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.0" } } + diff --git a/components/poper/poper.app.mjs b/components/poper/poper.app.mjs index 6a381e5734b42..b6798dc2cf74b 100644 --- a/components/poper/poper.app.mjs +++ b/components/poper/poper.app.mjs @@ -9,67 +9,48 @@ export default { label: "Poper ID", description: "The ID of the Poper popup", async options() { - const popups = await this.listPopups(); - return popups.map((popup) => ({ - label: popup.name, - value: popup.id, + const { popups } = await this.listPopups(); + return popups.map(({ + id: value, name: label, + }) => ({ + label, + value, })); }, }, - apiKey: { - type: "string", - label: "API Key", - description: "Your Poper API Key", - secret: true, - }, }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); - }, _baseUrl() { return "https://api.poper.ai/general/v1"; }, - async _makeRequest(opts = {}) { - const { - $ = this, - method = "POST", - path = "/", - headers, - ...otherOpts - } = opts; + _data(data = {}) { + return { + ...data, + api_key: `${this.$auth.api_key}`, + }; + }, + _makeRequest({ + $ = this, path, data, ...opts + }) { return axios($, { - ...otherOpts, - method, + method: "POST", url: this._baseUrl() + path, headers: { - ...headers, "Content-Type": "application/x-www-form-urlencoded", }, + data: this._data(data), + ...opts, }); }, - async listPopups() { + listPopups() { return this._makeRequest({ path: "/popup/list", - data: `api_key=${this.$auth.api_key}`, }); }, - async getPopupResponses({ poperId }) { + listPoperResponses(opts = {}) { return this._makeRequest({ path: "/popup/responses", - data: `api_key=${this.$auth.api_key}&popup_id=${poperId}`, - }); - }, - async emitNewLeadEvent({ poperId }) { - const responses = await this.getPopupResponses({ - poperId, - }); - responses.responses.forEach((response) => { - this.$emit(response, { - summary: `New lead from Poper ID: ${poperId}`, - id: response.id, - }); + ...opts, }); }, }, diff --git a/components/poper/sources/new-lead/new-lead.mjs b/components/poper/sources/new-lead/new-lead.mjs index 8e947826723af..a8817a4daf2aa 100644 --- a/components/poper/sources/new-lead/new-lead.mjs +++ b/components/poper/sources/new-lead/new-lead.mjs @@ -1,20 +1,22 @@ -import { axios } from "@pipedream/platform"; +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; import poper from "../../poper.app.mjs"; +import sampleEmit from "./test-event.mjs"; export default { key: "poper-new-lead", name: "New Lead from Poper Popup", - description: "Emit new event when a new lead is obtained from Poper popups. [See the documentation](https://help.poper.ai/portal/en/kb/articles/view-popup-responses)", - version: "0.0.{{ts}}", + description: "Emit new event when a new lead is obtained from Poper popups.", + version: "0.0.1", type: "source", - dedupe: "unique", props: { poper, db: "$.service.db", timer: { + label: "Polling interval", + description: "Pipedream will poll the Poper API on this schedule", type: "$.interface.timer", default: { - intervalSeconds: 60, + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, }, }, poperId: { @@ -25,48 +27,49 @@ export default { }, }, methods: { - _getLastLeadId() { - return this.db.get("lastLeadId"); + _getLastId() { + return this.db.get("lastId") || 0; }, - _setLastLeadId(id) { - this.db.set("lastLeadId", id); + _setLastId(lastId) { + this.db.set("lastId", lastId); + }, + getParams() { + return {}; + }, + async startEvent(maxResults = 0) { + const lastId = this._getLastId(); + const { responses } = await this.poper.listPoperResponses({ + maxResults, + data: { + popup_id: this.poperId, + }, + }); + + const filteredResponse = responses.filter((item) => item.id > lastId); + + if (filteredResponse.length) { + if (maxResults && filteredResponse.length > maxResults) { + filteredResponse.length = maxResults; + } + this._setLastId(filteredResponse[0].id); + } + + for (const item of filteredResponse.reverse()) { + this.$emit( item, { + id: item.id, + summary: `New lead with Id: ${item.id}`, + ts: Date.parse(item.time_stamp), + }); + } }, }, hooks: { async deploy() { - await this.emitNewLeads(); - }, - async activate() { - await this.emitNewLeads(); - }, - async deactivate() { - // No specific deactivation logic required + await this.startEvent(25); }, }, async run() { - await this.emitNewLeads(); - }, - async emitNewLeads() { - const poperId = this.poperId; - const responses = await this.poper.getPopupResponses({ - poperId, - }); - const lastLeadId = this._getLastLeadId(); - let newLastLeadId = lastLeadId; - - for (const response of responses.responses) { - if (!lastLeadId || response.id > lastLeadId) { - this.$emit(response, { - summary: `New lead from Poper ID: ${poperId}`, - id: response.id, - ts: Date.now(), - }); - newLastLeadId = response.id; - } - } - - if (newLastLeadId !== lastLeadId) { - this._setLastLeadId(newLastLeadId); - } + await this.startEvent(); }, + sampleEmit, }; diff --git a/components/poper/sources/new-lead/test-event.mjs b/components/poper/sources/new-lead/test-event.mjs new file mode 100644 index 0000000000000..4f695596adefd --- /dev/null +++ b/components/poper/sources/new-lead/test-event.mjs @@ -0,0 +1,24 @@ +export default { + "id": 6625, + "popup_id": 1233, + "time_stamp": "2024-07-05T16:22:23.830Z", + "response": { + "email": "email@test.com" + }, + "system": { + "page": "/", + "referrer": "https://site.com.br/wp-admin/admin.php?page=poper-settings", + "language": "pt-BR", + "os": "Windows", + "browser": "Chrome", + "ip": "123.456.78.90", + "city": "São Paulo", + "country": "BR", + "continent": "SA", + "latitude": "-22.123421", + "longitude": "-49.657476", + "postal_code": "05040001", + "region": "São Paulo", + "region_code": "SP" + } +} \ No newline at end of file From c01a4dd6b56dbf86750c94dfc34471a15d30fa12 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Fri, 5 Jul 2024 14:37:19 -0300 Subject: [PATCH 3/4] pnpm update --- pnpm-lock.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2b093c8922a4f..5cd11945f4000 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6786,7 +6786,10 @@ importers: '@pipedream/platform': 1.5.1 components/poper: - specifiers: {} + specifiers: + '@pipedream/platform': ^3.0.0 + dependencies: + '@pipedream/platform': 3.0.0 components/popupsmart: specifiers: {} From d0bb1f36c3bb7d3b110dea8cf16f2bb1cf59b5b1 Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Mon, 8 Jul 2024 11:30:31 -0300 Subject: [PATCH 4/4] Update components/poper/sources/new-lead/new-lead.mjs Co-authored-by: michelle0927 --- components/poper/sources/new-lead/new-lead.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/components/poper/sources/new-lead/new-lead.mjs b/components/poper/sources/new-lead/new-lead.mjs index a8817a4daf2aa..cd92423cb9d1b 100644 --- a/components/poper/sources/new-lead/new-lead.mjs +++ b/components/poper/sources/new-lead/new-lead.mjs @@ -39,7 +39,6 @@ export default { async startEvent(maxResults = 0) { const lastId = this._getLastId(); const { responses } = await this.poper.listPoperResponses({ - maxResults, data: { popup_id: this.poperId, },