• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LibreSign / libresign / 21258207663

22 Jan 2026 05:27PM UTC coverage: 44.867%. First build
21258207663

Pull #6520

github

web-flow
Merge 08ba7eaa4 into cf0454786
Pull Request #6520: fix: prevent cache race condition workers

108 of 162 new or added lines in 8 files covered. (66.67%)

7264 of 16190 relevant lines covered (44.87%)

4.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.11
/lib/Db/SignRequestMapper.php
1
<?php
2

3
declare(strict_types=1);
4
/**
5
 * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8

9
namespace OCA\Libresign\Db;
10

11
use OCA\Libresign\Enum\FileStatus;
12
use OCA\Libresign\Enum\SignRequestStatus;
13
use OCA\Libresign\Helper\Pagination;
14
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
15
use OCA\Libresign\Service\IdentifyMethodService;
16
use OCP\AppFramework\Db\DoesNotExistException;
17
use OCP\AppFramework\Db\Entity;
18
use OCP\DB\QueryBuilder\IQueryBuilder;
19
use OCP\ICacheFactory;
20
use OCP\IDBConnection;
21
use OCP\IL10N;
22
use OCP\IURLGenerator;
23
use OCP\IUser;
24

25
/**
26
 * Class SignRequestMapper
27
 *
28
 * @package OCA\Libresign\DB
29
 * @template-extends CachedQBMapper<SignRequest>
30
 */
31
class SignRequestMapper extends CachedQBMapper {
32
        private bool $firstNotification = false;
33

34
        public function __construct(
35
                IDBConnection $db,
36
                protected IL10N $l10n,
37
                protected FileMapper $fileMapper,
38
                private IURLGenerator $urlGenerator,
39
                ICacheFactory $cacheFactory,
40
        ) {
41
                parent::__construct($db, $cacheFactory, 'libresign_sign_request');
47✔
42
        }
43

44
        #[\Override]
45
        public function update(Entity $entity): SignRequest {
46
                $entityId = $entity->getId();
13✔
47
                if ($entityId !== null) {
13✔
48
                        $cached = $this->cacheGet('id:' . $entityId);
13✔
49
                        if ($cached instanceof SignRequest) {
13✔
50
                                $uuid = $cached->getUuid();
13✔
51
                                if ($uuid !== '') {
13✔
52
                                        $this->cacheRemove('uuid:' . $uuid);
13✔
53
                                }
54
                        }
55
                }
56
                /** @var SignRequest */
57
                return parent::update($entity);
13✔
58
        }
59

60
        #[\Override]
61
        protected function cacheEntity(Entity $entity): void {
62
                parent::cacheEntity($entity);
16✔
63
                if ($entity instanceof SignRequest) {
16✔
64
                        $uuid = $entity->getUuid();
16✔
65
                        if ($uuid !== '') {
16✔
66
                                $this->cacheSet('uuid:' . $uuid, $entity->getId());
16✔
67
                        }
68
                }
69
        }
70

71
        /**
72
         * @return boolean true when is the first notification
73
         */
74
        public function incrementNotificationCounter(SignRequest $signRequest, string $method): bool {
75
                $this->db->beginTransaction();
13✔
76
                try {
77
                        $fromDatabase = $this->getById($signRequest->getId());
13✔
78
                        $metadata = $fromDatabase->getMetadata();
13✔
79
                        if (!isset($metadata['notify'])) {
13✔
80
                                $this->firstNotification = true;
13✔
81
                        }
82

83
                        $notificationEntry = [
13✔
84
                                'method' => $method,
13✔
85
                                'date' => time(),
13✔
86
                        ];
13✔
87

88
                        if (!empty($fromDatabase->getDescription())) {
13✔
89
                                $notificationEntry['description'] = $fromDatabase->getDescription();
×
90
                        }
91

92
                        $metadata['notify'][] = $notificationEntry;
13✔
93
                        $fromDatabase->setMetadata($metadata);
13✔
94
                        $this->update($fromDatabase);
13✔
95
                        $this->db->commit();
13✔
96
                } catch (\Throwable) {
×
97
                        $this->db->rollBack();
×
98
                }
99
                return $this->firstNotification;
13✔
100
        }
101

102
        /**
103
         * Get sign request by UUID
104
         *
105
         * @throws DoesNotExistException
106
         */
107
        public function getByUuid(string $uuid): SignRequest {
108
                if ($uuid !== '') {
5✔
109
                        $cachedId = $this->cacheGet('uuid:' . $uuid);
5✔
110
                        if (is_int($cachedId) || (is_string($cachedId) && ctype_digit($cachedId))) {
5✔
111
                                return $this->getById((int)$cachedId);
4✔
112
                        }
113
                }
114
                $qb = $this->db->getQueryBuilder();
1✔
115

116
                $qb->select('*')
1✔
117
                        ->from($this->getTableName())
1✔
118
                        ->where(
1✔
119
                                $qb->expr()->eq('uuid', $qb->createNamedParameter($uuid))
1✔
120
                        );
1✔
121
                /** @var SignRequest */
122
                $signRequest = $this->findEntity($qb);
1✔
NEW
123
                $this->cacheEntity($signRequest);
×
124
                return $signRequest;
×
125
        }
126

127
        public function getByEmailAndFileId(string $email, int $fileId): SignRequest {
128
                $qb = $this->db->getQueryBuilder();
×
129

130
                $qb->select('*')
×
131
                        ->from($this->getTableName())
×
132
                        ->where(
×
133
                                $qb->expr()->eq('email', $qb->createNamedParameter($email))
×
134
                        )
×
135
                        ->andWhere(
×
136
                                $qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))
×
137
                        );
×
138
                /** @var SignRequest */
NEW
139
                $signRequest = $this->findEntity($qb);
×
NEW
140
                $this->cacheEntity($signRequest);
×
NEW
141
                return $signRequest;
×
142
        }
143

144
        public function getByIdentifyMethodAndFileId(IIdentifyMethod $identifyMethod, int $fileId): SignRequest {
145
                $qb = $this->db->getQueryBuilder();
13✔
146
                $qb->select('sr.*')
13✔
147
                        ->from($this->getTableName(), 'sr')
13✔
148
                        ->join('sr', 'libresign_identify_method', 'im', 'sr.id = im.sign_request_id')
13✔
149
                        ->where($qb->expr()->eq('im.identifier_key', $qb->createNamedParameter($identifyMethod->getEntity()->getIdentifierKey())))
13✔
150
                        ->andWhere($qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($identifyMethod->getEntity()->getIdentifierValue())))
13✔
151
                        ->andWhere($qb->expr()->eq('sr.file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
13✔
152
                /** @var SignRequest */
153
                $signRequest = $this->findEntity($qb);
13✔
NEW
154
                $this->cacheEntity($signRequest);
×
NEW
155
                return $signRequest;
×
156
        }
157

158
        /**
159
         * Get all signers by fileId
160
         *
161
         * @return SignRequest[]
162
         */
163
        public function getByFileId(int $fileId): array {
164
                $qb = $this->db->getQueryBuilder();
15✔
165

166
                $qb->select('*')
15✔
167
                        ->from($this->getTableName())
15✔
168
                        ->where(
15✔
169
                                $qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))
15✔
170
                        );
15✔
171
                /** @var SignRequest[] */
172
                $signers = $this->findEntities($qb);
15✔
173
                foreach ($signers as $signRequest) {
15✔
174
                        $this->cacheEntity($signRequest);
13✔
175
                }
176
                return $signers;
15✔
177
        }
178

179
        /**
180
         * @throws DoesNotExistException
181
         */
182
        public function getById(int $signRequestId): SignRequest {
183
                $cached = $this->cacheGet('id:' . $signRequestId);
15✔
184
                if ($cached instanceof SignRequest) {
15✔
185
                        return $cached;
14✔
186
                }
187
                $qb = $this->db->getQueryBuilder();
1✔
188

189
                $qb->select('*')
1✔
190
                        ->from($this->getTableName())
1✔
191
                        ->where(
1✔
192
                                $qb->expr()->eq('id', $qb->createNamedParameter($signRequestId, IQueryBuilder::PARAM_INT))
1✔
193
                        );
1✔
194

195
                /** @var SignRequest */
196
                $signRequest = $this->findEntity($qb);
1✔
197
                $this->cacheEntity($signRequest);
1✔
198
                return $signRequest;
1✔
199
        }
200

201
        /**
202
         * Get sign requests of child files from an envelope for the same signer
203
         *
204
         * @return SignRequest[]
205
         */
206
        public function getByEnvelopeChildrenAndIdentifyMethod(int $parentFileId, int $signRequestId): array {
207
                $qb = $this->db->getQueryBuilder();
×
208

209
                $qb->select('sr.*')
×
210
                        ->from('libresign_file', 'f')
×
211
                        ->innerJoin('f', $this->getTableName(), 'sr', $qb->expr()->eq('sr.file_id', 'f.id'))
×
212
                        ->innerJoin('sr', 'libresign_identify_method', 'im', $qb->expr()->eq('im.sign_request_id', 'sr.id'))
×
213
                        ->innerJoin('im', 'libresign_identify_method', 'im2',
×
214
                                $qb->expr()->andX(
×
215
                                        $qb->expr()->eq('im2.sign_request_id', $qb->createNamedParameter($signRequestId, IQueryBuilder::PARAM_INT)),
×
216
                                        $qb->expr()->eq('im2.identifier_key', 'im.identifier_key'),
×
217
                                        $qb->expr()->eq('im2.identifier_value', 'im.identifier_value')
×
218
                                )
×
219
                        )
×
220
                        ->where(
×
221
                                $qb->expr()->eq('f.parent_file_id', $qb->createNamedParameter($parentFileId, IQueryBuilder::PARAM_INT))
×
222
                        );
×
223

224
                /** @var SignRequest[] */
225
                $signRequests = $this->findEntities($qb);
×
226
                foreach ($signRequests as $signRequest) {
×
NEW
227
                        $this->cacheEntity($signRequest);
×
228
                }
229
                return $signRequests;
×
230
        }
231

232
        /**
233
         * @return \Generator<IdentifyMethod>
234
         */
235
        public function findRemindersCandidates(): \Generator {
236
                $qb = $this->db->getQueryBuilder();
×
237
                $qb->select(
×
238
                        'sr.id AS sr_id',
×
239
                        'sr.file_id AS sr_file_id',
×
240
                        'sr.uuid AS sr_uuid',
×
241
                        'sr.display_name AS sr_display_name',
×
242
                        'sr.description AS sr_description',
×
243
                        'sr.metadata AS sr_metadata',
×
244
                        'sr.signed_hash AS sr_signed_hash',
×
245
                        'sr.created_at AS sr_created_at',
×
246
                        'sr.signed AS sr_signed',
×
247

248
                        'im.id AS im_id',
×
249
                        'im.mandatory AS im_mandatory',
×
250
                        'im.code AS im_code',
×
251
                        'im.identifier_key AS im_identifier_key',
×
252
                        'im.identifier_value AS im_identifier_value',
×
253
                        'im.attempts AS im_attempts',
×
254
                        'im.identified_at_date AS im_identified_at_date',
×
255
                        'im.last_attempt_date AS im_last_attempt_date',
×
256
                        'im.sign_request_id AS im_sign_request_id',
×
257
                        'im.metadata AS im_metadata',
×
258
                )
×
259
                        ->from('libresign_sign_request', 'sr')
×
260
                        ->join('sr', 'libresign_identify_method', 'im', 'sr.id = im.sign_request_id')
×
261
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
262
                        ->where($qb->expr()->isNull('sr.signed'))
×
263
                        ->andWhere($qb->expr()->neq('im.identifier_value', $qb->createNamedParameter('deleted_users')))
×
264
                        ->andWhere($qb->expr()->in('f.status', $qb->createNamedParameter([
×
265
                                FileStatus::ABLE_TO_SIGN->value,
×
266
                                FileStatus::PARTIAL_SIGNED->value
×
267
                        ], IQueryBuilder::PARAM_INT_ARRAY)))
×
268
                        ->setParameter('st', [1,2], IQueryBuilder::PARAM_INT_ARRAY)
×
269
                        ->orderBy('sr.id', 'ASC');
×
270

271
                $result = $qb->executeQuery();
×
272
                try {
273
                        /** @var array<string, mixed> $row */
274
                        while ($row = $result->fetch()) {
×
275
                                $signRequest = new SignRequest();
×
276
                                $identifyMethod = new IdentifyMethod();
×
277
                                foreach ($row as $key => $value) {
×
278
                                        $prop = $identifyMethod->columnToProperty(substr($key, 3));
×
279
                                        if (str_starts_with($key, 'sr_')) {
×
280
                                                $signRequest->{'set' . lcfirst($prop)}($value);
×
281
                                        } else {
282
                                                $identifyMethod->{'set' . lcfirst($prop)}($value);
×
283
                                        }
284
                                }
285
                                $signRequest->resetUpdatedFields();
×
286
                                $identifyMethod->resetUpdatedFields();
×
NEW
287
                                $this->cacheEntity($signRequest);
×
288
                                yield $identifyMethod;
×
289
                        }
290
                } finally {
291
                        $result->closeCursor();
×
292
                }
293
        }
294

295
        /**
296
         * Get all signers by multiple fileId
297
         * Includes signers from both the files themselves and their children files (for envelopes)
298
         *
299
         * @return SignRequest[]
300
         */
301
        public function getByMultipleFileId(array $fileId) {
302
                $qb = $this->db->getQueryBuilder();
4✔
303

304
                $qb->select('sr.*')
4✔
305
                        ->from($this->getTableName(), 'sr')
4✔
306
                        ->join('sr', 'libresign_file', 'f', $qb->expr()->eq('sr.file_id', 'f.id'))
4✔
307
                        ->where(
4✔
308
                                $qb->expr()->orX(
4✔
309
                                        $qb->expr()->in('f.id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY)),
4✔
310
                                        $qb->expr()->in('f.parent_file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY))
4✔
311
                                )
4✔
312
                        )
4✔
313
                        ->orderBy('sr.id', 'ASC');
4✔
314

315
                /** @var SignRequest[] */
316
                $signers = $this->findEntities($qb);
4✔
317
                foreach ($signers as $signRequest) {
4✔
318
                        $this->cacheEntity($signRequest);
2✔
319
                }
320
                return $signers;
4✔
321
        }
322

323
        /**
324
         * Get all signers by fileId
325
         *
326
         * @return SignRequest[]
327
         */
328
        public function getByNodeId(int $nodeId) {
329
                $qb = $this->db->getQueryBuilder();
×
330

331
                $qb->select('sr.*')
×
332
                        ->from($this->getTableName(), 'sr')
×
333
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
334
                        ->where(
×
335
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT))
×
336
                        );
×
337

338
                /** @var SignRequest[] */
339
                $signers = $this->findEntities($qb);
×
NEW
340
                foreach ($signers as $signRequest) {
×
NEW
341
                        $this->cacheEntity($signRequest);
×
342
                }
343
                return $signers;
×
344
        }
345

346
        /**
347
         * Get all signers by File Uuid
348
         *
349
         * @param string $nodeId
350
         * @return SignRequest[]
351
         */
352
        public function getByFileUuid(string $uuid) {
353
                $qb = $this->db->getQueryBuilder();
×
354

355
                $qb->select('sr.*')
×
356
                        ->from($this->getTableName(), 'sr')
×
357
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
358
                        ->where(
×
359
                                $qb->expr()->eq('f.uuid', $qb->createNamedParameter($uuid))
×
360
                        );
×
361

362
                /** @var SignRequest[] */
363
                $signers = $this->findEntities($qb);
×
364
                foreach ($signers as $signRequest) {
×
NEW
365
                        $this->cacheEntity($signRequest);
×
366
                }
367
                return $signers;
×
368
        }
369

370
        public function getBySignerUuidAndUserId(string $uuid): SignRequest {
371
                $qb = $this->db->getQueryBuilder();
×
372

373
                $qb->select('sr.*')
×
374
                        ->from($this->getTableName(), 'sr')
×
375
                        ->where(
×
376
                                $qb->expr()->eq('sr.uuid', $qb->createNamedParameter($uuid))
×
377
                        );
×
378

379
                /** @var SignRequest */
380
                $signRequest = $this->findEntity($qb);
×
NEW
381
                $this->cacheEntity($signRequest);
×
382
                return $signRequest;
×
383
        }
384

385
        public function getByFileIdAndUserId(int $file_id): SignRequest {
386
                $qb = $this->db->getQueryBuilder();
×
387

388
                $qb->select('sr.*')
×
389
                        ->from($this->getTableName(), 'sr')
×
390
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
391
                        ->where(
×
392
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT))
×
393
                        );
×
394

395
                /** @var SignRequest */
NEW
396
                $signRequest = $this->findEntity($qb);
×
NEW
397
                $this->cacheEntity($signRequest);
×
NEW
398
                return $signRequest;
×
399
        }
400

401
        public function getByFileIdAndEmail(int $file_id, string $email): SignRequest {
402
                $qb = $this->db->getQueryBuilder();
×
403

404
                $qb->select('sr.*')
×
405
                        ->from($this->getTableName(), 'sr')
×
406
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
407
                        ->where(
×
408
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT))
×
409
                        )
×
410
                        ->andWhere(
×
411
                                $qb->expr()->eq('sr.email', $qb->createNamedParameter($email))
×
412
                        );
×
413

414
                /** @var SignRequest */
NEW
415
                $signRequest = $this->findEntity($qb);
×
NEW
416
                $this->cacheEntity($signRequest);
×
NEW
417
                return $signRequest;
×
418
        }
419

420
        public function getByFileIdAndSignRequestId(int $fileId, int $signRequestId): SignRequest {
421
                $qb = $this->db->getQueryBuilder();
2✔
422

423
                $qb->select('sr.*')
2✔
424
                        ->from($this->getTableName(), 'sr')
2✔
425
                        ->where(
2✔
426
                                $qb->expr()->eq('sr.file_id', $qb->createNamedParameter($fileId))
2✔
427
                        )
2✔
428
                        ->andWhere(
2✔
429
                                $qb->expr()->eq('sr.id', $qb->createNamedParameter($signRequestId))
2✔
430
                        );
2✔
431

432
                /** @var SignRequest */
433
                $signRequest = $this->findEntity($qb);
2✔
434
                $this->cacheEntity($signRequest);
2✔
435
                return $signRequest;
2✔
436
        }
437

438
        /**
439
         * @return array{data: list<File>, pagination: Pagination}
440
         */
441
        public function getFilesAssociatedFilesWithMe(
442
                IUser $user,
443
                array $filter,
444
                ?int $page = null,
445
                ?int $length = null,
446
                ?array $sort = [],
447
        ): array {
448
                $filter['email'] = $user->getEMailAddress();
1✔
449
                $filter['length'] = $length;
1✔
450
                $filter['page'] = $page;
1✔
451
                $pagination = $this->getFilesAssociatedFilesWithMeStmt($user->getUID(), $filter, $sort);
1✔
452
                $pagination->setMaxPerPage($length);
1✔
453
                $pagination->setCurrentPage($page);
1✔
454
                $currentPageResults = $pagination->getCurrentPageResults();
1✔
455

456
                $data = [];
1✔
457
                foreach ($currentPageResults as $row) {
1✔
458
                        $file = new File();
×
459
                        $data[] = $file->fromRow($row);
×
460
                }
461
                /** @var array{data: list<File>, pagination: Pagination} */
462
                return [
1✔
463
                        'data' => $data,
1✔
464
                        'pagination' => $pagination,
1✔
465
                ];
1✔
466
        }
467

468
        public function getFilesToSearchProvider(IUser $user, string $fileName, int $limit, int $offset): array {
469
                $filter = [
×
470
                        'page' => ($offset / $limit) + 1,
×
471
                        'length' => $limit,
×
472
                        'fileName' => $fileName,
×
473
                ];
×
474

475
                $sort = [
×
476
                        'sortBy' => 'created_at',
×
477
                        'sortDirection' => 'desc',
×
478
                ];
×
479

480
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder($user->getUID(), $filter, false, $sort);
×
481

482
                $result = $qb->executeQuery();
×
483
                $files = [];
×
484

485
                while ($row = $result->fetch()) {
×
486
                        try {
487
                                $file = File::fromRow($row);
×
488

489
                                $files[] = $file;
×
490
                        } catch (\Exception $e) {
×
491
                                continue;
×
492
                        }
493
                }
494

495
                $result->closeCursor();
×
496

497
                return $files;
×
498
        }
499

500
        /**
501
         * @param array<SignRequest> $signRequests
502
         * @return FileElement[][]
503
         */
504
        public function getVisibleElementsFromSigners(array $signRequests): array {
505
                $signRequestIds = array_map(fn (SignRequest $signRequest): int => $signRequest->getId(), $signRequests);
6✔
506
                if (!$signRequestIds) {
6✔
507
                        return [];
2✔
508
                }
509
                $qb = $this->db->getQueryBuilder();
4✔
510

511
                $qb->select('fe.*')
4✔
512
                        ->from('libresign_file_element', 'fe')
4✔
513
                        ->where(
4✔
514
                                $qb->expr()->in('fe.sign_request_id', $qb->createParameter('signRequestIds'))
4✔
515
                        );
4✔
516

517
                $return = [];
4✔
518
                foreach (array_chunk($signRequestIds, 1000) as $signRequestIdsChunk) {
4✔
519
                        $qb->setParameter('signRequestIds', $signRequestIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
4✔
520
                        $cursor = $qb->executeQuery();
4✔
521
                        while ($row = $cursor->fetch()) {
4✔
522
                                $return[$row['sign_request_id']][] = (new FileElement())->fromRow($row);
×
523
                        }
524
                }
525
                return $return;
4✔
526
        }
527

528
        /**
529
         * @param array<SignRequest> $signRequests
530
         * @return array<array-key, array<array-key, \OCP\AppFramework\Db\Entity&\OCA\Libresign\Db\IdentifyMethod>>
531
         */
532
        public function getIdentifyMethodsFromSigners(array $signRequests): array {
533
                $signRequestIds = array_map(fn (SignRequest $signRequest): int => $signRequest->getId(), $signRequests);
4✔
534
                if (!$signRequestIds) {
4✔
535
                        return [];
2✔
536
                }
537
                $qb = $this->db->getQueryBuilder();
2✔
538
                $qb->select('im.*')
2✔
539
                        ->from('libresign_identify_method', 'im')
2✔
540
                        ->where(
2✔
541
                                $qb->expr()->in('im.sign_request_id', $qb->createParameter('signRequestIds'))
2✔
542
                        )
2✔
543
                        ->orderBy('im.mandatory', 'DESC')
2✔
544
                        ->addOrderBy('im.identified_at_date', 'ASC');
2✔
545

546
                $return = [];
2✔
547
                foreach (array_chunk($signRequestIds, 1000) as $signRequestIdsChunk) {
2✔
548
                        $qb->setParameter('signRequestIds', $signRequestIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
2✔
549
                        $cursor = $qb->executeQuery();
2✔
550
                        while ($row = $cursor->fetch()) {
2✔
551
                                $identifyMethod = new IdentifyMethod();
2✔
552
                                $return[$row['sign_request_id']][$row['identifier_key']] = $identifyMethod->fromRow($row);
2✔
553
                        }
554
                }
555
                return $return;
2✔
556
        }
557

558
        public function getMyLibresignFile(string $userId, ?array $filter = []): File {
559
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
×
560
                        userId: $userId,
×
561
                        filter: $filter,
×
562
                );
×
563
                $cursor = $qb->executeQuery();
×
564
                $row = $cursor->fetch();
×
565
                if (!$row) {
×
566
                        throw new DoesNotExistException('LibreSign file not found');
×
567
                }
568

569
                $file = new File();
×
570
                return $file->fromRow($row);
×
571
        }
572

573
        private function getFilesAssociatedFilesWithMeQueryBuilder(
574
                string $userId,
575
                array $filter = [],
576
                bool $count = false,
577
                array $sort = [],
578
        ): IQueryBuilder {
579
                $qb = $this->db->getQueryBuilder();
1✔
580
                $qb->from('libresign_file', 'f')
1✔
581
                        ->leftJoin('f', 'libresign_sign_request', 'sr', 'sr.file_id = f.id')
1✔
582
                        ->leftJoin('f', 'libresign_identify_method', 'im', $qb->expr()->eq('sr.id', 'im.sign_request_id'))
1✔
583
                        ->leftJoin('f', 'libresign_id_docs', 'id', 'id.file_id = f.id');
1✔
584
                if ($count) {
1✔
585
                        $qb->select($qb->func()->count())
1✔
586
                                ->setFirstResult(0)
1✔
587
                                ->setMaxResults(null);
1✔
588
                } else {
589
                        $qb->select(
1✔
590
                                'f.id',
1✔
591
                                'f.node_id',
1✔
592
                                'f.signed_node_id',
1✔
593
                                'f.user_id',
1✔
594
                                'f.uuid',
1✔
595
                                'f.name',
1✔
596
                                'f.status',
1✔
597
                                'f.metadata',
1✔
598
                                'f.created_at',
1✔
599
                                'f.signature_flow',
1✔
600
                                'f.docmdp_level',
1✔
601
                                'f.node_type',
1✔
602
                                'f.parent_file_id'
1✔
603
                        )
1✔
604
                                ->groupBy(
1✔
605
                                        'f.id',
1✔
606
                                        'f.node_id',
1✔
607
                                        'f.signed_node_id',
1✔
608
                                        'f.user_id',
1✔
609
                                        'f.uuid',
1✔
610
                                        'f.name',
1✔
611
                                        'f.status',
1✔
612
                                        'f.created_at',
1✔
613
                                        'f.signature_flow',
1✔
614
                                        'f.docmdp_level',
1✔
615
                                        'f.node_type',
1✔
616
                                        'f.parent_file_id'
1✔
617
                                );
1✔
618
                        // metadata is a json column, the right way is to use f.metadata::text
619
                        // when the database is PostgreSQL. The problem is that the command
620
                        // addGroupBy add quotes over all text send as argument. With
621
                        // PostgreSQL json columns don't have problem if not added to group by.
622
                        if ($qb->getConnection()->getDatabaseProvider() !== IDBConnection::PLATFORM_POSTGRES) {
1✔
623
                                $qb->addGroupBy('f.metadata');
1✔
624
                        }
625
                        if (isset($filter['length']) && isset($filter['page'])) {
1✔
626
                                $qb->setFirstResult($filter['length'] * ($filter['page'] - 1));
1✔
627
                                $qb->setMaxResults($filter['length']);
1✔
628
                        }
629
                }
630

631
                $or = [
1✔
632
                        $qb->expr()->eq('f.user_id', $qb->createNamedParameter($userId)),
1✔
633
                        $qb->expr()->andX(
1✔
634
                                $qb->expr()->eq('im.identifier_key', $qb->createNamedParameter(IdentifyMethodService::IDENTIFY_ACCOUNT)),
1✔
635
                                $qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($userId)),
1✔
636
                                $qb->expr()->neq('f.status', $qb->createNamedParameter(FileStatus::DRAFT->value)),
1✔
637
                                $qb->expr()->neq('sr.status', $qb->createNamedParameter(SignRequestStatus::DRAFT->value)),
1✔
638
                        )
1✔
639
                ];
1✔
640
                $qb->where($qb->expr()->orX(...$or))
1✔
641
                        ->andWhere($qb->expr()->isNull('id.id'));
1✔
642

643
                if ($filter) {
1✔
644
                        if (isset($filter['email']) && filter_var($filter['email'], FILTER_VALIDATE_EMAIL)) {
1✔
645
                                $or[] = $qb->expr()->andX(
×
646
                                        $qb->expr()->eq('im.identifier_key', $qb->createNamedParameter(IdentifyMethodService::IDENTIFY_EMAIL)),
×
647
                                        $qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($filter['email']))
×
648
                                );
×
649
                        }
650
                        if (!empty($filter['signer_uuid']) && !empty($filter['parentFileId'])) {
1✔
651
                                $qb->leftJoin('f', 'libresign_file', 'parent', $qb->expr()->eq('f.parent_file_id', 'parent.id'));
×
652
                                $qb->innerJoin('parent', 'libresign_sign_request', 'psr', $qb->expr()->eq('psr.file_id', 'parent.id'))
×
653
                                        ->andWhere($qb->expr()->eq('psr.uuid', $qb->createNamedParameter($filter['signer_uuid'])));
×
654
                        } elseif (!empty($filter['signer_uuid'])) {
1✔
655
                                $qb->andWhere(
×
656
                                        $qb->expr()->eq('sr.uuid', $qb->createNamedParameter($filter['signer_uuid']))
×
657
                                );
×
658
                        }
659
                        if (!empty($filter['nodeIds'])) {
1✔
660
                                $qb->andWhere(
×
661
                                        $qb->expr()->orX(
×
662
                                                $qb->expr()->in('f.node_id', $qb->createNamedParameter($filter['nodeIds'], IQueryBuilder::PARAM_INT_ARRAY)),
×
663
                                                $qb->expr()->in('f.signed_node_id', $qb->createNamedParameter($filter['nodeIds'], IQueryBuilder::PARAM_INT_ARRAY))
×
664
                                        )
×
665
                                );
×
666
                        }
667
                        if (!empty($filter['fileIds'])) {
1✔
668
                                $qb->andWhere(
×
669
                                        $qb->expr()->in('f.id', $qb->createNamedParameter($filter['fileIds'], IQueryBuilder::PARAM_INT_ARRAY))
×
670
                                );
×
671
                        }
672
                        if (!empty($filter['status'])) {
1✔
673
                                $qb->andWhere(
×
674
                                        $qb->expr()->in('f.status', $qb->createNamedParameter($filter['status'], IQueryBuilder::PARAM_INT_ARRAY))
×
675
                                );
×
676
                        }
677
                        if (!empty($filter['start'])) {
1✔
678
                                $start = (new \DateTime('@' . $filter['start'], new \DateTimeZone('UTC')))->format('Y-m-d H:i:s');
×
679
                                $qb->andWhere(
×
680
                                        $qb->expr()->gte('f.created_at', $qb->createNamedParameter($start, IQueryBuilder::PARAM_STR))
×
681
                                );
×
682
                        }
683
                        if (!empty($filter['end'])) {
1✔
684
                                $end = (new \DateTime('@' . $filter['end'], new \DateTimeZone('UTC')))->format('Y-m-d H:i:s');
×
685
                                $qb->andWhere(
×
686
                                        $qb->expr()->lte('f.created_at', $qb->createNamedParameter($end, IQueryBuilder::PARAM_STR))
×
687
                                );
×
688
                        }
689
                        if (!empty($filter['fileName'])) {
1✔
690
                                $qb->andWhere(
×
691
                                        $qb->expr()->like('f.name', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($filter['fileName']) . '%'))
×
692
                                );
×
693
                        }
694
                        if (!empty($filter['parentFileId'])) {
1✔
695
                                $qb->andWhere(
×
696
                                        $qb->expr()->eq('f.parent_file_id', $qb->createNamedParameter($filter['parentFileId'], IQueryBuilder::PARAM_INT))
×
697
                                );
×
698
                        } else {
699
                                $qb->andWhere($qb->expr()->isNull('f.parent_file_id'));
1✔
700
                        }
701
                }
702

703
                if (!empty($sort['sortBy']) && !empty($sort['sortDirection'])) {
1✔
704
                        switch ($sort['sortBy']) {
×
705
                                case 'name':
×
706
                                case 'status':
×
707
                                        $qb->orderBy(
×
708
                                                $qb->func()->lower('f.' . $sort['sortBy']),
×
709
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
710
                                        );
×
711
                                        break;
×
712
                                case 'created_at':
×
713
                                        $qb->orderBy(
×
714
                                                'f.' . $sort['sortBy'],
×
715
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
716
                                        );
×
717
                        }
718
                }
719

720
                return $qb;
1✔
721
        }
722

723
        private function getFilesAssociatedFilesWithMeStmt(
724
                string $userId,
725
                ?array $filter = [],
726
                ?array $sort = [],
727
        ): Pagination {
728
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder($userId, $filter, false, $sort);
1✔
729

730
                $countQb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
1✔
731
                        userId: $userId,
1✔
732
                        filter: $filter,
1✔
733
                        count: true,
1✔
734
                );
1✔
735

736
                $pagination = new Pagination($qb, $this->urlGenerator, $countQb);
1✔
737
                return $pagination;
1✔
738
        }
739

740
        public function getTextOfSignerStatus(int $status): string {
741
                return SignRequestStatus::from($status)->getLabel($this->l10n);
4✔
742
        }
743
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc