Skip to content

Commit ae34fcd

Browse files
committed
[Components] openum
1 parent 0cae7a4 commit ae34fcd

File tree

4 files changed

+218
-7
lines changed

4 files changed

+218
-7
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import {
2+
ConfigurationError, getFileStreamAndMetadata,
3+
} from "@pipedream/platform";
4+
import app from "../../openum.app.mjs";
5+
import utils from "../../common/utils.mjs";
6+
7+
export default {
8+
key: "openum-start-signature",
9+
name: "Start Signature",
10+
description: "Initiates an electronic document delivery process to recipient(s). [See the documentation](https://api.lleida.net/dtd/openum/v1/en/)",
11+
version: "0.0.1",
12+
type: "action",
13+
annotations: {
14+
readOnlyHint: false,
15+
destructiveHint: false,
16+
openWorldHint: true,
17+
},
18+
props: {
19+
app,
20+
requestId: {
21+
type: "string",
22+
label: "Request ID",
23+
description: "Optional reference of the request.",
24+
optional: true,
25+
},
26+
configId: {
27+
propDefinition: [
28+
app,
29+
"configId",
30+
],
31+
},
32+
contractId: {
33+
type: "string",
34+
label: "Contract ID",
35+
description: "Client reference of the process. This reference is provided as the unique identifier for the process. If `auto_cancel` is enabled in the configuration and an existing reference is specified, the previous pending process will be automatically canceled.",
36+
},
37+
levels: {
38+
type: "string",
39+
label: "Levels",
40+
description: `Definition of the viewing level/order as a JSON array. Each level object supports: \`level_order\` (integer, 0 to N), \`required_signatories_to_complete_level\` (optional integer), and \`signatories\` (array of recipient objects with fields: \`phone\`, \`email\`, \`name\`, \`surname\`, \`id_number\`, \`landing_access_methods\`, \`landing_access_code\`, \`landing_information\`). The maximum is 20 recipients across all levels.
41+
42+
**Example**:
43+
\`\`\`json
44+
[
45+
{
46+
"level_order": 0,
47+
"signatories": [{
48+
"email": "user@example.com",
49+
"name": "John",
50+
"surname": "Doe",
51+
"phone": "+34666666666"
52+
}]
53+
}
54+
]
55+
\`\`\`
56+
`,
57+
},
58+
filePaths: {
59+
type: "string[]",
60+
label: "Files",
61+
description: "PDF files to deliver to the recipients. Provide paths to files in the `/tmp` directory (e.g. `/tmp/contract.pdf`) or direct URLs to PDF files. Only PDF format is accepted. Maximum 20 files, 25 MB total.",
62+
},
63+
syncDir: {
64+
type: "dir",
65+
accessMode: "read",
66+
sync: true,
67+
},
68+
},
69+
async run({ $ }) {
70+
const {
71+
app,
72+
requestId,
73+
configId,
74+
contractId,
75+
levels,
76+
filePaths,
77+
} = this;
78+
79+
const parsedLevels = utils.parseJson(levels);
80+
81+
const invalidFiles = filePaths.filter((filePath) => !utils.isPdf(filePath));
82+
if (invalidFiles.length) {
83+
throw new ConfigurationError(`Only PDF files are allowed. Invalid files: ${invalidFiles.join(", ")}`);
84+
}
85+
86+
const files = await Promise.all(
87+
filePaths.map(async (filePath) => {
88+
const {
89+
stream, metadata,
90+
} = await getFileStreamAndMetadata(filePath);
91+
const content = await utils.streamToBase64Url(stream);
92+
return {
93+
filename: metadata.name,
94+
content,
95+
sign_on_landing: "Y",
96+
};
97+
}),
98+
);
99+
100+
const response = await app.startSignature({
101+
$,
102+
data: {
103+
request: "START_SIGNATURE",
104+
...(requestId && {
105+
request_id: requestId,
106+
}),
107+
user: app.getUsername(),
108+
signature: {
109+
config_id: configId,
110+
contract_id: contractId,
111+
level: parsedLevels,
112+
file: files,
113+
},
114+
},
115+
});
116+
117+
$.export("$summary", `Successfully started signature process with ID \`${response.signature?.signature_id}\``);
118+
return response;
119+
},
120+
};

components/openum/common/utils.mjs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { ConfigurationError } from "@pipedream/platform";
2+
3+
const parseJson = (value) => {
4+
if (typeof value !== "string") {
5+
return value;
6+
}
7+
try {
8+
return JSON.parse(value);
9+
} catch {
10+
throw new ConfigurationError("Must be a valid JSON value.");
11+
}
12+
};
13+
14+
const isPdf = (filePath) => {
15+
const path = filePath.split("?")[0].split("#")[0];
16+
return path.toLowerCase().endsWith(".pdf");
17+
};
18+
19+
const streamToBase64Url = async (stream) => {
20+
const chunks = [];
21+
for await (const chunk of stream) {
22+
chunks.push(chunk);
23+
}
24+
return Buffer.concat(chunks).toString("base64url");
25+
};
26+
27+
export default {
28+
isPdf,
29+
parseJson,
30+
streamToBase64Url,
31+
};

components/openum/openum.app.mjs

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,71 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "openum",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
configId: {
8+
type: "string",
9+
label: "Config ID",
10+
description: "Numerical ID of the configuration.",
11+
async options() {
12+
const { config } = await this.getConfigList({
13+
data: {
14+
request: "GET_CONFIG_LIST",
15+
user: this.getUsername(),
16+
status: "enabled",
17+
},
18+
});
19+
return config
20+
.map(({
21+
config_id: value, name,
22+
}) => ({
23+
label: name || value,
24+
value,
25+
}));
26+
},
27+
},
28+
},
529
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
30+
getUsername() {
31+
return this.$auth.username;
32+
},
33+
_baseUrl() {
34+
return "https://api.lleida.net/cs/v1";
35+
},
36+
_headers() {
37+
return {
38+
"Authorization": `x-api-key ${this.$auth.api_key}`,
39+
"Content-Type": "application/json; charset=utf-8",
40+
"Accept": "application/json",
41+
};
42+
},
43+
async _makeRequest({
44+
$ = this, path, ...opts
45+
}) {
46+
const response = await axios($, {
47+
url: `${this._baseUrl()}${path}`,
48+
headers: this._headers(),
49+
...opts,
50+
});
51+
if (response.code !== 200) {
52+
throw new Error(JSON.stringify(response, null, 2));
53+
}
54+
return response;
55+
},
56+
getConfigList(opts = {}) {
57+
return this._makeRequest({
58+
path: "/get_config_list",
59+
method: "POST",
60+
...opts,
61+
});
62+
},
63+
startSignature(opts = {}) {
64+
return this._makeRequest({
65+
path: "/start_signature",
66+
method: "POST",
67+
...opts,
68+
});
969
},
1070
},
11-
};
71+
};

components/openum/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/openum",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Openum Components",
55
"main": "openum.app.mjs",
66
"keywords": [
@@ -12,4 +12,4 @@
1212
"publishConfig": {
1313
"access": "public"
1414
}
15-
}
15+
}

0 commit comments

Comments
 (0)