Created
September 26, 2023 01:25
-
-
Save valterbarros/494a8a4e02bc1475787ec7b4aaace7c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { processCoordinateForBackend } from '..'; | |
import { groupBy, merge } from 'lodash'; | |
export const useMountPositionSignPayload = (archives, signatures) => { | |
if (signatures.value.length === 0) { | |
return archives.value.reduce((acc, curr) => { | |
const url = curr.links.rubrics; | |
acc[url] ||= { rubric: [] }; | |
return acc; | |
}, {}); | |
} | |
const getArchive = (key) => archives.value.find((archive) => archive.key === key); | |
const signGroupedByDocument = groupBy( | |
signatures.value, | |
(signature) => `${signature.documentKey}.${signature.signerKey}` | |
); | |
signGroupedByDocument[Symbol.iterator] = function* () { | |
const keys = Object.keys(this); | |
for (let index = 0; index < keys.length; index++) { | |
const documentAndSignerKey = keys[index]; | |
const [documentKey, signerKey] = documentAndSignerKey.split('.'); | |
const signs = this[keys[index]]; | |
// yield { documentKey, signerKey, value: this[keys[index]] } | |
const signersAndPages = signs.map((signature) => { | |
const { x, y } = signature.position; | |
const relativeToPageX = processCoordinateForBackend( | |
x, | |
signature.pageWidth, | |
signature.pageOffsetLeft | |
); | |
const relativeToPageY = processCoordinateForBackend( | |
y, | |
signature.pageHeight, | |
signature.pageOffsetTop | |
); | |
return { | |
page: signature.dataPageNumber, | |
signerKey, | |
position: [relativeToPageX, relativeToPageY], | |
kind: signature.isRubric ? 'initials' : 'manuscript', | |
pageOffset: [signature.pageOffsetLeft, signature.pageOffsetTop], | |
}; | |
}); | |
// this[url] ||= { rubric: [] }; | |
const url = getArchive(documentKey).links.rubrics; | |
yield { [url]: { rubric: [...signersAndPages] } }; | |
} | |
}; | |
return merge(...signGroupedByDocument) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment