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

LibreSign / libresign / 19875236052

02 Dec 2025 10:12PM UTC coverage: 41.686%. First build
19875236052

Pull #4464

github

web-flow
Merge 8eae8d550 into da0973853
Pull Request #4464: refactor: attach document

113 of 256 new or added lines in 15 files covered. (44.14%)

5122 of 12287 relevant lines covered (41.69%)

4.18 hits per line

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

46.67
/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

163
                if (!$count || !empty($filter['userId'])) {
1✔
164
                        if (!$count) {
1✔
165
                                $qb->selectAlias('u.uid_lower', 'account_uid')
1✔
166
                                        ->selectAlias('u.displayname', 'account_displayname')
1✔
167
                                        ->addGroupBy('u.uid_lower')
1✔
168
                                        ->addGroupBy('u.displayname');
1✔
169
                        }
170
                        $joinType = !empty($filter['userId']) ? 'join' : 'leftJoin';
1✔
171
                        $qb->$joinType('id', 'users', 'u', 'id.user_id = u.uid');
1✔
172
                }
173

174
                if (!empty($filter['userId'])) {
1✔
175
                        $qb->where(
×
NEW
176
                                $qb->expr()->eq('id.user_id', $qb->createNamedParameter($filter['userId'])),
×
177
                        );
×
178
                }
179
                if (!empty($filter['approved'])) {
1✔
180
                        if ($filter['approved'] === 'yes') {
×
181
                                $qb->andWhere(
×
182
                                        $qb->expr()->eq('f.status', $qb->createNamedParameter(File::STATUS_SIGNED, Types::INTEGER)),
×
183
                                );
×
184
                        }
185
                }
186
                return $qb;
1✔
187
        }
188

189
        private function getDocs(array $filter = []): Pagination {
190
                $qb = $this->getQueryBuilder(
1✔
191
                        filter: $filter,
1✔
192
                );
1✔
193
                $countQb = $this->getQueryBuilder(
1✔
194
                        filter: $filter,
1✔
195
                        count: true,
1✔
196
                );
1✔
197

198
                $pagination = new Pagination($qb, $this->urlGenerator, $countQb);
1✔
199
                return $pagination;
1✔
200
        }
201

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

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