Skip to content

Commit a1ee41c

Browse files
michelle0927lcaresia
authored andcommitted
New Components - gocanvas (#14544)
* init * wip * new components * pnpm.lock.yaml * fix import statement
1 parent cadde76 commit a1ee41c

File tree

7 files changed

+447
-4
lines changed

7 files changed

+447
-4
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import gocanvas from "../../gocanvas.app.mjs";
2+
3+
export default {
4+
key: "gocanvas-create-dispatch",
5+
name: "Create Dispatch",
6+
description: "Creates a dispatch item in GoCanvas. [See the documentation](https://help.gocanvas.com/hc/en-us/article_attachments/26468076609559)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
gocanvas,
11+
form: {
12+
propDefinition: [
13+
gocanvas,
14+
"form",
15+
],
16+
description: "The name of the form you want to create a prepopulated submission for",
17+
},
18+
entries: {
19+
type: "object",
20+
label: "Entries",
21+
description: `DIEntry elements consisting of label/value pairs.
22+
\n Label: Either the Export Label or plain Label field attribute (caseinsensitive) as defined in the form builder.
23+
\n Value: The value assigned to this Dispatch Item entry.
24+
`,
25+
},
26+
},
27+
async run({ $ }) {
28+
let entriesString = "";
29+
for (const [
30+
key,
31+
value,
32+
] of Object.entries(this.entries)) {
33+
entriesString += `<DIEntry Label="${key}" Value="${value}"/>`;
34+
}
35+
const response = await this.gocanvas.dispatchItems({
36+
$,
37+
data: `
38+
<?xml version="1.0" encoding="utf-8"?>
39+
<List>
40+
<DI FormName="${this.form}">
41+
${entriesString}
42+
</DI>
43+
</List>
44+
`,
45+
});
46+
$.export("$summary", `Successfully created dispatch item in ${this.form}`);
47+
return response;
48+
},
49+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import gocanvas from "../../gocanvas.app.mjs";
2+
import { parse } from "csv-parse/sync";
3+
import { ConfigurationError } from "@pipedream/platform";
4+
5+
export default {
6+
key: "gocanvas-create-or-update-reference-data",
7+
name: "Create or Update Reference Data",
8+
description: "Creates or updates GoCanvas reference data. [See the documentation](https://help.gocanvas.com/hc/en-us/article_attachments/26468076609559)",
9+
version: "0.0.1",
10+
type: "action",
11+
props: {
12+
gocanvas,
13+
name: {
14+
type: "string",
15+
label: "Reference Data Name",
16+
description: "The attribute name of the dataset to operate on. Will be created if it doesn't already exist.",
17+
},
18+
data: {
19+
type: "string",
20+
label: "Data",
21+
description: `A string of comma separated values representing the data to create/update. **Include Column names**:
22+
\n Example:
23+
\n Column1,Column2,Column3
24+
\n Data1Column1,Data1Column2,Data1Column3
25+
\n Data2Column1,Data2Column2,Data3Column3
26+
`,
27+
},
28+
},
29+
methods: {
30+
csvToXml(data) {
31+
const records = parse(data, {
32+
columns: true,
33+
trim: true,
34+
});
35+
36+
if (!records?.length) {
37+
throw new ConfigurationError("No data items found to create/update. Please enter column names and at least 1 row of data.");
38+
}
39+
40+
// Extract columns
41+
const columns = Object.keys(records[0]);
42+
let result = "<Columns>";
43+
result += columns.map((col) => `<c>${col}</c>`).join("");
44+
result += "</Columns>\n<Rows>\n";
45+
46+
// Extract rows
47+
result += records
48+
.map((row) => {
49+
const rowValues = columns.map((col) => `<v>${row[col]}</v>`).join("");
50+
return ` <r>${rowValues}</r>`;
51+
})
52+
.join("\n");
53+
54+
result += "\n</Rows>";
55+
return result;
56+
},
57+
},
58+
async run({ $ }) {
59+
const response = await this.gocanvas.createUpdateReferenceData({
60+
$,
61+
data: `
62+
<?xml version="1.0" encoding="utf-8"?>
63+
<List Name="${this.name}">
64+
${await this.csvToXml(this.data)}
65+
</List>
66+
`,
67+
});
68+
$.export("$summary", "Successfully created/updated reference data");
69+
return response;
70+
},
71+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import gocanvas from "../../gocanvas.app.mjs";
2+
3+
export default {
4+
key: "gocanvas-delete-dispatch",
5+
name: "Delete Dispatch",
6+
description: "Removes a specific dispatch from GoCanvas. [See the documentation](https://help.gocanvas.com/hc/en-us/article_attachments/26468076609559)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
gocanvas,
11+
form: {
12+
propDefinition: [
13+
gocanvas,
14+
"form",
15+
],
16+
},
17+
dispatchId: {
18+
propDefinition: [
19+
gocanvas,
20+
"dispatchId",
21+
(c) => ({
22+
form: c.form,
23+
}),
24+
],
25+
},
26+
},
27+
async run({ $ }) {
28+
const description = await this.gocanvas.getDispatchDescription({
29+
$,
30+
dispatchId: this.dispatchId,
31+
});
32+
const response = await this.gocanvas.dispatchItems({
33+
$,
34+
data: `
35+
<?xml version="1.0" encoding="utf-8"?>
36+
<List>
37+
<DI FormName="${this.form}" Action="Delete" OriginalDescription="${description}">
38+
<DIEntry Label="Date" Value="${Date.now()}"/>
39+
</DI>
40+
</List>
41+
`,
42+
});
43+
$.export("$summary", `Successfully deleted dispatch with ID ${this.dispatchId}`);
44+
return response;
45+
},
46+
};
Lines changed: 144 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,151 @@
1+
import { axios } from "@pipedream/platform";
2+
import xml2js from "xml2js";
3+
14
export default {
25
type: "app",
36
app: "gocanvas",
4-
propDefinitions: {},
7+
propDefinitions: {
8+
form: {
9+
type: "string",
10+
label: "Form",
11+
description: "The identifier of a form",
12+
async options() {
13+
const forms = await this.listForms();
14+
return forms?.map((form) => form.Name[0]) || [];
15+
},
16+
},
17+
dispatchId: {
18+
type: "string",
19+
label: "Dispatch ID",
20+
description: "Identifier of a dispatch",
21+
async options({
22+
page, form,
23+
}) {
24+
const dispatches = await this.getActiveDispatches({
25+
form,
26+
data: {
27+
page: page + 1,
28+
},
29+
});
30+
return dispatches?.map(({
31+
$, Description: desc,
32+
}) => ({
33+
value: $.Id,
34+
label: desc[0],
35+
})) || [];
36+
},
37+
},
38+
},
539
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
40+
_baseUrl() {
41+
return "https://www.gocanvas.com/apiv2/";
42+
},
43+
_makeRequest(opts = {}) {
44+
const {
45+
$ = this,
46+
path,
47+
params,
48+
...otherOpts
49+
} = opts;
50+
return axios($, {
51+
...otherOpts,
52+
url: `${this._baseUrl()}${path}`,
53+
params: {
54+
...params,
55+
username: `${this.$auth.username}`,
56+
},
57+
headers: {
58+
Authorization: `Bearer ${this.$auth.api_key}`,
59+
},
60+
});
61+
},
62+
async getActiveDispatches({
63+
form, ...opts
64+
}) {
65+
const response = await this._makeRequest({
66+
path: "/dispatch_export",
67+
...opts,
68+
});
69+
const { CanvasResult: { Dispatches: dispatches } } = await new xml2js
70+
.Parser().parseStringPromise(response);
71+
if (!dispatches?.length) {
72+
return [];
73+
}
74+
return dispatches
75+
.flatMap((d) => d.Dispatch || [])
76+
.filter((d) => d.Status[0] !== "deleted")
77+
.filter((d) => !form || d.Form[0] === form);
78+
},
79+
async listForms(opts = {}) {
80+
const response = await this._makeRequest({
81+
path: "/forms",
82+
...opts,
83+
});
84+
const { CanvasResult: { Forms: forms } } = await new xml2js
85+
.Parser().parseStringPromise(response);
86+
if (!forms?.length) {
87+
return [];
88+
}
89+
return forms.flatMap((form) => form.Form || []);
90+
},
91+
async listSubmissions(opts = {}) {
92+
const response = await this._makeRequest({
93+
path: "/submissions",
94+
...opts,
95+
});
96+
const { CanvasResult: { Submissions: submissions } } = await new xml2js
97+
.Parser().parseStringPromise(response);
98+
if (!submissions?.length) {
99+
return [];
100+
}
101+
return submissions.flatMap((sub) => sub.Submission || []);
102+
},
103+
async getDispatchDescription({
104+
dispatchId, ...opts
105+
}) {
106+
const dispatches = await this.getActiveDispatches({
107+
...opts,
108+
});
109+
const dispatch = dispatches.find(({ $ }) => $.Id === dispatchId);
110+
return dispatch.Description[0];
111+
},
112+
dispatchItems(opts = {}) {
113+
return this._makeRequest({
114+
method: "POST",
115+
path: "/dispatch_items",
116+
...opts,
117+
});
118+
},
119+
createUpdateReferenceData(opts = {}) {
120+
return this._makeRequest({
121+
method: "POST",
122+
path: "/reference_datas",
123+
...opts,
124+
});
125+
},
126+
async *paginate({
127+
fn,
128+
params,
129+
max,
130+
}) {
131+
params = {
132+
...params,
133+
page: 1,
134+
};
135+
let total, count = 0;
136+
do {
137+
const results = await fn({
138+
params,
139+
});
140+
for (const item of results) {
141+
yield item;
142+
if (max && ++count >= max) {
143+
return;
144+
}
145+
}
146+
total = results?.length;
147+
params.page++;
148+
} while (total);
9149
},
10150
},
11151
};

components/gocanvas/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@pipedream/gocanvas",
3+
"version": "0.0.1",
4+
"description": "Pipedream GoCanvas Components",
5+
"main": "gocanvas.app.mjs",
6+
"keywords": [
7+
"pipedream",
8+
"gocanvas"
9+
],
10+
"homepage": "https://pipedream.com/apps/gocanvas",
11+
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
12+
"publishConfig": {
13+
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3",
17+
"csv-parse": "^5.5.6",
18+
"xml2js": "^0.6.2"
19+
}
20+
}

0 commit comments

Comments
 (0)