|
| 1 | +/* eslint-disable no-unused-vars */ |
| 2 | +import { |
| 3 | + ConfigurationError, |
| 4 | + getFileStreamAndMetadata, |
| 5 | +} from "@pipedream/platform"; |
| 6 | +import FormData from "form-data"; |
| 7 | +import { camelCaseToSnakeCase } from "../../common/utils.mjs"; |
| 8 | +import intelliprint from "../../intelliprint.app.mjs"; |
| 9 | + |
| 10 | +export default { |
| 11 | + key: "intelliprint-create-a-print-job", |
| 12 | + name: "Create a Print Job", |
| 13 | + description: "Creates a new print job in the Intelliprint API. [See the documentation](https://api-docs.intelliprint.net/?_gl=1*19r3k2k*_gcl_au*MTU2NDU2MDgzMS4xNzY0MDIwNDQx#print_jobs-create)", |
| 14 | + version: "0.0.1", |
| 15 | + type: "action", |
| 16 | + annotations: { |
| 17 | + destructiveHint: false, |
| 18 | + openWorldHint: true, |
| 19 | + readOnlyHint: false, |
| 20 | + }, |
| 21 | + props: { |
| 22 | + intelliprint, |
| 23 | + filePath: { |
| 24 | + type: "string", |
| 25 | + label: "File Path", |
| 26 | + description: "The file to upload. Provide either a file URL or a path to a file in the `/tmp` directory (for example, `/tmp/myFile.txt`)", |
| 27 | + }, |
| 28 | + reference: { |
| 29 | + type: "string", |
| 30 | + label: "Reference", |
| 31 | + description: "An user-provided reference for this Print Job.", |
| 32 | + optional: true, |
| 33 | + }, |
| 34 | + confirmed: { |
| 35 | + type: "boolean", |
| 36 | + label: "Confirmed", |
| 37 | + description: "Whether to confirm this Print Job immediately, or to leave it as a draft.", |
| 38 | + optional: true, |
| 39 | + }, |
| 40 | + testmode: { |
| 41 | + type: "boolean", |
| 42 | + label: "Test Mode", |
| 43 | + description: "Whether to mark this Print Job as a test.", |
| 44 | + optional: true, |
| 45 | + }, |
| 46 | + splittingMethod: { |
| 47 | + type: "string", |
| 48 | + label: "Splitting Method", |
| 49 | + description: "The method to use to split the Print Job into multiple Print Jobs.", |
| 50 | + options: [ |
| 51 | + "none", |
| 52 | + "split_on_phrase", |
| 53 | + "split_on_page", |
| 54 | + ], |
| 55 | + optional: true, |
| 56 | + }, |
| 57 | + splitOnPhrase: { |
| 58 | + type: "string", |
| 59 | + label: "Split On Phrase", |
| 60 | + description: "The word or phrase to split letters using. Only used when **Splitting Method** is set to `split_on_phrase`.", |
| 61 | + optional: true, |
| 62 | + }, |
| 63 | + splitOnPage: { |
| 64 | + type: "integer", |
| 65 | + label: "Split On Page", |
| 66 | + description: "The number of pages each letter should be. Only used when **Splitting Method** is set to `split_on_page`.", |
| 67 | + optional: true, |
| 68 | + }, |
| 69 | + doubleSided: { |
| 70 | + type: "string", |
| 71 | + label: "Double Sided", |
| 72 | + description: "Whether to print these letters double sided.", |
| 73 | + options: [ |
| 74 | + "no", |
| 75 | + "yes", |
| 76 | + "mixed", |
| 77 | + ], |
| 78 | + optional: true, |
| 79 | + }, |
| 80 | + doubleSidedSpecificPages: { |
| 81 | + type: "string", |
| 82 | + label: "Double Sided Specific Pages", |
| 83 | + description: "The array of pages to print double sided. Only used when printing.double_sided is set to `mixed`. Example: **[[1, 3], [6, 7]]**.", |
| 84 | + optional: true, |
| 85 | + }, |
| 86 | + premiumQuality: { |
| 87 | + type: "boolean", |
| 88 | + label: "Premium Quality", |
| 89 | + description: "Whether to print these letters in premium quality.", |
| 90 | + optional: true, |
| 91 | + }, |
| 92 | + postageService: { |
| 93 | + type: "string", |
| 94 | + label: "Postage Service", |
| 95 | + description: "The postage service to use for this Print Job.", |
| 96 | + options: [ |
| 97 | + "uk_first_class", |
| 98 | + "uk_second_class", |
| 99 | + "uk_first_class_signed_for", |
| 100 | + "uk_second_class_signed_for", |
| 101 | + "uk_special_delivery", |
| 102 | + "international", |
| 103 | + ], |
| 104 | + optional: true, |
| 105 | + }, |
| 106 | + idealEnvelope: { |
| 107 | + type: "string", |
| 108 | + label: "Ideal Envelope", |
| 109 | + description: "The ideal envelope size for these letters.", |
| 110 | + options: [ |
| 111 | + "c4", |
| 112 | + "c5", |
| 113 | + "c4_plus", |
| 114 | + "a4_box", |
| 115 | + ], |
| 116 | + optional: true, |
| 117 | + }, |
| 118 | + mailDate: { |
| 119 | + type: "string", |
| 120 | + label: "Mail Date", |
| 121 | + description: "The date to send this letter out on. Format: **YYYY-MM-DD**", |
| 122 | + optional: true, |
| 123 | + }, |
| 124 | + backgroundFirstPage: { |
| 125 | + type: "string", |
| 126 | + label: "Background First Page", |
| 127 | + description: "The ID of the Background to apply to the first page of these letters.", |
| 128 | + optional: true, |
| 129 | + }, |
| 130 | + backgroundOtherPages: { |
| 131 | + type: "string", |
| 132 | + label: "Background Other Pages", |
| 133 | + description: "The ID of the Background to apply to the other pages of these letters.", |
| 134 | + optional: true, |
| 135 | + }, |
| 136 | + confidential: { |
| 137 | + type: "boolean", |
| 138 | + label: "Confidential", |
| 139 | + description: "Whether to mark letters of this Print Job as confidential.", |
| 140 | + optional: true, |
| 141 | + }, |
| 142 | + removeLetters: { |
| 143 | + type: "string", |
| 144 | + label: "Remove Letters", |
| 145 | + description: "Remove letter objects that have this phrase in their content.", |
| 146 | + optional: true, |
| 147 | + }, |
| 148 | + nudgeX: { |
| 149 | + type: "integer", |
| 150 | + label: "Nudge X", |
| 151 | + description: "What amount in mm to move the first page of each letter horizontally. A positive number moves the page right, a negative number moves the page left.", |
| 152 | + optional: true, |
| 153 | + }, |
| 154 | + nudgeY: { |
| 155 | + type: "integer", |
| 156 | + label: "Nudge Y", |
| 157 | + description: "What amount in mm to move the first page of each letter vertically. A positive number moves the page down, a negative number moves the page up.", |
| 158 | + optional: true, |
| 159 | + }, |
| 160 | + confirmationEmail: { |
| 161 | + type: "string", |
| 162 | + label: "Confirmation Email", |
| 163 | + description: "Whether a confirmation email should be sent to the user or account's email address when this letter is confirmed.", |
| 164 | + optional: true, |
| 165 | + }, |
| 166 | + metadata: { |
| 167 | + type: "object", |
| 168 | + label: "Metadata", |
| 169 | + description: "A key-value object for storing any information you want to along with this Print Job.", |
| 170 | + optional: true, |
| 171 | + }, |
| 172 | + syncDir: { |
| 173 | + type: "dir", |
| 174 | + accessMode: "read", |
| 175 | + sync: true, |
| 176 | + optional: false, |
| 177 | + }, |
| 178 | + }, |
| 179 | + async run({ $ }) { |
| 180 | + try { |
| 181 | + const { |
| 182 | + intelliprint, |
| 183 | + filePath, |
| 184 | + syncDir, |
| 185 | + ...data |
| 186 | + } = this; |
| 187 | + |
| 188 | + const { |
| 189 | + stream, metadata, |
| 190 | + } = await getFileStreamAndMetadata(filePath); |
| 191 | + |
| 192 | + const formData = new FormData(); |
| 193 | + formData.append("file", stream, { |
| 194 | + contentType: metadata.contentType, |
| 195 | + knownLength: metadata.size, |
| 196 | + filename: metadata.name, |
| 197 | + }); |
| 198 | + for (const [ |
| 199 | + key, |
| 200 | + value, |
| 201 | + ] of Object.entries(data)) { |
| 202 | + formData.append(camelCaseToSnakeCase(key), `${value}`); |
| 203 | + } |
| 204 | + |
| 205 | + const response = await intelliprint.createPrintJob({ |
| 206 | + $, |
| 207 | + data: formData, |
| 208 | + headers: formData.getHeaders(), |
| 209 | + }); |
| 210 | + |
| 211 | + $.export("$summary", `Successfully created print job with ID: ${response.id}`); |
| 212 | + return response; |
| 213 | + } catch (error) { |
| 214 | + throw new ConfigurationError(`Error creating print job: ${error.response.data.error.message}`); |
| 215 | + } |
| 216 | + }, |
| 217 | +}; |
0 commit comments