Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Components - waitlist #12846

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions components/waitlist/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/waitlist",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Waitlist Components",
"main": "waitlist.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,9 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.0"
}
}
}

62 changes: 62 additions & 0 deletions components/waitlist/sources/common/base.mjs
Original file line number Diff line number Diff line change
@@ -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();
},
};
Comment on lines +1 to +62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of base event handling

The integration of DEFAULT_POLLING_SOURCE_TIMER_INTERVAL and the waitlist component is correctly done. The methods _getLastValue, _setLastValue, and startEvent are crucial for event handling and are implemented with clear logic. However, ensure that error handling is robust, especially in database interactions and during event emissions.

+      try {
        return this.db.get("lastValue") || 0;
+      } catch (error) {
+        console.error("Error accessing database:", error);
+        return 0; // Return default value on error
+      }
+      try {
        if (filteredResponse.length) {
          if (maxResults && filteredResponse.length > maxResults) {
            filteredResponse.length = maxResults;
          }
          this._setLastValue(filteredResponse[filteredResponse.length - 1][field]);
        }
+      } catch (error) {
+        console.error("Error during event processing:", error);
+        throw error; // Re-throw to handle upstream
+      }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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();
},
};
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() {
try {
return this.db.get("lastValue") || 0;
} catch (error) {
console.error("Error accessing database:", error);
return 0; // Return default value on error
}
},
_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));
try {
if (filteredResponse.length) {
if (maxResults && filteredResponse.length > maxResults) {
filteredResponse.length = maxResults;
}
this._setLastValue(filteredResponse[filteredResponse.length - 1][field]);
}
} catch (error) {
console.error("Error during event processing:", error);
throw error; // Re-throw to handle upstream
}
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();
},
};

28 changes: 28 additions & 0 deletions components/waitlist/sources/new-list/new-list.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 waitlist is created. [See the documentation](https://getwaitlist.com/docs/api-docs/waitlist)",
version: "0.0.1",
type: "source",
dedupe: "unique",
methods: {
...common.methods,
getFunction() {
return this.waitlist.listWaitlists;
},
getSummary(item) {
return `New waitlist with Id: ${item.id}`;
},
getField() {
return "id";
},
getFilter(item, lastValue) {
return item > lastValue;
},
},
sampleEmit,
};
40 changes: 40 additions & 0 deletions components/waitlist/sources/new-list/test-event.mjs
Original file line number Diff line number Diff line change
@@ -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",
}
44 changes: 44 additions & 0 deletions components/waitlist/sources/new-subscriber/new-subscriber.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 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: {
...common.props,
waitlistId: {
propDefinition: [
common.props.waitlist,
"waitlistId",
],
},
},
methods: {
...common.methods,
getFunction() {
return this.waitlist.listSignups;
},
getProps() {
return {
waitlistId: this.waitlistId,
};
},
getSummary(item) {
return `New signup with Id: ${item.uuid}`;
},
getField() {
return "created_at";
},
getFilter(item, lastValue) {
let parseDate = item.split("_");
const itemDate = `${parseDate[0]}T${parseDate[1].replace(/-/g, ":")}`;
return Date.parse(itemDate) > Date.parse(lastValue);
},
},
sampleEmit,
};
19 changes: 19 additions & 0 deletions components/waitlist/sources/new-subscriber/test-event.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default {
"amount_referred": 0,
"created_at": "2022-04-10_18-34-28",
"email": "[email protected]",
"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
}
56 changes: 51 additions & 5 deletions components/waitlist/waitlist.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,57 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "waitlist",
propDefinitions: {},
propDefinitions: {
waitlistId: {
type: "string",
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,
}));
},
},
},
Comment on lines +6 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of propDefinitions

The addition of waitlistId with dynamic options fetching is a powerful feature. However, consider handling possible exceptions during the API call to ensure robustness.

+        try {
          const waitlists = await this.listWaitlists();
+        } catch (error) {
+          console.error("Failed to fetch waitlists:", error);
+          return []; // Return an empty list on failure
+        }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
propDefinitions: {
waitlistId: {
type: "string",
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,
}));
},
},
},
propDefinitions: {
waitlistId: {
type: "string",
label: "Waitlist Id",
description: "The ID of your waitlist.",
async options() {
try {
const waitlists = await this.listWaitlists();
} catch (error) {
console.error("Failed to fetch waitlists:", error);
return []; // Return an empty list on failure
}
return waitlists.map(({
id: value, waitlist_name: label,
}) => ({
label,
value,
}));
},
},
},

methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://api.getwaitlist.com/api/v1";
},
_headers() {
return {
"Content-Type": "application/json",
"api-key": `${this.$auth.api_key}`,
};
},
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(),
...opts,
});
},
listWaitlists(opts = {}) {
return this._makeRequest({
path: "/waitlist",
...opts,
});
},
listSignups({
waitlistId, ...opts
}) {
return this._makeRequest({
path: `/signup/waitlist/${waitlistId}`,
...opts,
});
Comment on lines +24 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of added methods

  1. _baseUrl and _headers: These methods are well-implemented for setting base URLs and headers.
  2. _makeRequest: This method centralizes the request-making process using axios, which is good for maintainability. Ensure to catch and handle any potential errors that might arise from the axios call.
  3. listWaitlists and listSignups: These methods effectively abstract the API endpoints. Ensure proper error handling in the calling context.

Overall, these methods are structured well but consider adding error handling in _makeRequest.

+      try {
        return axios($, {
          url: this._baseUrl() + path,
          headers: this._headers(),
          ...opts,
        });
+      } catch (error) {
+        console.error("Request failed:", error);
+        throw error; // Re-throw to handle upstream
+      }

Committable suggestion was skipped due to low confidence.

},
},
};
};
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading