diff --git a/components/formcarry/formcarry.app.mjs b/components/formcarry/formcarry.app.mjs index 28b0481b10085..ef2b929af8e18 100644 --- a/components/formcarry/formcarry.app.mjs +++ b/components/formcarry/formcarry.app.mjs @@ -1,11 +1,63 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "formcarry", - propDefinitions: {}, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://formcarry.com/api"; + }, + _headers() { + return { + api_key: `${this.$auth.api_key}`, + }; + }, + _makeRequest({ + $ = this, + path, + ...opts + }) { + return axios($, { + url: `${this._baseUrl()}${path}`, + headers: this._headers(), + ...opts, + }); + }, + listSubmissions({ + formId, ...opts + }) { + return this._makeRequest({ + path: `/form/${formId}/submissions`, + ...opts, + }); + }, + async *paginate({ + fn, + args = {}, + resourceKey, + max, + }) { + args = { + ...args, + params: { + ...args?.params, + limit: 50, + page: 1, + }, + }; + let nextPage, count = 0; + do { + const response = await fn(args); + const items = response[resourceKey]; + for (const item of items) { + yield item; + if (max && ++count >= max) { + return; + } + } + nextPage = response.pagination.next_page; + args.params.page = nextPage; + } while (nextPage); }, }, }; diff --git a/components/formcarry/package.json b/components/formcarry/package.json new file mode 100644 index 0000000000000..ae0f801777cf4 --- /dev/null +++ b/components/formcarry/package.json @@ -0,0 +1,18 @@ +{ + "name": "@pipedream/formcarry", + "version": "0.1.0", + "description": "Pipedream Formcarry Components", + "main": "formcarry.app.mjs", + "keywords": [ + "pipedream", + "formcarry" + ], + "homepage": "https://pipedream.com/apps/formcarry", + "author": "Pipedream (https://pipedream.com/)", + "publishConfig": { + "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.0.3" + } +} diff --git a/components/formcarry/sources/new-form-submission/new-form-submission.mjs b/components/formcarry/sources/new-form-submission/new-form-submission.mjs new file mode 100644 index 0000000000000..99e75453e5caa --- /dev/null +++ b/components/formcarry/sources/new-form-submission/new-form-submission.mjs @@ -0,0 +1,82 @@ +import formcarry from "../../formcarry.app.mjs"; +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; + +export default { + key: "formcarry-new-form-submission", + name: "New Form Submission", + description: "Emit new event when the specified form receives a new submission. [See the documentation](https://formcarry.com/docs/formcarry-api/submissions-api#cc7f3010897b4c938c8829db46b18656)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + formcarry, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + formId: { + type: "string", + label: "Form ID", + description: "The ID of the form to watch for new submissions", + }, + }, + hooks: { + async deploy() { + await this.processEvent(25); + }, + }, + methods: { + _getLastTs() { + return this.db.get("lastTs") || 0; + }, + _setLastTs(lastTs) { + this.db.set("lastTs", lastTs); + }, + generateMeta(submission) { + return { + id: submission._id, + summary: `New Form Submission ID: ${submission._id}`, + ts: Date.parse(submission.createdAt), + }; + }, + async processEvent(max) { + const lastTs = this._getLastTs(); + + const results = this.formcarry.paginate({ + fn: this.formcarry.listSubmissions, + args: { + formId: this.formId, + }, + resourceKey: "submissions", + max, + }); + + const submissions = []; + for await (const item of results) { + const ts = Date.parse(item.createdAt); + if (ts >= lastTs) { + submissions.push(item); + } else { + break; + } + } + + if (!submissions.length) { + return; + } + + this._setLastTs(Date.parse(submissions[0].createdAt)); + + submissions.forEach((submission) => { + const meta = this.generateMeta(submission); + this.$emit(submission, meta); + }); + }, + }, + async run() { + await this.processEvent(); + }, +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4f738769868e4..a41569ce3a87d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3561,6 +3561,12 @@ importers: components/formcan: specifiers: {} + components/formcarry: + specifiers: + '@pipedream/platform': ^3.0.3 + dependencies: + '@pipedream/platform': 3.0.3 + components/formdesk: specifiers: {}