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

LibreSign / libresign / 20179022714

12 Dec 2025 08:27PM UTC coverage: 43.981%. First build
20179022714

Pull #6169

github

web-flow
Merge e97580bc2 into c05b15a8e
Pull Request #6169: feat: file level signature flow

36 of 42 new or added lines in 8 files covered. (85.71%)

5816 of 13224 relevant lines covered (43.98%)

5.13 hits per line

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

47.91
/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
                        $metadata['notify'][] = [
13✔
64
                                'method' => $method,
13✔
65
                                'date' => time(),
13✔
66
                        ];
13✔
67
                        $fromDatabase->setMetadata($metadata);
13✔
68
                        $this->update($fromDatabase);
13✔
69
                        $this->db->commit();
13✔
70
                } catch (\Throwable) {
×
71
                        $this->db->rollBack();
×
72
                }
73
                return $this->firstNotification;
13✔
74
        }
75

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

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

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

113
        public function getByEmailAndFileId(string $email, int $fileId): SignRequest {
114
                $qb = $this->db->getQueryBuilder();
×
115

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

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

140
        /**
141
         * Get all signers by fileId
142
         *
143
         * @return SignRequest[]
144
         */
145
        public function getByFileId(int $fileId): array {
146
                $qb = $this->db->getQueryBuilder();
14✔
147

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

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

172
                $qb->select('*')
14✔
173
                        ->from($this->getTableName())
14✔
174
                        ->where(
14✔
175
                                $qb->expr()->eq('id', $qb->createNamedParameter($signRequestId, IQueryBuilder::PARAM_INT))
14✔
176
                        );
14✔
177

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

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

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

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

251
        /**
252
         * Get all signers by multiple fileId
253
         *
254
         * @return SignRequest[]
255
         */
256
        public function getByMultipleFileId(array $fileId) {
257
                $qb = $this->db->getQueryBuilder();
6✔
258

259
                $qb->select('*')
6✔
260
                        ->from($this->getTableName(), 'sr')
6✔
261
                        ->where(
6✔
262
                                $qb->expr()->in('sr.file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY))
6✔
263
                        )
6✔
264
                        ->orderBy('sr.id', 'ASC');
6✔
265

266
                /** @var SignRequest[] */
267
                return $this->findEntities($qb);
6✔
268
        }
269

270
        /**
271
         * Get all signers by fileId
272
         *
273
         * @return SignRequest[]
274
         */
275
        public function getByNodeId(int $nodeId) {
276
                $qb = $this->db->getQueryBuilder();
3✔
277

278
                $qb->select('sr.*')
3✔
279
                        ->from($this->getTableName(), 'sr')
3✔
280
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
3✔
281
                        ->where(
3✔
282
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT))
3✔
283
                        );
3✔
284

285
                /** @var SignRequest[] */
286
                $signers = $this->findEntities($qb);
3✔
287
                return $signers;
3✔
288
        }
289

290
        /**
291
         * Get all signers by File Uuid
292
         *
293
         * @param string $nodeId
294
         * @return SignRequest[]
295
         */
296
        public function getByFileUuid(string $uuid) {
297
                $qb = $this->db->getQueryBuilder();
1✔
298

299
                $qb->select('sr.*')
1✔
300
                        ->from($this->getTableName(), 'sr')
1✔
301
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
1✔
302
                        ->where(
1✔
303
                                $qb->expr()->eq('f.uuid', $qb->createNamedParameter($uuid))
1✔
304
                        );
1✔
305

306
                /** @var SignRequest[] */
307
                $signers = $this->findEntities($qb);
1✔
308
                foreach ($signers as $signRequest) {
1✔
309
                        if (!isset($this->signers[$signRequest->getId()])) {
1✔
310
                                $this->signers[$signRequest->getId()] = $signRequest;
×
311
                        }
312
                }
313
                return $signers;
1✔
314
        }
315

316
        public function getBySignerUuidAndUserId(string $uuid): SignRequest {
317
                $qb = $this->db->getQueryBuilder();
×
318

319
                $qb->select('sr.*')
×
320
                        ->from($this->getTableName(), 'sr')
×
321
                        ->where(
×
322
                                $qb->expr()->eq('sr.uuid', $qb->createNamedParameter($uuid))
×
323
                        );
×
324

325
                /** @var SignRequest */
326
                $signRequest = $this->findEntity($qb);
×
327
                if (!isset($this->signers[$signRequest->getId()])) {
×
328
                        $this->signers[$signRequest->getId()] = $signRequest;
×
329
                }
330
                return $signRequest;
×
331
        }
332

333
        public function getByFileIdAndUserId(int $file_id): SignRequest {
334
                $qb = $this->db->getQueryBuilder();
×
335

336
                $qb->select('sr.*')
×
337
                        ->from($this->getTableName(), 'sr')
×
338
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
339
                        ->where(
×
340
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT))
×
341
                        );
×
342

343
                /** @var SignRequest */
344
                return $this->findEntity($qb);
×
345
        }
346

347
        public function getByFileIdAndEmail(int $file_id, string $email): SignRequest {
348
                $qb = $this->db->getQueryBuilder();
×
349

350
                $qb->select('sr.*')
×
351
                        ->from($this->getTableName(), 'sr')
×
352
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
353
                        ->where(
×
354
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($file_id, IQueryBuilder::PARAM_INT))
×
355
                        )
×
356
                        ->andWhere(
×
357
                                $qb->expr()->eq('sr.email', $qb->createNamedParameter($email))
×
358
                        );
×
359

360
                /** @var SignRequest */
361
                return $this->findEntity($qb);
×
362
        }
363

364
        public function getByFileIdAndSignRequestId(int $fileId, int $signRequestId): SignRequest {
365
                if (isset($this->signers[$signRequestId])) {
2✔
366
                        return $this->signers[$signRequestId];
2✔
367
                }
368
                $qb = $this->db->getQueryBuilder();
×
369

370
                $qb->select('sr.*')
×
371
                        ->from($this->getTableName(), 'sr')
×
372
                        ->join('sr', 'libresign_file', 'f', 'sr.file_id = f.id')
×
373
                        ->where(
×
374
                                $qb->expr()->eq('f.node_id', $qb->createNamedParameter($fileId))
×
375
                        )
×
376
                        ->andWhere(
×
377
                                $qb->expr()->eq('sr.id', $qb->createNamedParameter($signRequestId))
×
378
                        );
×
379

380
                $signRequest = $this->findEntity($qb);
×
381
                if (!isset($this->signers[$signRequest->getId()])) {
×
382
                        $this->signers[$signRequest->getId()] = $signRequest;
×
383
                }
384
                /** @var SignRequest */
385
                return end($this->signers);
×
386
        }
387

388
        public function getFilesAssociatedFilesWithMeFormatted(
389
                IUser $user,
390
                array $filter,
391
                ?int $page = null,
392
                ?int $length = null,
393
                ?array $sort = [],
394
        ): array {
395
                $filter['email'] = $user->getEMailAddress();
1✔
396
                $filter['length'] = $length;
1✔
397
                $filter['page'] = $page;
1✔
398
                $pagination = $this->getFilesAssociatedFilesWithMeStmt($user->getUID(), $filter, $sort);
1✔
399
                $pagination->setMaxPerPage($length);
1✔
400
                $pagination->setCurrentPage($page);
1✔
401
                $currentPageResults = $pagination->getCurrentPageResults();
1✔
402

403
                $data = [];
1✔
404
                foreach ($currentPageResults as $row) {
1✔
405
                        $data[] = $this->formatListRow($row);
×
406
                }
407
                $return['data'] = $data;
1✔
408
                $return['pagination'] = $pagination;
1✔
409
                return $return;
1✔
410
        }
411

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

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

457
                $return = [];
×
458
                foreach (array_chunk($signRequestIds, 1000) as $signRequestIdsChunk) {
×
459
                        $qb->setParameter('signRequestIds', $signRequestIdsChunk, IQueryBuilder::PARAM_INT_ARRAY);
×
460
                        $cursor = $qb->executeQuery();
×
461
                        while ($row = $cursor->fetch()) {
×
462
                                $identifyMethod = new IdentifyMethod();
×
463
                                $return[$row['sign_request_id']][$row['identifier_key']] = $identifyMethod->fromRow($row);
×
464
                        }
465
                }
466
                return $return;
×
467
        }
468

469
        public function getMyLibresignFile(string $userId, ?array $filter = []): File {
470
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
×
471
                        userId: $userId,
×
472
                        filter: $filter,
×
473
                );
×
474
                $cursor = $qb->executeQuery();
×
475
                $row = $cursor->fetch();
×
476
                if (!$row) {
×
477
                        throw new DoesNotExistException('LibreSign file not found');
×
478
                }
479
                $file = new File();
×
480
                return $file->fromRow($row);
×
481
        }
482

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

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

578
        private function getFilesAssociatedFilesWithMeStmt(
579
                string $userId,
580
                ?array $filter = [],
581
                ?array $sort = [],
582
        ): Pagination {
583
                $qb = $this->getFilesAssociatedFilesWithMeQueryBuilder($userId, $filter);
1✔
584
                if (!empty($sort['sortBy'])) {
1✔
585
                        switch ($sort['sortBy']) {
×
586
                                case 'name':
×
587
                                case 'status':
×
588
                                        $qb->orderBy(
×
589
                                                $qb->func()->lower('f.' . $sort['sortBy']),
×
590
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
591
                                        );
×
592
                                        break;
×
593
                                case 'created_at':
×
594
                                        $qb->orderBy(
×
595
                                                'f.' . $sort['sortBy'],
×
596
                                                $sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
×
597
                                        );
×
598
                        }
599
                }
600

601
                $countQb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
1✔
602
                        userId: $userId,
1✔
603
                        filter: $filter,
1✔
604
                        count: true,
1✔
605
                );
1✔
606

607
                $pagination = new Pagination($qb, $this->urlGenerator, $countQb);
1✔
608
                return $pagination;
1✔
609
        }
610

611
        private function formatListRow(array $row): array {
612
                $row['id'] = (int)$row['id'];
×
613
                $row['status'] = (int)$row['status'];
×
614
                $row['statusText'] = $this->fileMapper->getTextOfStatus($row['status']);
×
615
                $row['nodeId'] = (int)$row['node_id'];
×
616
                $row['signedNodeId'] = (int)$row['signed_node_id'];
×
617
                $row['requested_by'] = [
×
618
                        'userId' => $row['user_id'],
×
619
                        'displayName' => $this->userManager->get($row['user_id'])?->getDisplayName(),
×
620
                ];
×
621
                $row['created_at'] = (new \DateTime($row['created_at']))->setTimezone(new \DateTimeZone('UTC'))->format(DateTimeInterface::ATOM);
×
622
                $row['file'] = $this->urlGenerator->linkToRoute('libresign.page.getPdf', ['uuid' => $row['uuid']]);
×
623
                $row['nodeId'] = (int)$row['node_id'];
×
624

625
                $row['name'] = $this->removeExtensionFromName($row['name'], $row['metadata']);
×
NEW
626
                $row['signatureFlow'] = SignatureFlow::fromNumeric((int)($row['signature_flow']))->value;
×
627

628
                unset(
×
629
                        $row['user_id'],
×
630
                        $row['node_id'],
×
631
                        $row['signed_node_id'],
×
NEW
632
                        $row['signature_flow'],
×
633
                );
×
634
                return $row;
×
635
        }
636

637
        private function removeExtensionFromName(string $name, ?string $metadataJson): string {
638
                if (empty($name) || empty($metadataJson)) {
×
639
                        return $name;
×
640
                }
641

642
                $metadata = json_decode($metadataJson, true);
×
643
                if (!isset($metadata['extension'])) {
×
644
                        return $name;
×
645
                }
646

647
                $extensionPattern = '/\.' . preg_quote($metadata['extension'], '/') . '$/i';
×
648
                $result = preg_replace($extensionPattern, '', $name);
×
649
                return $result ?? $name;
×
650
        }
651

652
        public function getTextOfSignerStatus(int $status): string {
653
                return SignRequestStatus::from($status)->getLabel($this->l10n);
5✔
654
        }
655
}
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