-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
new-submission.mjs
90 lines (87 loc) · 2.2 KB
/
new-submission.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import jotform from "../../jotform.app.mjs";
import sampleEmit from "./test-event.mjs";
export default {
key: "jotform-new-submission",
name: "New Submission (Instant)",
description: "Emit new event when a form is submitted",
version: "0.1.5",
type: "source",
dedupe: "unique",
props: {
jotform,
http: "$.interface.http",
teamId: {
propDefinition: [
jotform,
"teamId",
],
},
formId: {
propDefinition: [
jotform,
"formId",
(c) => ({
teamId: c.teamId,
excludeDeleted: true,
}),
],
},
},
hooks: {
async deploy() {
const { content: form } = await this.jotform.getForm(this.formId, this.teamId);
const { content: submissions } = await this.jotform.getFormSubmissions({
formId: this.formId,
teamId: this.teamId,
params: {
limit: 25,
orderby: "created_at",
},
});
for (let submission of submissions.reverse()) {
const meta = {
id: submission.id,
summary: form.title,
ts: Date.now(),
};
this.$emit(submission, meta);
}
},
async activate() {
return (await this.jotform.createHook({
endpoint: this.http.endpoint,
formId: this.formId,
teamId: this.teamId,
}));
},
async deactivate() {
return (await this.jotform.deleteHook({
endpoint: this.http.endpoint,
formId: this.formId,
teamId: this.teamId,
}));
},
},
async run(event) {
const { body } = event;
let { content: submission } = await this.jotform.getFormSubmission({
submissionId: body.submissionID,
teamId: this.teamId,
});
// insert answers from the webhook event
const rawRequest = JSON.parse(body.rawRequest);
for (const key of Object.keys(rawRequest)) {
const regex = /^q(\d+)_/;
const match = key.match(regex);
if (match && match[1]) {
submission.answers[match[1]].answer = rawRequest[key];
}
}
this.$emit(submission, {
summary: body.formTitle || JSON.stringify(body),
id: body.submissionID,
ts: Date.now(),
});
},
sampleEmit,
};