Skip to content

Commit b4cfd53

Browse files
authored
New Components - goformz (#17378)
* new components * pnpm-lock.yaml
1 parent ea28f84 commit b4cfd53

File tree

6 files changed

+340
-7
lines changed

6 files changed

+340
-7
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import goformz from "../../goformz.app.mjs";
2+
import { parseObject } from "../../common/utils.mjs";
3+
import { ConfigurationError } from "@pipedream/platform";
4+
5+
export default {
6+
key: "goformz-create-form",
7+
name: "Create Form",
8+
description: "Create a new form in GoFormz. [See the documentation](https://developers.goformz.com/reference/create-a-form)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
goformz,
13+
name: {
14+
type: "string",
15+
label: "Name",
16+
description: "The name of the form",
17+
},
18+
templateId: {
19+
propDefinition: [
20+
goformz,
21+
"templateId",
22+
],
23+
reloadProps: true,
24+
},
25+
userId: {
26+
propDefinition: [
27+
goformz,
28+
"userId",
29+
],
30+
optional: true,
31+
},
32+
groupId: {
33+
propDefinition: [
34+
goformz,
35+
"groupId",
36+
],
37+
optional: true,
38+
},
39+
overrideDefaultFormName: {
40+
type: "boolean",
41+
label: "Override Default Form Name",
42+
description: "Set to `true` to override the automatic form name rules",
43+
optional: true,
44+
default: false,
45+
},
46+
},
47+
async additionalProps() {
48+
const props = {};
49+
if (!this.templateId) {
50+
return props;
51+
}
52+
props["alert"] = {
53+
type: "alert",
54+
alertType: "info",
55+
content: "See the [Form Field Reference](https://developers.goformz.com/reference/form-field-reference) for more information about form field types",
56+
};
57+
const { fields } = await this.goformz.getTemplate({
58+
templateId: this.templateId,
59+
});
60+
for (const field of Object.values(fields)) {
61+
props[field.id] = {
62+
type: "object",
63+
label: field.name,
64+
description: `Value for ${field.name}. Type: ${field.type}`,
65+
optional: true,
66+
};
67+
}
68+
return props;
69+
},
70+
async run({ $ }) {
71+
if (!this.userId && !this.groupId) {
72+
throw new ConfigurationError("Form must be assigned to a User or Group");
73+
}
74+
if (this.userId && this.groupId) {
75+
throw new ConfigurationError("Form can only be assigned to one of User or Group");
76+
}
77+
78+
const assignment = this.userId
79+
? {
80+
id: this.userId,
81+
type: "User",
82+
}
83+
: {
84+
id: this.groupId,
85+
type: "Group",
86+
};
87+
88+
const { fields } = await this.goformz.getTemplate({
89+
templateId: this.templateId,
90+
});
91+
92+
const fieldProps = {};
93+
for (const field of Object.values(fields)) {
94+
fieldProps[field.name] = parseObject(this[field.id]);
95+
}
96+
97+
const response = await this.goformz.createForm({
98+
$,
99+
data: {
100+
name: this.name,
101+
overrideDefaultFormName: this.overrideDefaultFormName,
102+
templateId: this.templateId,
103+
assignment,
104+
fields: fieldProps,
105+
},
106+
});
107+
$.export("$summary", `Successfully created form with ID: ${response.id}`);
108+
return response;
109+
},
110+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const parseObject = (obj) => {
2+
if (!obj) {
3+
return undefined;
4+
}
5+
if (typeof obj === "string") {
6+
try {
7+
return JSON.parse(obj);
8+
} catch (error) {
9+
return obj;
10+
}
11+
}
12+
if (Array.isArray(obj)) {
13+
return obj.map(parseObject);
14+
}
15+
if (typeof obj === "object") {
16+
return Object.fromEntries(Object.entries(obj).map(([
17+
key,
18+
value,
19+
]) => [
20+
key,
21+
parseObject(value),
22+
]));
23+
}
24+
return obj;
25+
};

components/goformz/goformz.app.mjs

Lines changed: 127 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,134 @@
1+
import { axios } from "@pipedream/platform";
2+
const DEFAULT_PAGE_SIZE = 25;
3+
14
export default {
25
type: "app",
36
app: "goformz",
4-
propDefinitions: {},
7+
propDefinitions: {
8+
templateId: {
9+
type: "string",
10+
label: "Template ID",
11+
description: "The ID of the template to use for the form",
12+
async options({ page }) {
13+
const templates = await this.listTemplates({
14+
params: {
15+
pageSize: DEFAULT_PAGE_SIZE,
16+
pageNumber: page + 1,
17+
},
18+
});
19+
return templates?.map((template) => ({
20+
label: template.name,
21+
value: template.id,
22+
})) || [];
23+
},
24+
},
25+
userId: {
26+
type: "string",
27+
label: "User ID",
28+
description: "The ID of the user to assign the form to",
29+
async options({ page }) {
30+
const users = await this.listUsers({
31+
params: {
32+
pageSize: DEFAULT_PAGE_SIZE,
33+
pageNumber: page + 1,
34+
},
35+
});
36+
return users?.map((user) => ({
37+
label: user.username,
38+
value: user.id,
39+
})) || [];
40+
},
41+
},
42+
groupId: {
43+
type: "string",
44+
label: "Group ID",
45+
description: "The ID of the group to assign the form to",
46+
async options({ page }) {
47+
const groups = await this.listGroups({
48+
params: {
49+
pageSize: DEFAULT_PAGE_SIZE,
50+
pageNumber: page + 1,
51+
},
52+
});
53+
return groups?.map((group) => ({
54+
label: group.name,
55+
value: group.id,
56+
})) || [];
57+
},
58+
},
59+
},
560
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
61+
_baseUrl() {
62+
return "https://api.goformz.com/v2";
63+
},
64+
_makeRequest({
65+
$ = this, path, ...opts
66+
}) {
67+
return axios($, {
68+
url: `${this._baseUrl()}${path}`,
69+
headers: {
70+
"Authorization": `Bearer ${this.$auth.oauth_access_token}`,
71+
"Content-Type": "application/json",
72+
},
73+
...opts,
74+
});
75+
},
76+
createWebhook(opts = {}) {
77+
return this._makeRequest({
78+
method: "POST",
79+
path: "/webhooks",
80+
...opts,
81+
});
82+
},
83+
deleteWebhook({
84+
hookId, ...opts
85+
}) {
86+
return this._makeRequest({
87+
method: "DELETE",
88+
path: `/webhooks/${hookId}`,
89+
...opts,
90+
});
91+
},
92+
getTemplate({
93+
templateId, ...opts
94+
}) {
95+
return this._makeRequest({
96+
path: `/templates/${templateId}`,
97+
...opts,
98+
});
99+
},
100+
getForm({
101+
formId, ...opts
102+
}) {
103+
return this._makeRequest({
104+
path: `/formz/${formId}`,
105+
...opts,
106+
});
107+
},
108+
listTemplates(opts = {}) {
109+
return this._makeRequest({
110+
path: "/templates",
111+
...opts,
112+
});
113+
},
114+
listUsers(opts = {}) {
115+
return this._makeRequest({
116+
path: "/users",
117+
...opts,
118+
});
119+
},
120+
listGroups(opts = {}) {
121+
return this._makeRequest({
122+
path: "/groups",
123+
...opts,
124+
});
125+
},
126+
createForm( opts = {}) {
127+
return this._makeRequest({
128+
method: "POST",
129+
path: "/formz",
130+
...opts,
131+
});
9132
},
10133
},
11134
};

components/goformz/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/goformz",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream GoFormz Components",
55
"main": "goformz.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.1.0"
1417
}
15-
}
18+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import goformz from "../../goformz.app.mjs";
2+
3+
export default {
4+
key: "goformz-new-form-completed",
5+
name: "New Form Completed",
6+
description: "Emit new event when a new form is completed in GoFormz",
7+
version: "0.0.1",
8+
type: "source",
9+
dedupe: "unique",
10+
props: {
11+
goformz,
12+
db: "$.service.db",
13+
http: "$.interface.http",
14+
templateId: {
15+
propDefinition: [
16+
goformz,
17+
"templateId",
18+
],
19+
description: "The ID of the template to watch for form completions",
20+
},
21+
},
22+
hooks: {
23+
async activate() {
24+
const { id } = await this.goformz.createWebhook({
25+
data: {
26+
eventType: "form.complete",
27+
targetUrl: this.http.endpoint,
28+
entityId: this.templateId,
29+
},
30+
});
31+
this._setHookId(id);
32+
},
33+
async deactivate() {
34+
const hookId = this._getHookId();
35+
if (hookId) {
36+
await this.goformz.deleteWebhook({
37+
hookId,
38+
});
39+
}
40+
},
41+
},
42+
methods: {
43+
_getHookId() {
44+
return this.db.get("hookId");
45+
},
46+
_setHookId(hookId) {
47+
this.db.set("hookId", hookId);
48+
},
49+
generateMeta(form) {
50+
return {
51+
id: form.formId,
52+
summary: `New Form Completed: ${form.name}`,
53+
ts: Date.now(),
54+
};
55+
},
56+
},
57+
async run(event) {
58+
const { body } = event;
59+
if (!body) {
60+
return;
61+
}
62+
const form = await this.goformz.getForm({
63+
formId: body.Item.Id,
64+
});
65+
const meta = this.generateMeta(form);
66+
this.$emit(form, meta);
67+
},
68+
};

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)