-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathSequentialSigningService.php
More file actions
173 lines (144 loc) · 4.49 KB
/
SequentialSigningService.php
File metadata and controls
173 lines (144 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Enum\SignatureFlow;
use OCA\Libresign\Enum\SignRequestStatus;
use OCP\IAppConfig;
class SequentialSigningService {
private int $currentOrder = 1;
public function __construct(
private IAppConfig $appConfig,
private SignRequestMapper $signRequestMapper,
private IdentifyMethodService $identifyMethodService,
) {
}
/**
* Check if ordered numeric flow is enabled
*/
public function isOrderedNumericFlow(): bool {
return $this->getSignatureFlow() === SignatureFlow::ORDERED_NUMERIC;
}
/**
* Reset the internal order counter
*/
public function resetOrderCounter(): void {
$this->currentOrder = 1;
}
/**
* Determine signing order based on flow configuration
* Manages internal counter automatically
*
* @param int|null $userProvidedOrder Order explicitly set by user
* @return int The order to use
*/
public function determineSigningOrder(?int $userProvidedOrder): int {
if (!$this->isOrderedNumericFlow()) {
return 1;
}
if ($userProvidedOrder !== null) {
if ($userProvidedOrder > $this->currentOrder) {
$this->currentOrder = $userProvidedOrder;
}
return $userProvidedOrder;
}
return $this->currentOrder++;
}
/**
* Release next order of signers after current order is completed
* Called when a signature is saved
*
* @param int $fileId
* @param int $completedOrder The order that was just completed
*/
public function releaseNextOrder(int $fileId, int $completedOrder): void {
if (!$this->isOrderedNumericFlow()) {
return;
}
$allSignRequests = $this->signRequestMapper->getByFileId($fileId);
if (!$this->isOrderFullyCompleted($allSignRequests, $completedOrder)) {
return;
}
$nextOrder = $this->findNextOrder($allSignRequests, $completedOrder);
if ($nextOrder === null) {
return;
}
$this->activateSignersForOrder($allSignRequests, $nextOrder);
}
/**
* Reorder and activate signers after a SignRequest deletion
* This ensures no gaps in the signing sequence
*
* @param int $fileId The file ID
* @param int $deletedOrder The order that was deleted
*/
public function reorderAfterDeletion(int $fileId, int $deletedOrder): void {
if (!$this->isOrderedNumericFlow()) {
return;
}
$allSignRequests = $this->signRequestMapper->getByFileId($fileId);
$hasSignersAtDeletedOrder = !empty(array_filter(
$allSignRequests,
fn ($sr) => $sr->getSigningOrder() === $deletedOrder
));
if (!$hasSignersAtDeletedOrder) {
$previousOrder = $deletedOrder - 1;
if ($previousOrder > 0 && $this->isOrderFullyCompleted($allSignRequests, $previousOrder)) {
$nextOrder = $this->findNextOrder($allSignRequests, $deletedOrder);
if ($nextOrder !== null) {
$this->activateSignersForOrder($allSignRequests, $nextOrder);
}
}
}
}
private function isOrderFullyCompleted(array $signRequests, int $order): bool {
$pendingSigners = array_filter(
$signRequests,
fn ($sr) => $sr->getSigningOrder() === $order
&& $sr->getStatusEnum() !== SignRequestStatus::SIGNED
);
return empty($pendingSigners);
}
private function findNextOrder(array $signRequests, int $completedOrder): ?int {
$allOrders = array_unique(array_map(fn ($sr) => $sr->getSigningOrder(), $signRequests));
sort($allOrders);
foreach ($allOrders as $order) {
if ($order > $completedOrder) {
return $order;
}
}
return null;
}
private function activateSignersForOrder(array $signRequests, int $order): void {
$signersToActivate = array_filter(
$signRequests,
fn ($sr) => $sr->getSigningOrder() === $order
);
foreach ($signersToActivate as $signer) {
if ($signer->getStatusEnum() === SignRequestStatus::DRAFT) {
$signer->setStatusEnum(SignRequestStatus::ABLE_TO_SIGN);
$this->signRequestMapper->update($signer);
$identifyMethods = $this->identifyMethodService->getIdentifyMethodsFromSignRequestId($signer->getId());
foreach ($identifyMethods as $methodGroup) {
foreach ($methodGroup as $identifyMethod) {
$identifyMethod->willNotifyUser(true);
$identifyMethod->notify();
}
}
}
}
}
private function getSignatureFlow(): SignatureFlow {
$value = $this->appConfig->getValueString(
Application::APP_ID,
'signature_flow',
SignatureFlow::PARALLEL->value
);
return SignatureFlow::from($value);
}
}