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

LibreSign / libresign / 19868313373

02 Dec 2025 05:50PM UTC coverage: 41.522%. First build
19868313373

Pull #4464

github

web-flow
Merge f1243a78b into 198609762
Pull Request #4464: refactor: attach document

89 of 246 new or added lines in 14 files covered. (36.18%)

5103 of 12290 relevant lines covered (41.52%)

4.18 hits per line

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

40.74
/lib/Db/IdDocsMapper.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\Helper\Pagination;
12
use OCP\AppFramework\Db\QBMapper;
13
use OCP\DB\QueryBuilder\IQueryBuilder;
14
use OCP\DB\Types;
15
use OCP\IDBConnection;
16
use OCP\IURLGenerator;
17

18
/**
19
 * Class FileMapper
20
 *
21
 * @package OCA\Libresign\DB
22
 * @template-extends QBMapper<IdDocs>
23
 */
24
class IdDocsMapper extends QBMapper {
25
        public function __construct(
26
                IDBConnection $db,
27
                private IURLGenerator $urlGenerator,
28
                private FileMapper $fileMapper,
29
                private SignRequestMapper $signRequestMapper,
30
                private FileTypeMapper $fileTypeMapper,
31
        ) {
32
                parent::__construct($db, 'libresign_id_docs');
47✔
33
        }
34

35
        public function save(int $fileId, ?int $signRequestId, ?string $userId, string $fileType): IdDocs {
36
                $idDocs = new IdDocs();
1✔
37
                $idDocs->setFileId($fileId);
1✔
38
                $idDocs->setSignRequestId($signRequestId);
1✔
39
                $idDocs->setUserId($userId);
1✔
40
                $idDocs->setFileType($fileType);
1✔
41
                return $this->insert($idDocs);
1✔
42
        }
43

44
        public function getByUserIdAndFileId(string $userId, int $fileId): IdDocs {
NEW
45
                $qb = $this->db->getQueryBuilder();
×
46
                $qb->select('*')
×
47
                        ->from($this->getTableName())
×
NEW
48
                        ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
×
NEW
49
                        ->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
×
50
                return $this->findEntity($qb);
×
51
        }
52

53
        /**
54
         * @return IdDocs[]
55
         */
56
        public function getByUserId(string $userId): array {
57
                $qb = $this->db->getQueryBuilder();
×
NEW
58
                $qb->select('*')
×
NEW
59
                        ->from($this->getTableName())
×
NEW
60
                        ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
×
NEW
61
                return $this->findEntities($qb);
×
62
        }
63

64
        public function getByUserIdAndNodeId(string $userId, int $nodeId): IdDocs {
NEW
65
                $qb = $this->db->getQueryBuilder();
×
NEW
66
                $qb->select('id.*')
×
NEW
67
                        ->from($this->getTableName(), 'id')
×
NEW
68
                        ->join('id', 'libresign_file', 'f', 'f.id = id.file_id')
×
NEW
69
                        ->where($qb->expr()->eq('id.user_id', $qb->createNamedParameter($userId)))
×
NEW
70
                        ->andWhere($qb->expr()->eq('f.node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT)));
×
NEW
71
                return $this->findEntity($qb);
×
72
        }
73

74
        public function getByFileId(int $fileId): IdDocs {
NEW
75
                $qb = $this->db->getQueryBuilder();
×
NEW
76
                $qb->select('*')
×
NEW
77
                        ->from($this->getTableName())
×
NEW
78
                        ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
×
79
                return $this->findEntity($qb);
×
80
        }
81

82
        public function deleteByFileId(int $fileId): void {
83
                $qb = $this->db->getQueryBuilder();
×
NEW
84
                $qb->delete($this->getTableName())
×
NEW
85
                        ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
×
NEW
86
                $qb->executeStatement();
×
87
        }
88

89
        public function getByUserAndType(string $userId, string $fileType): ?IdDocs {
90
                $qb = $this->db->getQueryBuilder();
1✔
91
                $qb->select('*')
1✔
92
                        ->from($this->getTableName())
1✔
93
                        ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
1✔
94
                        ->andWhere($qb->expr()->eq('file_type', $qb->createNamedParameter($fileType)));
1✔
95
                try {
96
                        return $this->findEntity($qb);
1✔
97
                } catch (\Throwable) {
1✔
98
                        return null;
1✔
99
                }
100
        }
101

102
        public function list(array $filter, ?int $page = null, ?int $length = null): array {
103
                $filter['length'] = $length;
1✔
104
                $filter['page'] = $page;
1✔
105
                $pagination = $this->getDocs($filter);
1✔
106
                $pagination->setMaxPerPage($length);
1✔
107
                $pagination->setCurrentPage($page);
1✔
108
                $currentPageResults = $pagination->getCurrentPageResults();
1✔
109

110
                $url = $this->urlGenerator->linkToRoute('libresign.page.getPdfFile', ['uuid' => '_replace_']);
1✔
111
                $url = str_replace('_replace_', '', $url);
1✔
112

113
                $data = [];
1✔
114
                $fileIds = [];
1✔
115

116
                foreach ($currentPageResults as $row) {
1✔
117
                        $fileIds[] = $row['id'];
×
118
                        $data[] = $this->formatListRow($row, $url);
×
119
                }
120
                $signers = $this->signRequestMapper->getByMultipleFileId($fileIds);
1✔
121
                $return['data'] = $this->assocFileToSignRequestAndFormat($data, $signers);
1✔
122
                $return['pagination'] = $pagination;
1✔
123
                return $return;
1✔
124
        }
125

126
        private function getQueryBuilder(array $filter = [], bool $count = false): IQueryBuilder {
127
                $qb = $this->db->getQueryBuilder();
1✔
128
                if ($count) {
1✔
129
                        $qb->select($qb->func()->count())
1✔
130
                                ->setFirstResult(0)
1✔
131
                                ->setMaxResults(null);
1✔
132
                } else {
133
                        $qb
1✔
134
                                ->select(
1✔
135
                                        'f.id',
1✔
136
                                        'f.uuid',
1✔
137
                                        'f.name',
1✔
138
                                        'f.callback',
1✔
139
                                        'f.status',
1✔
140
                                        'f.node_id',
1✔
141
                                        'id.file_type',
1✔
142
                                        'f.created_at',
1✔
143
                                )
1✔
144
                                ->groupBy(
1✔
145
                                        'f.id',
1✔
146
                                        'f.uuid',
1✔
147
                                        'f.name',
1✔
148
                                        'f.callback',
1✔
149
                                        'f.status',
1✔
150
                                        'f.node_id',
1✔
151
                                        'f.created_at',
1✔
152
                                        'id.file_type',
1✔
153
                                );
1✔
154
                        if (isset($filter['length']) && isset($filter['page'])) {
1✔
155
                                $qb->setFirstResult($filter['length'] * ($filter['page'] - 1));
1✔
156
                                $qb->setMaxResults($filter['length']);
1✔
157
                        }
158
                }
159
                $qb
1✔
160
                        ->from($this->getTableName(), 'id')
1✔
161
                        ->join('id', 'libresign_file', 'f', 'f.id = id.file_id');
1✔
162
                if (!empty($filter['signRequestId'])) {
1✔
NEW
163
                        if (!$count) {
×
NEW
164
                                $qb->addSelect('im.identifier_key')
×
NEW
165
                                        ->addSelect('im.identifier_value')
×
NEW
166
                                        ->addGroupBy('im.identifier_key')
×
NEW
167
                                        ->addGroupBy('im.identifier_value');
×
168
                        }
NEW
169
                        $qb
×
NEW
170
                                ->join('f', 'libresign_sign_request', 'sr', 'sr.file_id = f.id')
×
NEW
171
                                ->join('sr', 'libresign_identify_method', 'im', 'im.sign_request_id = sr.id')
×
NEW
172
                                ->where(
×
NEW
173
                                        $qb->expr()->eq('sr.id', $qb->createNamedParameter($filter['signRequestId'])),
×
NEW
174
                                );
×
175
                }
176
                if (!empty($filter['userId'])) {
1✔
NEW
177
                        if (!$count) {
×
NEW
178
                                $qb->selectAlias('u.uid_lower', 'account_uid')
×
NEW
179
                                        ->selectAlias('u.displayname', 'account_displayname')
×
NEW
180
                                        ->addGroupBy('u.uid_lower')
×
NEW
181
                                        ->addGroupBy('u.displayname');
×
182
                        }
NEW
183
                        $qb
×
NEW
184
                                ->join('id', 'users', 'u', 'id.user_id = u.uid')
×
NEW
185
                                ->where(
×
NEW
186
                                        $qb->expr()->eq('id.user_id', $qb->createNamedParameter($filter['userId'])),
×
NEW
187
                                );
×
188
                }
189
                if (!empty($filter['approved'])) {
1✔
190
                        if ($filter['approved'] === 'yes') {
×
191
                                $qb->andWhere(
×
192
                                        $qb->expr()->eq('f.status', $qb->createNamedParameter(File::STATUS_SIGNED, Types::INTEGER)),
×
193
                                );
×
194
                        }
195
                }
196
                return $qb;
1✔
197
        }
198

199
        private function getDocs(array $filter = []): Pagination {
200
                $qb = $this->getQueryBuilder(
1✔
201
                        filter: $filter,
1✔
202
                );
1✔
203
                $countQb = $this->getQueryBuilder(
1✔
204
                        filter: $filter,
1✔
205
                        count: true,
1✔
206
                );
1✔
207

208
                $pagination = new Pagination($qb, $this->urlGenerator, $countQb);
1✔
209
                return $pagination;
1✔
210
        }
211

212
        private function formatListRow(array $row, string $url): array {
213
                $row['account'] = [
×
214
                        'uid' => $row['account_uid'],
×
215
                        'displayName' => $row['account_displayname']
×
216
                ];
×
217
                $row['file_type'] = [
×
218
                        'type' => $row['file_type'],
×
219
                        'name' => $this->fileTypeMapper->getNameOfType($row['file_type']),
×
220
                        'description' => $this->fileTypeMapper->getDescriptionOfType($row['file_type']),
×
221
                ];
×
NEW
222
                $row['created_at'] = (new \DateTime())
×
NEW
223
                        ->setTimestamp((int)$row['created_at'])
×
NEW
224
                        ->format('Y-m-d H:i:s');
×
225
                $row['file'] = [
×
226
                        'name' => $row['name'],
×
227
                        'status' => $row['status'],
×
228
                        'statusText' => $this->fileMapper->getTextOfStatus((int)$row['status']),
×
229
                        'created_at' => $row['created_at'],
×
230
                        'file' => [
×
231
                                'type' => 'pdf',
×
232
                                'nodeId' => (int)$row['node_id'],
×
233
                                'url' => $url . $row['uuid'],
×
234
                        ],
×
235
                        'callback' => $row['callback'],
×
236
                        'uuid' => $row['uuid'],
×
237
                ];
×
238
                unset(
×
239
                        $row['node_id'],
×
240
                        $row['name'],
×
241
                        $row['status'],
×
242
                        $row['created_at'],
×
243
                        $row['account_displayname'],
×
244
                        $row['account_uid'],
×
245
                        $row['callback'],
×
246
                        $row['uuid'],
×
247
                        $row['account_uid'],
×
248
                );
×
249
                return $row;
×
250
        }
251

252
        /**
253
         * @param array $files
254
         * @param SignRequest[] $signers
255
         */
256
        private function assocFileToSignRequestAndFormat(array $files, array $signers): array {
257
                foreach ($files as $key => $file) {
1✔
258
                        $totalSigned = 0;
×
259
                        $files[$key]['file']['signers'] = [];
×
260
                        foreach ($signers as $signerKey => $signer) {
×
261
                                if ($signer->getFileId() === $file['id']) {
×
262
                                        $data = [
×
263
                                                'description' => $signer->getDescription(),
×
264
                                                'displayName' => $signer->getDisplayName(),
×
NEW
265
                                                'request_sign_date' => (new \DateTime())
×
NEW
266
                                                        ->setTimestamp($signer->getCreatedAt()->getTimestamp())
×
NEW
267
                                                        ->format('Y-m-d H:i:s'),
×
NEW
268
                                                'sign_date' => null,
×
269
                                                'signRequestId' => $signer->getId(),
×
270
                                        ];
×
NEW
271
                                        if ($signer->getSigned()) {
×
NEW
272
                                                $data['sign_date'] = (new \DateTime())
×
NEW
273
                                                        ->setTimestamp($signer->getSigned()->getTimestamp())
×
NEW
274
                                                        ->format('Y-m-d H:i:s');
×
275
                                                $totalSigned++;
×
276
                                        }
277
                                        $files[$key]['file']['signers'][] = $data;
×
278
                                        unset($signers[$signerKey]);
×
279
                                }
280
                        }
281
                        unset($files[$key]['id']);
×
282
                }
283
                return $files;
1✔
284
        }
285
}
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