Skip to content

New Components - moaform #14133

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 7 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions components/moaform/common/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const RETENTION_DAYS_OPTIONS = [
{
label: "1",
value: 1,
},
{
label: "3",
value: 3,
},
{
label: "5",
value: 5,
},
{
label: "7",
value: 7,
},
{
label: "10",
value: 10,
},
{
label: "15",
value: 15,
},
{
label: "30",
value: 30,
},
];
67 changes: 62 additions & 5 deletions components/moaform/moaform.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,68 @@
import { axios } from "@pipedream/platform";

export default {
type: "app",
app: "moaform",
propDefinitions: {},
propDefinitions: {
formId: {
type: "string",
label: "Form ID",
description: "The ID of the form to monitor for new submissions",
async options({ page }) {
const { items } = await this.getForms({
params: {
page: page + 1,
},
});
return items.map(({
id: value, title: label,
}) => ({
label,
value,
}));
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://api.moaform.com/v1";
},
_headers() {
return {
Authorization: `Bearer ${this.$auth.api_key}`,
};
},
_makeRequest({
$ = this, path, ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(),
...opts,
});
},
getForms(opts = {}) {
return this._makeRequest({
...opts,
path: "/forms",
});
},
createWebhook({
formId, ...opts
}) {
return this._makeRequest({
method: "POST",
path: `/forms/${formId}/webhooks`,
...opts,
});
},
deleteWebhook({
formId, webhookId,
}) {
return this._makeRequest({
method: "DELETE",
path: `/forms/${formId}/webhooks/${webhookId}`,
});
},
},
};
};
7 changes: 5 additions & 2 deletions components/moaform/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/moaform",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream Moaform Components",
"main": "moaform.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <support@pipedream.com> (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,100 @@
import crypto from "crypto";
import { RETENTION_DAYS_OPTIONS } from "../../common/constants.mjs";
import moaform from "../../moaform.app.mjs";
import sampleEmit from "./test-event.mjs";

export default {
key: "moaform-new-submission-instant",
name: "New Submission (Instant)",
description: "Emit new event every time a new form submission is received.",
version: "0.0.1",
type: "source",
dedupe: "unique",
props: {
moaform,
http: {
type: "$.interface.http",
customResponse: true,
},
db: "$.service.db",
formId: {
propDefinition: [
moaform,
"formId",
],
},
retentionDays: {
type: "integer",
label: "Retention Days",
description: "Resend restriction days",
options: RETENTION_DAYS_OPTIONS,
optional: true,
},
secret: {
type: "string",
label: "Secret Code",
description: "This code is used to verify that the data received at the specified endpoint has indeed been sent from Moaform and has not been tampered with.",
secret: true,
optional: true,
},
},
methods: {
_getWebhookId() {
return this.db.get("webhookId");
},
_setWebhookId(id) {
this.db.set("webhookId", id);
},
},
hooks: {
async activate() {
const response = await this.moaform.createWebhook({
formId: this.formId,
data: {
endpoint: this.http.endpoint,
enabled: true,
secret: this.secret,
verify_ssl: true,
retention_days: this.retentionDays,
},
});
this._setWebhookId(response.id);
},
async deactivate() {
const webhookId = this.db.get("webhookId");
await this.moaform.deleteWebhook({
formId: this.formId,
webhookId,
});
},
},
async run({
bodyRaw, body, headers,
}) {

if (this.secret) {
const signature = headers["moaform-signature"];
const receivedSig = signature.split("sha256=")[1];

const calculatedSig = crypto
.createHmac("sha256", this.secret)
.update(bodyRaw)
.digest("base64");

if (receivedSig !== calculatedSig) {
return this.http.respond({
status: 401,
body: "Unauthorized",
});
}
}

const ts = Date.parse(body.submitted_at);
this.$emit(body, {
id: body.event_id,
summary: `New submission received for form ${this.formId}`,
ts: ts,
});
},
sampleEmit,
};
18 changes: 18 additions & 0 deletions components/moaform/sources/new-submission-instant/test-event.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default {
"event_id": "105fa72f-9e6e-4667-8ead-22b0a3ed254a",
"event_type": "response_completed",
"hidden": {},
"response_id": "test#123-45-67890",
"submitted_at": "2024-09-27T21:00:55Z",
"form": {
"id": "MG7eAk",
"title": "form title",
"report_url": "https://www.moaform.com/reports/q945EegynYD8G2xM",
"answer_url": "https://moaform.com/q/not-started-collecting"
},
"answers": [],
"thankyou": {
"id": "cm1l7axyf2t9x0fqrly27vdn7",
"url": "https://answer.moaform.com/answers/MG7eAk/thankyou/Wo2b1Z09wx5"
}
}
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

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

Loading