From d910115d40f675bd05292245f6d9c121fb5c118f Mon Sep 17 00:00:00 2001 From: Luan Cazarine Date: Wed, 12 Nov 2025 20:45:55 -0300 Subject: [PATCH] Enhance Aidaform component with new sources for monitoring form creation and responses. Added `New Form Created` and `New Form Response` sources, enabling event emission for new forms and responses. Updated `aidaform.app.mjs` with new prop definitions and methods for form management. Bump version to 0.7.0 in package.json. --- components/aidaform/aidaform.app.mjs | 81 ++++++++++++++++++- components/aidaform/package.json | 2 +- .../new-form-response/new-form-response.mjs | 76 +++++++++++++++++ .../aidaform/sources/new-form/new-form.mjs | 60 ++++++++++++++ 4 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 components/aidaform/sources/new-form-response/new-form-response.mjs create mode 100644 components/aidaform/sources/new-form/new-form.mjs diff --git a/components/aidaform/aidaform.app.mjs b/components/aidaform/aidaform.app.mjs index 25c2cf94cb166..f988cb0a0e66c 100644 --- a/components/aidaform/aidaform.app.mjs +++ b/components/aidaform/aidaform.app.mjs @@ -1,11 +1,84 @@ +import { axios } from "@pipedream/platform"; +const LIMIT = 100; + export default { type: "app", app: "aidaform", - propDefinitions: {}, + propDefinitions: { + formId: { + type: "string", + label: "Form ID", + description: "The ID of the form to monitor for new responses", + async options() { + const { items } = await this.listForms(); + return items.map(({ + id, data: { name }, + }) => ({ + label: name, + value: id, + })); + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _apiUrl() { + return "https://api.aidaform.com/v1"; + }, + _getHeaders() { + return { + "Authorization": `${this.$auth.api_key}`, + }; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: `${this._apiUrl()}/${path}`, + headers: this._getHeaders(), + ...opts, + }); + }, + listForms(args = {}) { + return this._makeRequest({ + path: "forms", + ...args, + }); + }, + listResponses({ + formId, ...args + }) { + return this._makeRequest({ + path: `forms/${formId}/responses`, + ...args, + }); + }, + async *paginate({ + fn, params = {}, maxResults = null, ...opts + }) { + let hasMore = null; + let count = 0; + + do { + params.limit = LIMIT; + params.marker = hasMore; + const { + items, marker: nextMarker, + } = await fn({ + params, + ...opts, + }); + + for (const d of items) { + yield d; + + if (maxResults && ++count === maxResults) { + return count; + } + } + + hasMore = nextMarker; + + } while (hasMore); }, }, }; diff --git a/components/aidaform/package.json b/components/aidaform/package.json index 82b668ef1dc22..2c274860dde26 100644 --- a/components/aidaform/package.json +++ b/components/aidaform/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/aidaform", - "version": "0.6.0", + "version": "0.7.0", "description": "Pipedream aidaform Components", "main": "aidaform.app.mjs", "keywords": [ diff --git a/components/aidaform/sources/new-form-response/new-form-response.mjs b/components/aidaform/sources/new-form-response/new-form-response.mjs new file mode 100644 index 0000000000000..ba5fbdfa5dff8 --- /dev/null +++ b/components/aidaform/sources/new-form-response/new-form-response.mjs @@ -0,0 +1,76 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import aidaform from "../../aidaform.app.mjs"; + +export default { + key: "aidaform-new-form-response", + name: "New Form Response", + description: "Emit new event when a new form is responded to. [See the documentation](https://app.swaggerhub.com/apis/aidaform/AidaForm/1.1.0#/default/RetrieveResponsesList)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + aidaform, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + formId: { + propDefinition: [ + aidaform, + "formId", + ], + }, + }, + methods: { + _getLastDate() { + return this.db.get("lastDate"); + }, + _setLastDate(lastDate) { + this.db.set("lastDate", lastDate); + }, + async emitEvent(maxResults = false) { + const lastDate = this._getLastDate() || 0; + const response = this.aidaform.paginate({ + fn: this.aidaform.listResponses, + formId: this.formId, + params: { + from: lastDate, + }, + }); + + let responseArray = []; + for await (const item of response) { + responseArray.push(item); + } + + responseArray = responseArray + .sort((a, b) => b.created_at - a.created_at); + + if (responseArray.length) { + if (maxResults && (responseArray.length > maxResults)) { + responseArray.length = maxResults; + } + this._setLastDate(Date.parse(responseArray[0].created_at)); + } + + for (const item of responseArray.reverse()) { + this.$emit(item, { + id: item.id, + summary: `New form response with ID: ${item.id}`, + ts: Date.parse(item.created_at), + }); + } + }, + }, + hooks: { + async deploy() { + await this.emitEvent(25); + }, + }, + async run() { + await this.emitEvent(); + }, +}; diff --git a/components/aidaform/sources/new-form/new-form.mjs b/components/aidaform/sources/new-form/new-form.mjs new file mode 100644 index 0000000000000..80b7f856a5fb5 --- /dev/null +++ b/components/aidaform/sources/new-form/new-form.mjs @@ -0,0 +1,60 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import aidaform from "../../aidaform.app.mjs"; + +export default { + key: "aidaform-new-form", + name: "New Form Created", + description: "Emit new event when a new form is created. [See the documentation](https://app.swaggerhub.com/apis/aidaform/AidaForm/1.1.0#/default/RetrieveFormsList)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + aidaform, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + }, + methods: { + _getLastDate() { + return this.db.get("lastDate"); + }, + _setLastDate(lastDate) { + this.db.set("lastDate", lastDate); + }, + async emitEvent(maxResults = false) { + const lastDate = this._getLastDate() || 0; + let { items: responseArray } = await this.aidaform.listForms(); + + responseArray = responseArray + .filter((item) => item.created_at > lastDate) + .sort((a, b) => b.created_at - a.created_at); + + if (responseArray.length) { + if (maxResults && (responseArray.length > maxResults)) { + responseArray.length = maxResults; + } + this._setLastDate(Date.parse(responseArray[0].created_at)); + } + + for (const item of responseArray.reverse()) { + this.$emit(item, { + id: item.id, + summary: `New form created: ${item.data.name}`, + ts: Date.parse(item.created_at), + }); + } + }, + }, + hooks: { + async deploy() { + await this.emitEvent(25); + }, + }, + async run() { + await this.emitEvent(); + }, +};