refactor(annotation): simplify field mapping and return structured objects

Refactored the mapSignature function to:
- Return cleaner structured objects for formFields, frames, and signatures
- Include `type` and `value` properties in returned objects
- Remove direct mutation of field and annotation objects
- Improve readability and maintainability of data mapping logic
This commit is contained in:
2025-10-21 09:38:47 +02:00
parent 9d6074874f
commit 6cc631111c

View File

@@ -320,34 +320,44 @@ function mapSignature(iJSON) {
return { return {
formFields: iJSON.formFieldValues.filter(field => !field.name.includes("label")).map((field) => { formFields: iJSON.formFieldValues.filter(field => !field.name.includes("label")).map((field) => {
const nameParts = field.name.split('#'); const nameParts = field.name.split('#');
field.elementId = Number(nameParts[2]); return {
field.name = nameParts[3]; elementId: Number(nameParts[2]),
return field; name: nameParts[3],
value: field.value,
type: field.type
};
}), }),
frames: iJSON.annotations.filter(annot => annot.description === 'FRAME').map((annot) => { frames: iJSON.annotations.filter(annot => annot.description === 'FRAME').map((annot) => {
const preElement = findNearest(annot, e => e.bbox[0], e => e.bbox[1], ...iJSON.annotations.filter(field => field.id.includes("signature"))); const preElement = findNearest(annot, e => e.bbox[0], e => e.bbox[1], ...iJSON.annotations.filter(field => field.id.includes("signature")));
const idPartsOfPre = preElement.id.split('#'); const idPartsOfPre = preElement.id.split('#');
annot.elementId = Number(idPartsOfPre[2]); return {
annot.name = 'frame'; elementId: Number(idPartsOfPre[2]),
annot.value = fixBase64(iJSON.attachments[annot.imageAttachmentId]?.binary); name: 'frame',
return annot; value: fixBase64(iJSON.attachments[annot.imageAttachmentId]?.binary),
type: annot.type
};
}), }),
signatures: iJSON.annotations.filter(annot => annot.isSignature).map(annot => { signatures: iJSON.annotations.filter(annot => annot.isSignature).map(annot => {
const preElement = findNearest(annot, e => e.bbox[0], e => e.bbox[1], ...iJSON.annotations.filter(field => field.id.includes("signature"))); const preElement = findNearest(annot, e => e.bbox[0], e => e.bbox[1], ...iJSON.annotations.filter(field => field.id.includes("signature")));
const idPartsOfPre = preElement.id.split('#'); const idPartsOfPre = preElement.id.split('#');
annot.elementId = Number(idPartsOfPre[2]);
let value;
if (annot.imageAttachmentId) if (annot.imageAttachmentId)
annot.value = iJSON.attachments[annot.imageAttachmentId]?.binary; value = iJSON.attachments[annot.imageAttachmentId]?.binary;
else if (annot.lines && annot.strokeColor) else if (annot.lines && annot.strokeColor)
annot.value = JSON.stringify({ value = JSON.stringify({
lines: annot.lines, lines: annot.lines,
strokeColor: annot.strokeColor strokeColor: annot.strokeColor
}); });
else else
throw new Error("Signature mapping failed: The data structure from the third-party library is incompatible or missing required fields."); throw new Error("Signature mapping failed: The data structure from the third-party library is incompatible or missing required fields.");
annot.name = 'signature'; return {
return annot; elementId: Number(idPartsOfPre[2]),
name: 'signature',
value,
type: annot.type
};
}) })
}; };
} }