Skip to content

Commit a798f73

Browse files
committed
fix: refactor envelope signing flow and envelope status propagation
- Remove unnecessary state saving/restoration in sign() method - Extract buildEnvelopeSignRequests() for envelope-specific logic - Create getEnvelopeContext() to resolve envelope and its sign request - Fix envelope status not updating when all children are signed - Ensure envelope sign_request is marked as SIGNED with timestamp - Improve envelope resolution via parent lookup and identify method matching - Simplify sign() to use envelope context from getEnvelopeContext() - Separate concerns: getSignRequestsToSign() handles requests only - Eliminate code duplication in envelope resolution logic Fixes: Envelope remains unsigned when all child files are signed Signed-off-by: Vitor Mattos <[email protected]>
1 parent 3287d3f commit a798f73

File tree

1 file changed

+75
-39
lines changed

1 file changed

+75
-39
lines changed

lib/Service/SignFileService.php

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -325,17 +325,14 @@ public function getVisibleElements(): array {
325325
}
326326

327327
public function sign(): void {
328-
$originalLibreSignFile = $this->libreSignFile;
329-
$originalSignRequest = $this->signRequest;
330-
$envelopeLastSignedDate = null;
331-
$lastSignedFile = null;
332-
333328
$signRequests = $this->getSignRequestsToSign();
334329

335330
if (empty($signRequests)) {
336331
throw new LibresignException('No sign requests found to process');
337332
}
338333

334+
$envelopeLastSignedDate = null;
335+
339336
foreach ($signRequests as $signRequestData) {
340337
$this->libreSignFile = $signRequestData['file'];
341338
$this->signRequest = $signRequestData['signRequest'];
@@ -345,7 +342,6 @@ public function sign(): void {
345342

346343
$this->validateDocMdpAllowsSignatures();
347344
$signedFile = $this->getEngine()->sign();
348-
$lastSignedFile = $signedFile;
349345

350346
$hash = $this->computeHash($signedFile);
351347
$envelopeLastSignedDate = $this->getEngine()->getLastSignedDate();
@@ -356,36 +352,20 @@ public function sign(): void {
356352
$this->dispatchSignedEvent();
357353
}
358354

359-
$this->libreSignFile = $originalLibreSignFile;
360-
$this->signRequest = $originalSignRequest;
361-
362-
if ($originalLibreSignFile->isEnvelope()) {
363-
if ($envelopeLastSignedDate) {
364-
$this->signRequest->setSigned($envelopeLastSignedDate);
365-
$this->signRequest->setStatusEnum(\OCA\Libresign\Enum\SignRequestStatus::SIGNED);
366-
$this->signRequestMapper->update($this->signRequest);
367-
$this->sequentialSigningService
368-
->setFile($this->libreSignFile)
369-
->releaseNextOrder(
370-
$this->signRequest->getFileId(),
371-
$this->signRequest->getSigningOrder()
372-
);
373-
}
374-
$this->updateEnvelopeStatus();
375-
376-
if ($lastSignedFile instanceof File) {
377-
$event = $this->signedEventFactory->make(
378-
$this->signRequest,
379-
$this->libreSignFile,
380-
$lastSignedFile,
381-
);
382-
$this->eventDispatcher->dispatchTyped($event);
383-
}
355+
$envelopeContext = $this->getEnvelopeContext();
356+
if ($envelopeContext['envelope'] instanceof FileEntity) {
357+
$this->updateEnvelopeStatus(
358+
$envelopeContext['envelope'],
359+
$envelopeContext['envelopeSignRequest'] ?? null,
360+
$envelopeLastSignedDate
361+
);
384362
}
385363
}
386364

387365
/**
388-
* @return array Array of ['file' => FileEntity, 'signRequest' => SignRequestEntity]
366+
* Get sign requests to process.
367+
*
368+
* @return array Array of sign request data with 'file' => FileEntity, 'signRequest' => SignRequestEntity
389369
*/
390370
private function getSignRequestsToSign(): array {
391371
if (!$this->libreSignFile->isEnvelope()
@@ -397,6 +377,13 @@ private function getSignRequestsToSign(): array {
397377
]];
398378
}
399379

380+
return $this->buildEnvelopeSignRequests();
381+
}
382+
383+
/**
384+
* @return array Array of sign request data with 'file' => FileEntity, 'signRequest' => SignRequestEntity
385+
*/
386+
private function buildEnvelopeSignRequests(): array {
400387
$envelopeId = $this->libreSignFile->isEnvelope()
401388
? $this->libreSignFile->getId()
402389
: $this->libreSignFile->getParentFileId();
@@ -433,8 +420,46 @@ private function getSignRequestsToSign(): array {
433420
return $signRequestsData;
434421
}
435422

436-
private function updateEnvelopeStatus(): void {
437-
$childFiles = $this->fileMapper->getChildrenFiles($this->libreSignFile->getId());
423+
/**
424+
* Get envelope context if the current file is or belongs to an envelope.
425+
*
426+
* @return array Array with 'envelope' => FileEntity or null, 'envelopeSignRequest' => SignRequestEntity or null
427+
*/
428+
private function getEnvelopeContext(): array {
429+
$result = [
430+
'envelope' => null,
431+
'envelopeSignRequest' => null,
432+
];
433+
434+
if (!$this->libreSignFile->isEnvelope() && !$this->libreSignFile->hasParent()) {
435+
return $result;
436+
}
437+
438+
if ($this->libreSignFile->isEnvelope()) {
439+
$result['envelope'] = $this->libreSignFile;
440+
$result['envelopeSignRequest'] = $this->signRequest;
441+
return $result;
442+
}
443+
444+
try {
445+
$envelopeId = $this->libreSignFile->isEnvelope()
446+
? $this->libreSignFile->getId()
447+
: $this->libreSignFile->getParentFileId();
448+
$result['envelope'] = $this->fileMapper->getById($envelopeId);
449+
$identifyMethod = $this->identifyMethodService->getIdentifiedMethod($this->signRequest->getId());
450+
$result['envelopeSignRequest'] = $this->signRequestMapper->getByIdentifyMethodAndFileId(
451+
$identifyMethod,
452+
$result['envelope']->getId()
453+
);
454+
} catch (DoesNotExistException $e) {
455+
// Envelope not found or sign request not found, leave as null
456+
}
457+
458+
return $result;
459+
}
460+
461+
private function updateEnvelopeStatus(FileEntity $envelope, ?SignRequestEntity $envelopeSignRequest = null, ?DateTimeInterface $signedDate = null): void {
462+
$childFiles = $this->fileMapper->getChildrenFiles($envelope->getId());
438463

439464
$totalSignRequests = 0;
440465
$signedSignRequests = 0;
@@ -451,16 +476,27 @@ private function updateEnvelopeStatus(): void {
451476
}
452477

453478
if ($totalSignRequests === 0) {
454-
$this->libreSignFile->setStatus(FileEntity::STATUS_DRAFT);
479+
$envelope->setStatus(FileEntity::STATUS_DRAFT);
455480
} elseif ($signedSignRequests === 0) {
456-
$this->libreSignFile->setStatus(FileEntity::STATUS_ABLE_TO_SIGN);
481+
$envelope->setStatus(FileEntity::STATUS_ABLE_TO_SIGN);
457482
} elseif ($signedSignRequests === $totalSignRequests) {
458-
$this->libreSignFile->setStatus(FileEntity::STATUS_SIGNED);
483+
$envelope->setStatus(FileEntity::STATUS_SIGNED);
484+
if ($envelopeSignRequest instanceof SignRequestEntity) {
485+
$envelopeSignRequest->setSigned($signedDate ?: new DateTime());
486+
$envelopeSignRequest->setStatusEnum(\OCA\Libresign\Enum\SignRequestStatus::SIGNED);
487+
$this->signRequestMapper->update($envelopeSignRequest);
488+
$this->sequentialSigningService
489+
->setFile($envelope)
490+
->releaseNextOrder(
491+
$envelopeSignRequest->getFileId(),
492+
$envelopeSignRequest->getSigningOrder()
493+
);
494+
}
459495
} else {
460-
$this->libreSignFile->setStatus(FileEntity::STATUS_PARTIAL_SIGNED);
496+
$envelope->setStatus(FileEntity::STATUS_PARTIAL_SIGNED);
461497
}
462498

463-
$this->fileMapper->update($this->libreSignFile);
499+
$this->fileMapper->update($envelope);
464500
}
465501

466502
/**

0 commit comments

Comments
 (0)