Skip to content

Commit d13851a

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 2e2633f commit d13851a

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;
@@ -290,16 +288,21 @@ public function findRemindersCandidates(): \Generator {
290288

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

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

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

428-
public function getFilesAssociatedFilesWithMeFormatted(
431+
public function getFilesAssociatedFilesWithMe(
429432
IUser $user,
430433
array $filter,
431434
?int $page = null,
@@ -442,7 +445,8 @@ public function getFilesAssociatedFilesWithMeFormatted(
442445

443446
$data = [];
444447
foreach ($currentPageResults as $row) {
445-
$data[] = $this->formatListRow($row);
448+
$file = new File();
449+
$data[] = $file->fromRow($row);
446450
}
447451
$return['data'] = $data;
448452
$return['pagination'] = $pagination;
@@ -518,14 +522,6 @@ public function getMyLibresignFile(string $userId, ?array $filter = []): File {
518522
throw new DoesNotExistException('LibreSign file not found');
519523
}
520524

521-
unset(
522-
$row['parent_id'],
523-
$row['parent_uuid'],
524-
$row['parent_name'],
525-
$row['parent_status'],
526-
$row['parent_created_at'],
527-
);
528-
529525
$file = new File();
530526
return $file->fromRow($row);
531527
}
@@ -535,8 +531,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
535531
$qb->from('libresign_file', 'f')
536532
->leftJoin('f', 'libresign_sign_request', 'sr', 'sr.file_id = f.id')
537533
->leftJoin('f', 'libresign_identify_method', 'im', $qb->expr()->eq('sr.id', 'im.sign_request_id'))
538-
->leftJoin('f', 'libresign_id_docs', 'id', 'id.file_id = f.id')
539-
->leftJoin('f', 'libresign_file', 'parent', $qb->expr()->eq('f.parent_file_id', 'parent.id'));
534+
->leftJoin('f', 'libresign_id_docs', 'id', 'id.file_id = f.id');
540535
if ($count) {
541536
$qb->select($qb->func()->count())
542537
->setFirstResult(0)
@@ -555,12 +550,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
555550
'f.signature_flow',
556551
'f.docmdp_level',
557552
'f.node_type',
558-
'f.parent_file_id',
559-
'parent.id as parent_id',
560-
'parent.uuid as parent_uuid',
561-
'parent.name as parent_name',
562-
'parent.status as parent_status',
563-
'parent.created_at as parent_created_at'
553+
'f.parent_file_id'
564554
)
565555
->groupBy(
566556
'f.id',
@@ -574,12 +564,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
574564
'f.signature_flow',
575565
'f.docmdp_level',
576566
'f.node_type',
577-
'f.parent_file_id',
578-
'parent.id',
579-
'parent.uuid',
580-
'parent.name',
581-
'parent.status',
582-
'parent.created_at'
567+
'f.parent_file_id'
583568
);
584569
// metadata is a json column, the right way is to use f.metadata::text
585570
// when the database is PostgreSQL. The problem is that the command
@@ -614,6 +599,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
614599
);
615600
}
616601
if (!empty($filter['signer_uuid']) && !empty($filter['parentNodeId'])) {
602+
$qb->leftJoin('f', 'libresign_file', 'parent', $qb->expr()->eq('f.parent_file_id', 'parent.id'));
617603
$qb->innerJoin('parent', 'libresign_sign_request', 'psr', $qb->expr()->eq('psr.file_id', 'parent.id'))
618604
->andWhere($qb->expr()->eq('psr.uuid', $qb->createNamedParameter($filter['signer_uuid'])));
619605
} elseif (!empty($filter['signer_uuid'])) {
@@ -644,6 +630,7 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array
644630
);
645631
}
646632
if (!empty($filter['parentNodeId'])) {
633+
$qb->leftJoin('f', 'libresign_file', 'parent', $qb->expr()->eq('f.parent_file_id', 'parent.id'));
647634
$qb->andWhere(
648635
$qb->expr()->eq('parent.node_id', $qb->createNamedParameter($filter['parentNodeId'], IQueryBuilder::PARAM_INT))
649636
);
@@ -689,75 +676,6 @@ private function getFilesAssociatedFilesWithMeStmt(
689676
return $pagination;
690677
}
691678

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

0 commit comments

Comments
 (0)