some update
This commit is contained in:
72
custom/Espo/Custom/Api/JunctionData/GetAktenDokumentes.php
Normal file
72
custom/Espo/Custom/Api/JunctionData/GetAktenDokumentes.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?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
|
||||
]);
|
||||
}
|
||||
}
|
||||
161
custom/Espo/Custom/Api/JunctionData/LinkAktenDokument.php
Normal file
161
custom/Espo/Custom/Api/JunctionData/LinkAktenDokument.php
Normal file
@@ -0,0 +1,161 @@
|
||||
<?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\Conflict;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
/**
|
||||
* POST /api/v1/JunctionData/CAdvowareAkten/:akteId/dokumentes/:documentId
|
||||
*
|
||||
* Links a document to an Akte and sets junction table columns in one operation
|
||||
*/
|
||||
class LinkAktenDokument implements Action
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
public function process(Request $request): Response
|
||||
{
|
||||
$akteId = $request->getRouteParam('akteId');
|
||||
$documentId = $request->getRouteParam('documentId');
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
if (!$akteId || !$documentId) {
|
||||
throw new BadRequest('Akte ID and Document ID are required');
|
||||
}
|
||||
|
||||
// Verify entities exist
|
||||
$akte = $this->entityManager->getEntityById('CAdvowareAkten', $akteId);
|
||||
if (!$akte) {
|
||||
throw new NotFound('Akte not found');
|
||||
}
|
||||
|
||||
$document = $this->entityManager->getEntityById('CDokumente', $documentId);
|
||||
if (!$document) {
|
||||
throw new NotFound('Document not found');
|
||||
}
|
||||
|
||||
// Check if already linked
|
||||
if ($this->checkIfLinked($akteId, $documentId)) {
|
||||
// Already linked, just update junction columns
|
||||
return $this->updateExisting($akteId, $documentId, $data);
|
||||
}
|
||||
|
||||
// Create relationship via ORM (triggers hooks)
|
||||
$this->entityManager
|
||||
->getRDBRepository('CAdvowareAkten')
|
||||
->getRelation($akte, 'dokumentes')
|
||||
->relate($document);
|
||||
|
||||
// Then update junction columns via SQL (no hooks)
|
||||
return $this->updateExisting($akteId, $documentId, $data);
|
||||
}
|
||||
|
||||
private function checkIfLinked(string $akteId, string $documentId): bool
|
||||
{
|
||||
$pdo = $this->entityManager->getPDO();
|
||||
|
||||
$sql = "
|
||||
SELECT COUNT(*) as count
|
||||
FROM c_advoware_akten_dokumente
|
||||
WHERE c_advoware_akten_id = :akteId
|
||||
AND c_dokumente_id = :documentId
|
||||
AND deleted = 0
|
||||
";
|
||||
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([
|
||||
'akteId' => $akteId,
|
||||
'documentId' => $documentId
|
||||
]);
|
||||
|
||||
$result = $sth->fetch(\PDO::FETCH_ASSOC);
|
||||
return $result['count'] > 0;
|
||||
}
|
||||
|
||||
private function updateExisting(string $akteId, string $documentId, object $data): Response
|
||||
{
|
||||
$pdo = $this->entityManager->getPDO();
|
||||
|
||||
// Build dynamic UPDATE
|
||||
$setClauses = [];
|
||||
$params = [
|
||||
'akteId' => $akteId,
|
||||
'documentId' => $documentId
|
||||
];
|
||||
|
||||
if (isset($data->hnr)) {
|
||||
if (!is_numeric($data->hnr)) {
|
||||
throw new BadRequest('hnr must be a number');
|
||||
}
|
||||
$setClauses[] = "hnr = :hnr";
|
||||
$params['hnr'] = (int)$data->hnr;
|
||||
}
|
||||
|
||||
if (isset($data->syncstatus)) {
|
||||
$allowedStatuses = ['new', 'unclean', 'synced', 'failed', 'unsupported'];
|
||||
if (!in_array($data->syncstatus, $allowedStatuses)) {
|
||||
throw new BadRequest('Invalid syncstatus. Allowed: ' . implode(', ', $allowedStatuses));
|
||||
}
|
||||
$setClauses[] = "syncstatus = :syncstatus";
|
||||
$params['syncstatus'] = $data->syncstatus;
|
||||
}
|
||||
|
||||
if (isset($data->lastSync)) {
|
||||
$setClauses[] = "last_sync = :lastSync";
|
||||
$params['lastSync'] = $data->lastSync;
|
||||
} elseif (isset($data->updateLastSync) && $data->updateLastSync === true) {
|
||||
$setClauses[] = "last_sync = NOW()";
|
||||
}
|
||||
|
||||
if (!empty($setClauses)) {
|
||||
$sql = "
|
||||
UPDATE c_advoware_akten_dokumente
|
||||
SET " . implode(', ', $setClauses) . "
|
||||
WHERE c_advoware_akten_id = :akteId
|
||||
AND c_dokumente_id = :documentId
|
||||
AND deleted = 0
|
||||
";
|
||||
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute($params);
|
||||
}
|
||||
|
||||
// Return final junction entry
|
||||
$sql = "
|
||||
SELECT
|
||||
id as junctionId,
|
||||
c_advoware_akten_id as cAdvowareAktenId,
|
||||
c_dokumente_id as cDokumenteId,
|
||||
hnr,
|
||||
syncstatus,
|
||||
last_sync as lastSync
|
||||
FROM c_advoware_akten_dokumente
|
||||
WHERE c_advoware_akten_id = :akteId
|
||||
AND c_dokumente_id = :documentId
|
||||
AND deleted = 0
|
||||
";
|
||||
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([
|
||||
'akteId' => $akteId,
|
||||
'documentId' => $documentId
|
||||
]);
|
||||
|
||||
$result = $sth->fetch(\PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$result) {
|
||||
throw new NotFound('Junction entry not found after creation');
|
||||
}
|
||||
|
||||
return ResponseComposer::json($result);
|
||||
}
|
||||
}
|
||||
122
custom/Espo/Custom/Api/JunctionData/UpdateAktenJunction.php
Normal file
122
custom/Espo/Custom/Api/JunctionData/UpdateAktenJunction.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* PUT /api/v1/JunctionData/CAdvowareAkten/:akteId/dokumentes/:documentId
|
||||
*
|
||||
* Updates junction table columns for an existing relationship
|
||||
*/
|
||||
class UpdateAktenJunction implements Action
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
public function process(Request $request): Response
|
||||
{
|
||||
$akteId = $request->getRouteParam('akteId');
|
||||
$documentId = $request->getRouteParam('documentId');
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
if (!$akteId || !$documentId) {
|
||||
throw new BadRequest('Akte ID and Document ID are required');
|
||||
}
|
||||
|
||||
$pdo = $this->entityManager->getPDO();
|
||||
|
||||
// Build dynamic UPDATE with only provided fields
|
||||
$setClauses = [];
|
||||
$params = [
|
||||
'akteId' => $akteId,
|
||||
'documentId' => $documentId
|
||||
];
|
||||
|
||||
if (isset($data->hnr)) {
|
||||
if (!is_numeric($data->hnr)) {
|
||||
throw new BadRequest('hnr must be a number');
|
||||
}
|
||||
$setClauses[] = "hnr = :hnr";
|
||||
$params['hnr'] = (int)$data->hnr;
|
||||
}
|
||||
|
||||
if (isset($data->syncstatus)) {
|
||||
$allowedStatuses = ['new', 'unclean', 'synced', 'failed', 'unsupported'];
|
||||
if (!in_array($data->syncstatus, $allowedStatuses)) {
|
||||
throw new BadRequest('Invalid syncstatus. Allowed: ' . implode(', ', $allowedStatuses));
|
||||
}
|
||||
$setClauses[] = "syncstatus = :syncstatus";
|
||||
$params['syncstatus'] = $data->syncstatus;
|
||||
}
|
||||
|
||||
if (isset($data->lastSync)) {
|
||||
$setClauses[] = "last_sync = :lastSync";
|
||||
$params['lastSync'] = $data->lastSync;
|
||||
} elseif (isset($data->updateLastSync) && $data->updateLastSync === true) {
|
||||
$setClauses[] = "last_sync = NOW()";
|
||||
}
|
||||
|
||||
if (empty($setClauses)) {
|
||||
throw new BadRequest('No fields to update. Provide: hnr, syncstatus, or lastSync');
|
||||
}
|
||||
|
||||
$sql = "
|
||||
UPDATE c_advoware_akten_dokumente
|
||||
SET " . implode(', ', $setClauses) . "
|
||||
WHERE c_advoware_akten_id = :akteId
|
||||
AND c_dokumente_id = :documentId
|
||||
AND deleted = 0
|
||||
";
|
||||
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute($params);
|
||||
|
||||
if ($sth->rowCount() === 0) {
|
||||
throw new NotFound('Junction entry not found or no changes made');
|
||||
}
|
||||
|
||||
// Return updated data
|
||||
return ResponseComposer::json($this->getJunctionEntry($akteId, $documentId));
|
||||
}
|
||||
|
||||
private function getJunctionEntry(string $akteId, string $documentId): array
|
||||
{
|
||||
$pdo = $this->entityManager->getPDO();
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
id as junctionId,
|
||||
c_advoware_akten_id as cAdvowareAktenId,
|
||||
c_dokumente_id as cDokumenteId,
|
||||
hnr,
|
||||
syncstatus,
|
||||
last_sync as lastSync
|
||||
FROM c_advoware_akten_dokumente
|
||||
WHERE c_advoware_akten_id = :akteId
|
||||
AND c_dokumente_id = :documentId
|
||||
AND deleted = 0
|
||||
";
|
||||
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([
|
||||
'akteId' => $akteId,
|
||||
'documentId' => $documentId
|
||||
]);
|
||||
|
||||
$result = $sth->fetch(\PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$result) {
|
||||
throw new NotFound('Junction entry not found');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user