From cbbae668c746d2da4ecf38a47ea72c3344baee3e Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Tue, 17 Dec 2024 11:41:39 -0500 Subject: [PATCH 1/4] rejoiner init --- .../add-customer-to-list.mjs | 37 +++++ .../actions/start-journey/start-journey.mjs | 50 +++++++ .../update-customer-profile.mjs | 37 +++++ components/rejoiner/package.json | 2 +- components/rejoiner/rejoiner.app.mjs | 127 +++++++++++++++++- .../new-customer-opt-out-instant.mjs | 108 +++++++++++++++ 6 files changed, 359 insertions(+), 2 deletions(-) create mode 100644 components/rejoiner/actions/add-customer-to-list/add-customer-to-list.mjs create mode 100644 components/rejoiner/actions/start-journey/start-journey.mjs create mode 100644 components/rejoiner/actions/update-customer-profile/update-customer-profile.mjs create mode 100644 components/rejoiner/sources/new-customer-opt-out-instant/new-customer-opt-out-instant.mjs diff --git a/components/rejoiner/actions/add-customer-to-list/add-customer-to-list.mjs b/components/rejoiner/actions/add-customer-to-list/add-customer-to-list.mjs new file mode 100644 index 0000000000000..7f56455fad0d6 --- /dev/null +++ b/components/rejoiner/actions/add-customer-to-list/add-customer-to-list.mjs @@ -0,0 +1,37 @@ +import rejoiner from "../../rejoiner.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "rejoiner-add-customer-to-list", + name: "Add Customer to List", + description: "Adds a customer to a specific list. [See the documentation](https://docs.rejoiner.com/docs/getting-started)", + version: "0.0.{{ts}}", + type: "action", + props: { + rejoiner, + customerId: { + propDefinition: [ + rejoiner, + "customerId", + ], + }, + listId: { + propDefinition: [ + rejoiner, + "listId", + ], + }, + listSource: { + propDefinition: [ + rejoiner, + "listSource", + ], + optional: true, + }, + }, + async run({ $ }) { + const response = await this.rejoiner.addCustomerToList(); + $.export("$summary", `Added customer ${this.customerId} to list ${this.listId}`); + return response; + }, +}; diff --git a/components/rejoiner/actions/start-journey/start-journey.mjs b/components/rejoiner/actions/start-journey/start-journey.mjs new file mode 100644 index 0000000000000..c1ca954a8c45e --- /dev/null +++ b/components/rejoiner/actions/start-journey/start-journey.mjs @@ -0,0 +1,50 @@ +import rejoiner from "../../rejoiner.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "rejoiner-start-journey", + name: "Start Journey", + description: "Triggers the beginning of a customer journey in Rejoiner. [See the documentation]()", + version: "0.0.{{ts}}", + type: "action", + props: { + rejoiner: { + type: "app", + app: "rejoiner", + }, + journeyId: { + propDefinition: [ + rejoiner, + "journeyId", + ], + }, + customerId: { + propDefinition: [ + rejoiner, + "customerId", + ], + }, + metadata: { + propDefinition: [ + rejoiner, + "metadata", + ], + optional: true, + }, + }, + async run({ $ }) { + this.rejoiner.journeyId = this.journeyId; + this.rejoiner.customerId = this.customerId; + if (this.metadata) { + this.rejoiner.metadata = this.metadata; + } + + const response = await this.rejoiner.triggerCustomerJourney(); + + $.export( + "$summary", + `Triggered journey ${this.journeyId} for customer ${this.customerId}`, + ); + return response; + }, +}; diff --git a/components/rejoiner/actions/update-customer-profile/update-customer-profile.mjs b/components/rejoiner/actions/update-customer-profile/update-customer-profile.mjs new file mode 100644 index 0000000000000..3b2d1a6d20780 --- /dev/null +++ b/components/rejoiner/actions/update-customer-profile/update-customer-profile.mjs @@ -0,0 +1,37 @@ +import rejoiner from "../../rejoiner.app.mjs"; +import { axios } from "@pipedream/platform"; + +export default { + key: "rejoiner-update-customer-profile", + name: "Update Customer Profile", + description: "Updates a customer's profile information. [See the documentation](https://docs.rejoiner.com/docs/getting-started)", + version: "0.0.{{ts}}", + type: "action", + props: { + rejoiner, + customerId: { + propDefinition: [ + rejoiner, + "customerId", + ], + }, + profileData: { + propDefinition: [ + rejoiner, + "profileData", + ], + }, + updateSource: { + propDefinition: [ + rejoiner, + "updateSource", + ], + optional: true, + }, + }, + async run({ $ }) { + const response = await this.rejoiner.updateCustomerProfile(); + $.export("$summary", `Updated customer profile for customer_id ${this.customerId}`); + return response; + }, +}; diff --git a/components/rejoiner/package.json b/components/rejoiner/package.json index d1db9bf5f640f..6147ae6623f06 100644 --- a/components/rejoiner/package.json +++ b/components/rejoiner/package.json @@ -12,4 +12,4 @@ "publishConfig": { "access": "public" } -} \ No newline at end of file +} diff --git a/components/rejoiner/rejoiner.app.mjs b/components/rejoiner/rejoiner.app.mjs index 6b7e0ac72eb6b..7d3d3f5aa223f 100644 --- a/components/rejoiner/rejoiner.app.mjs +++ b/components/rejoiner/rejoiner.app.mjs @@ -1,11 +1,136 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "rejoiner", - propDefinitions: {}, + propDefinitions: { + customerId: { + type: "string", + label: "Customer ID", + description: "Unique identifier for the customer", + }, + listId: { + type: "string", + label: "List ID", + description: "Unique identifier for the list", + }, + listSource: { + type: "string", + label: "List Source", + description: "Source context of the list", + optional: true, + }, + journeyId: { + type: "string", + label: "Journey ID", + description: "Unique identifier for the journey", + }, + metadata: { + type: "string", + label: "Metadata", + description: "Additional data relevant to the journey start", + optional: true, + }, + profileData: { + type: "object", + label: "Profile Data", + description: "An object containing updated profile attributes", + }, + updateSource: { + type: "string", + label: "Update Source", + description: "Context for the update", + optional: true, + }, + }, methods: { // this.$auth contains connected account data authKeys() { console.log(Object.keys(this.$auth)); }, + _baseUrl() { + return "https://api.rejoiner.com"; + }, + async _makeRequest(opts = {}) { + const { + $ = this, + method = "GET", + path = "/", + headers, + ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + method, + url: this._baseUrl() + path, + headers: { + ...headers, + "Authorization": `Bearer ${this.$auth.api_token}`, + "Content-Type": "application/json", + }, + }); + }, + async emitOptOutEvent() { + return this._makeRequest({ + method: "POST", + path: "/events/opt-out", + data: { + customer_id: this.customerId, + }, + }); + }, + async addCustomerToList() { + const data = { + customer_id: this.customerId, + list_id: this.listId, + }; + if (this.listSource) { + data.list_source = this.listSource; + } + return this._makeRequest({ + method: "POST", + path: "/customers/lists", + data, + }); + }, + async triggerCustomerJourney() { + const data = { + journey_id: this.journeyId, + customer_id: this.customerId, + }; + if (this.metadata) { + try { + data.metadata = JSON.parse(this.metadata); + } catch (error) { + throw new Error("Invalid JSON format for metadata"); + } + } + return this._makeRequest({ + method: "POST", + path: "/journeys/trigger", + data, + }); + }, + async updateCustomerProfile() { + if (!this.customerId) { + throw new Error("customer_id is required"); + } + if (!this.profileData || typeof this.profileData !== "object") { + throw new Error("profile_data is required and must be an object"); + } + const data = { + customer_id: this.customerId, + profile_data: this.profileData, + }; + if (this.updateSource) { + data.update_source = this.updateSource; + } + return this._makeRequest({ + method: "PUT", + path: `/customers/${this.customerId}/profile`, + data, + }); + }, }, + version: "0.0.{{ts}}", }; diff --git a/components/rejoiner/sources/new-customer-opt-out-instant/new-customer-opt-out-instant.mjs b/components/rejoiner/sources/new-customer-opt-out-instant/new-customer-opt-out-instant.mjs new file mode 100644 index 0000000000000..1e54e7b0c721d --- /dev/null +++ b/components/rejoiner/sources/new-customer-opt-out-instant/new-customer-opt-out-instant.mjs @@ -0,0 +1,108 @@ +import { + axios, DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, +} from "@pipedream/platform"; +import rejoiner from "../../rejoiner.app.mjs"; + +export default { + key: "rejoiner-new-customer-opt-out-instant", + name: "New Customer Opt-Out Instant", + description: "Emit a new event when a customer opts out of receiving communications. [See the documentation]().", + version: "0.0.{{ts}}", + type: "source", + dedupe: "unique", + props: { + rejoiner: { + type: "app", + app: "rejoiner", + }, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + methods: { + _getLastTimestamp() { + return this.db.get("lastTimestamp") ?? 0; + }, + _setLastTimestamp(timestamp) { + this.db.set("lastTimestamp", timestamp); + }, + }, + hooks: { + async deploy() { + let lastTimestamp = this._getLastTimestamp(); + if (!lastTimestamp) { + lastTimestamp = Date.now(); + } + + const optOutEvents = await this.rejoiner._makeRequest({ + method: "GET", + path: "/events/opt-out", + params: { + limit: 50, + }, + }); + + optOutEvents.reverse(); + + for (const event of optOutEvents) { + const ts = event.timestamp + ? Date.parse(event.timestamp) + : Date.now(); + this.$emit(event, { + id: event.id + ? event.id.toString() + : undefined, + summary: `Customer opted out: ${event.customer_id}`, + ts, + }); + if (ts > lastTimestamp) { + lastTimestamp = ts; + } + } + + this._setLastTimestamp(lastTimestamp); + }, + async activate() { + // No activation steps required + }, + async deactivate() { + // No deactivation steps required + }, + }, + async run() { + const lastTimestamp = this._getLastTimestamp(); + const optOutEvents = await this.rejoiner._makeRequest({ + method: "GET", + path: "/events/opt-out", + params: { + since: lastTimestamp, + }, + }); + + let newLastTimestamp = lastTimestamp; + + for (const event of optOutEvents) { + const ts = event.timestamp + ? Date.parse(event.timestamp) + : Date.now(); + this.$emit(event, { + id: event.id + ? event.id.toString() + : undefined, + summary: `Customer opted out: ${event.customer_id}`, + ts, + }); + if (ts > newLastTimestamp) { + newLastTimestamp = ts; + } + } + + if (newLastTimestamp > lastTimestamp) { + this._setLastTimestamp(newLastTimestamp); + } + }, +}; From 6ba66f34a3841fe68e6f25e62860576c3cb51957 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Tue, 17 Dec 2024 13:50:26 -0500 Subject: [PATCH 2/4] new components --- .../add-customer-to-list.mjs | 99 +++++++-- .../actions/start-journey/start-journey.mjs | 56 +++-- .../update-customer-profile.mjs | 94 ++++++-- components/rejoiner/package.json | 5 +- components/rejoiner/rejoiner.app.mjs | 203 +++++++++++------- .../new-contact-in-list.mjs | 49 +++++ .../new-customer-opt-out-instant.mjs | 108 ---------- 7 files changed, 368 insertions(+), 246 deletions(-) create mode 100644 components/rejoiner/sources/new-contact-in-list/new-contact-in-list.mjs delete mode 100644 components/rejoiner/sources/new-customer-opt-out-instant/new-customer-opt-out-instant.mjs diff --git a/components/rejoiner/actions/add-customer-to-list/add-customer-to-list.mjs b/components/rejoiner/actions/add-customer-to-list/add-customer-to-list.mjs index 7f56455fad0d6..af3f2989ff67f 100644 --- a/components/rejoiner/actions/add-customer-to-list/add-customer-to-list.mjs +++ b/components/rejoiner/actions/add-customer-to-list/add-customer-to-list.mjs @@ -1,37 +1,112 @@ import rejoiner from "../../rejoiner.app.mjs"; -import { axios } from "@pipedream/platform"; export default { key: "rejoiner-add-customer-to-list", name: "Add Customer to List", - description: "Adds a customer to a specific list. [See the documentation](https://docs.rejoiner.com/docs/getting-started)", - version: "0.0.{{ts}}", + description: "Adds a customer to a specific list, or if the customer already exists, will update the record of that customer with the supplied data. [See the documentation](https://docs.rejoiner.com/reference/add-customer-to-list)", + version: "0.0.1", type: "action", props: { rejoiner, - customerId: { + listId: { propDefinition: [ rejoiner, - "customerId", + "listId", ], }, - listId: { + email: { propDefinition: [ rejoiner, - "listId", + "email", + ], + }, + firstName: { + propDefinition: [ + rejoiner, + "firstName", + ], + }, + lastName: { + propDefinition: [ + rejoiner, + "lastName", + ], + }, + phone: { + propDefinition: [ + rejoiner, + "phone", + ], + }, + timezone: { + propDefinition: [ + rejoiner, + "timezone", + ], + }, + language: { + propDefinition: [ + rejoiner, + "language", + ], + }, + address1: { + propDefinition: [ + rejoiner, + "address1", + ], + }, + address2: { + propDefinition: [ + rejoiner, + "address2", + ], + }, + city: { + propDefinition: [ + rejoiner, + "city", + ], + }, + state: { + propDefinition: [ + rejoiner, + "state", + ], + }, + postalCode: { + propDefinition: [ + rejoiner, + "postalCode", ], }, - listSource: { + country: { propDefinition: [ rejoiner, - "listSource", + "country", ], - optional: true, }, }, async run({ $ }) { - const response = await this.rejoiner.addCustomerToList(); - $.export("$summary", `Added customer ${this.customerId} to list ${this.listId}`); + const response = await this.rejoiner.addCustomerToList({ + $, + listId: this.listId, + data: { + email: this.email, + first_name: this.firstName, + last_name: this.lastName, + phone: this.phone, + timezone: this.timezone, + language: this.language, + address1: this.address1, + address2: this.address2, + city: this.city, + state: this.state, + postal_code: this.postalCode, + country: this.country, + }, + }); + $.export("$summary", `Added customer ${this.email} to list ${this.listId}`); return response; }, }; diff --git a/components/rejoiner/actions/start-journey/start-journey.mjs b/components/rejoiner/actions/start-journey/start-journey.mjs index c1ca954a8c45e..e193ff53ac00e 100644 --- a/components/rejoiner/actions/start-journey/start-journey.mjs +++ b/components/rejoiner/actions/start-journey/start-journey.mjs @@ -1,50 +1,46 @@ import rejoiner from "../../rejoiner.app.mjs"; -import { axios } from "@pipedream/platform"; export default { key: "rejoiner-start-journey", name: "Start Journey", - description: "Triggers the beginning of a customer journey in Rejoiner. [See the documentation]()", - version: "0.0.{{ts}}", + description: "Triggers the beginning of a customer journey in Rejoiner. [See the documentation](https://docs.rejoiner.com/reference/trigger-webhook-journey)", + version: "0.0.1", type: "action", props: { - rejoiner: { - type: "app", - app: "rejoiner", + rejoiner, + webhookUrl: { + type: "string", + label: "Webhook Endpoint URL", + description: "Webhook URL of the journey. A webhook-triggered journey will provide an explicit Webhook Endpoint URL to be used for triggering the journey", }, - journeyId: { + email: { propDefinition: [ rejoiner, - "journeyId", - ], - }, - customerId: { - propDefinition: [ - rejoiner, - "customerId", + "email", ], }, metadata: { - propDefinition: [ - rejoiner, - "metadata", - ], + type: "object", + label: "Metadata", + description: "Metadata to be attached to the customer's journey session metadata", optional: true, }, }, async run({ $ }) { - this.rejoiner.journeyId = this.journeyId; - this.rejoiner.customerId = this.customerId; - if (this.metadata) { - this.rejoiner.metadata = this.metadata; - } - - const response = await this.rejoiner.triggerCustomerJourney(); - - $.export( - "$summary", - `Triggered journey ${this.journeyId} for customer ${this.customerId}`, - ); + const response = await this.rejoiner._makeRequest({ + $, + method: "POST", + url: this.webhookUrl, + data: { + email: this.email, + session_data: this.metadata + ? typeof this.metadata === "string" + ? JSON.parse(this.metadata) + : this.metadata + : undefined, + }, + }); + $.export("$summary", `Triggered journey for customer ${this.email}`); return response; }, }; diff --git a/components/rejoiner/actions/update-customer-profile/update-customer-profile.mjs b/components/rejoiner/actions/update-customer-profile/update-customer-profile.mjs index 3b2d1a6d20780..e1101b0834b75 100644 --- a/components/rejoiner/actions/update-customer-profile/update-customer-profile.mjs +++ b/components/rejoiner/actions/update-customer-profile/update-customer-profile.mjs @@ -1,37 +1,107 @@ import rejoiner from "../../rejoiner.app.mjs"; -import { axios } from "@pipedream/platform"; export default { key: "rejoiner-update-customer-profile", name: "Update Customer Profile", - description: "Updates a customer's profile information. [See the documentation](https://docs.rejoiner.com/docs/getting-started)", - version: "0.0.{{ts}}", + description: "Updates a customer's profile information. [See the documentation](https://docs.rejoiner.com/reference/update-customer-profile)", + version: "0.0.1", type: "action", props: { rejoiner, - customerId: { + email: { propDefinition: [ rejoiner, - "customerId", + "email", ], }, - profileData: { + firstName: { propDefinition: [ rejoiner, - "profileData", + "firstName", ], }, - updateSource: { + lastName: { propDefinition: [ rejoiner, - "updateSource", + "lastName", + ], + }, + phone: { + propDefinition: [ + rejoiner, + "phone", + ], + }, + timezone: { + propDefinition: [ + rejoiner, + "timezone", + ], + }, + language: { + propDefinition: [ + rejoiner, + "language", + ], + }, + address1: { + propDefinition: [ + rejoiner, + "address1", + ], + }, + address2: { + propDefinition: [ + rejoiner, + "address2", + ], + }, + city: { + propDefinition: [ + rejoiner, + "city", + ], + }, + state: { + propDefinition: [ + rejoiner, + "state", + ], + }, + postalCode: { + propDefinition: [ + rejoiner, + "postalCode", + ], + }, + country: { + propDefinition: [ + rejoiner, + "country", ], - optional: true, }, }, async run({ $ }) { - const response = await this.rejoiner.updateCustomerProfile(); - $.export("$summary", `Updated customer profile for customer_id ${this.customerId}`); + const response = await this.rejoiner.updateCustomerProfile({ + $, + params: { + email: this.email, + }, + data: { + first_name: this.firstName, + last_name: this.lastName, + phone: this.phone, + timezone: this.timezone, + language: this.language, + address1: this.address1, + address2: this.address2, + city: this.city, + state: this.state, + postal_code: this.postalCode, + country: this.country, + }, + }); + $.export("$summary", `Updated customer profile for customer ${this.email}`); return response; }, }; diff --git a/components/rejoiner/package.json b/components/rejoiner/package.json index 6147ae6623f06..7a44d0c90e943 100644 --- a/components/rejoiner/package.json +++ b/components/rejoiner/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/rejoiner", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Rejoiner Components", "main": "rejoiner.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" } } diff --git a/components/rejoiner/rejoiner.app.mjs b/components/rejoiner/rejoiner.app.mjs index 7d3d3f5aa223f..20141c6250dd3 100644 --- a/components/rejoiner/rejoiner.app.mjs +++ b/components/rejoiner/rejoiner.app.mjs @@ -4,133 +4,170 @@ export default { type: "app", app: "rejoiner", propDefinitions: { - customerId: { - type: "string", - label: "Customer ID", - description: "Unique identifier for the customer", - }, listId: { type: "string", label: "List ID", description: "Unique identifier for the list", + async options() { + const lists = await this.listLists(); + return lists?.map(({ + id: value, name: label, + }) => ({ + value, + label, + })) || []; + }, + }, + email: { + type: "string", + label: "Email", + description: "The email address of the customer", + }, + firstName: { + type: "string", + label: "First Name", + description: "The first name of the customer", + optional: true, + }, + lastName: { + type: "string", + label: "Last Name", + description: "The last name of the customer", + optional: true, + }, + phone: { + type: "string", + label: "Phone", + description: "A phone number for the customer", + optional: true, + }, + timezone: { + type: "string", + label: "Timezone", + description: "Timezone of customer, should be in list of [TZ database names](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)", + optional: true, + }, + language: { + type: "string", + label: "Language", + description: "Two letter [code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) of customers language", + optional: true, + }, + address1: { + type: "string", + label: "Address Line 1", + description: "First line of address for customer", + optional: true, }, - listSource: { + address2: { type: "string", - label: "List Source", - description: "Source context of the list", + label: "Address Line 2", + description: "Second line of address for customer", optional: true, }, - journeyId: { + city: { type: "string", - label: "Journey ID", - description: "Unique identifier for the journey", + label: "City", + description: "City where customer lives", + optional: true, }, - metadata: { + state: { type: "string", - label: "Metadata", - description: "Additional data relevant to the journey start", + label: "State", + description: "State where customer lives", optional: true, }, - profileData: { - type: "object", - label: "Profile Data", - description: "An object containing updated profile attributes", + postalCode: { + type: "string", + label: "Postal Code", + description: "Postal code of customer's address", + optional: true, }, - updateSource: { + country: { type: "string", - label: "Update Source", - description: "Context for the update", + label: "Country", + description: "Country where customer lives", optional: true, }, }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); - }, _baseUrl() { - return "https://api.rejoiner.com"; + return `https://rj2.rejoiner.com/api/v2/${this.$auth.site_id}`; }, - async _makeRequest(opts = {}) { + _makeRequest(opts = {}) { const { $ = this, - method = "GET", - path = "/", - headers, + path, ...otherOpts } = opts; return axios($, { - ...otherOpts, - method, - url: this._baseUrl() + path, + url: `${this._baseUrl()}${path}`, headers: { - ...headers, - "Authorization": `Bearer ${this.$auth.api_token}`, + "Authorization": `Rejoiner ${this.$auth.api_key}`, "Content-Type": "application/json", }, + debug: true, + ...otherOpts, }); }, - async emitOptOutEvent() { + listLists(opts = {}) { return this._makeRequest({ - method: "POST", - path: "/events/opt-out", - data: { - customer_id: this.customerId, - }, + path: "/lists", + ...opts, }); }, - async addCustomerToList() { - const data = { - customer_id: this.customerId, - list_id: this.listId, - }; - if (this.listSource) { - data.list_source = this.listSource; - } + listListContacts({ + listId, ...opts + }) { return this._makeRequest({ - method: "POST", - path: "/customers/lists", - data, + path: `/lists/${listId}/contacts/`, + ...opts, }); }, - async triggerCustomerJourney() { - const data = { - journey_id: this.journeyId, - customer_id: this.customerId, - }; - if (this.metadata) { - try { - data.metadata = JSON.parse(this.metadata); - } catch (error) { - throw new Error("Invalid JSON format for metadata"); - } - } + addCustomerToList({ + listId, ...opts + }) { return this._makeRequest({ method: "POST", - path: "/journeys/trigger", - data, + path: `/lists/${listId}/contacts/`, + ...opts, }); }, - async updateCustomerProfile() { - if (!this.customerId) { - throw new Error("customer_id is required"); - } - if (!this.profileData || typeof this.profileData !== "object") { - throw new Error("profile_data is required and must be an object"); - } - const data = { - customer_id: this.customerId, - profile_data: this.profileData, - }; - if (this.updateSource) { - data.update_source = this.updateSource; - } + updateCustomerProfile(opts = {}) { return this._makeRequest({ method: "PUT", - path: `/customers/${this.customerId}/profile`, - data, + path: "/customers/by_email/", + ...opts, }); }, + async *paginate({ + fn, + args, + max, + }) { + args = { + ...args, + params: { + ...args?.params, + page: 1, + }, + }; + let total, itemCount = 0; + + do { + const { + results, count, + } = await fn(args); + for (const item of results) { + yield item; + itemCount++; + if (max && itemCount >= max) { + return; + } + } + total = count; + args.params.page++; + } while (itemCount < total); + }, }, - version: "0.0.{{ts}}", }; diff --git a/components/rejoiner/sources/new-contact-in-list/new-contact-in-list.mjs b/components/rejoiner/sources/new-contact-in-list/new-contact-in-list.mjs new file mode 100644 index 0000000000000..5d49aa57d5078 --- /dev/null +++ b/components/rejoiner/sources/new-contact-in-list/new-contact-in-list.mjs @@ -0,0 +1,49 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import rejoiner from "../../rejoiner.app.mjs"; + +export default { + key: "rejoiner-new-contact-in-list", + name: "New Contact in List", + description: "Emit new event when a contact is added to the specified list. [See the documentation](https://docs.rejoiner.com/reference/retrieve-list-contacts).", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + rejoiner, + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + listId: { + propDefinition: [ + rejoiner, + "listId", + ], + }, + }, + methods: { + generateMeta(contact) { + return { + id: contact.id, + summary: contact.email, + ts: Date.now(), + }; + }, + }, + async run() { + const results = this.rejoiner.paginate({ + fn: this.rejoiner.listListContacts, + args: { + listId: this.listId, + }, + }); + + for await (const item of results) { + const contact = item.customer; + const meta = this.generateMeta(contact); + this.$emit(item, meta); + } + }, +}; diff --git a/components/rejoiner/sources/new-customer-opt-out-instant/new-customer-opt-out-instant.mjs b/components/rejoiner/sources/new-customer-opt-out-instant/new-customer-opt-out-instant.mjs deleted file mode 100644 index 1e54e7b0c721d..0000000000000 --- a/components/rejoiner/sources/new-customer-opt-out-instant/new-customer-opt-out-instant.mjs +++ /dev/null @@ -1,108 +0,0 @@ -import { - axios, DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, -} from "@pipedream/platform"; -import rejoiner from "../../rejoiner.app.mjs"; - -export default { - key: "rejoiner-new-customer-opt-out-instant", - name: "New Customer Opt-Out Instant", - description: "Emit a new event when a customer opts out of receiving communications. [See the documentation]().", - version: "0.0.{{ts}}", - type: "source", - dedupe: "unique", - props: { - rejoiner: { - type: "app", - app: "rejoiner", - }, - db: "$.service.db", - timer: { - type: "$.interface.timer", - default: { - intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, - }, - }, - }, - methods: { - _getLastTimestamp() { - return this.db.get("lastTimestamp") ?? 0; - }, - _setLastTimestamp(timestamp) { - this.db.set("lastTimestamp", timestamp); - }, - }, - hooks: { - async deploy() { - let lastTimestamp = this._getLastTimestamp(); - if (!lastTimestamp) { - lastTimestamp = Date.now(); - } - - const optOutEvents = await this.rejoiner._makeRequest({ - method: "GET", - path: "/events/opt-out", - params: { - limit: 50, - }, - }); - - optOutEvents.reverse(); - - for (const event of optOutEvents) { - const ts = event.timestamp - ? Date.parse(event.timestamp) - : Date.now(); - this.$emit(event, { - id: event.id - ? event.id.toString() - : undefined, - summary: `Customer opted out: ${event.customer_id}`, - ts, - }); - if (ts > lastTimestamp) { - lastTimestamp = ts; - } - } - - this._setLastTimestamp(lastTimestamp); - }, - async activate() { - // No activation steps required - }, - async deactivate() { - // No deactivation steps required - }, - }, - async run() { - const lastTimestamp = this._getLastTimestamp(); - const optOutEvents = await this.rejoiner._makeRequest({ - method: "GET", - path: "/events/opt-out", - params: { - since: lastTimestamp, - }, - }); - - let newLastTimestamp = lastTimestamp; - - for (const event of optOutEvents) { - const ts = event.timestamp - ? Date.parse(event.timestamp) - : Date.now(); - this.$emit(event, { - id: event.id - ? event.id.toString() - : undefined, - summary: `Customer opted out: ${event.customer_id}`, - ts, - }); - if (ts > newLastTimestamp) { - newLastTimestamp = ts; - } - } - - if (newLastTimestamp > lastTimestamp) { - this._setLastTimestamp(newLastTimestamp); - } - }, -}; From ca1db16f300dcd8c00ed6aad04d481c792a91803 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Tue, 17 Dec 2024 13:52:17 -0500 Subject: [PATCH 3/4] pnpm-lock.yaml --- pnpm-lock.yaml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 021684e008992..f3a28652b6515 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8446,7 +8446,11 @@ importers: specifier: ^1.6.2 version: 1.6.6 - components/rejoiner: {} + components/rejoiner: + dependencies: + '@pipedream/platform': + specifier: ^3.0.3 + version: 3.0.3 components/relavate: dependencies: @@ -24525,22 +24529,22 @@ packages: superagent@3.8.1: resolution: {integrity: sha512-VMBFLYgFuRdfeNQSMLbxGSLfmXL/xc+OO+BZp41Za/NRDBet/BNbkRJrYzCUu0u4GU0i/ml2dtT8b9qgkw9z6Q==} engines: {node: '>= 4.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at . superagent@4.1.0: resolution: {integrity: sha512-FT3QLMasz0YyCd4uIi5HNe+3t/onxMyEho7C3PSqmti3Twgy2rXT4fmkTz6wRL6bTF4uzPcfkUCa8u4JWHw8Ag==} engines: {node: '>= 6.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at . superagent@5.3.1: resolution: {integrity: sha512-wjJ/MoTid2/RuGCOFtlacyGNxN9QLMgcpYLDQlWFIhhdJ93kNscFonGvrpAHSCVjRVj++DGCglocF7Aej1KHvQ==} engines: {node: '>= 7.0.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at . superagent@7.1.6: resolution: {integrity: sha512-gZkVCQR1gy/oUXr+kxJMLDjla434KmSOKbx5iGD30Ql+AkJQ/YlPKECJy2nhqOsHLjGHzoDTXNSjhnvWhzKk7g==} engines: {node: '>=6.4.0 <13 || >=14'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please downgrade to v7.1.5 if you need IE/ActiveXObject support OR upgrade to v8.0.0 as we no longer support IE and published an incorrect patch version (see https://github.com/visionmedia/superagent/issues/1731) supports-color@2.0.0: resolution: {integrity: sha512-KKNVtd6pCYgPIKU4cp2733HWYCpplQhddZLBUryaAHou723x+FRzQ5Df824Fj+IyyuiQTRoub4SnIFfIcrp70g==} From 16f1915e03122fe0b9636fac84bcfc912fd64861 Mon Sep 17 00:00:00 2001 From: michelle0927 Date: Tue, 17 Dec 2024 14:07:19 -0500 Subject: [PATCH 4/4] update --- components/rejoiner/rejoiner.app.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/components/rejoiner/rejoiner.app.mjs b/components/rejoiner/rejoiner.app.mjs index 20141c6250dd3..0a98dd63ff46c 100644 --- a/components/rejoiner/rejoiner.app.mjs +++ b/components/rejoiner/rejoiner.app.mjs @@ -106,7 +106,6 @@ export default { "Authorization": `Rejoiner ${this.$auth.api_key}`, "Content-Type": "application/json", }, - debug: true, ...otherOpts, }); },