|
| 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 | +}; |
0 commit comments