Files
espocrm/custom/Espo/Custom/Api/CPuls/AbschliessenFuerTeam.php
bsiggel e1a963ffab feat(CPuls): Enhance CPuls entity with new fields, tooltips, and options; add localization for German and English
- Added new fields to CPuls entity including status, syncStatus, kiAnalyse, and others.
- Implemented localization for CPuls in German (de_DE) and English (en_US).
- Introduced new API actions for team activation and completion of CPuls.
- Created hooks to update team statistics and manage document counts.
- Added new entity definitions and metadata for CPulsTeamZuordnung and Team.
- Implemented validation logic in formulas to prevent completion of unclean Puls.
- Updated layouts for detail and list views of CPuls.
- Enhanced user entity with absence tracking fields.
- Added scopes for CPuls and CPulsTeamZuordnung.
2026-02-13 10:09:19 +01:00

122 lines
3.9 KiB
PHP

<?php
namespace Espo\Custom\Api\CPuls;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
class AbschliessenFuerTeam implements Action
{
public function __construct(
private \Espo\ORM\EntityManager $entityManager,
private \Espo\Core\Acl\Table $acl,
private \Espo\Entities\User $user,
private \Espo\Core\Utils\Log $log
) {}
public function process(Request $request): Response
{
$pulsId = $request->getRouteParam('id');
$data = $request->getParsedBody();
$teamId = $data->teamId ?? null;
if (!$pulsId || !$teamId) {
throw new BadRequest('pulsId oder teamId fehlt');
}
// 1. Validierung: Ist User in diesem Team?
$userTeams = $this->user->getLinkMultipleIdList('teams');
if (!in_array($teamId, $userTeams)) {
throw new Forbidden('User nicht in angegebenem Team');
}
// 2. Lade Puls
$puls = $this->entityManager->getEntity('CPuls', $pulsId);
if (!$puls) {
throw new NotFound('Puls nicht gefunden');
}
// 3. Validierung: syncStatus = clean?
if ($puls->get('syncStatus') !== 'clean') {
throw new BadRequest('Puls hat neue Dokumente (unclean) - bitte warten Sie auf die KI-Analyse');
}
// 4. Finde Zuordnung
$zuordnung = $this->entityManager
->getRDBRepository('CPulsTeamZuordnung')
->where([
'pulsId' => $pulsId,
'teamId' => $teamId,
'aktiv' => true
])
->findOne();
if (!$zuordnung) {
throw new NotFound('Team-Zuordnung nicht gefunden oder nicht aktiv');
}
// 5. Bereits abgeschlossen?
if ($zuordnung->get('abgeschlossen')) {
return Response::json([
'success' => true,
'message' => 'Bereits abgeschlossen',
'alreadyCompleted' => true
]);
}
// 6. Abschluss setzen
$zuordnung->set([
'abgeschlossen' => true,
'abgeschlossenAm' => date('Y-m-d H:i:s'),
'abgeschlossenVonId' => $this->user->getId()
]);
$this->entityManager->saveEntity($zuordnung);
// 6.5. FIRST-READ-CLOSES: Finalisiere Block bei erstem Abschluss
if (!$puls->get('finalisiert')) {
$puls->set([
'finalisiert' => true,
'finalisierungsGrund' => 'Erstes Team',
'finalisiertAm' => date('Y-m-d H:i:s'),
'finalisiertVonId' => $this->user->getId()
]);
$this->log->info("Block finalisiert durch erstes Team (Team {$teamId}, User {$this->user->getId()})");
}
// 7. Prüfe: Alle Teams abgeschlossen?
$offeneTeams = $this->entityManager
->getRDBRepository('CPulsTeamZuordnung')
->where([
'pulsId' => $pulsId,
'aktiv' => true,
'abgeschlossen' => false
])
->count();
// 8. Update Puls-Status
if ($offeneTeams === 0) {
$puls->set('status', 'Abgeschlossen');
} else {
$puls->set('status', 'Teilweise abgeschlossen');
}
$this->entityManager->saveEntity($puls);
$this->log->info("Team {$teamId} hat Puls {$pulsId} abgeschlossen");
return Response::json([
'success' => true,
'status' => $puls->get('status'),
'finalisiert' => $puls->get('finalisiert'),
'offeneTeams' => $offeneTeams
]);
}
}