2222use OCA \Libresign \Helper \FileUploadHelper ;
2323use OCA \Libresign \Helper \ValidateHelper ;
2424use OCA \Libresign \Service \IdentifyMethod \IIdentifyMethod ;
25- use OCP \AppFramework \Db \DoesNotExistException ;
2625use OCP \EventDispatcher \IEventDispatcher ;
2726use OCP \Files \IMimeTypeDetector ;
2827use OCP \Files \Node ;
@@ -57,10 +56,10 @@ public function __construct(
5756 protected IAppConfig $ appConfig ,
5857 protected IEventDispatcher $ eventDispatcher ,
5958 protected FileStatusService $ fileStatusService ,
60- protected SignRequestStatusService $ signRequestStatusService ,
6159 protected DocMdpConfigService $ docMdpConfigService ,
6260 protected EnvelopeService $ envelopeService ,
6361 protected FileUploadHelper $ uploadHelper ,
62+ protected SignRequestService $ signRequestService ,
6463 ) {
6564 }
6665
@@ -121,7 +120,7 @@ public function save(array $data): FileEntity {
121120 $ data ['status ' ] = $ file ->getStatus ();
122121 }
123122 $ this ->sequentialSigningService ->setFile ($ file );
124- $ this ->associateToSigners ($ data , $ file-> getId () );
123+ $ this ->associateToSigners ($ data , $ file );
125124 $ this ->propagateSignersToChildren ($ file , $ data );
126125
127126 return $ file ;
@@ -142,7 +141,7 @@ private function propagateSignersToChildren(FileEntity $envelope, array $data):
142141 foreach ($ children as $ child ) {
143142 $ this ->identifyMethod ->clearCache ();
144143 $ this ->sequentialSigningService ->setFile ($ child );
145- $ this ->associateToSigners ($ dataWithoutNotification , $ child-> getId () );
144+ $ this ->associateToSigners ($ dataWithoutNotification , $ child );
146145 }
147146 }
148147
@@ -396,9 +395,8 @@ private function removeExtensionFromName(string $name, array $metadata): string
396395 return $ result ?? $ name ;
397396 }
398397
399- private function deleteIdentifyMethodIfNotExits (array $ users , int $ fileId ): void {
400- $ file = $ this ->fileMapper ->getById ($ fileId );
401- $ signRequests = $ this ->signRequestMapper ->getByFileId ($ fileId );
398+ private function deleteIdentifyMethodIfNotExits (array $ users , FileEntity $ file ): void {
399+ $ signRequests = $ this ->signRequestMapper ->getByFileId ($ file ->getId ());
402400 foreach ($ signRequests as $ key => $ signRequest ) {
403401 $ identifyMethods = $ this ->identifyMethod ->getIdentifyMethodsFromSignRequestId ($ signRequest ->getId ());
404402 if (empty ($ identifyMethods )) {
@@ -447,10 +445,10 @@ private function identifyMethodExists(array $users, IIdentifyMethod $identifyMet
447445 *
448446 * @psalm-return list<SignRequestEntity>
449447 */
450- private function associateToSigners (array $ data , int $ fileId ): array {
448+ private function associateToSigners (array $ data , FileEntity $ file ): array {
451449 $ return = [];
452450 if (!empty ($ data ['users ' ])) {
453- $ this ->deleteIdentifyMethodIfNotExits ($ data ['users ' ], $ fileId );
451+ $ this ->deleteIdentifyMethodIfNotExits ($ data ['users ' ], $ file );
454452
455453 $ this ->sequentialSigningService ->resetOrderCounter ();
456454 $ fileStatus = $ data ['status ' ] ?? null ;
@@ -463,26 +461,26 @@ private function associateToSigners(array $data, int $fileId): array {
463461
464462 if (isset ($ user ['identifyMethods ' ])) {
465463 foreach ($ user ['identifyMethods ' ] as $ identifyMethod ) {
466- $ return [] = $ this ->associateToSigner (
464+ $ return [] = $ this ->signRequestService -> createOrUpdateSignRequest (
467465 identifyMethods: [
468466 $ identifyMethod ['method ' ] => $ identifyMethod ['value ' ],
469467 ],
470468 displayName: $ user ['displayName ' ] ?? '' ,
471469 description: $ user ['description ' ] ?? '' ,
472470 notify: $ shouldNotify ,
473- fileId: $ fileId ,
471+ fileId: $ file -> getId () ,
474472 signingOrder: $ signingOrder ,
475473 fileStatus: $ fileStatus ,
476474 signerStatus: $ signerStatus ,
477475 );
478476 }
479477 } else {
480- $ return [] = $ this ->associateToSigner (
478+ $ return [] = $ this ->signRequestService -> createOrUpdateSignRequest (
481479 identifyMethods: $ user ['identify ' ],
482480 displayName: $ user ['displayName ' ] ?? '' ,
483481 description: $ user ['description ' ] ?? '' ,
484482 notify: $ shouldNotify ,
485- fileId: $ fileId ,
483+ fileId: $ file -> getId () ,
486484 signingOrder: $ signingOrder ,
487485 fileStatus: $ fileStatus ,
488486 signerStatus: $ signerStatus ,
@@ -493,73 +491,7 @@ private function associateToSigners(array $data, int $fileId): array {
493491 return $ return ;
494492 }
495493
496- private function associateToSigner (
497- array $ identifyMethods ,
498- string $ displayName ,
499- string $ description ,
500- bool $ notify ,
501- int $ fileId ,
502- int $ signingOrder = 0 ,
503- ?int $ fileStatus = null ,
504- ?int $ signerStatus = null ,
505- ): SignRequestEntity {
506- $ identifyMethodsIncances = $ this ->identifyMethod ->getByUserData ($ identifyMethods );
507- if (empty ($ identifyMethodsIncances )) {
508- throw new \Exception ($ this ->l10n ->t ('Invalid identification method ' ));
509- }
510- $ signRequest = $ this ->getSignRequestByIdentifyMethod (
511- current ($ identifyMethodsIncances ),
512- $ fileId
513- );
514- $ displayName = $ this ->getDisplayNameFromIdentifyMethodIfEmpty ($ identifyMethodsIncances , $ displayName );
515- $ this ->setDataToUser ($ signRequest , $ displayName , $ description , $ fileId );
516-
517- $ signRequest ->setSigningOrder ($ signingOrder );
518494
519- $ isNewSignRequest = !$ signRequest ->getId ();
520- $ currentStatus = $ signRequest ->getStatusEnum ();
521-
522- if ($ isNewSignRequest || $ currentStatus === \OCA \Libresign \Enum \SignRequestStatus::DRAFT ) {
523- $ desiredStatus = $ this ->signRequestStatusService ->determineInitialStatus ($ signingOrder , $ fileId , $ fileStatus , $ signerStatus , $ currentStatus );
524- $ this ->signRequestStatusService ->updateStatusIfAllowed ($ signRequest , $ currentStatus , $ desiredStatus , $ isNewSignRequest );
525- }
526-
527- $ this ->saveSignRequest ($ signRequest );
528-
529- $ shouldNotify = $ notify && $ this ->signRequestStatusService ->shouldNotifySignRequest (
530- $ signRequest ->getStatusEnum (),
531- $ fileStatus
532- );
533-
534- foreach ($ identifyMethodsIncances as $ identifyMethod ) {
535- $ identifyMethod ->getEntity ()->setSignRequestId ($ signRequest ->getId ());
536- $ identifyMethod ->willNotifyUser ($ shouldNotify );
537- $ identifyMethod ->save ();
538- }
539- return $ signRequest ;
540- }
541-
542- /**
543- * @param IIdentifyMethod[] $identifyMethodsIncances
544- * @param string $displayName
545- * @return string
546- */
547- private function getDisplayNameFromIdentifyMethodIfEmpty (array $ identifyMethodsIncances , string $ displayName ): string {
548- if (!empty ($ displayName )) {
549- return $ displayName ;
550- }
551- foreach ($ identifyMethodsIncances as $ identifyMethod ) {
552- if ($ identifyMethod ->getName () === 'account ' ) {
553- return $ this ->userManager ->get ($ identifyMethod ->getEntity ()->getIdentifierValue ())->getDisplayName ();
554- }
555- }
556- foreach ($ identifyMethodsIncances as $ identifyMethod ) {
557- if ($ identifyMethod ->getName () !== 'account ' ) {
558- return $ identifyMethod ->getEntity ()->getIdentifierValue ();
559- }
560- }
561- return '' ;
562- }
563495
564496 private function saveVisibleElements (array $ data , FileEntity $ file ): array {
565497 if (empty ($ data ['visibleElements ' ])) {
@@ -605,41 +537,7 @@ public function validateUsers(array $data): void {
605537 }
606538 }
607539
608- public function saveSignRequest (SignRequestEntity $ signRequest ): void {
609- if ($ signRequest ->getId ()) {
610- $ this ->signRequestMapper ->update ($ signRequest );
611- } else {
612- $ this ->signRequestMapper ->insert ($ signRequest );
613- }
614- }
615-
616- /**
617- * @psalm-suppress MixedMethodCall
618- */
619- private function setDataToUser (SignRequestEntity $ signRequest , string $ displayName , string $ description , int $ fileId ): void {
620- $ signRequest ->setFileId ($ fileId );
621- if (!$ signRequest ->getUuid ()) {
622- $ signRequest ->setUuid (UUIDUtil::getUUID ());
623- }
624- if (!empty ($ displayName )) {
625- $ signRequest ->setDisplayName ($ displayName );
626- }
627- if (!empty ($ description )) {
628- $ signRequest ->setDescription ($ description );
629- }
630- if (!$ signRequest ->getId ()) {
631- $ signRequest ->setCreatedAt (new \DateTime ('now ' , new \DateTimeZone ('UTC ' )));
632- }
633- }
634540
635- private function getSignRequestByIdentifyMethod (IIdentifyMethod $ identifyMethod , int $ fileId ): SignRequestEntity {
636- try {
637- $ signRequest = $ this ->signRequestMapper ->getByIdentifyMethodAndFileId ($ identifyMethod , $ fileId );
638- } catch (DoesNotExistException ) {
639- $ signRequest = new SignRequestEntity ();
640- }
641- return $ signRequest ;
642- }
643541
644542 public function unassociateToUser (int $ fileId , int $ signRequestId ): void {
645543 $ file = $ this ->fileMapper ->getByFileId ($ fileId );
0 commit comments