Skip to content

Commit

Permalink
New Components - formcarry (#14277)
Browse files Browse the repository at this point in the history
* new component

* pnpm-lock.yaml

* Update components/formcarry/package.json

Co-authored-by: Luan Cazarine <[email protected]>

---------

Co-authored-by: Luan Cazarine <[email protected]>
  • Loading branch information
michelle0927 and luancazarine authored Oct 15, 2024
1 parent 653b81d commit 792d115
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 4 deletions.
60 changes: 56 additions & 4 deletions components/formcarry/formcarry.app.mjs
Original file line number Diff line number Diff line change
@@ -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);
},
},
};
18 changes: 18 additions & 0 deletions components/formcarry/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^3.0.3"
}
}
Original file line number Diff line number Diff line change
@@ -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();
},
};
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 792d115

Please sign in to comment.