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

LibreSign / libresign / 20286980878

17 Dec 2025 12:11AM UTC coverage: 43.712%. First build
20286980878

Pull #6216

github

web-flow
Merge 0848b2fef into f68ceae6c
Pull Request #6216: feat: custom message for signers

5 of 68 new or added lines in 7 files covered. (7.35%)

5926 of 13557 relevant lines covered (43.71%)

5.16 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

193
        /**
194
         * @return \Generator<IdentifyMethod>
195
         */
196
        public function findRemindersCandidates(): \Generator {
197
                $qb = $this->db->getQueryBuilder();
×
198
                $qb->select(
×
199
                        'sr.id AS sr_id',
×
200
                        'sr.file_id AS sr_file_id',
×
201
                        'sr.uuid AS sr_uuid',
×
202
                        'sr.display_name AS sr_display_name',
×
203
                        'sr.description AS sr_description',
×
204
                        'sr.metadata AS sr_metadata',
×
205
                        'sr.signed_hash AS sr_signed_hash',
×
206
                        'sr.created_at AS sr_created_at',
×
207
                        'sr.signed AS sr_signed',
×
208

209
                        'im.id AS im_id',
×
210
                        'im.mandatory AS im_mandatory',
×
211
                        'im.code AS im_code',
×
212
                        'im.identifier_key AS im_identifier_key',
×
213
                        'im.identifier_value AS im_identifier_value',
×
214
                        'im.attempts AS im_attempts',
×
215
                        'im.identified_at_date AS im_identified_at_date',
×
216
                        'im.last_attempt_date AS im_last_attempt_date',
×
217
                        'im.sign_request_id AS im_sign_request_id',
×
218
                        'im.metadata AS im_metadata',
×
219
                )
×
220
                        ->from('libresign_sign_request', 'sr')
×
221
                        ->join('sr', 'libresign_identify_method', 'im', 'sr.id = im.sign_request_id')
×
222
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
223
                        ->where($qb->expr()->isNull('sr.signed'))
×
224
                        ->andWhere($qb->expr()->neq('im.identifier_value', $qb->createNamedParameter('deleted_users')))
×
225
                        ->andWhere($qb->expr()->in('f.status', $qb->createNamedParameter([
×
226
                                File::STATUS_ABLE_TO_SIGN,
×
227
                                File::STATUS_PARTIAL_SIGNED
×
228
                        ], IQueryBuilder::PARAM_INT_ARRAY)))
×
229
                        ->setParameter('st', [1,2], IQueryBuilder::PARAM_INT_ARRAY)
×
230
                        ->orderBy('sr.id', 'ASC');
×
231

232
                $result = $qb->executeQuery();
×
233
                try {
234
                        /** @var array<string, mixed> $row */
235
                        while ($row = $result->fetch()) {
×
236
                                $signRequest = new SignRequest();
×
237
                                $identifyMethod = new IdentifyMethod();
×
238
                                foreach ($row as $key => $value) {
×
239
                                        $prop = $identifyMethod->columnToProperty(substr($key, 3));
×
240
                                        if (str_starts_with($key, 'sr_')) {
×
241
                                                $signRequest->{'set' . lcfirst($prop)}($value);
×
242
                                        } else {
243
                                                $identifyMethod->{'set' . lcfirst($prop)}($value);
×
244
                                        }
245
                                }
246
                                $signRequest->resetUpdatedFields();
×
247
                                $identifyMethod->resetUpdatedFields();
×
248
                                if (!isset($this->signers[$signRequest->getId()])) {
×
249
                                        $this->signers[$signRequest->getId()] = $signRequest;
×
250
                                }
251
                                yield $identifyMethod;
×
252
                        }
253
                } finally {
254
                        $result->closeCursor();
×
255
                }
256
        }
257

258
        /**
259
         * Get all signers by multiple fileId
260
         *
261
         * @return SignRequest[]
262
         */
263
        public function getByMultipleFileId(array $fileId) {
264
                $qb = $this->db->getQueryBuilder();
6✔
265

266
                $qb->select('*')
6✔
267
                        ->from($this->getTableName(), 'sr')
6✔
268
                        ->where(
6✔
269
                                $qb->expr()->in('sr.file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY))
6✔
270
                        )
6✔
271
                        ->orderBy('sr.id', 'ASC');
6✔
272

273
                /** @var SignRequest[] */
274
                return $this->findEntities($qb);
6✔
275
        }
276

277
        /**
278
         * Get all signers by fileId
279
         *
280
         * @return SignRequest[]
281
         */
282
        public function getByNodeId(int $nodeId) {
283
                $qb = $this->db->getQueryBuilder();
3✔
284

285
                $qb->select('sr.*')
3✔
286
                        ->from($this->getTableName(), 'sr')
3✔
287
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
3✔
288
                        ->where(
3✔
289
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT))
3✔
290
                        );
3✔
291

292
                /** @var SignRequest[] */
293
                $signers = $this->findEntities($qb);
3✔
294
                return $signers;
3✔
295
        }
296

297
        /**
298
         * Get all signers by File Uuid
299
         *
300
         * @param string $nodeId
301
         * @return SignRequest[]
302
         */
303
        public function getByFileUuid(string $uuid) {
304
                $qb = $this->db->getQueryBuilder();
1✔
305

306
                $qb->select('sr.*')
1✔
307
                        ->from($this->getTableName(), 'sr')
1✔
308
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
1✔
309
                        ->where(
1✔
310
                                $qb->expr()->eq('f.uuid', $qb->createNamedParameter($uuid))
1✔
311
                        );
1✔
312

313
                /** @var SignRequest[] */
314
                $signers = $this->findEntities($qb);
1✔
315
                foreach ($signers as $signRequest) {
1✔
316
                        if (!isset($this->signers[$signRequest->getId()])) {
1✔
317
                                $this->signers[$signRequest->getId()] = $signRequest;
×
318
                        }
319
                }
320
                return $signers;
1✔
321
        }
322

323
        public function getBySignerUuidAndUserId(string $uuid): SignRequest {
324
                $qb = $this->db->getQueryBuilder();
×
325

326
                $qb->select('sr.*')
×
327
                        ->from($this->getTableName(), 'sr')
×
328
                        ->where(
×
329
                                $qb->expr()->eq('sr.uuid', $qb->createNamedParameter($uuid))
×
330
                        );
×
331

332
                /** @var SignRequest */
333
                $signRequest = $this->findEntity($qb);
×
334
                if (!isset($this->signers[$signRequest->getId()])) {
×
335
                        $this->signers[$signRequest->getId()] = $signRequest;
×
336
                }
337
                return $signRequest;
×
338
        }
339

340
        public function getByFileIdAndUserId(int $file_id): SignRequest {
341
                $qb = $this->db->getQueryBuilder();
×
342

343
                $qb->select('sr.*')
×
344
                        ->from($this->getTableName(), 'sr')
×
345
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
346
                        ->where(
×
347
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT))
×
348
                        );
×
349

350
                /** @var SignRequest */
351
                return $this->findEntity($qb);
×
352
        }
353

354
        public function getByFileIdAndEmail(int $file_id, string $email): SignRequest {
355
                $qb = $this->db->getQueryBuilder();
×
356

357
                $qb->select('sr.*')
×
358
                        ->from($this->getTableName(), 'sr')
×
359
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
360
                        ->where(
×
361
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT))
×
362
                        )
×
363
                        ->andWhere(
×
364
                                $qb->expr()->eq('sr.email', $qb->createNamedParameter($email))
×
365
                        );
×
366

367
                /** @var SignRequest */
368
                return $this->findEntity($qb);
×
369
        }
370

371
        public function getByFileIdAndSignRequestId(int $fileId, int $signRequestId): SignRequest {
372
                if (isset($this->signers[$signRequestId])) {
2✔
373
                        return $this->signers[$signRequestId];
2✔
374
                }
375
                $qb = $this->db->getQueryBuilder();
×
376

377
                $qb->select('sr.*')
×
378
                        ->from($this->getTableName(), 'sr')
×
379
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
380
                        ->where(
×
381
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($fileId))
×
382
                        )
×
383
                        ->andWhere(
×
384
                                $qb->expr()->eq('sr.id', $qb->createNamedParameter($signRequestId))
×
385
                        );
×
386

387
                $signRequest = $this->findEntity($qb);
×
388
                if (!isset($this->signers[$signRequest->getId()])) {
×
389
                        $this->signers[$signRequest->getId()] = $signRequest;
×
390
                }
391
                /** @var SignRequest */
392
                return end($this->signers);
×
393
        }
394

395
        public function getFilesAssociatedFilesWithMeFormatted(
396
                IUser $user,
397
                array $filter,
398
                ?int $page = null,
399
                ?int $length = null,
400
                ?array $sort = [],
401
        ): array {
402
                $filter['email'] = $user->getEMailAddress();
1✔
403
                $filter['length'] = $length;
1✔
404
                $filter['page'] = $page;
1✔
405
                $pagination = $this->getFilesAssociatedFilesWithMeStmt($user->getUID(), $filter, $sort);
1✔
406
                $pagination->setMaxPerPage($length);
1✔
407
                $pagination->setCurrentPage($page);
1✔
408
                $currentPageResults = $pagination->getCurrentPageResults();
1✔
409

410
                $data = [];
1✔
411
                foreach ($currentPageResults as $row) {
1✔
412
                        $data[] = $this->formatListRow($row);
×
413
                }
414
                $return['data'] = $data;
1✔
415
                $return['pagination'] = $pagination;
1✔
416
                return $return;
1✔
417
        }
418

419
        /**
420
         * @param array<SignRequest> $signRequests
421
         * @return FileElement[][]
422
         */
423
        public function getVisibleElementsFromSigners(array $signRequests): array {
424
                $signRequestIds = array_map(fn (SignRequest $signRequest): int => $signRequest->getId(), $signRequests);
5✔
425
                if (!$signRequestIds) {
5✔
426
                        return [];
1✔
427
                }
428
                $qb = $this->db->getQueryBuilder();
4✔
429
                $qb->select('fe.*')
4✔
430
                        ->from('libresign_file_element', 'fe')
4✔
431
                        ->where(
4✔
432
                                $qb->expr()->in('fe.sign_request_id', $qb->createParameter('signRequestIds'))
4✔
433
                        );
4✔
434
                $return = [];
4✔
435
                foreach (array_chunk($signRequestIds, 1000) as $signRequestIdsChunk) {
4✔
436
                        $qb->setParameter('signRequestIds', $signRequestIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
4✔
437
                        $cursor = $qb->executeQuery();
4✔
438
                        while ($row = $cursor->fetch()) {
4✔
439
                                $fileElement = new FileElement();
×
440
                                $return[$row['sign_request_id']][] = $fileElement->fromRow($row);
×
441
                        }
442
                }
443
                return $return;
4✔
444
        }
445

446
        /**
447
         * @param array<SignRequest> $signRequests
448
         * @return array<array-key, array<array-key, \OCP\AppFramework\Db\Entity&\OCA\Libresign\Db\IdentifyMethod>>
449
         */
450
        public function getIdentifyMethodsFromSigners(array $signRequests): array {
451
                $signRequestIds = array_map(fn (SignRequest $signRequest): int => $signRequest->getId(), $signRequests);
1✔
452
                if (!$signRequestIds) {
1✔
453
                        return [];
1✔
454
                }
455
                $qb = $this->db->getQueryBuilder();
×
456
                $qb->select('im.*')
×
457
                        ->from('libresign_identify_method', 'im')
×
458
                        ->where(
×
459
                                $qb->expr()->in('im.sign_request_id', $qb->createParameter('signRequestIds'))
×
460
                        )
×
461
                        ->orderBy('im.mandatory', 'DESC')
×
462
                        ->addOrderBy('im.identified_at_date', 'ASC');
×
463

464
                $return = [];
×
465
                foreach (array_chunk($signRequestIds, 1000) as $signRequestIdsChunk) {
×
466
                        $qb->setParameter('signRequestIds', $signRequestIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
×
467
                        $cursor = $qb->executeQuery();
×
468
                        while ($row = $cursor->fetch()) {
×
469
                                $identifyMethod = new IdentifyMethod();
×
470
                                $return[$row['sign_request_id']][$row['identifier_key']] = $identifyMethod->fromRow($row);
×
471
                        }
472
                }
473
                return $return;
×
474
        }
475

476
        public function getMyLibresignFile(string $userId, ?array $filter = []): File {
477
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
×
478
                        userId: $userId,
×
479
                        filter: $filter,
×
480
                );
×
481
                $cursor = $qb->executeQuery();
×
482
                $row = $cursor->fetch();
×
483
                if (!$row) {
×
484
                        throw new DoesNotExistException('LibreSign file not found');
×
485
                }
486
                $file = new File();
×
487
                return $file->fromRow($row);
×
488
        }
489

490
        private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array $filter = [], bool $count = false): IQueryBuilder {
491
                $qb = $this->db->getQueryBuilder();
1✔
492
                $qb->from('libresign_file', 'f')
1✔
493
                        ->leftJoin('f', 'libresign_sign_request', 'sr', 'sr.file_id = f.id')
1✔
494
                        ->leftJoin('f', 'libresign_identify_method', 'im', $qb->expr()->eq('sr.id', 'im.sign_request_id'))
1✔
495
                        ->leftJoin('f', 'libresign_id_docs', 'id', 'id.file_id = f.id');
1✔
496
                if ($count) {
1✔
497
                        $qb->select($qb->func()->count())
1✔
498
                                ->setFirstResult(0)
1✔
499
                                ->setMaxResults(null);
1✔
500
                } else {
501
                        $qb->select(
1✔
502
                                'f.id',
1✔
503
                                'f.node_id',
1✔
504
                                'f.signed_node_id',
1✔
505
                                'f.user_id',
1✔
506
                                'f.uuid',
1✔
507
                                'f.name',
1✔
508
                                'f.status',
1✔
509
                                'f.metadata',
1✔
510
                                'f.created_at',
1✔
511
                                'f.signature_flow',
1✔
512
                                'f.docmdp_level',
1✔
513
                        )
1✔
514
                                ->groupBy(
1✔
515
                                        'f.id',
1✔
516
                                        'f.node_id',
1✔
517
                                        'f.signed_node_id',
1✔
518
                                        'f.user_id',
1✔
519
                                        'f.uuid',
1✔
520
                                        'f.name',
1✔
521
                                        'f.status',
1✔
522
                                        'f.created_at',
1✔
523
                                        'f.signature_flow',
1✔
524
                                        'f.docmdp_level',
1✔
525
                                );
1✔
526
                        // metadata is a json column, the right way is to use f.metadata::text
527
                        // when the database is PostgreSQL. The problem is that the command
528
                        // addGroupBy add quotes over all text send as argument. With
529
                        // PostgreSQL json columns don't have problem if not added to group by.
530
                        if ($qb->getConnection()->getDatabaseProvider() !== IDBConnection::PLATFORM_POSTGRES) {
1✔
531
                                $qb->addGroupBy('f.metadata');
1✔
532
                        }
533
                        if (isset($filter['length']) && isset($filter['page'])) {
1✔
534
                                $qb->setFirstResult($filter['length'] * ($filter['page'] - 1));
1✔
535
                                $qb->setMaxResults($filter['length']);
1✔
536
                        }
537
                }
538

539
                $or = [
1✔
540
                        $qb->expr()->eq('f.user_id', $qb->createNamedParameter($userId)),
1✔
541
                        $qb->expr()->andX(
1✔
542
                                $qb->expr()->eq('im.identifier_key', $qb->createNamedParameter(IdentifyMethodService::IDENTIFY_ACCOUNT)),
1✔
543
                                $qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($userId)),
1✔
544
                                $qb->expr()->neq('f.status', $qb->createNamedParameter(File::STATUS_DRAFT)),
1✔
545
                                $qb->expr()->neq('sr.status', $qb->createNamedParameter(SignRequestStatus::DRAFT->value)),
1✔
546
                        )
1✔
547
                ];
1✔
548
                $qb->where($qb->expr()->orX(...$or))->andWhere($qb->expr()->isNull('id.id'));
1✔
549
                if ($filter) {
1✔
550
                        if (isset($filter['email']) && filter_var($filter['email'], FILTER_VALIDATE_EMAIL)) {
1✔
551
                                $or[] = $qb->expr()->andX(
×
552
                                        $qb->expr()->eq('im.identifier_key', $qb->createNamedParameter(IdentifyMethodService::IDENTIFY_EMAIL)),
×
553
                                        $qb->expr()->eq('im.identifier_value', $qb->createNamedParameter($filter['email']))
×
554
                                );
×
555
                        }
556
                        if (!empty($filter['signer_uuid'])) {
1✔
557
                                $qb->andWhere(
×
558
                                        $qb->expr()->eq('sr.uuid', $qb->createNamedParameter($filter['signer_uuid']))
×
559
                                );
×
560
                        }
561
                        if (!empty($filter['nodeIds'])) {
1✔
562
                                $qb->andWhere(
×
563
                                        $qb->expr()->in('f.node_id', $qb->createNamedParameter($filter['nodeIds'], IQueryBuilder::PARAM_STR_ARRAY))
×
564
                                );
×
565
                        }
566
                        if (!empty($filter['status'])) {
1✔
567
                                $qb->andWhere(
×
568
                                        $qb->expr()->in('f.status', $qb->createNamedParameter($filter['status'], IQueryBuilder::PARAM_INT_ARRAY))
×
569
                                );
×
570
                        }
571
                        if (!empty($filter['start'])) {
1✔
572
                                $start = (new \DateTime('@' . $filter['start'], new \DateTimeZone('UTC')))->format('Y-m-d H:i:s');
×
573
                                $qb->andWhere(
×
574
                                        $qb->expr()->gte('f.created_at', $qb->createNamedParameter($start, IQueryBuilder::PARAM_STR))
×
575
                                );
×
576
                        }
577
                        if (!empty($filter['end'])) {
1✔
578
                                $end = (new \DateTime('@' . $filter['end'], new \DateTimeZone('UTC')))->format('Y-m-d H:i:s');
×
579
                                $qb->andWhere(
×
580
                                        $qb->expr()->lte('f.created_at', $qb->createNamedParameter($end, IQueryBuilder::PARAM_STR))
×
581
                                );
×
582
                        }
583
                }
584
                return $qb;
1✔
585
        }
586

587
        private function getFilesAssociatedFilesWithMeStmt(
588
                string $userId,
589
                ?array $filter = [],
590
                ?array $sort = [],
591
        ): Pagination {
592
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder($userId, $filter);
1✔
593
                if (!empty($sort['sortBy'])) {
1✔
594
                        switch ($sort['sortBy']) {
×
595
                                case 'name':
×
596
                                case 'status':
×
597
                                        $qb->orderBy(
×
598
                                                $qb->func()->lower('f.' . $sort['sortBy']),
×
599
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
600
                                        );
×
601
                                        break;
×
602
                                case 'created_at':
×
603
                                        $qb->orderBy(
×
604
                                                'f.' . $sort['sortBy'],
×
605
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
606
                                        );
×
607
                        }
608
                }
609

610
                $countQb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
1✔
611
                        userId: $userId,
1✔
612
                        filter: $filter,
1✔
613
                        count: true,
1✔
614
                );
1✔
615

616
                $pagination = new Pagination($qb, $this->urlGenerator, $countQb);
1✔
617
                return $pagination;
1✔
618
        }
619

620
        private function formatListRow(array $row): array {
621
                $row['id'] = (int)$row['id'];
×
622
                $row['status'] = (int)$row['status'];
×
623
                $row['statusText'] = $this->fileMapper->getTextOfStatus($row['status']);
×
624
                $row['nodeId'] = (int)$row['node_id'];
×
625
                $row['signedNodeId'] = (int)$row['signed_node_id'];
×
626
                $row['requested_by'] = [
×
627
                        'userId' => $row['user_id'],
×
628
                        'displayName' => $this->userManager->get($row['user_id'])?->getDisplayName(),
×
629
                ];
×
630
                $row['created_at'] = (new \DateTime($row['created_at']))->setTimezone(new \DateTimeZone('UTC'))->format(DateTimeInterface::ATOM);
×
631
                $row['file'] = $this->urlGenerator->linkToRoute('libresign.page.getPdf', ['uuid' => $row['uuid']]);
×
632
                $row['nodeId'] = (int)$row['node_id'];
×
633

634
                $row['name'] = $this->removeExtensionFromName($row['name'], $row['metadata']);
×
635
                $row['signatureFlow'] = SignatureFlow::fromNumeric((int)($row['signature_flow']))->value;
×
636
                $row['docmdpLevel'] = (int)($row['docmdp_level'] ?? 0);
×
637

638
                unset(
×
639
                        $row['user_id'],
×
640
                        $row['node_id'],
×
641
                        $row['signed_node_id'],
×
642
                        $row['signature_flow'],
×
643
                        $row['docmdp_level'],
×
644
                );
×
645
                return $row;
×
646
        }
647

648
        private function removeExtensionFromName(string $name, ?string $metadataJson): string {
649
                if (empty($name) || empty($metadataJson)) {
×
650
                        return $name;
×
651
                }
652

653
                $metadata = json_decode($metadataJson, true);
×
654
                if (!isset($metadata['extension'])) {
×
655
                        return $name;
×
656
                }
657

658
                $extensionPattern = '/\.' . preg_quote($metadata['extension'], '/') . '$/i';
×
659
                $result = preg_replace($extensionPattern, '', $name);
×
660
                return $result ?? $name;
×
661
        }
662

663
        public function getTextOfSignerStatus(int $status): string {
664
                return SignRequestStatus::from($status)->getLabel($this->l10n);
5✔
665
        }
666
}
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