73 lines
2.1 KiB
PHP
73 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Espo\Custom\Api\JunctionData;
|
|
|
|
use Espo\Core\Api\Action;
|
|
use Espo\Core\Api\Request;
|
|
use Espo\Core\Api\Response;
|
|
use Espo\Core\Api\ResponseComposer;
|
|
use Espo\Core\Exceptions\BadRequest;
|
|
use Espo\Core\Exceptions\NotFound;
|
|
use Espo\ORM\EntityManager;
|
|
|
|
/**
|
|
* GET /api/v1/JunctionData/CAdvowareAkten/:akteId/dokumentes
|
|
*
|
|
* Returns all documents linked to an Akte with junction table data
|
|
*/
|
|
class GetAktenDokumentes implements Action
|
|
{
|
|
public function __construct(
|
|
private EntityManager $entityManager
|
|
) {}
|
|
|
|
public function process(Request $request): Response
|
|
{
|
|
$akteId = $request->getRouteParam('akteId');
|
|
|
|
if (!$akteId) {
|
|
throw new BadRequest('Akte ID is required');
|
|
}
|
|
|
|
// Verify akte exists
|
|
$akte = $this->entityManager->getEntityById('CAdvowareAkten', $akteId);
|
|
if (!$akte) {
|
|
throw new NotFound('Akte not found');
|
|
}
|
|
|
|
$pdo = $this->entityManager->getPDO();
|
|
|
|
// Direct SQL query with JOIN - much more efficient!
|
|
$sql = "
|
|
SELECT
|
|
j.id as junctionId,
|
|
j.c_advoware_akten_id as cAdvowareAktenId,
|
|
j.c_dokumente_id as cDokumenteId,
|
|
j.hnr,
|
|
j.syncstatus,
|
|
j.last_sync as lastSync,
|
|
d.id as documentId,
|
|
d.name as documentName,
|
|
d.blake3hash as blake3hash,
|
|
d.created_at as documentCreatedAt,
|
|
d.modified_at as documentModifiedAt
|
|
FROM c_advoware_akten_dokumente j
|
|
INNER JOIN c_dokumente d ON j.c_dokumente_id = d.id
|
|
WHERE j.c_advoware_akten_id = :akteId
|
|
AND j.deleted = 0
|
|
AND d.deleted = 0
|
|
ORDER BY j.hnr ASC, j.id DESC
|
|
";
|
|
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute(['akteId' => $akteId]);
|
|
|
|
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
return ResponseComposer::json([
|
|
'total' => count($results),
|
|
'list' => $results
|
|
]);
|
|
}
|
|
}
|