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

LibreSign / libresign / 20771127101

07 Jan 2026 04:59AM UTC coverage: 44.717%. First build
20771127101

Pull #6365

github

web-flow
Merge 00613d753 into 374d4f18b
Pull Request #6365: feat: file id selection

2 of 24 new or added lines in 2 files covered. (8.33%)

6729 of 15048 relevant lines covered (44.72%)

5.02 hits per line

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

51.49
/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();
15✔
152

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

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

309
                /** @var SignRequest[] */
310
                return $this->findEntities($qb);
4✔
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
        /**
461
         * @param array<SignRequest> $signRequests
462
         * @return FileElement[][]
463
         */
464
        public function getVisibleElementsFromSigners(array $signRequests): array {
465
                $signRequestIds = array_map(fn (SignRequest $signRequest): int => $signRequest->getId(), $signRequests);
6✔
466
                if (!$signRequestIds) {
6✔
467
                        return [];
2✔
468
                }
469
                $qb = $this->db->getQueryBuilder();
4✔
470

471
                $qb->select('fe.*')
4✔
472
                        ->from('libresign_file_element', 'fe')
4✔
473
                        ->where(
4✔
474
                                $qb->expr()->in('fe.sign_request_id', $qb->createParameter('signRequestIds'))
4✔
475
                        );
4✔
476

477
                $return = [];
4✔
478
                foreach (array_chunk($signRequestIds, 1000) as $signRequestIdsChunk) {
4✔
479
                        $qb->setParameter('signRequestIds', $signRequestIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
4✔
480
                        $cursor = $qb->executeQuery();
4✔
481
                        while ($row = $cursor->fetch()) {
4✔
482
                                $return[$row['sign_request_id']][] = (new FileElement())->fromRow($row);
×
483
                        }
484
                }
485
                return $return;
4✔
486
        }
487

488
        /**
489
         * @param array<SignRequest> $signRequests
490
         * @return array<array-key, array<array-key, \OCP\AppFramework\Db\Entity&\OCA\Libresign\Db\IdentifyMethod>>
491
         */
492
        public function getIdentifyMethodsFromSigners(array $signRequests): array {
493
                $signRequestIds = array_map(fn (SignRequest $signRequest): int => $signRequest->getId(), $signRequests);
4✔
494
                if (!$signRequestIds) {
4✔
495
                        return [];
2✔
496
                }
497
                $qb = $this->db->getQueryBuilder();
2✔
498
                $qb->select('im.*')
2✔
499
                        ->from('libresign_identify_method', 'im')
2✔
500
                        ->where(
2✔
501
                                $qb->expr()->in('im.sign_request_id', $qb->createParameter('signRequestIds'))
2✔
502
                        )
2✔
503
                        ->orderBy('im.mandatory', 'DESC')
2✔
504
                        ->addOrderBy('im.identified_at_date', 'ASC');
2✔
505

506
                $return = [];
2✔
507
                foreach (array_chunk($signRequestIds, 1000) as $signRequestIdsChunk) {
2✔
508
                        $qb->setParameter('signRequestIds', $signRequestIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
2✔
509
                        $cursor = $qb->executeQuery();
2✔
510
                        while ($row = $cursor->fetch()) {
2✔
511
                                $identifyMethod = new IdentifyMethod();
2✔
512
                                $return[$row['sign_request_id']][$row['identifier_key']] = $identifyMethod->fromRow($row);
2✔
513
                        }
514
                }
515
                return $return;
2✔
516
        }
517

518
        public function getMyLibresignFile(string $userId, ?array $filter = []): File {
519
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
×
520
                        userId: $userId,
×
521
                        filter: $filter,
×
522
                );
×
523
                $cursor = $qb->executeQuery();
×
524
                $row = $cursor->fetch();
×
525
                if (!$row) {
×
526
                        throw new DoesNotExistException('LibreSign file not found');
×
527
                }
528

529
                $file = new File();
×
530
                return $file->fromRow($row);
×
531
        }
532

533
        private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array $filter = [], bool $count = false): IQueryBuilder {
534
                $qb = $this->db->getQueryBuilder();
1✔
535
                $qb->from('libresign_file', 'f')
1✔
536
                        ->leftJoin('f', 'libresign_sign_request', 'sr', 'sr.file_id = f.id')
1✔
537
                        ->leftJoin('f', 'libresign_identify_method', 'im', $qb->expr()->eq('sr.id', 'im.sign_request_id'))
1✔
538
                        ->leftJoin('f', 'libresign_id_docs', 'id', 'id.file_id = f.id');
1✔
539
                if ($count) {
1✔
540
                        $qb->select($qb->func()->count())
1✔
541
                                ->setFirstResult(0)
1✔
542
                                ->setMaxResults(null);
1✔
543
                } else {
544
                        $qb->select(
1✔
545
                                'f.id',
1✔
546
                                'f.node_id',
1✔
547
                                'f.signed_node_id',
1✔
548
                                'f.user_id',
1✔
549
                                'f.uuid',
1✔
550
                                'f.name',
1✔
551
                                'f.status',
1✔
552
                                'f.metadata',
1✔
553
                                'f.created_at',
1✔
554
                                'f.signature_flow',
1✔
555
                                'f.docmdp_level',
1✔
556
                                'f.node_type',
1✔
557
                                'f.parent_file_id'
1✔
558
                        )
1✔
559
                                ->groupBy(
1✔
560
                                        'f.id',
1✔
561
                                        'f.node_id',
1✔
562
                                        'f.signed_node_id',
1✔
563
                                        'f.user_id',
1✔
564
                                        'f.uuid',
1✔
565
                                        'f.name',
1✔
566
                                        'f.status',
1✔
567
                                        'f.created_at',
1✔
568
                                        'f.signature_flow',
1✔
569
                                        'f.docmdp_level',
1✔
570
                                        'f.node_type',
1✔
571
                                        'f.parent_file_id'
1✔
572
                                );
1✔
573
                        // metadata is a json column, the right way is to use f.metadata::text
574
                        // when the database is PostgreSQL. The problem is that the command
575
                        // addGroupBy add quotes over all text send as argument. With
576
                        // PostgreSQL json columns don't have problem if not added to group by.
577
                        if ($qb->getConnection()->getDatabaseProvider() !== IDBConnection::PLATFORM_POSTGRES) {
1✔
578
                                $qb->addGroupBy('f.metadata');
1✔
579
                        }
580
                        if (isset($filter['length']) && isset($filter['page'])) {
1✔
581
                                $qb->setFirstResult($filter['length'] * ($filter['page'] - 1));
1✔
582
                                $qb->setMaxResults($filter['length']);
1✔
583
                        }
584
                }
585

586
                $or = [
1✔
587
                        $qb->expr()->eq('f.user_id', $qb->createNamedParameter($userId)),
1✔
588
                        $qb->expr()->andX(
1✔
589
                                $qb->expr()->eq('im.identifier_key', $qb->createNamedParameter(IdentifyMethodService::IDENTIFY_ACCOUNT)),
1✔
590
                                $qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($userId)),
1✔
591
                                $qb->expr()->neq('f.status', $qb->createNamedParameter(File::STATUS_DRAFT)),
1✔
592
                                $qb->expr()->neq('sr.status', $qb->createNamedParameter(SignRequestStatus::DRAFT->value)),
1✔
593
                        )
1✔
594
                ];
1✔
595
                $qb->where($qb->expr()->orX(...$or))
1✔
596
                        ->andWhere($qb->expr()->isNull('id.id'));
1✔
597

598
                if ($filter) {
1✔
599
                        if (isset($filter['email']) && filter_var($filter['email'], FILTER_VALIDATE_EMAIL)) {
1✔
600
                                $or[] = $qb->expr()->andX(
×
601
                                        $qb->expr()->eq('im.identifier_key', $qb->createNamedParameter(IdentifyMethodService::IDENTIFY_EMAIL)),
×
602
                                        $qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($filter['email']))
×
603
                                );
×
604
                        }
605
                        if (!empty($filter['signer_uuid']) && !empty($filter['parentFileId'])) {
1✔
606
                                $qb->leftJoin('f', 'libresign_file', 'parent', $qb->expr()->eq('f.parent_file_id', 'parent.id'));
×
607
                                $qb->innerJoin('parent', 'libresign_sign_request', 'psr', $qb->expr()->eq('psr.file_id', 'parent.id'))
×
608
                                        ->andWhere($qb->expr()->eq('psr.uuid', $qb->createNamedParameter($filter['signer_uuid'])));
×
609
                        } elseif (!empty($filter['signer_uuid'])) {
1✔
610
                                $qb->andWhere(
×
611
                                        $qb->expr()->eq('sr.uuid', $qb->createNamedParameter($filter['signer_uuid']))
×
612
                                );
×
613
                        }
614
                        if (!empty($filter['nodeIds'])) {
1✔
615
                                $qb->andWhere(
×
NEW
616
                                        $qb->expr()->orX(
×
NEW
617
                                                $qb->expr()->in('f.node_id', $qb->createNamedParameter($filter['nodeIds'], IQueryBuilder::PARAM_INT_ARRAY)),
×
NEW
618
                                                $qb->expr()->in('f.signed_node_id', $qb->createNamedParameter($filter['nodeIds'], IQueryBuilder::PARAM_INT_ARRAY))
×
NEW
619
                                        )
×
NEW
620
                                );
×
621
                        }
622
                        if (!empty($filter['fileIds'])) {
1✔
NEW
623
                                $qb->andWhere(
×
NEW
624
                                        $qb->expr()->in('f.id', $qb->createNamedParameter($filter['fileIds'], IQueryBuilder::PARAM_INT_ARRAY))
×
625
                                );
×
626
                        }
627
                        if (!empty($filter['status'])) {
1✔
628
                                $qb->andWhere(
×
629
                                        $qb->expr()->in('f.status', $qb->createNamedParameter($filter['status'], IQueryBuilder::PARAM_INT_ARRAY))
×
630
                                );
×
631
                        }
632
                        if (!empty($filter['start'])) {
1✔
633
                                $start = (new \DateTime('@' . $filter['start'], new \DateTimeZone('UTC')))->format('Y-m-d H:i:s');
×
634
                                $qb->andWhere(
×
635
                                        $qb->expr()->gte('f.created_at', $qb->createNamedParameter($start, IQueryBuilder::PARAM_STR))
×
636
                                );
×
637
                        }
638
                        if (!empty($filter['end'])) {
1✔
639
                                $end = (new \DateTime('@' . $filter['end'], new \DateTimeZone('UTC')))->format('Y-m-d H:i:s');
×
640
                                $qb->andWhere(
×
641
                                        $qb->expr()->lte('f.created_at', $qb->createNamedParameter($end, IQueryBuilder::PARAM_STR))
×
642
                                );
×
643
                        }
644
                        if (!empty($filter['parentFileId'])) {
1✔
645
                                $qb->andWhere(
×
646
                                        $qb->expr()->eq('f.parent_file_id', $qb->createNamedParameter($filter['parentFileId'], IQueryBuilder::PARAM_INT))
×
647
                                );
×
648
                        } else {
649
                                $qb->andWhere($qb->expr()->isNull('f.parent_file_id'));
1✔
650
                        }
651
                } else {
652
                        $qb->andWhere($qb->expr()->isNull('f.parent_file_id'));
×
653
                }
654
                return $qb;
1✔
655
        }
656

657
        private function getFilesAssociatedFilesWithMeStmt(
658
                string $userId,
659
                ?array $filter = [],
660
                ?array $sort = [],
661
        ): Pagination {
662
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder($userId, $filter);
1✔
663
                if (!empty($sort['sortBy'])) {
1✔
664
                        switch ($sort['sortBy']) {
×
665
                                case 'name':
×
666
                                case 'status':
×
667
                                        $qb->orderBy(
×
668
                                                $qb->func()->lower('f.' . $sort['sortBy']),
×
669
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
670
                                        );
×
671
                                        break;
×
672
                                case 'created_at':
×
673
                                        $qb->orderBy(
×
674
                                                'f.' . $sort['sortBy'],
×
675
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
676
                                        );
×
677
                        }
678
                }
679

680
                $countQb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
1✔
681
                        userId: $userId,
1✔
682
                        filter: $filter,
1✔
683
                        count: true,
1✔
684
                );
1✔
685

686
                $pagination = new Pagination($qb, $this->urlGenerator, $countQb);
1✔
687
                return $pagination;
1✔
688
        }
689

690
        public function getTextOfSignerStatus(int $status): string {
691
                return SignRequestStatus::from($status)->getLabel($this->l10n);
4✔
692
        }
693
}
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