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

LibreSign / libresign / 20882165631

10 Jan 2026 05:56PM UTC coverage: 44.646%. First build
20882165631

Pull #6433

github

web-flow
Merge eead2e4b3 into ecd36974e
Pull Request #6433: refactor: move all constants to FileStatus enum

31 of 56 new or added lines in 16 files covered. (55.36%)

6742 of 15101 relevant lines covered (44.65%)

5.01 hits per line

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

51.36
/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\AppFramework\Db\QBMapper;
19
use OCP\DB\QueryBuilder\IQueryBuilder;
20
use OCP\IDateTimeFormatter;
21
use OCP\IDBConnection;
22
use OCP\IL10N;
23
use OCP\IURLGenerator;
24
use OCP\IUser;
25
use OCP\IUserManager;
26

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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