-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Enhance Aidaform component with new sources for monitoring form creat… #19055
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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, | ||
| }); | ||
luancazarine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for (const d of items) { | ||
| yield d; | ||
|
|
||
| if (maxResults && ++count === maxResults) { | ||
| return count; | ||
| } | ||
| } | ||
|
|
||
| hasMore = nextMarker; | ||
|
|
||
| } while (hasMore); | ||
| }, | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
components/aidaform/sources/new-form-response/new-form-response.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| }, | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.