Skip to content

Commit

Permalink
fix: list files from PostgreSQL
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <vitor@php.rio>
  • Loading branch information
vitormattos committed Nov 29, 2024
1 parent 20a716c commit 7a74c36
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 120 deletions.
89 changes: 54 additions & 35 deletions lib/Db/AccountFileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,48 @@ public function accountFileList(array $filter, ?int $page = null, ?int $length =
return $return;
}

private function getUserAccountFile(array $filter = []): Pagination {
private function getQueryBuilder(array $filter = [], bool $count = false): IQueryBuilder {
$qb = $this->db->getQueryBuilder();
$qb->select(
'f.id',
'f.uuid',
'f.name',
'f.callback',
'f.status',
'f.node_id',
'af.file_type'
)
->selectAlias('u.uid_lower', 'account_uid')
->selectAlias('u.displayname', 'account_displayname')
->selectAlias('f.created_at', 'request_date')
if ($count) {
$qb->select($qb->func()->count())
->setFirstResult(0)
->setMaxResults(null);
} else {
$qb
->select(
'f.id',
'f.uuid',
'f.name',
'f.callback',
'f.status',
'f.node_id',
'af.file_type',
'f.created_at',
)
->selectAlias('u.uid_lower', 'account_uid')
->selectAlias('u.displayname', 'account_displayname')
->groupBy(
'f.id',
'f.uuid',
'f.name',
'f.callback',
'f.status',
'f.node_id',
'f.created_at',
'af.file_type',
'u.uid_lower',
'u.displayname'
);
if (isset($filter['length']) && isset($filter['page'])) {
$qb->setFirstResult($filter['length'] * ($filter['page'] - 1));
$qb->setMaxResults($filter['length']);
}
}
$qb
->from($this->getTableName(), 'af')
->join('af', 'libresign_file', 'f', 'f.id = af.file_id')
->join('af', 'users', 'u', 'af.user_id = u.uid')
->leftJoin('f', 'libresign_sign_request', 'sr', 'sr.file_id = f.id')
->groupBy(
'f.id',
'f.uuid',
'f.name',
'f.callback',
'f.status',
'f.node_id',
'f.created_at',
'af.file_type',
'u.uid_lower',
'u.displayname'
);
->leftJoin('f', 'libresign_sign_request', 'sr', 'sr.file_id = f.id');
if (!empty($filter['userId'])) {
$qb->where(
$qb->expr()->eq('af.user_id', $qb->createNamedParameter($filter['userId'])),
Expand All @@ -146,12 +158,19 @@ private function getUserAccountFile(array $filter = []): Pagination {
$qb->expr()->eq('af.user_id', $qb->createNamedParameter($filter['userId'])),
);
}
if (isset($filter['length']) && isset($filter['page'])) {
$qb->setFirstResult($filter['length'] * ($filter['page'] - 1));
$qb->setMaxResults($filter['length']);
}
return $qb;
}

private function getUserAccountFile(array $filter = []): Pagination {
$qb = $this->getQueryBuilder(
filter: $filter,
);
$countQb = $this->getQueryBuilder(
filter: $filter,
count: true,
);

$pagination = new Pagination($qb, $this->urlGenerator);
$pagination = new Pagination($qb, $this->urlGenerator, $countQb);
return $pagination;
}

Expand All @@ -165,14 +184,14 @@ private function formatListRow(array $row, string $url): array {
'name' => $this->fileTypeMapper->getNameOfType($row['file_type']),
'description' => $this->fileTypeMapper->getDescriptionOfType($row['file_type']),
];
$row['request_date'] = (new \DateTime())
->setTimestamp((int)$row['request_date'])
$row['created_at'] = (new \DateTime())
->setTimestamp((int)$row['created_at'])
->format('Y-m-d H:i:s');
$row['file'] = [
'name' => $row['name'],
'status' => $row['status'],
'statusText' => $this->fileMapper->getTextOfStatus((int)$row['status']),
'request_date' => $row['request_date'],
'created_at' => $row['created_at'],
'file' => [
'type' => 'pdf',
'nodeId' => (int)$row['node_id'],
Expand All @@ -185,7 +204,7 @@ private function formatListRow(array $row, string $url): array {
$row['node_id'],
$row['name'],
$row['status'],
$row['request_date'],
$row['created_at'],
$row['account_displayname'],
$row['account_uid'],
$row['callback'],
Expand Down
22 changes: 2 additions & 20 deletions lib/Db/PagerFantaQueryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class PagerFantaQueryAdapter implements AdapterInterface {
*/
public function __construct(
private IQueryBuilder $queryBuilder,
private IQueryBuilder $countQueryBuilder,
) {
if ($queryBuilder->getType() !== QueryBuilder::SELECT) {
// @codeCoverageIgnoreStart
Expand All @@ -32,26 +33,7 @@ public function __construct(
}

public function getNbResults(): int {
/**
* The clone isn't working fine if we clone the property $this->queryBuilder
* because the internal property "queryBuilder" of $this->queryBuilder is
* a reference and the clone don't work with reference. To solve this
* was used reflection.
*/
$reflect = new ReflectionClass($this->queryBuilder);
$reflectionProperty = $reflect->getProperty('queryBuilder');
$reflectionProperty->setAccessible(true);
$qb = $reflectionProperty->getValue($this->queryBuilder);
$originalQueryBuilder = clone $qb;

$this->queryBuilder->resetQueryPart('select')
->resetQueryPart('groupBy')
->select($this->queryBuilder->func()->count())
->setFirstResult(0)
->setMaxResults(null);
$total = $this->queryBuilder->executeQuery()->fetchOne();

$reflectionProperty->setValue($this->queryBuilder, $originalQueryBuilder);
$total = $this->countQueryBuilder->executeQuery()->fetchOne();

return abs((int)$total);
}
Expand Down
74 changes: 38 additions & 36 deletions lib/Db/SignRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ public function getMyLibresignFile(string $userId, ?array $filter = []): File {
userId: $userId,
filter: $filter,
);
$qb->select('f.*');
$cursor = $qb->executeQuery();
$row = $cursor->fetch();
if (!$row) {
Expand All @@ -423,26 +422,46 @@ public function getMyLibresignFile(string $userId, ?array $filter = []): File {
return $file->fromRow($row);
}

private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, ?array $filter = []): IQueryBuilder {
private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, array $filter = [], bool $count = false): IQueryBuilder {
$qb = $this->db->getQueryBuilder();
$qb->from('libresign_file', 'f')
->leftJoin('f', 'libresign_sign_request', 'sr', 'sr.file_id = f.id')
->leftJoin('f', 'libresign_identify_method', 'im', $qb->expr()->eq('sr.id', 'im.sign_request_id'))
->groupBy(
->leftJoin('f', 'libresign_identify_method', 'im', $qb->expr()->eq('sr.id', 'im.sign_request_id'));
if ($count) {
$qb->select($qb->func()->count())
->setFirstResult(0)
->setMaxResults(null);
} else {
$qb->select(
'f.id',
'f.node_id',
'f.user_id',
'f.uuid',
'f.name',
'f.status',
'f.metadata',
'f.created_at',
);
// metadata is a json column, the right way is to use f.metadata::text
// when the database is PostgreSQL. The problem is that the command
// addGroupBy add quotes over all text send as argument. With
// PostgreSQL json columns don't have problem if not added to group by.
if ($qb->getConnection()->getDatabaseProvider() !== IDBConnection::PLATFORM_POSTGRES) {
$qb->addGroupBy('f.metadata');
)
->groupBy(
'f.id',
'f.node_id',
'f.user_id',
'f.uuid',
'f.name',
'f.status',
'f.created_at',
);
// metadata is a json column, the right way is to use f.metadata::text
// when the database is PostgreSQL. The problem is that the command
// addGroupBy add quotes over all text send as argument. With
// PostgreSQL json columns don't have problem if not added to group by.
if ($qb->getConnection()->getDatabaseProvider() !== IDBConnection::PLATFORM_POSTGRES) {
$qb->addGroupBy('f.metadata');
}
if (isset($filter['length']) && isset($filter['page'])) {
$qb->setFirstResult($filter['length'] * ($filter['page'] - 1));
$qb->setMaxResults($filter['length']);
}
}

$or = [
Expand Down Expand Up @@ -485,10 +504,6 @@ private function getFilesAssociatedFilesWithMeQueryBuilder(string $userId, ?arra
$qb->expr()->lte('f.created_at', $qb->createNamedParameter($filter['end'], IQueryBuilder::PARAM_INT))
);
}
if (isset($filter['length']) && isset($filter['page'])) {
$qb->setFirstResult($filter['length'] * ($filter['page'] - 1));
$qb->setMaxResults($filter['length']);
}
}
return $qb;
}
Expand All @@ -499,33 +514,20 @@ private function getFilesAssociatedFilesWithMeStmt(
?array $sort = [],
): Pagination {
$qb = $this->getFilesAssociatedFilesWithMeQueryBuilder($userId, $filter);
$qb->select(
'f.id',
'f.node_id',
'f.user_id',
'f.uuid',
'f.name',
'f.status',
'f.metadata',
);
if (!empty($sort) && in_array($sort['sortBy'], ['name', 'status', 'created_at'])) {
$qb->orderBy(
$qb->func()->lower('f.' . $sort['sortBy']),
$sort['sortDirection'] == 'asc' ? 'asc' : 'desc'
);
}
$qb->selectAlias('f.created_at', 'request_date');

$countQueryBuilderModifier = function (IQueryBuilder $qb): int {
$qb->resetQueryPart('select')
->resetQueryPart('groupBy')
->select($qb->func()->count())
->setFirstResult(0)
->setMaxResults(null);
return (int)$qb->executeQuery()->fetchOne();
};
$countQb = $this->getFilesAssociatedFilesWithMeQueryBuilder(
userId: $userId,
filter: $filter,
count: true,
);

$pagination = new Pagination($qb, $this->urlGenerator);
$pagination = new Pagination($qb, $this->urlGenerator, $countQb);
return $pagination;
}

Expand All @@ -538,8 +540,8 @@ private function formatListRow(array $row): array {
'userId' => $row['user_id'],
'displayName' => $this->userManager->get($row['user_id'])?->getDisplayName(),
];
$row['request_date'] = (new \DateTime())
->setTimestamp((int)$row['request_date'])
$row['created_at'] = (new \DateTime())
->setTimestamp((int)$row['created_at'])
->format('Y-m-d H:i:s');
$row['file'] = $this->urlGenerator->linkToRoute('libresign.page.getPdf', ['uuid' => $row['uuid']]);
$row['nodeId'] = (int)$row['node_id'];
Expand Down
3 changes: 2 additions & 1 deletion lib/Helper/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class Pagination extends Pagerfanta {
public function __construct(
IQueryBuilder $queryBuilder,
private IURLGenerator $urlGenerator,
private IQueryBuilder $countQueryBuilder,
) {
$adapter = new PagerFantaQueryAdapter($queryBuilder);
$adapter = new PagerFantaQueryAdapter($queryBuilder, $countQueryBuilder);
parent::__construct($adapter);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
* status: 0|1|2|3|4,
* statusText: string,
* nodeId: non-negative-int,
* request_date: string,
* created_at: string,
* requested_by: array{
* userId: string,
* displayName: string,
Expand All @@ -165,12 +165,12 @@
* name: string,
* description: ?string,
* },
* request_date: string,
* created_at: string,
* file: array{
* name: string,
* status: 0|1|2|3|4,
* statusText: string,
* request_date: string,
* created_at: string,
* file: array{
* type: string,
* nodeId: non-negative-int,
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ private function getFile(): array {
$return['uuid'] = $this->file->getUuid();
$return['name'] = $this->file->getName();
$return['status'] = $this->file->getStatus();
$return['request_date'] = (new \DateTime())
$return['created_at'] = (new \DateTime())
->setTimestamp($this->file->getCreatedAt())
->format('Y-m-d H:i:s');
$return['statusText'] = $this->fileMapper->getTextOfStatus($this->file->getStatus());
Expand Down Expand Up @@ -519,7 +519,7 @@ private function associateAllAndFormat(IUser $user, array $files, array $signers
}, false),
'visibleElements' => $this->formatVisibleElementsToArray(
$visibleElements[$signer->getId()] ?? [],
json_decode($file['metadata'], true)
!empty($file['metadata'])?json_decode($file['metadata'], true):[]
),
'identifyMethods' => array_map(function (IdentifyMethod $identifyMethod) use ($signer): array {
return [
Expand Down
12 changes: 6 additions & 6 deletions openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
"required": [
"account",
"file_type",
"request_date",
"created_at",
"file"
],
"properties": {
Expand Down Expand Up @@ -233,7 +233,7 @@
}
}
},
"request_date": {
"created_at": {
"type": "string"
},
"file": {
Expand All @@ -242,7 +242,7 @@
"name",
"status",
"statusText",
"request_date",
"created_at",
"file",
"callback",
"uuid",
Expand All @@ -266,7 +266,7 @@
"statusText": {
"type": "string"
},
"request_date": {
"created_at": {
"type": "string"
},
"file": {
Expand Down Expand Up @@ -711,7 +711,7 @@
"status",
"statusText",
"nodeId",
"request_date",
"created_at",
"requested_by",
"file"
],
Expand Down Expand Up @@ -741,7 +741,7 @@
"format": "int64",
"minimum": 0
},
"request_date": {
"created_at": {
"type": "string"
},
"requested_by": {
Expand Down
Loading

0 comments on commit 7a74c36

Please sign in to comment.