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

LibreSign / libresign / 20602615611

30 Dec 2025 05:53PM UTC coverage: 44.726%. First build
20602615611

Pull #6243

github

web-flow
Merge 608a30374 into 4ec2116a5
Pull Request #6243: feat: unified search

4 of 87 new or added lines in 2 files covered. (4.6%)

6611 of 14781 relevant lines covered (44.73%)

5.05 hits per line

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

49.52
/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\SignRequestStatus;
12
use OCA\Libresign\Helper\Pagination;
13
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
14
use OCA\Libresign\Service\IdentifyMethodService;
15
use OCP\AppFramework\Db\DoesNotExistException;
16
use OCP\AppFramework\Db\Entity;
17
use OCP\AppFramework\Db\QBMapper;
18
use OCP\DB\QueryBuilder\IQueryBuilder;
19
use OCP\IDateTimeFormatter;
20
use OCP\IDBConnection;
21
use OCP\IL10N;
22
use OCP\IURLGenerator;
23
use OCP\IUser;
24
use OCP\IUserManager;
25

26
/**
27
 * Class SignRequestMapper
28
 *
29
 * @package OCA\Libresign\DB
30
 * @template-extends QBMapper<SignRequest>
31
 */
32
class SignRequestMapper extends QBMapper {
33
        /**
34
         * @var SignRequest[]
35
         */
36
        private $signers = [];
37
        private bool $firstNotification = false;
38

39
        public function __construct(
40
                IDBConnection $db,
41
                protected IL10N $l10n,
42
                protected FileMapper $fileMapper,
43
                private IUserManager $userManager,
44
                private IDateTimeFormatter $dateTimeFormatter,
45
                private IURLGenerator $urlGenerator,
46
        ) {
47
                parent::__construct($db, 'libresign_sign_request');
47✔
48
        }
49

50
        /**
51
         * @return boolean true when is the first notification
52
         */
53
        public function incrementNotificationCounter(SignRequest $signRequest, string $method): bool {
54
                $this->db->beginTransaction();
13✔
55
                try {
56
                        $fromDatabase = $this->getById($signRequest->getId());
13✔
57
                        $metadata = $fromDatabase->getMetadata();
13✔
58
                        if (!isset($metadata['notify'])) {
13✔
59
                                $this->firstNotification = true;
13✔
60
                        }
61

62
                        $notificationEntry = [
13✔
63
                                'method' => $method,
13✔
64
                                'date' => time(),
13✔
65
                        ];
13✔
66

67
                        if (!empty($fromDatabase->getDescription())) {
13✔
68
                                $notificationEntry['description'] = $fromDatabase->getDescription();
×
69
                        }
70

71
                        $metadata['notify'][] = $notificationEntry;
13✔
72
                        $fromDatabase->setMetadata($metadata);
13✔
73
                        $this->update($fromDatabase);
13✔
74
                        $this->db->commit();
13✔
75
                } catch (\Throwable) {
×
76
                        $this->db->rollBack();
×
77
                }
78
                return $this->firstNotification;
13✔
79
        }
80

81
        /**
82
         * @inheritDoc
83
         */
84
        #[\Override]
85
        public function update(Entity $entity): SignRequest {
86
                /** @var SignRequest */
87
                $signRequest = parent::update($entity);
13✔
88
                $this->signers[$signRequest->getId()] = $signRequest;
13✔
89
                return $signRequest;
13✔
90
        }
91

92
        /**
93
         * Get sign request by UUID
94
         *
95
         * @throws DoesNotExistException
96
         */
97
        public function getByUuid(string $uuid): SignRequest {
98
                foreach ($this->signers as $signRequest) {
5✔
99
                        if ($signRequest->getUuid() === $uuid) {
5✔
100
                                return $signRequest;
4✔
101
                        }
102
                }
103
                $qb = $this->db->getQueryBuilder();
1✔
104

105
                $qb->select('*')
1✔
106
                        ->from($this->getTableName())
1✔
107
                        ->where(
1✔
108
                                $qb->expr()->eq('uuid', $qb->createNamedParameter($uuid))
1✔
109
                        );
1✔
110
                /** @var SignRequest */
111
                $signRequest = $this->findEntity($qb);
1✔
112
                if (!isset($this->signers[$signRequest->getId()])) {
×
113
                        $this->signers[$signRequest->getId()] = $signRequest;
×
114
                }
115
                return $signRequest;
×
116
        }
117

118
        public function getByEmailAndFileId(string $email, int $fileId): SignRequest {
119
                $qb = $this->db->getQueryBuilder();
×
120

121
                $qb->select('*')
×
122
                        ->from($this->getTableName())
×
123
                        ->where(
×
124
                                $qb->expr()->eq('email', $qb->createNamedParameter($email))
×
125
                        )
×
126
                        ->andWhere(
×
127
                                $qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))
×
128
                        );
×
129
                /** @var SignRequest */
130
                return $this->findEntity($qb);
×
131
        }
132

133
        public function getByIdentifyMethodAndFileId(IIdentifyMethod $identifyMethod, int $fileId): SignRequest {
134
                $qb = $this->db->getQueryBuilder();
13✔
135
                $qb->select('sr.*')
13✔
136
                        ->from($this->getTableName(), 'sr')
13✔
137
                        ->join('sr', 'libresign_identify_method', 'im', 'sr.id = im.sign_request_id')
13✔
138
                        ->where($qb->expr()->eq('im.identifier_key', $qb->createNamedParameter($identifyMethod->getEntity()->getIdentifierKey())))
13✔
139
                        ->andWhere($qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($identifyMethod->getEntity()->getIdentifierValue())))
13✔
140
                        ->andWhere($qb->expr()->eq('sr.file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
13✔
141
                /** @var SignRequest */
142
                return $this->findEntity($qb);
13✔
143
        }
144

145
        /**
146
         * Get all signers by fileId
147
         *
148
         * @return SignRequest[]
149
         */
150
        public function getByFileId(int $fileId): array {
151
                $qb = $this->db->getQueryBuilder();
14✔
152

153
                $qb->select('*')
14✔
154
                        ->from($this->getTableName())
14✔
155
                        ->where(
14✔
156
                                $qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))
14✔
157
                        );
14✔
158
                /** @var SignRequest[] */
159
                $signers = $this->findEntities($qb);
14✔
160
                foreach ($signers as $signRequest) {
14✔
161
                        if (!isset($this->signers[$signRequest->getId()])) {
13✔
162
                                $this->signers[$signRequest->getId()] = $signRequest;
1✔
163
                        }
164
                }
165
                return $signers;
14✔
166
        }
167

168
        /**
169
         * @throws DoesNotExistException
170
         */
171
        public function getById(int $signRequestId): SignRequest {
172
                if (isset($this->signers[$signRequestId])) {
15✔
173
                        return $this->signers[$signRequestId];
14✔
174
                }
175
                $qb = $this->db->getQueryBuilder();
14✔
176

177
                $qb->select('*')
14✔
178
                        ->from($this->getTableName())
14✔
179
                        ->where(
14✔
180
                                $qb->expr()->eq('id', $qb->createNamedParameter($signRequestId, IQueryBuilder::PARAM_INT))
14✔
181
                        );
14✔
182

183
                /** @var SignRequest */
184
                $signRequest = $this->findEntity($qb);
14✔
185
                if (!isset($this->signers[$signRequest->getId()])) {
14✔
186
                        $this->signers[$signRequest->getId()] = $signRequest;
14✔
187
                }
188
                return $signRequest;
14✔
189
        }
190

191
        /**
192
         * Get sign requests of child files from an envelope for the same signer
193
         *
194
         * @return SignRequest[]
195
         */
196
        public function getByEnvelopeChildrenAndIdentifyMethod(int $parentFileId, int $signRequestId): array {
197
                $qb = $this->db->getQueryBuilder();
×
198

199
                $qb->select('sr.*')
×
200
                        ->from('libresign_file', 'f')
×
201
                        ->innerJoin('f', $this->getTableName(), 'sr', $qb->expr()->eq('sr.file_id', 'f.id'))
×
202
                        ->innerJoin('sr', 'libresign_identify_method', 'im', $qb->expr()->eq('im.sign_request_id', 'sr.id'))
×
203
                        ->innerJoin('im', 'libresign_identify_method', 'im2',
×
204
                                $qb->expr()->andX(
×
205
                                        $qb->expr()->eq('im2.sign_request_id', $qb->createNamedParameter($signRequestId, IQueryBuilder::PARAM_INT)),
×
206
                                        $qb->expr()->eq('im2.identifier_key', 'im.identifier_key'),
×
207
                                        $qb->expr()->eq('im2.identifier_value', 'im.identifier_value')
×
208
                                )
×
209
                        )
×
210
                        ->where(
×
211
                                $qb->expr()->eq('f.parent_file_id', $qb->createNamedParameter($parentFileId, IQueryBuilder::PARAM_INT))
×
212
                        );
×
213

214
                /** @var SignRequest[] */
215
                $signRequests = $this->findEntities($qb);
×
216
                foreach ($signRequests as $signRequest) {
×
217
                        if (!isset($this->signers[$signRequest->getId()])) {
×
218
                                $this->signers[$signRequest->getId()] = $signRequest;
×
219
                        }
220
                }
221
                return $signRequests;
×
222
        }
223

224
        /**
225
         * @return \Generator<IdentifyMethod>
226
         */
227
        public function findRemindersCandidates(): \Generator {
228
                $qb = $this->db->getQueryBuilder();
×
229
                $qb->select(
×
230
                        'sr.id AS sr_id',
×
231
                        'sr.file_id AS sr_file_id',
×
232
                        'sr.uuid AS sr_uuid',
×
233
                        'sr.display_name AS sr_display_name',
×
234
                        'sr.description AS sr_description',
×
235
                        'sr.metadata AS sr_metadata',
×
236
                        'sr.signed_hash AS sr_signed_hash',
×
237
                        'sr.created_at AS sr_created_at',
×
238
                        'sr.signed AS sr_signed',
×
239

240
                        'im.id AS im_id',
×
241
                        'im.mandatory AS im_mandatory',
×
242
                        'im.code AS im_code',
×
243
                        'im.identifier_key AS im_identifier_key',
×
244
                        'im.identifier_value AS im_identifier_value',
×
245
                        'im.attempts AS im_attempts',
×
246
                        'im.identified_at_date AS im_identified_at_date',
×
247
                        'im.last_attempt_date AS im_last_attempt_date',
×
248
                        'im.sign_request_id AS im_sign_request_id',
×
249
                        'im.metadata AS im_metadata',
×
250
                )
×
251
                        ->from('libresign_sign_request', 'sr')
×
252
                        ->join('sr', 'libresign_identify_method', 'im', 'sr.id = im.sign_request_id')
×
253
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
254
                        ->where($qb->expr()->isNull('sr.signed'))
×
255
                        ->andWhere($qb->expr()->neq('im.identifier_value', $qb->createNamedParameter('deleted_users')))
×
256
                        ->andWhere($qb->expr()->in('f.status', $qb->createNamedParameter([
×
257
                                File::STATUS_ABLE_TO_SIGN,
×
258
                                File::STATUS_PARTIAL_SIGNED
×
259
                        ], IQueryBuilder::PARAM_INT_ARRAY)))
×
260
                        ->setParameter('st', [1,2], IQueryBuilder::PARAM_INT_ARRAY)
×
261
                        ->orderBy('sr.id', 'ASC');
×
262

263
                $result = $qb->executeQuery();
×
264
                try {
265
                        /** @var array<string, mixed> $row */
266
                        while ($row = $result->fetch()) {
×
267
                                $signRequest = new SignRequest();
×
268
                                $identifyMethod = new IdentifyMethod();
×
269
                                foreach ($row as $key => $value) {
×
270
                                        $prop = $identifyMethod->columnToProperty(substr($key, 3));
×
271
                                        if (str_starts_with($key, 'sr_')) {
×
272
                                                $signRequest->{'set' . lcfirst($prop)}($value);
×
273
                                        } else {
274
                                                $identifyMethod->{'set' . lcfirst($prop)}($value);
×
275
                                        }
276
                                }
277
                                $signRequest->resetUpdatedFields();
×
278
                                $identifyMethod->resetUpdatedFields();
×
279
                                if (!isset($this->signers[$signRequest->getId()])) {
×
280
                                        $this->signers[$signRequest->getId()] = $signRequest;
×
281
                                }
282
                                yield $identifyMethod;
×
283
                        }
284
                } finally {
285
                        $result->closeCursor();
×
286
                }
287
        }
288

289
        /**
290
         * Get all signers by multiple fileId
291
         * Includes signers from both the files themselves and their children files (for envelopes)
292
         *
293
         * @return SignRequest[]
294
         */
295
        public function getByMultipleFileId(array $fileId) {
296
                $qb = $this->db->getQueryBuilder();
6✔
297

298
                $qb->select('sr.*')
6✔
299
                        ->from($this->getTableName(), 'sr')
6✔
300
                        ->join('sr', 'libresign_file', 'f', $qb->expr()->eq('sr.file_id', 'f.id'))
6✔
301
                        ->where(
6✔
302
                                $qb->expr()->orX(
6✔
303
                                        $qb->expr()->in('f.id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY)),
6✔
304
                                        $qb->expr()->in('f.parent_file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY))
6✔
305
                                )
6✔
306
                        )
6✔
307
                        ->orderBy('sr.id', 'ASC');
6✔
308

309
                /** @var SignRequest[] */
310
                return $this->findEntities($qb);
6✔
311
        }
312

313
        /**
314
         * Get all signers by fileId
315
         *
316
         * @return SignRequest[]
317
         */
318
        public function getByNodeId(int $nodeId) {
319
                $qb = $this->db->getQueryBuilder();
×
320

321
                $qb->select('sr.*')
×
322
                        ->from($this->getTableName(), 'sr')
×
323
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
324
                        ->where(
×
325
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT))
×
326
                        );
×
327

328
                /** @var SignRequest[] */
329
                $signers = $this->findEntities($qb);
×
330
                return $signers;
×
331
        }
332

333
        /**
334
         * Get all signers by File Uuid
335
         *
336
         * @param string $nodeId
337
         * @return SignRequest[]
338
         */
339
        public function getByFileUuid(string $uuid) {
340
                $qb = $this->db->getQueryBuilder();
1✔
341

342
                $qb->select('sr.*')
1✔
343
                        ->from($this->getTableName(), 'sr')
1✔
344
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
1✔
345
                        ->where(
1✔
346
                                $qb->expr()->eq('f.uuid', $qb->createNamedParameter($uuid))
1✔
347
                        );
1✔
348

349
                /** @var SignRequest[] */
350
                $signers = $this->findEntities($qb);
1✔
351
                foreach ($signers as $signRequest) {
1✔
352
                        if (!isset($this->signers[$signRequest->getId()])) {
1✔
353
                                $this->signers[$signRequest->getId()] = $signRequest;
×
354
                        }
355
                }
356
                return $signers;
1✔
357
        }
358

359
        public function getBySignerUuidAndUserId(string $uuid): SignRequest {
360
                $qb = $this->db->getQueryBuilder();
×
361

362
                $qb->select('sr.*')
×
363
                        ->from($this->getTableName(), 'sr')
×
364
                        ->where(
×
365
                                $qb->expr()->eq('sr.uuid', $qb->createNamedParameter($uuid))
×
366
                        );
×
367

368
                /** @var SignRequest */
369
                $signRequest = $this->findEntity($qb);
×
370
                if (!isset($this->signers[$signRequest->getId()])) {
×
371
                        $this->signers[$signRequest->getId()] = $signRequest;
×
372
                }
373
                return $signRequest;
×
374
        }
375

376
        public function getByFileIdAndUserId(int $file_id): SignRequest {
377
                $qb = $this->db->getQueryBuilder();
×
378

379
                $qb->select('sr.*')
×
380
                        ->from($this->getTableName(), 'sr')
×
381
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
382
                        ->where(
×
383
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT))
×
384
                        );
×
385

386
                /** @var SignRequest */
387
                return $this->findEntity($qb);
×
388
        }
389

390
        public function getByFileIdAndEmail(int $file_id, string $email): SignRequest {
391
                $qb = $this->db->getQueryBuilder();
×
392

393
                $qb->select('sr.*')
×
394
                        ->from($this->getTableName(), 'sr')
×
395
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
396
                        ->where(
×
397
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT))
×
398
                        )
×
399
                        ->andWhere(
×
400
                                $qb->expr()->eq('sr.email', $qb->createNamedParameter($email))
×
401
                        );
×
402

403
                /** @var SignRequest */
404
                return $this->findEntity($qb);
×
405
        }
406

407
        public function getByFileIdAndSignRequestId(int $fileId, int $signRequestId): SignRequest {
408
                if (isset($this->signers[$signRequestId])) {
2✔
409
                        return $this->signers[$signRequestId];
2✔
410
                }
411
                $qb = $this->db->getQueryBuilder();
×
412

413
                $qb->select('sr.*')
×
414
                        ->from($this->getTableName(), 'sr')
×
415
                        ->where(
×
416
                                $qb->expr()->eq('sr.file_id', $qb->createNamedParameter($fileId))
×
417
                        )
×
418
                        ->andWhere(
×
419
                                $qb->expr()->eq('sr.id', $qb->createNamedParameter($signRequestId))
×
420
                        );
×
421

422
                $signRequest = $this->findEntity($qb);
×
423
                if (!isset($this->signers[$signRequest->getId()])) {
×
424
                        $this->signers[$signRequest->getId()] = $signRequest;
×
425
                }
426
                /** @var SignRequest */
427
                return end($this->signers);
×
428
        }
429

430
        /**
431
         * @return array{data: list<File>, pagination: Pagination}
432
         */
433
        public function getFilesAssociatedFilesWithMe(
434
                IUser $user,
435
                array $filter,
436
                ?int $page = null,
437
                ?int $length = null,
438
                ?array $sort = [],
439
        ): array {
440
                $filter['email'] = $user->getEMailAddress();
1✔
441
                $filter['length'] = $length;
1✔
442
                $filter['page'] = $page;
1✔
443
                $pagination = $this->getFilesAssociatedFilesWithMeStmt($user->getUID(), $filter, $sort);
1✔
444
                $pagination->setMaxPerPage($length);
1✔
445
                $pagination->setCurrentPage($page);
1✔
446
                $currentPageResults = $pagination->getCurrentPageResults();
1✔
447

448
                $data = [];
1✔
449
                foreach ($currentPageResults as $row) {
1✔
450
                        $file = new File();
×
451
                        $data[] = $file->fromRow($row);
×
452
                }
453
                /** @var array{data: list<File>, pagination: Pagination} */
454
                return [
1✔
455
                        'data' => $data,
1✔
456
                        'pagination' => $pagination,
1✔
457
                ];
1✔
458
        }
459

460
        public function getFilesToSearchProvider(IUser $user, string $fileName, int $limit, int $offset): array {
NEW
461
                $filter = [
×
NEW
462
                        'page' => ($offset / $limit) + 1,
×
NEW
463
                        'length' => $limit,
×
NEW
464
                        'fileName' => $fileName,
×
NEW
465
                ];
×
466

NEW
467
                $sort = [
×
NEW
468
                        'sortBy' => 'created_at',
×
NEW
469
                        'sortDirection' => 'desc',
×
NEW
470
                ];
×
471

NEW
472
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder($user->getUID(), $filter, false, $sort);
×
473

474

NEW
475
                $qb->orderBy('f.created_at', 'DESC');
×
476

NEW
477
                $result = $qb->executeQuery();
×
NEW
478
                $files = [];
×
479

NEW
480
                while ($row = $result->fetch()) {
×
481
                        try {
NEW
482
                                $file = File::fromRow($row);
×
483

NEW
484
                                $files[] = $file;
×
NEW
485
                        } catch (\Exception $e) {
×
NEW
486
                                continue;
×
487
                        }
488
                }
489

NEW
490
                $result->closeCursor();
×
491

NEW
492
                return $files;
×
493
        }
494

495
        /**
496
         * @param array<SignRequest> $signRequests
497
         * @return FileElement[][]
498
         */
499
        public function getVisibleElementsFromSigners(array $signRequests): array {
500
                $signRequestIds = array_map(fn (SignRequest $signRequest): int => $signRequest->getId(), $signRequests);
5✔
501
                if (!$signRequestIds) {
5✔
502
                        return [];
1✔
503
                }
504
                $qb = $this->db->getQueryBuilder();
4✔
505

506
                $qb->select('fe.*')
4✔
507
                        ->from('libresign_file_element', 'fe')
4✔
508
                        ->where(
4✔
509
                                $qb->expr()->in('fe.sign_request_id', $qb->createParameter('signRequestIds'))
4✔
510
                        );
4✔
511

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

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

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

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

564
                $file = new File();
×
565
                return $file->fromRow($row);
×
566
        }
567

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

621
                $or = [
1✔
622
                        $qb->expr()->eq('f.user_id', $qb->createNamedParameter($userId)),
1✔
623
                        $qb->expr()->andX(
1✔
624
                                $qb->expr()->eq('im.identifier_key', $qb->createNamedParameter(IdentifyMethodService::IDENTIFY_ACCOUNT)),
1✔
625
                                $qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($userId)),
1✔
626
                                $qb->expr()->neq('f.status', $qb->createNamedParameter(File::STATUS_DRAFT)),
1✔
627
                                $qb->expr()->neq('sr.status', $qb->createNamedParameter(SignRequestStatus::DRAFT->value)),
1✔
628
                        )
1✔
629
                ];
1✔
630
                $qb->where($qb->expr()->orX(...$or))
1✔
631
                        ->andWhere($qb->expr()->isNull('id.id'));
1✔
632

633
                if ($filter) {
1✔
634
                        if (isset($filter['email']) && filter_var($filter['email'], FILTER_VALIDATE_EMAIL)) {
1✔
635
                                $or[] = $qb->expr()->andX(
×
636
                                        $qb->expr()->eq('im.identifier_key', $qb->createNamedParameter(IdentifyMethodService::IDENTIFY_EMAIL)),
×
637
                                        $qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($filter['email']))
×
638
                                );
×
639
                        }
640
                        if (!empty($filter['signer_uuid']) && !empty($filter['parentFileId'])) {
1✔
641
                                $qb->leftJoin('f', 'libresign_file', 'parent', $qb->expr()->eq('f.parent_file_id', 'parent.id'));
×
642
                                $qb->innerJoin('parent', 'libresign_sign_request', 'psr', $qb->expr()->eq('psr.file_id', 'parent.id'))
×
643
                                        ->andWhere($qb->expr()->eq('psr.uuid', $qb->createNamedParameter($filter['signer_uuid'])));
×
644
                        } elseif (!empty($filter['signer_uuid'])) {
1✔
645
                                $qb->andWhere(
×
646
                                        $qb->expr()->eq('sr.uuid', $qb->createNamedParameter($filter['signer_uuid']))
×
647
                                );
×
648
                        }
649
                        if (!empty($filter['nodeIds'])) {
1✔
650
                                $qb->andWhere(
×
651
                                        $qb->expr()->in('f.node_id', $qb->createNamedParameter($filter['nodeIds'], IQueryBuilder::PARAM_STR_ARRAY))
×
652
                                );
×
653
                        }
654
                        if (!empty($filter['status'])) {
1✔
655
                                $qb->andWhere(
×
656
                                        $qb->expr()->in('f.status', $qb->createNamedParameter($filter['status'], IQueryBuilder::PARAM_INT_ARRAY))
×
657
                                );
×
658
                        }
659
                        if (!empty($filter['start'])) {
1✔
660
                                $start = (new \DateTime('@' . $filter['start'], new \DateTimeZone('UTC')))->format('Y-m-d H:i:s');
×
661
                                $qb->andWhere(
×
662
                                        $qb->expr()->gte('f.created_at', $qb->createNamedParameter($start, IQueryBuilder::PARAM_STR))
×
663
                                );
×
664
                        }
665
                        if (!empty($filter['end'])) {
1✔
666
                                $end = (new \DateTime('@' . $filter['end'], new \DateTimeZone('UTC')))->format('Y-m-d H:i:s');
×
667
                                $qb->andWhere(
×
668
                                        $qb->expr()->lte('f.created_at', $qb->createNamedParameter($end, IQueryBuilder::PARAM_STR))
×
669
                                );
×
670
                        }
671
                        if (!empty($filter['fileName'])) {
1✔
NEW
672
                                $qb->andWhere(
×
NEW
673
                                        $qb->expr()->like('f.name', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($filter['fileName']) . '%'))
×
NEW
674
                                );
×
675
                        }
676
                        if (!empty($filter['parentFileId'])) {
1✔
677
                                $qb->andWhere(
×
678
                                        $qb->expr()->eq('f.parent_file_id', $qb->createNamedParameter($filter['parentFileId'], IQueryBuilder::PARAM_INT))
×
679
                                );
×
680
                        } else {
681
                                $qb->andWhere($qb->expr()->isNull('f.parent_file_id'));
1✔
682
                        }
683
                }
684

685
                if (!empty($sort['sortBy']) && !empty($sort['sortDirection'])) {
1✔
686
                        switch ($sort['sortBy']) {
×
687
                                case 'name':
×
688
                                case 'status':
×
689
                                        $qb->orderBy(
×
690
                                                $qb->func()->lower('f.' . $sort['sortBy']),
×
691
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
692
                                        );
×
693
                                        break;
×
694
                                case 'created_at':
×
695
                                        $qb->orderBy(
×
696
                                                'f.' . $sort['sortBy'],
×
697
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
698
                                        );
×
699
                        }
700
                }
701

702
                return $qb;
1✔
703
        }
704

705
        private function getFilesAssociatedFilesWithMeStmt(
706
                string $userId,
707
                ?array $filter = [],
708
                ?array $sort = [],
709
        ): Pagination {
710
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder($userId, $filter, false, $sort);
1✔
711

712
                $countQb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
1✔
713
                        userId: $userId,
1✔
714
                        filter: $filter,
1✔
715
                        count: true,
1✔
716
                );
1✔
717

718
                $pagination = new Pagination($qb, $this->urlGenerator, $countQb);
1✔
719
                return $pagination;
1✔
720
        }
721

722
        public function getTextOfSignerStatus(int $status): string {
723
                return SignRequestStatus::from($status)->getLabel($this->l10n);
4✔
724
        }
725
}
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