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 {
formFields: iJSON.formFieldValues.filter(field => !field.name.includes("label")).map((field) => {
const nameParts = field.name.split('#');
field.elementId = Number(nameParts[2]);
field.name = nameParts[3];
return field;
return {
elementId: Number(nameParts[2]),
name: nameParts[3],
value: field.value,
type: field.type
};
}),
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 idPartsOfPre = preElement.id.split('#');
annot.elementId = Number(idPartsOfPre[2]);
annot.name = 'frame';
annot.value = fixBase64(iJSON.attachments[annot.imageAttachmentId]?.binary);
return annot;
return {
elementId: Number(idPartsOfPre[2]),
name: 'frame',
value: fixBase64(iJSON.attachments[annot.imageAttachmentId]?.binary),
type: annot.type
};
}),
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 idPartsOfPre = preElement.id.split('#');
annot.elementId = Number(idPartsOfPre[2]);
let value;
if (annot.imageAttachmentId)
annot.value = iJSON.attachments[annot.imageAttachmentId]?.binary;
value = iJSON.attachments[annot.imageAttachmentId]?.binary;
else if (annot.lines && annot.strokeColor)
annot.value = JSON.stringify({
value = JSON.stringify({
lines: annot.lines,
strokeColor: annot.strokeColor
});
else
throw new Error("Signature mapping failed: The data structure from the third-party library is incompatible or missing required fields.");
annot.name = 'signature';
return annot;
return {
elementId: Number(idPartsOfPre[2]),
name: 'signature',
value,
type: annot.type
};
})
};
}