|
| 1 | +/* eslint-disable no-unused-vars */ |
| 2 | +import { |
| 3 | + ConfigurationError, |
| 4 | + getFileStreamAndMetadata, |
| 5 | +} from "@pipedream/platform"; |
| 6 | +import FormData from "form-data"; |
| 7 | +import { |
| 8 | + DOUBLE_SIDED_OPTIONS, |
| 9 | + IDEAL_ENVELOPE_OPTIONS, |
| 10 | + POSTAGE_SERVICE_OPTIONS, |
| 11 | + SPLITTING_METHOD_OPTIONS, |
| 12 | +} from "../../common/constants.mjs"; |
| 13 | +import { |
| 14 | + camelCaseToSnakeCase, parseObject, |
| 15 | +} from "../../common/utils.mjs"; |
| 16 | +import intelliprint from "../../intelliprint.app.mjs"; |
| 17 | + |
| 18 | +export default { |
| 19 | + key: "intelliprint-create-a-print-job", |
| 20 | + name: "Create a Print Job", |
| 21 | + 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)", |
| 22 | + version: "0.0.1", |
| 23 | + type: "action", |
| 24 | + annotations: { |
| 25 | + destructiveHint: false, |
| 26 | + openWorldHint: true, |
| 27 | + readOnlyHint: false, |
| 28 | + }, |
| 29 | + props: { |
| 30 | + intelliprint, |
| 31 | + filePath: { |
| 32 | + type: "string", |
| 33 | + label: "File Path", |
| 34 | + 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`)", |
| 35 | + }, |
| 36 | + reference: { |
| 37 | + type: "string", |
| 38 | + label: "Reference", |
| 39 | + description: "An user-provided reference for this Print Job.", |
| 40 | + optional: true, |
| 41 | + }, |
| 42 | + confirmed: { |
| 43 | + type: "boolean", |
| 44 | + label: "Confirmed", |
| 45 | + description: "Whether to confirm this Print Job immediately, or to leave it as a draft.", |
| 46 | + optional: true, |
| 47 | + }, |
| 48 | + testmode: { |
| 49 | + type: "boolean", |
| 50 | + label: "Test Mode", |
| 51 | + description: "Whether to mark this Print Job as a test.", |
| 52 | + optional: true, |
| 53 | + }, |
| 54 | + splittingMethod: { |
| 55 | + type: "string", |
| 56 | + label: "Splitting Method", |
| 57 | + description: "The method to use to split the Print Job into multiple Print Jobs.", |
| 58 | + options: SPLITTING_METHOD_OPTIONS, |
| 59 | + optional: true, |
| 60 | + }, |
| 61 | + splitOnPhrase: { |
| 62 | + type: "string", |
| 63 | + label: "Split On Phrase", |
| 64 | + description: "The word or phrase to split letters using. Only used when **Splitting Method** is set to `split_on_phrase`.", |
| 65 | + optional: true, |
| 66 | + }, |
| 67 | + splitOnPage: { |
| 68 | + type: "integer", |
| 69 | + label: "Split On Page", |
| 70 | + description: "The number of pages each letter should be. Only used when **Splitting Method** is set to `split_on_page`.", |
| 71 | + optional: true, |
| 72 | + }, |
| 73 | + doubleSided: { |
| 74 | + type: "string", |
| 75 | + label: "Double Sided", |
| 76 | + description: "Whether to print these letters double sided.", |
| 77 | + options: DOUBLE_SIDED_OPTIONS, |
| 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 **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: POSTAGE_SERVICE_OPTIONS, |
| 97 | + optional: true, |
| 98 | + }, |
| 99 | + idealEnvelope: { |
| 100 | + type: "string", |
| 101 | + label: "Ideal Envelope", |
| 102 | + description: "The ideal envelope size for these letters.", |
| 103 | + options: IDEAL_ENVELOPE_OPTIONS, |
| 104 | + optional: true, |
| 105 | + }, |
| 106 | + mailDate: { |
| 107 | + type: "string", |
| 108 | + label: "Mail Date", |
| 109 | + description: "The date to send this letter out on. Format: **YYYY-MM-DD**", |
| 110 | + optional: true, |
| 111 | + }, |
| 112 | + backgroundFirstPage: { |
| 113 | + type: "string", |
| 114 | + label: "Background First Page", |
| 115 | + description: "The ID of the Background to apply to the first page of these letters.", |
| 116 | + optional: true, |
| 117 | + }, |
| 118 | + backgroundOtherPages: { |
| 119 | + type: "string", |
| 120 | + label: "Background Other Pages", |
| 121 | + description: "The ID of the Background to apply to the other pages of these letters.", |
| 122 | + optional: true, |
| 123 | + }, |
| 124 | + confidential: { |
| 125 | + type: "boolean", |
| 126 | + label: "Confidential", |
| 127 | + description: "Whether to mark letters of this Print Job as confidential.", |
| 128 | + optional: true, |
| 129 | + }, |
| 130 | + removeLettersWithPhrase: { |
| 131 | + type: "string", |
| 132 | + label: "Remove Letters With Phrase", |
| 133 | + description: "Remove letter objects that have this phrase in their content.", |
| 134 | + optional: true, |
| 135 | + }, |
| 136 | + removeLettersSeries: { |
| 137 | + type: "string[]", |
| 138 | + label: "Remove Letters Series", |
| 139 | + description: "An array of letters' indexes that have been removed.", |
| 140 | + optional: true, |
| 141 | + }, |
| 142 | + nudgeX: { |
| 143 | + type: "integer", |
| 144 | + label: "Nudge X", |
| 145 | + 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.", |
| 146 | + optional: true, |
| 147 | + }, |
| 148 | + nudgeY: { |
| 149 | + type: "integer", |
| 150 | + label: "Nudge Y", |
| 151 | + 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.", |
| 152 | + optional: true, |
| 153 | + }, |
| 154 | + confirmationEmail: { |
| 155 | + type: "boolean", |
| 156 | + label: "Confirmation Email", |
| 157 | + description: "Whether a confirmation email should be sent to the user or account's email address when this letter is confirmed.", |
| 158 | + optional: true, |
| 159 | + }, |
| 160 | + metadata: { |
| 161 | + type: "object", |
| 162 | + label: "Metadata", |
| 163 | + description: "A key-value object for storing any information you want to along with this Print Job.", |
| 164 | + optional: true, |
| 165 | + }, |
| 166 | + syncDir: { |
| 167 | + type: "dir", |
| 168 | + accessMode: "read", |
| 169 | + sync: true, |
| 170 | + optional: false, |
| 171 | + }, |
| 172 | + }, |
| 173 | + async run({ $ }) { |
| 174 | + |
| 175 | + try { |
| 176 | + const { |
| 177 | + intelliprint, |
| 178 | + confirmed, |
| 179 | + testmode, |
| 180 | + filePath, |
| 181 | + syncDir, |
| 182 | + splittingMethod, |
| 183 | + splitOnPhrase, |
| 184 | + splitOnPage, |
| 185 | + doubleSided, |
| 186 | + doubleSidedSpecificPages, |
| 187 | + premiumQuality, |
| 188 | + postageService, |
| 189 | + idealEnvelope, |
| 190 | + mailDate, |
| 191 | + backgroundFirstPage, |
| 192 | + backgroundOtherPages, |
| 193 | + nudgeX, |
| 194 | + nudgeY, |
| 195 | + confidential, |
| 196 | + removeLettersWithPhrase, |
| 197 | + removeLettersSeries, |
| 198 | + confirmationEmail, |
| 199 | + ...data |
| 200 | + } = this; |
| 201 | + |
| 202 | + const { |
| 203 | + stream, metadata, |
| 204 | + } = await getFileStreamAndMetadata(filePath); |
| 205 | + |
| 206 | + const formData = new FormData(); |
| 207 | + formData.append("file", stream, { |
| 208 | + contentType: metadata.contentType, |
| 209 | + knownLength: metadata.size, |
| 210 | + filename: metadata.name, |
| 211 | + }); |
| 212 | + |
| 213 | + if (confirmed) formData.append("confirmed", `${confirmed}`); |
| 214 | + if (testmode) formData.append("testmode", `${testmode}`); |
| 215 | + if (doubleSided) formData.append("printing.double_sided", doubleSided); |
| 216 | + if (doubleSidedSpecificPages) formData.append("printing.double_sided_specific_pages", doubleSidedSpecificPages); |
| 217 | + if (premiumQuality) formData.append("printing.premium_quality", `${premiumQuality}`); |
| 218 | + if (splittingMethod) formData.append("splitting.method", splittingMethod); |
| 219 | + if (splitOnPhrase) formData.append("splitting.phrase", splitOnPhrase); |
| 220 | + if (splitOnPage) formData.append("splitting.pages", splitOnPage); |
| 221 | + if (postageService) formData.append("postage.service", postageService); |
| 222 | + if (idealEnvelope) formData.append("postage.ideal_envelope", idealEnvelope); |
| 223 | + if (mailDate) formData.append("postage.mail_date", Date.parse(mailDate) / 1000); |
| 224 | + if (backgroundFirstPage) formData.append("background.first_page", backgroundFirstPage); |
| 225 | + if (backgroundOtherPages) formData.append("background.other_pages", backgroundOtherPages); |
| 226 | + if (nudgeX) formData.append("nudge.x", nudgeX); |
| 227 | + if (nudgeY) formData.append("nudge.y", nudgeY); |
| 228 | + if (confidential) formData.append("confidential", `${confidential}`); |
| 229 | + if (removeLettersWithPhrase) formData.append("remove_letters.with_phrase", removeLettersWithPhrase); |
| 230 | + if (removeLettersSeries) formData.append("remove_letters.series", JSON.stringify(parseObject(removeLettersSeries))); |
| 231 | + if (confirmationEmail) formData.append("confirmation_email", `${confirmationEmail}`); |
| 232 | + for (const [ |
| 233 | + key, |
| 234 | + value, |
| 235 | + ] of Object.entries(data)) { |
| 236 | + formData.append(camelCaseToSnakeCase(key), `${value}`); |
| 237 | + } |
| 238 | + |
| 239 | + const response = await intelliprint.createPrintJob({ |
| 240 | + $, |
| 241 | + data: formData, |
| 242 | + headers: formData.getHeaders(), |
| 243 | + }); |
| 244 | + |
| 245 | + $.export("$summary", `Successfully created print job with ID: ${response.id}`); |
| 246 | + return response; |
| 247 | + } catch (error) { |
| 248 | + throw new ConfigurationError(`Error creating print job: ${error.response.data.error.message}`); |
| 249 | + } |
| 250 | + }, |
| 251 | +}; |
0 commit comments