Skip to content

Commit b8ae5cb

Browse files
committed
refactor(controllers): integrate FileListService changes
- Update FileController to use refactored FileListService - Update RequestSignatureController with new service methods - Update SignRequestMapper to support new query patterns - Align controllers with centralized file formatting Controllers now leverage the improved FileListService for consistent file response formatting across endpoints. Signed-off-by: Vitor Mattos <[email protected]>
1 parent 0cef50f commit b8ae5cb

File tree

3 files changed

+22
-135
lines changed

3 files changed

+22
-135
lines changed

lib/Controller/FileController.php

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function __construct(
7474
private FileService $fileService,
7575
private fileListService $fileListService,
7676
private ValidateHelper $validateHelper,
77+
private \OCA\Libresign\Service\File\SettingsLoader $settingsLoader,
7778
) {
7879
parent::__construct(Application::APP_ID, $request);
7980
}
@@ -287,26 +288,8 @@ public function list(
287288
$user = $this->userSession->getUser();
288289
$return = $this->fileListService->listAssociatedFilesOfSignFlow($user, $page, $length, $filter, $sort);
289290

290-
if ($user && !empty($return['data'])) {
291-
$firstFile = null;
292-
foreach ($return['data'] as $file) {
293-
if (($file['nodeType'] ?? 'file') !== 'envelope') {
294-
$firstFile = $file;
295-
break;
296-
}
297-
}
298-
299-
if ($firstFile) {
300-
$fileSettings = $this->fileService
301-
->setFileByType('FileId', $firstFile['nodeId'])
302-
->showSettings()
303-
->toArray();
304-
305-
$return['settings'] = [
306-
'needIdentificationDocuments' => $fileSettings['settings']['needIdentificationDocuments'] ?? false,
307-
'identificationDocumentsWaitingApproval' => $fileSettings['settings']['identificationDocumentsWaitingApproval'] ?? false,
308-
];
309-
}
291+
if ($user) {
292+
$return['settings'] = $this->settingsLoader->getUserIdentificationSettings($user->getUID());
310293
}
311294

312295
return new DataResponse($return, Http::STATUS_OK);

lib/Controller/RequestSignatureController.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCA\Libresign\Helper\ValidateHelper;
1414
use OCA\Libresign\Middleware\Attribute\RequireManager;
1515
use OCA\Libresign\ResponseDefinitions;
16+
use OCA\Libresign\Service\File\FileListService;
1617
use OCA\Libresign\Service\FileService;
1718
use OCA\Libresign\Service\RequestSignatureService;
1819
use OCP\AppFramework\Http;
@@ -38,6 +39,7 @@ public function __construct(
3839
protected IL10N $l10n,
3940
protected IUserSession $userSession,
4041
protected FileService $fileService,
42+
protected FileListService $fileListService,
4143
protected ValidateHelper $validateHelper,
4244
protected RequestSignatureService $requestSignatureService,
4345
) {
@@ -90,15 +92,7 @@ public function request(
9092
try {
9193
$this->requestSignatureService->validateNewRequestToFile($data);
9294
$file = $this->requestSignatureService->save($data);
93-
$return = $this->fileService
94-
->setFile($file)
95-
->setHost($this->request->getServerHost())
96-
->setMe($data['userManager'])
97-
->showVisibleElements()
98-
->showSigners()
99-
->showSettings()
100-
->showMessages()
101-
->toArray();
95+
$return = $this->fileListService->formatSingleFile($user, $file);
10296
return new DataResponse(
10397
[
10498
'message' => $this->l10n->t('Success'),
@@ -175,15 +169,7 @@ public function updateSign(
175169
$this->validateHelper->validateVisibleElements($data['visibleElements'], $this->validateHelper::TYPE_VISIBLE_ELEMENT_PDF);
176170
}
177171
$file = $this->requestSignatureService->save($data);
178-
$return = $this->fileService
179-
->setFile($file)
180-
->setHost($this->request->getServerHost())
181-
->setMe($data['userManager'])
182-
->showVisibleElements()
183-
->showSigners()
184-
->showSettings()
185-
->showMessages()
186-
->toArray();
172+
$return = $this->fileListService->formatSingleFile($user, $file);
187173
return new DataResponse(
188174
[
189175
'message' => $this->l10n->t('Success'),

lib/Db/SignRequestMapper.php

Lines changed: 15 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
namespace OCA\Libresign\Db;
1010

11-
use DateTimeInterface;
12-
use OCA\Libresign\Enum\SignatureFlow;
1311
use OCA\Libresign\Enum\SignRequestStatus;
1412
use OCA\Libresign\Helper\Pagination;
1513
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
@@ -289,16 +287,21 @@ public function findRemindersCandidates(): \Generator {
289287

290288
/**
291289
* Get all signers by multiple fileId
290+
* Includes signers from both the files themselves and their children files (for envelopes)
292291
*
293292
* @return SignRequest[]
294293
*/
295294
public function getByMultipleFileId(array $fileId) {
296295
$qb = $this->db->getQueryBuilder();
297296

298-
$qb->select('*')
297+
$qb->select('sr.*')
299298
->from($this->getTableName(), 'sr')
299+
->join('sr', 'libresign_file', 'f', $qb->expr()->eq('sr.file_id', 'f.id'))
300300
->where(
301-
$qb->expr()->in('sr.file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY))
301+
$qb->expr()->orX(
302+
$qb->expr()->in('f.id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY)),
303+
$qb->expr()->in('f.parent_file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT_ARRAY))
304+
)
302305
)
303306
->orderBy('sr.id', 'ASC');
304307

@@ -424,7 +427,7 @@ public function getByFileIdAndSignRequestId(int $fileId, int $signRequestId): Si
424427
return end($this->signers);
425428
}
426429

427-
public function getFilesAssociatedFilesWithMeFormatted(
430+
public function getFilesAssociatedFilesWithMe(
428431
IUser $user,
429432
array $filter,
430433
?int $page = null,
@@ -441,7 +444,8 @@ public function getFilesAssociatedFilesWithMeFormatted(
441444

442445
$data = [];
443446
foreach ($currentPageResults as $row) {
444-
$data[] = $this->formatListRow($row);
447+
$file = new File();
448+
$data[] = $file->fromRow($row);
445449
}
446450
$return['data'] = $data;
447451
$return['pagination'] = $pagination;
@@ -517,14 +521,6 @@ public function getMyLibresignFile(string $userId, ?array $filter = []): File {
517521
throw new DoesNotExistException('LibreSign file not found');
518522
}
519523

520-
unset(
521-
$row['parent_id'],
522-
$row['parent_uuid'],
523-
$row['parent_name'],
524-
$row['parent_status'],
525-
$row['parent_created_at'],
526-
);
527-
528524
$file = new File();
529525
return $file->fromRow($row);
530526
}
@@ -534,8 +530,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
534530
$qb->from('libresign_file', 'f')
535531
->leftJoin('f', 'libresign_sign_request', 'sr', 'sr.file_id = f.id')
536532
->leftJoin('f', 'libresign_identify_method', 'im', $qb->expr()->eq('sr.id', 'im.sign_request_id'))
537-
->leftJoin('f', 'libresign_id_docs', 'id', 'id.file_id = f.id')
538-
->leftJoin('f', 'libresign_file', 'parent', $qb->expr()->eq('f.parent_file_id', 'parent.id'));
533+
->leftJoin('f', 'libresign_id_docs', 'id', 'id.file_id = f.id');
539534
if ($count) {
540535
$qb->select($qb->func()->count())
541536
->setFirstResult(0)
@@ -554,12 +549,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
554549
'f.signature_flow',
555550
'f.docmdp_level',
556551
'f.node_type',
557-
'f.parent_file_id',
558-
'parent.id as parent_id',
559-
'parent.uuid as parent_uuid',
560-
'parent.name as parent_name',
561-
'parent.status as parent_status',
562-
'parent.created_at as parent_created_at'
552+
'f.parent_file_id'
563553
)
564554
->groupBy(
565555
'f.id',
@@ -573,12 +563,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
573563
'f.signature_flow',
574564
'f.docmdp_level',
575565
'f.node_type',
576-
'f.parent_file_id',
577-
'parent.id',
578-
'parent.uuid',
579-
'parent.name',
580-
'parent.status',
581-
'parent.created_at'
566+
'f.parent_file_id'
582567
);
583568
// metadata is a json column, the right way is to use f.metadata::text
584569
// when the database is PostgreSQL. The problem is that the command
@@ -613,6 +598,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
613598
);
614599
}
615600
if (!empty($filter['signer_uuid']) && !empty($filter['parentNodeId'])) {
601+
$qb->leftJoin('f', 'libresign_file', 'parent', $qb->expr()->eq('f.parent_file_id', 'parent.id'));
616602
$qb->innerJoin('parent', 'libresign_sign_request', 'psr', $qb->expr()->eq('psr.file_id', 'parent.id'))
617603
->andWhere($qb->expr()->eq('psr.uuid', $qb->createNamedParameter($filter['signer_uuid'])));
618604
} elseif (!empty($filter['signer_uuid'])) {
@@ -643,6 +629,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
643629
);
644630
}
645631
if (!empty($filter['parentNodeId'])) {
632+
$qb->leftJoin('f', 'libresign_file', 'parent', $qb->expr()->eq('f.parent_file_id', 'parent.id'));
646633
$qb->andWhere(
647634
$qb->expr()->eq('parent.node_id', $qb->createNamedParameter($filter['parentNodeId'], IQueryBuilder::PARAM_INT))
648635
);
@@ -688,75 +675,6 @@ private function getFilesAssociatedFilesWithMeStmt(
688675
return $pagination;
689676
}
690677

691-
private function formatListRow(array $row): array {
692-
$internalId = (int)$row['id'];
693-
$row['fileId'] = $internalId;
694-
$row['id'] = (int)$row['node_id'];
695-
$row['status'] = (int)$row['status'];
696-
$row['statusText'] = $this->fileMapper->getTextOfStatus($row['status']);
697-
$row['nodeId'] = (int)$row['node_id'];
698-
$row['signedNodeId'] = (int)$row['signed_node_id'];
699-
$row['requested_by'] = [
700-
'userId' => $row['user_id'],
701-
'displayName' => $this->userManager->get($row['user_id'])?->getDisplayName(),
702-
];
703-
$row['created_at'] = (new \DateTime($row['created_at']))->setTimezone(new \DateTimeZone('UTC'))->format(DateTimeInterface::ATOM);
704-
$row['file'] = $this->urlGenerator->linkToRoute('libresign.page.getPdf', ['uuid' => $row['uuid']]);
705-
$row['nodeId'] = (int)$row['node_id'];
706-
707-
$row['name'] = $this->removeExtensionFromName($row['name'], $row['metadata']);
708-
$row['signatureFlow'] = SignatureFlow::fromNumeric((int)($row['signature_flow']))->value;
709-
$row['docmdpLevel'] = (int)($row['docmdp_level'] ?? 0);
710-
$row['nodeType'] = $row['node_type'] ?? 'file';
711-
$row['metadata'] = json_decode($row['metadata'] ?? '{}', true);
712-
713-
if ($row['node_type'] === 'envelope') {
714-
$row['filesCount'] = $row['metadata']['filesCount'] ?? 0;
715-
$row['files'] = [];
716-
} else {
717-
$row['filesCount'] = 1;
718-
$row['files'] = [[
719-
'fileId' => (int)$row['id'],
720-
'nodeId' => (int)$row['node_id'],
721-
'uuid' => $row['uuid'],
722-
'name' => $row['name'],
723-
'status' => (int)$row['status'],
724-
'statusText' => $row['statusText'],
725-
]];
726-
}
727-
728-
unset(
729-
$row['user_id'],
730-
$row['node_id'],
731-
$row['signed_node_id'],
732-
$row['signature_flow'],
733-
$row['docmdp_level'],
734-
$row['node_type'],
735-
$row['parent_file_id'],
736-
$row['parent_id'],
737-
$row['parent_uuid'],
738-
$row['parent_name'],
739-
$row['parent_status'],
740-
$row['parent_created_at'],
741-
);
742-
return $row;
743-
}
744-
745-
private function removeExtensionFromName(string $name, ?string $metadataJson): string {
746-
if (empty($name) || empty($metadataJson)) {
747-
return $name;
748-
}
749-
750-
$metadata = json_decode($metadataJson, true);
751-
if (!isset($metadata['extension'])) {
752-
return $name;
753-
}
754-
755-
$extensionPattern = '/\.' . preg_quote($metadata['extension'], '/') . '$/i';
756-
$result = preg_replace($extensionPattern, '', $name);
757-
return $result ?? $name;
758-
}
759-
760678
public function getTextOfSignerStatus(int $status): string {
761679
return SignRequestStatus::from($status)->getLabel($this->l10n);
762680
}

0 commit comments

Comments
 (0)