pour prod
This commit is contained in:
144
app/Controllers/TeamController.php
Normal file
144
app/Controllers/TeamController.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Access;
|
||||
use App\Core\Csrf;
|
||||
use App\Core\JsonResponse;
|
||||
use App\Core\Request;
|
||||
use App\Models\AgentModel;
|
||||
use App\Models\StructureModel;
|
||||
use App\Models\TeamModel;
|
||||
use DateTimeImmutable;
|
||||
use PDO;
|
||||
use Throwable;
|
||||
|
||||
final class TeamController
|
||||
{
|
||||
private TeamModel $teamModel;
|
||||
private AgentModel $agentModel;
|
||||
private StructureModel $structureModel;
|
||||
private Access $access;
|
||||
|
||||
public function __construct(private PDO $pdo)
|
||||
{
|
||||
$this->teamModel = new TeamModel($pdo);
|
||||
$this->agentModel = new AgentModel($pdo);
|
||||
$this->structureModel = new StructureModel($pdo);
|
||||
$this->access = new Access($pdo);
|
||||
}
|
||||
|
||||
public function index(): void
|
||||
{
|
||||
$this->access->requireService();
|
||||
if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'GET') {
|
||||
$start = trim((string) ($_GET['date_debut'] ?? '')) ?: null;
|
||||
$end = trim((string) ($_GET['date_fin'] ?? '')) ?: null;
|
||||
JsonResponse::send(200, ['teams' => $this->teamModel->all($start, $end)]);
|
||||
}
|
||||
|
||||
Request::requireMethod('POST');
|
||||
$payload = Request::json();
|
||||
Csrf::assertPayload($payload);
|
||||
|
||||
$structureId = filter_var($payload['structure_id'] ?? null, FILTER_VALIDATE_INT);
|
||||
$type = strtoupper(trim((string) ($payload['type_equipe'] ?? '')));
|
||||
$label = trim((string) ($payload['libelle'] ?? ''));
|
||||
$start = trim((string) ($payload['date_debut'] ?? ''));
|
||||
$end = trim((string) ($payload['date_fin'] ?? ''));
|
||||
$startDate = DateTimeImmutable::createFromFormat('!Y-m-d', $start);
|
||||
$endDate = DateTimeImmutable::createFromFormat('!Y-m-d', $end);
|
||||
|
||||
if (!$structureId || $this->structureModel->findActive((int) $structureId) === null
|
||||
|| !in_array($type, ['MERCREDI_PERISCOLAIRE', 'EXTRASCOLAIRE', 'PERISCOLAIRE_SEMAINE'], true)
|
||||
|| $label === '' || !$startDate || !$endDate
|
||||
|| $startDate->format('Y-m-d') !== $start || $endDate->format('Y-m-d') !== $end
|
||||
|| $endDate < $startDate) {
|
||||
JsonResponse::send(422, ['error' => 'Les informations de l’équipe sont incomplètes ou invalides.']);
|
||||
}
|
||||
|
||||
$under6 = max(0, (int) ($payload['nombre_enfants_moins_6'] ?? 0));
|
||||
$over6 = max(0, (int) ($payload['nombre_enfants_6_plus'] ?? 0));
|
||||
$ratioUnder6 = max(1, (int) ($payload['ratio_moins_6'] ?? 8));
|
||||
$ratioOver6 = max(1, (int) ($payload['ratio_6_plus'] ?? 12));
|
||||
$minAgents = max(1, (int) ($payload['minimum_agents'] ?? 1));
|
||||
$qualified = (float) ($payload['pourcentage_diplomes_min'] ?? 50);
|
||||
if ($qualified < 0 || $qualified > 100) {
|
||||
JsonResponse::send(422, ['error' => 'Le pourcentage minimal de diplômés doit être compris entre 0 et 100 %.']);
|
||||
}
|
||||
|
||||
try {
|
||||
$id = $this->teamModel->create([
|
||||
'id_structure' => (int) $structureId,
|
||||
'type_equipe' => $type,
|
||||
'libelle' => $label,
|
||||
'date_debut' => $start,
|
||||
'date_fin' => $end,
|
||||
'nombre_enfants_moins_6' => $under6,
|
||||
'nombre_enfants_6_plus' => $over6,
|
||||
'ratio_moins_6' => $ratioUnder6,
|
||||
'ratio_6_plus' => $ratioOver6,
|
||||
'minimum_agents' => $minAgents,
|
||||
'pourcentage_diplomes_min' => $qualified,
|
||||
'commentaire' => trim((string) ($payload['commentaire'] ?? '')) ?: null,
|
||||
]);
|
||||
JsonResponse::send(201, ['message' => 'L’équipe a été créée.', 'id_equipe' => $id]);
|
||||
} catch (Throwable $e) {
|
||||
JsonResponse::send(500, ['error' => 'Impossible de créer l’équipe.', 'details' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
public function member(): void
|
||||
{
|
||||
$this->access->requireService();
|
||||
Request::requireMethod('POST');
|
||||
$payload = Request::json();
|
||||
Csrf::assertPayload($payload);
|
||||
|
||||
$action = strtoupper(trim((string) ($payload['action'] ?? 'ADD')));
|
||||
$teamId = filter_var($payload['team_id'] ?? null, FILTER_VALIDATE_INT);
|
||||
$agentId = filter_var($payload['agent_id'] ?? null, FILTER_VALIDATE_INT);
|
||||
if (!$teamId || !$agentId || $this->agentModel->findActive((int) $agentId) === null) {
|
||||
JsonResponse::send(422, ['error' => 'Équipe ou agent invalide.']);
|
||||
}
|
||||
|
||||
if ($action === 'REMOVE') {
|
||||
$deleted = $this->teamModel->removeMember((int) $teamId, (int) $agentId);
|
||||
JsonResponse::send($deleted ? 200 : 404, ['message' => $deleted ? 'L’agent a été retiré de l’équipe.' : 'Affectation introuvable.']);
|
||||
}
|
||||
|
||||
$function = strtoupper(trim((string) ($payload['fonction_equipe'] ?? 'ANIMATION')));
|
||||
if (!in_array($function, ['RESPONSABLE', 'ANIMATION', 'RESTAURATION_ENTRETIEN'], true)) {
|
||||
JsonResponse::send(422, ['error' => 'Fonction dans l’équipe invalide.']);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->teamModel->addMember(
|
||||
(int) $teamId,
|
||||
(int) $agentId,
|
||||
$function,
|
||||
trim((string) ($payload['commentaire'] ?? '')) ?: null
|
||||
);
|
||||
JsonResponse::send(200, ['message' => 'L’agent a été ajouté à l’équipe.']);
|
||||
} catch (Throwable $e) {
|
||||
JsonResponse::send(422, ['error' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
public function status(): void
|
||||
{
|
||||
$this->access->requireService();
|
||||
Request::requireMethod('POST');
|
||||
$payload = Request::json();
|
||||
Csrf::assertPayload($payload);
|
||||
$teamId = filter_var($payload['team_id'] ?? null, FILTER_VALIDATE_INT);
|
||||
$status = strtoupper(trim((string) ($payload['statut'] ?? '')));
|
||||
if (!$teamId || !in_array($status, ['BROUILLON', 'A_CONTROLER', 'VALIDEE'], true)) {
|
||||
JsonResponse::send(422, ['error' => 'Équipe ou statut invalide.']);
|
||||
}
|
||||
$this->teamModel->setStatus((int) $teamId, $status);
|
||||
JsonResponse::send(200, ['message' => 'Le statut de l’équipe a été mis à jour.']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user