From d8a17c351b2287b2748c309d8cc7e7f729c0343a Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Tue, 16 Jul 2024 11:33:53 -0300 Subject: [PATCH] [Components] waitlist #12804 Sources - New List - New Subscriber --- components/waitlist/package.json | 6 +- components/waitlist/sources/common/base.mjs | 62 +++++++++++++ .../waitlist/sources/new-list/new-list.mjs | 72 ++++----------- .../waitlist/sources/new-list/test-event.mjs | 40 +++++++++ .../sources/new-subscriber/new-subscriber.mjs | 90 ++++++------------- .../sources/new-subscriber/test-event.mjs | 19 ++++ components/waitlist/waitlist.app.mjs | 76 ++++++---------- 7 files changed, 195 insertions(+), 170 deletions(-) create mode 100644 components/waitlist/sources/common/base.mjs create mode 100644 components/waitlist/sources/new-list/test-event.mjs create mode 100644 components/waitlist/sources/new-subscriber/test-event.mjs diff --git a/components/waitlist/package.json b/components/waitlist/package.json index 3565e1bc996fe..414adf3421baa 100644 --- a/components/waitlist/package.json +++ b/components/waitlist/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/waitlist", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Waitlist Components", "main": "waitlist.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/waitlist/sources/common/base.mjs b/components/waitlist/sources/common/base.mjs new file mode 100644 index 0000000000000..bfd05af0e8ab8 --- /dev/null +++ b/components/waitlist/sources/common/base.mjs @@ -0,0 +1,62 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import waitlist from "../../waitlist.app.mjs"; + +export default { + props: { + waitlist, + db: "$.service.db", + timer: { + label: "Polling interval", + description: "Pipedream will poll the Waitlist API on this schedule", + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + methods: { + _getLastValue() { + return this.db.get("lastValue") || 0; + }, + _setLastValue(lastValue) { + this.db.set("lastValue", lastValue); + }, + getProps() { + return {}; + }, + async startEvent(maxResults = 0) { + const lastValue = this._getLastValue(); + const fn = this.getFunction(); + const field = this.getField(); + + const items = await fn({ + ...this.getProps(), + }); + + const filteredResponse = items.filter((item) => this.getFilter(item[field], lastValue)); + + if (filteredResponse.length) { + if (maxResults && filteredResponse.length > maxResults) { + filteredResponse.length = maxResults; + } + this._setLastValue(filteredResponse[filteredResponse.length - 1][field]); + } + + for (const item of filteredResponse) { + this.$emit( item, { + id: item.id || item.uuid, + summary: this.getSummary(item), + ts: Date.parse(item.created_at), + }); + } + }, + }, + hooks: { + async deploy() { + await this.startEvent(25); + }, + }, + async run() { + await this.startEvent(); + }, +}; diff --git a/components/waitlist/sources/new-list/new-list.mjs b/components/waitlist/sources/new-list/new-list.mjs index 304452278a167..c2a32f635ee80 100644 --- a/components/waitlist/sources/new-list/new-list.mjs +++ b/components/waitlist/sources/new-list/new-list.mjs @@ -1,70 +1,28 @@ -import waitlist from "../../waitlist.app.mjs"; -import { axios } from "@pipedream/platform"; +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; export default { + ...common, key: "waitlist-new-list", name: "New List Created", - description: "Emit new event each time a list is created. [See the documentation](https://getwaitlist.com/docs/api-docs/waitlist)", - version: "0.0.{{ts}}", + description: "Emit new event each time a waitlist is created. [See the documentation](https://getwaitlist.com/docs/api-docs/waitlist)", + version: "0.0.1", type: "source", dedupe: "unique", - props: { - waitlist, - db: "$.service.db", - timer: { - type: "$.interface.timer", - default: { - intervalSeconds: 60, - }, - }, - }, methods: { - _getLastListId() { - return this.db.get("lastListId"); - }, - _setLastListId(listId) { - this.db.set("lastListId", listId); + ...common.methods, + getFunction() { + return this.waitlist.listWaitlists; }, - async getLists() { - return this.waitlist._makeRequest({ - path: "/waitlist", - }); + getSummary(item) { + return `New waitlist with Id: ${item.id}`; }, - }, - hooks: { - async deploy() { - const lists = await this.getLists(); - if (lists.length > 0) { - this._setLastListId(lists[0].id); - for (const list of lists.slice(0, 50)) { - this.$emit(list, { - id: list.id, - summary: `New list created: ${list.waitlist_name}`, - ts: Date.parse(list.created_at), - }); - } - } - }, - async activate() { - // Hook to handle activation logic if necessary + getField() { + return "id"; }, - async deactivate() { - // Hook to handle deactivation logic if necessary + getFilter(item, lastValue) { + return item > lastValue; }, }, - async run() { - const lastListId = this._getLastListId(); - const lists = await this.getLists(); - for (const list of lists) { - if (list.id === lastListId) break; - this.$emit(list, { - id: list.id, - summary: `New list created: ${list.waitlist_name}`, - ts: Date.parse(list.created_at), - }); - } - if (lists.length > 0) { - this._setLastListId(lists[0].id); - } - }, + sampleEmit, }; diff --git a/components/waitlist/sources/new-list/test-event.mjs b/components/waitlist/sources/new-list/test-event.mjs new file mode 100644 index 0000000000000..c82b16c2b75e1 --- /dev/null +++ b/components/waitlist/sources/new-list/test-event.mjs @@ -0,0 +1,40 @@ +export default { + "id": 213, + "configuration_style_json": { + "social_links": { + "facebook": "", + "instagram": "", + "linkedin": "", + "pinterest": "", + "twitter": "https://twitter.com/WaitlistAPI" + }, + "status_description": "Thanks for signing up!", + "status_font_color": "#000000", + "status_main_color": "#222222", + "widget_background_color": "#4937E7", + "widget_button_color": "#000000", + "widget_font_color": "#000000" + }, + "logo": null, + "spots_to_move_upon_referral": 3, + "uses_firstname_lastname": false, + "uses_leaderboard": true, + "uses_signup_verification": false, + "waitlist_name": "Title", + "waitlist_url_location": "https://getwaitlist.com", + "statistics": { + 'total_signups': 2200, + 'current_signups': 2200, + }, + "title": null, + "success_title": null, + "required_contact_detail": "EMAIL", + "widget_shows_social_links": false, + "signup_button_title": "Sign Up", + "hide_counts": false, + "leaderboard_length": 5, + "remove_widget_headers": false, + "questions": [{'question_value': "What is your favorite animal?", 'optional': false, "answer_value": ["Cat", "Dog", "Duck", "Other"]}], + "twitter_message": "", + "organization_uuid_fk": "30120c24-0ddc-4f35-9bc6-f5e3c7b09257", +} diff --git a/components/waitlist/sources/new-subscriber/new-subscriber.mjs b/components/waitlist/sources/new-subscriber/new-subscriber.mjs index af512b457c1f4..43286931a82ae 100644 --- a/components/waitlist/sources/new-subscriber/new-subscriber.mjs +++ b/components/waitlist/sources/new-subscriber/new-subscriber.mjs @@ -1,84 +1,44 @@ -import { axios } from "@pipedream/platform"; -import waitlist from "../../waitlist.app.mjs"; +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; export default { + ...common, key: "waitlist-new-subscriber", name: "New Subscriber Added", - description: "Emit a new event each time a subscriber is added. [See the documentation](https://getwaitlist.com/docs/api-docs/waitlist)", - version: "0.0.{{ts}}", + description: "Emit new event each time a subscriber is added. [See the documentation](https://getwaitlist.com/docs/api-docs/waitlist)", + version: "0.0.1", type: "source", dedupe: "unique", props: { - waitlist, - db: "$.service.db", - listId: { + ...common.props, + waitlistId: { propDefinition: [ - waitlist, - "listId", + common.props.waitlist, + "waitlistId", ], }, - timer: { - type: "$.interface.timer", - default: { - intervalSeconds: 60, - }, - }, }, methods: { - _getLastSubscriberId() { - return this.db.get("lastSubscriberId"); - }, - _setLastSubscriberId(lastSubscriberId) { - this.db.set("lastSubscriberId", lastSubscriberId); + ...common.methods, + getFunction() { + return this.waitlist.listSignups; }, - async _getSubscribers() { - const listDetails = await this.waitlist.getListDetails({ - listId: this.listId, - }); - return listDetails.signups || []; + getProps() { + return { + waitlistId: this.waitlistId, + }; }, - }, - hooks: { - async deploy() { - const subscribers = await this._getSubscribers(); - const lastSubscriber = subscribers[0]; - if (lastSubscriber) { - this._setLastSubscriberId(lastSubscriber.uuid); - subscribers.slice(0, 50).forEach((subscriber) => { - this.$emit(subscriber, { - id: subscriber.uuid, - summary: `New subscriber added: ${subscriber.email}`, - ts: new Date(subscriber.created_at).getTime(), - }); - }); - } + getSummary(item) { + return `New signup with Id: ${item.uuid}`; }, - async activate() { - console.log("Source activated"); + getField() { + return "created_at"; }, - async deactivate() { - console.log("Source deactivated"); + getFilter(item, lastValue) { + let parseDate = item.split("_"); + const itemDate = `${parseDate[0]}T${parseDate[1].replace(/-/g, ":")}`; + return Date.parse(itemDate) > Date.parse(lastValue); }, }, - async run() { - const lastSubscriberId = this._getLastSubscriberId(); - const subscribers = await this._getSubscribers(); - const newSubscribers = []; - - for (const subscriber of subscribers) { - if (subscriber.uuid === lastSubscriberId) break; - newSubscribers.push(subscriber); - } - - if (newSubscribers.length > 0) { - this._setLastSubscriberId(newSubscribers[0].uuid); - newSubscribers.reverse().forEach((subscriber) => { - this.$emit(subscriber, { - id: subscriber.uuid, - summary: `New subscriber added: ${subscriber.email}`, - ts: new Date(subscriber.created_at).getTime(), - }); - }); - } - }, + sampleEmit, }; diff --git a/components/waitlist/sources/new-subscriber/test-event.mjs b/components/waitlist/sources/new-subscriber/test-event.mjs new file mode 100644 index 0000000000000..8892bfbe7006f --- /dev/null +++ b/components/waitlist/sources/new-subscriber/test-event.mjs @@ -0,0 +1,19 @@ +export default { + "amount_referred": 0, + "created_at": "2022-04-10_18-34-28", + "email": "maya@getwaitlist.com", + "priority": 4985, + "referral_link": "https://getwaitlist.com?ref_id=4F0BTBMAB", + "referral_token": "4F0BTBMAB", + "referred_by_signup_token": null, + "removed_date": null, + "removed_priority": null, + "uuid": "c60ff9f2-1a58-4551-87ea-414991184fba", + "verified": false, + "answers": [{'question_value': "What is your favorite animal?", 'optional': false, "answer_value": "Cat"}], + "phone": null, + "first_name": "Maya", + "last_name": "Kyler", + "metadata": {}, + "waitlist_id": 1234 +} diff --git a/components/waitlist/waitlist.app.mjs b/components/waitlist/waitlist.app.mjs index 4587c7cb3d45e..238191563350c 100644 --- a/components/waitlist/waitlist.app.mjs +++ b/components/waitlist/waitlist.app.mjs @@ -4,72 +4,54 @@ export default { type: "app", app: "waitlist", propDefinitions: { - listId: { + waitlistId: { type: "string", - label: "List ID", - description: "The ID of the waitlist", - }, - subscriberId: { - type: "string", - label: "Subscriber ID", - description: "The ID of the subscriber", + label: "Waitlist Id", + description: "The ID of your waitlist.", + async options() { + const waitlists = await this.listWaitlists(); + + return waitlists.map(({ + id: value, waitlist_name: label, + }) => ({ + label, + value, + })); + }, }, }, methods: { _baseUrl() { return "https://api.getwaitlist.com/api/v1"; }, - async _makeRequest(opts = {}) { - const { - $ = this, - method = "GET", - path = "/", - headers, - ...otherOpts - } = opts; + _headers() { + return { + "Content-Type": "application/json", + "api-key": `${this.$auth.api_key}`, + }; + }, + _makeRequest({ + $ = this, path, ...opts + }) { return axios($, { - ...otherOpts, - method, url: this._baseUrl() + path, - headers: { - ...headers, - Authorization: `Bearer ${this.$auth.oauth_access_token}`, - }, + headers: this._headers(), + ...opts, }); }, - async getListDetails({ - listId, ...opts - }) { + listWaitlists(opts = {}) { return this._makeRequest({ - path: `/waitlist/${listId}`, + path: "/waitlist", ...opts, }); }, - async getSubscriberDetails({ - subscriberId, ...opts + listSignups({ + waitlistId, ...opts }) { return this._makeRequest({ - path: `/signup/${subscriberId}`, + path: `/signup/waitlist/${waitlistId}`, ...opts, }); }, - async emitNewListEvent(listId) { - const listDetails = await this.getListDetails({ - listId, - }); - this.$emit(listDetails, { - name: "new_list", - summary: `New list created with ID: ${listId}`, - }); - }, - async emitNewSubscriberEvent(subscriberId) { - const subscriberDetails = await this.getSubscriberDetails({ - subscriberId, - }); - this.$emit(subscriberDetails, { - name: "new_subscriber", - summary: `New subscriber added with ID: ${subscriberId}`, - }); - }, }, };