@@ -217,6 +217,12 @@ import type {
217217} from ' ../../../types/index'
218218import { SigningRequirementValidator } from ' ../../../services/SigningRequirementValidator'
219219import { SignFlowHandler } from ' ../../../services/SignFlowHandler'
220+ import {
221+ buildSubmitSignaturePayload ,
222+ createBaseSubmitSignaturePayload ,
223+ getEnvelopeSubmitRequests ,
224+ resolveSignSubmissionOutcome ,
225+ } from ' ../../../services/signSubmit'
220226import {
221227 normalizeDocumentForVisibleElements ,
222228} from ' ../../../services/signingDocumentAdapter'
@@ -227,6 +233,12 @@ import {
227233 getVisibleElementsFromDocument ,
228234 idsMatch ,
229235} from ' ../../../services/visibleElementsService'
236+ import type {
237+ SignResult ,
238+ SignSubmissionAttempt ,
239+ SignatureMethodConfig ,
240+ SubmitSignaturePayload ,
241+ } from ' ../../../services/signSubmit'
230242
231243type OpenApiAccountMe = operations [' account-me' ][' responses' ][200 ][' content' ][' application/json' ][' ocs' ][' data' ]
232244type LibreSignAccountMe = Omit <OpenApiAccountMe , ' settings' > & {
@@ -251,13 +263,16 @@ defineOptions({
251263 this .signStore .clearSigningErrors ()
252264
253265 try {
254- const basePayload: SubmitSignaturePayload = {
255- method: methodConfig .method ,
256- }
266+ const basePayload = createBaseSubmitSignaturePayload (methodConfig )
267+ const envelopeRequests = getEnvelopeSubmitRequests ({
268+ document: this .signStore .document ,
269+ basePayload ,
270+ elements: this .elements ?? [],
271+ canCreateSignature: this .canCreateSignature ,
272+ signatures: this .signatureElementsStore .signs ,
273+ })
257274
258- if (methodConfig .token ) {
259- basePayload .token = methodConfig .token
260- }
275+ const attempts: SignSubmissionAttempt [] = []
261276
262277 const myEnvelopeSigners = this .signStore .document ?.nodeType === ' envelope'
263278 ? (this .signStore .document ?.signers ?? [])
@@ -344,24 +359,22 @@ defineOptions({
344359 documentId: this .signStore .document .id ,
345360 })
346361
347- if (result .status === ' signingInProgress' ) {
348- this .actionHandler .closeModal (methodConfig .modalCode || methodConfig .method || ' token' )
349- this .$emit (' signing-started' , {
350- signRequestUuid: this .signRequestUuid ,
351- async: true ,
352- })
353- } else if (result .status === ' signed' ) {
354- const signRequestUuid = typeof result .data .file ?.uuid === ' string'
355- && result .data .file .uuid .length > 0
356- ? result .data .file .uuid
357- : this .signRequestUuid
358- this .actionHandler .closeModal (methodConfig .modalCode || methodConfig .method || ' token' )
359- this .sidebarStore .hideSidebar ()
360- this .$emit (' signed' , {
361- ... result .data ,
362- signRequestUuid ,
363- })
364- }
362+ attempts .push ({
363+ result ,
364+ signRequestUuid: this .signRequestUuid ,
365+ })
366+ }
367+
368+ const outcome = resolveSignSubmissionOutcome (attempts )
369+ const modalCode = methodConfig .modalCode || methodConfig .method || ' token'
370+
371+ if (outcome ?.type === ' signed' ) {
372+ this .actionHandler .closeModal (modalCode )
373+ this .sidebarStore .hideSidebar ()
374+ this .$emit (' signed' , outcome .payload )
375+ } else if (outcome ?.type === ' signing-started' ) {
376+ this .actionHandler .closeModal (modalCode )
377+ this .$emit (' signing-started' , outcome .payload )
365378 }
366379 } catch (error : unknown ) {
367380 const signError = typeof error === ' object' && error !== null ? error as SignSubmissionError : {}
@@ -386,12 +399,6 @@ defineOptions({
386399
387400type UserInfo = LibreSignAccountMe
388401
389- type SignatureMethodConfig = {
390- method? : string
391- modalCode? : string
392- token? : string
393- }
394-
395402type SignError = {
396403 title? : string
397404 message: string
@@ -420,34 +427,6 @@ type SignatureProfile = LibreSignUserElement
420427type SignDocument = NonNullable <ReturnType <typeof useSignStore >[' document' ]>
421428type SignDocumentFile = NonNullable <SignDocument [' files' ]>[number ]
422429
423- type SignResult = {
424- status: ' signingInProgress' | ' signed' | ' unknown'
425- data: SignResultData
426- }
427-
428- type SignResultData = {
429- action? : number
430- file? : {
431- uuid? : string
432- }
433- job? : {
434- status? : string
435- file? : {
436- uuid? : string
437- }
438- }
439- [key : string ]: unknown
440- }
441-
442- type SubmitSignaturePayload = {
443- method? : string
444- token? : string
445- elements? : Array <{
446- documentElementId: number
447- profileNodeId? : number
448- }>
449- }
450-
451430type SignSubmissionError = {
452431 type? : string
453432 errors? : SignError []
@@ -493,21 +472,6 @@ type SubmitSignatureCompatContext = {
493472 $emit: (event : string , payload : unknown ) => void
494473}
495474
496- function getNavigationUuidFromSignResultData(
497- data : SignResultData | null | undefined ,
498- fallbackUuid : string ,
499- ): string {
500- if (typeof data ?.file ?.uuid === ' string' && data .file .uuid .length > 0 ) {
501- return data .file .uuid
502- }
503-
504- if (typeof data ?.job ?.file ?.uuid === ' string' && data .job .file .uuid .length > 0 ) {
505- return data .job .file .uuid
506- }
507-
508- return fallbackUuid
509- }
510-
511475function isSignSubmissionError(error : unknown ): error is SignSubmissionError {
512476 return typeof error === ' object' && error !== null
513477}
@@ -624,14 +588,10 @@ function onCloseConfirmPassword() {
624588}
625589
626590function resetSignMethodsState() {
627- if (typeof signMethodsStore ?.$reset === ' function' ) {
628- signMethodsStore .$reset ()
629- } else {
630- Object .keys (signMethodsStore .modal || {}).forEach ((key ) => {
631- signMethodsStore .closeModal (key )
632- })
633- signMethodsStore .settings = {}
634- }
591+ Object .keys (signMethodsStore .modal || {}).forEach ((key ) => {
592+ signMethodsStore .closeModal (key )
593+ })
594+ signMethodsStore .settings = {}
635595 signStore .clearSigningErrors ()
636596 showManagePassword .value = false
637597 signPassword .value = ' '
@@ -709,9 +669,15 @@ let submitSignature = async (methodConfig: SignatureMethodConfig = {}) => {
709669 signStore .clearSigningErrors ()
710670
711671 try {
712- const basePayload: SubmitSignaturePayload = {
713- method: methodConfig .method ,
714- }
672+ const basePayload = createBaseSubmitSignaturePayload (methodConfig )
673+ const envelopeRequests = getEnvelopeSubmitRequests ({
674+ document: signStore .document ,
675+ basePayload ,
676+ elements: elements .value ,
677+ canCreateSignature: canCreateSignature .value ,
678+ signatures: signatureElementsStore .signs ,
679+ })
680+ const attempts: SignSubmissionAttempt [] = []
715681
716682 if (methodConfig .token ) {
717683 basePayload .token = methodConfig .token
@@ -805,21 +771,23 @@ let submitSignature = async (methodConfig: SignatureMethodConfig = {}) => {
805771 },
806772 )
807773
808- ensureServices ()
809- if (result .status === ' signingInProgress' ) {
810- actionHandler ! .closeModal (methodConfig .modalCode || methodConfig .method || ' token' )
811- emit (' signing-started' , {
812- signRequestUuid: signRequestUuid .value ,
813- async: true ,
814- })
815- } else if (result .status === ' signed' ) {
816- actionHandler ! .closeModal (methodConfig .modalCode || methodConfig .method || ' token' )
817- sidebarStore .hideSidebar ()
818- emit (' signed' , {
819- ... result .data ,
820- signRequestUuid: getNavigationUuidFromSignResultData (result .data , signRequestUuid .value ),
821- })
822- }
774+ attempts .push ({
775+ result ,
776+ signRequestUuid: signRequestUuid .value ,
777+ })
778+ }
779+
780+ const outcome = resolveSignSubmissionOutcome (attempts )
781+ const modalCode = methodConfig .modalCode || methodConfig .method || ' token'
782+
783+ ensureServices ()
784+ if (outcome ?.type === ' signed' ) {
785+ actionHandler ! .closeModal (modalCode )
786+ sidebarStore .hideSidebar ()
787+ emit (' signed' , outcome .payload )
788+ } else if (outcome ?.type === ' signing-started' ) {
789+ actionHandler ! .closeModal (modalCode )
790+ emit (' signing-started' , outcome .payload )
823791 }
824792 } catch (error : unknown ) {
825793 const signError = isSignSubmissionError (error ) ? error : {}
0 commit comments