UNPKG

@gatekeeper_technology/report-utils

Version:
136 lines (135 loc) 6.94 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isAttachmentUploaded = exports.isAttachmentsUploaded = exports.loadPhotoOrSignature = exports.loadAttachments = exports.loadImageBase64 = exports.getImageContentForPDF = void 0; /** * This function serves as the central utility in photo_attachment_utils. * It takes a Journey Attachment (or an image path) as input and provides either the base64 data (if in-app or the path is given), * or the URL (if the function is called from Cloud code and a JA Attachment is provided). * If the attachment is still being uploaded, the Cloud code task is rescheduled. */ function getImageContentForPDF(image_1) { return __awaiter(this, arguments, void 0, function* (image, type = "url") { if (!image) return null; if (typeof image === "string") { console.log("[REPORT UTILS] Found string (assuming it's an image path) returning Base64."); return loadImageBase64(image); } //@ts-ignore if (!image.uploaded()) throw Error(`Attachment not done uploading`, { cause: "attachments-uploading" }); const is_url = type === "url"; console.log(`[REPORT UTILS] Getting ${is_url ? 'URL' : 'BASE64'}.`); if (is_url) return image.url(); // Determine which file format the attachment is. const is_signature = image.url().includes(".svg"); const base_data = is_signature ? 'data:image/svg+xml;base64,' : 'data:image/png;base64,'; return `${base_data}${yield image.toBase64()}`; }); } exports.getImageContentForPDF = getImageContentForPDF; /** * This function is used to read an images from a local directory (not a journeyapps) */ function loadImageBase64(path) { try { const fs = eval('require("fs")'); const image_data = fs.readFileSync(path, { encoding: 'base64' }); return `data:image/png;base64,${image_data}`; } catch (error) { console.log('Error:', error.message); return null; } } exports.loadImageBase64 = loadImageBase64; /** * This function takes all the signatures and photos of an object and checks if they are uploaded. If uploaded, then it modifies the target object * If a attachment is not done uploading, the calling task is rescheduled * NOTE: Use the ID of the object received in the params to refresh the object. * Otherwise an object is returned containing all the signatures and photos from the original object * @param {Record<string, any>} target The target hash where image data will be stored * @param {Record<string, any>} object_to_check The object to check whether attachments are uploaded */ function loadAttachments(target, object_to_check) { return __awaiter(this, void 0, void 0, function* () { // Put the field names of every attachment in the object_to_check in an array. const attachment_field_names = getAttachmentFields(object_to_check); // Check each attachment individually. for (let attachment_name of attachment_field_names) { isAttachmentUploaded(object_to_check, attachment_name); target[attachment_name] = yield getImageContentForPDF(object_to_check[attachment_name]); target[`has_${attachment_name}`] = true; console.log(`💨 The Attachment is done uploading: ${attachment_name}`); } console.log("⭐ All attachments are done uploading."); return target; }); } exports.loadAttachments = loadAttachments; /** * This function checks if the signature or photo was indeed taken * and then assigns the boolean to a value. And then assigns the actual image to * different variable * @param {Object} object The object containing the current image. * @param {String} field_name The field that is being checked. */ function loadPhotoOrSignature(object, field_name) { return __awaiter(this, void 0, void 0, function* () { console.log(`[REPORT UTILS] Photo: object.${field_name} = ${JSON.stringify(object[field_name])}`); const attachment_data = yield getImageContentForPDF(object[field_name], "base64"); if (attachment_data) { return { [`has_${field_name}`]: true, [field_name]: attachment_data }; } else { const attachment_data = yield getImageContentForPDF(object[field_name], "url"); return { [`has_${field_name}`]: !!attachment_data, [field_name]: attachment_data }; } }); } exports.loadPhotoOrSignature = loadPhotoOrSignature; function isAttachmentsUploaded(object_to_check) { const attachment_field_names = getAttachmentFields(object_to_check); // Check each attachment individually. for (let attachment_name of attachment_field_names) { isAttachmentUploaded(object_to_check, attachment_name); console.log(`💨 Attachment done uploading: ${attachment_name}`); } console.log("⭐ All attachments are done uploading."); return true; } exports.isAttachmentsUploaded = isAttachmentsUploaded; function isAttachmentUploaded(object, field_name) { var _a; const attachment = object[field_name]; if (!attachment || !attachment.uploaded()) { //@ts-ignore throw Error(`Attachment not done uploading: Field name: ${field_name}; Object Name: ${(_a = object === null || object === void 0 ? void 0 : object.type) === null || _a === void 0 ? void 0 : _a.name}; Object ID ${object === null || object === void 0 ? void 0 : object.id}`, { cause: "attachments-uploading" }); } return true; } exports.isAttachmentUploaded = isAttachmentUploaded; function getAttachmentFields(object) { if (!object) return []; const field_names = Object.keys(object); const attachments_field_names = field_names.filter((field_name) => { var _a, _b; return ((_a = object[field_name]) === null || _a === void 0 ? void 0 : _a.hasOwnProperty("state")) && ((_b = object[field_name]) === null || _b === void 0 ? void 0 : _b.hasOwnProperty("id")); }); console.log("[REPORT UTILS] List of attachment names:", attachments_field_names); return attachments_field_names; }