feat: Avertir l'enseignant quand un devoir ne respecte pas les règles (mode soft)
Quand un établissement configure des règles de devoirs en mode "soft", l'enseignant est maintenant averti avant la création si la date d'échéance ne respecte pas les contraintes (délai minimum, pas de lundi après un certain créneau). Il peut alors choisir de continuer (avec traçabilité) ou de modifier la date vers une date conforme. Le mode "hard" (blocage) reste protégé : acknowledgeWarning ne permet pas de contourner les règles bloquantes, préparant la story 5.5.
This commit is contained in:
@@ -14,6 +14,7 @@ final readonly class CreateHomeworkCommand
|
||||
public string $title,
|
||||
public ?string $description,
|
||||
public string $dueDate,
|
||||
public bool $acknowledgeWarning = false,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,9 @@ use App\Administration\Domain\Model\Subject\SubjectId;
|
||||
use App\Administration\Domain\Model\User\UserId;
|
||||
use App\Scolarite\Application\Port\CurrentCalendarProvider;
|
||||
use App\Scolarite\Application\Port\EnseignantAffectationChecker;
|
||||
use App\Scolarite\Application\Port\HomeworkRulesChecker;
|
||||
use App\Scolarite\Domain\Exception\EnseignantNonAffecteException;
|
||||
use App\Scolarite\Domain\Exception\ReglesDevoirsNonRespecteesException;
|
||||
use App\Scolarite\Domain\Model\Homework\Homework;
|
||||
use App\Scolarite\Domain\Repository\HomeworkRepository;
|
||||
use App\Scolarite\Domain\Service\DueDateValidator;
|
||||
@@ -26,6 +28,7 @@ final readonly class CreateHomeworkHandler
|
||||
private EnseignantAffectationChecker $affectationChecker,
|
||||
private CurrentCalendarProvider $calendarProvider,
|
||||
private DueDateValidator $dueDateValidator,
|
||||
private HomeworkRulesChecker $rulesChecker,
|
||||
private Clock $clock,
|
||||
) {
|
||||
}
|
||||
@@ -46,6 +49,16 @@ final readonly class CreateHomeworkHandler
|
||||
$dueDate = new DateTimeImmutable($command->dueDate);
|
||||
$this->dueDateValidator->valider($dueDate, $now, $calendar);
|
||||
|
||||
$rulesResult = $this->rulesChecker->verifier($tenantId, $dueDate, $now);
|
||||
|
||||
if ($rulesResult->estBloquant()) {
|
||||
throw new ReglesDevoirsNonRespecteesException($rulesResult->toArray());
|
||||
}
|
||||
|
||||
if ($rulesResult->estAvertissement() && !$command->acknowledgeWarning) {
|
||||
throw new ReglesDevoirsNonRespecteesException($rulesResult->toArray());
|
||||
}
|
||||
|
||||
$homework = Homework::creer(
|
||||
tenantId: $tenantId,
|
||||
classId: $classId,
|
||||
@@ -57,6 +70,10 @@ final readonly class CreateHomeworkHandler
|
||||
now: $now,
|
||||
);
|
||||
|
||||
if ($command->acknowledgeWarning && $rulesResult->estAvertissement()) {
|
||||
$homework->acknowledgeRuleWarning($rulesResult->ruleTypes(), $now);
|
||||
}
|
||||
|
||||
$this->homeworkRepository->save($homework);
|
||||
|
||||
return $homework;
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Port;
|
||||
|
||||
use function array_map;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
* Résultat de la vérification des règles de devoirs.
|
||||
*
|
||||
* Transporté à travers la frontière Scolarite ← Administration,
|
||||
* indépendant des types internes d'Administration.
|
||||
*/
|
||||
final readonly class HomeworkRulesCheckResult
|
||||
{
|
||||
/**
|
||||
* @param RuleWarning[] $warnings
|
||||
*/
|
||||
public function __construct(
|
||||
public array $warnings,
|
||||
public bool $bloquant,
|
||||
) {
|
||||
}
|
||||
|
||||
public function estValide(): bool
|
||||
{
|
||||
return count($this->warnings) === 0;
|
||||
}
|
||||
|
||||
public function estAvertissement(): bool
|
||||
{
|
||||
return !$this->estValide() && !$this->bloquant;
|
||||
}
|
||||
|
||||
public function estBloquant(): bool
|
||||
{
|
||||
return !$this->estValide() && $this->bloquant;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return array_map(
|
||||
static fn (RuleWarning $w): string => $w->message,
|
||||
$this->warnings,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function ruleTypes(): array
|
||||
{
|
||||
return array_map(
|
||||
static fn (RuleWarning $w): string => $w->ruleType,
|
||||
$this->warnings,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<array{ruleType: string, message: string, params: array<string, mixed>}>
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return array_map(
|
||||
static fn (RuleWarning $w): array => $w->toArray(),
|
||||
$this->warnings,
|
||||
);
|
||||
}
|
||||
|
||||
public static function ok(): self
|
||||
{
|
||||
return new self(warnings: [], bloquant: false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Port;
|
||||
|
||||
use App\Shared\Domain\Tenant\TenantId;
|
||||
use DateTimeImmutable;
|
||||
|
||||
/**
|
||||
* Port pour vérifier les règles de devoirs configurées par l'établissement.
|
||||
*
|
||||
* L'implémentation consulte la configuration des règles du tenant
|
||||
* et retourne le résultat de validation.
|
||||
*/
|
||||
interface HomeworkRulesChecker
|
||||
{
|
||||
public function verifier(
|
||||
TenantId $tenantId,
|
||||
DateTimeImmutable $dueDate,
|
||||
DateTimeImmutable $creationDate,
|
||||
): HomeworkRulesCheckResult;
|
||||
}
|
||||
33
backend/src/Scolarite/Application/Port/RuleWarning.php
Normal file
33
backend/src/Scolarite/Application/Port/RuleWarning.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Scolarite\Application\Port;
|
||||
|
||||
/**
|
||||
* Avertissement d'une règle de devoir non respectée.
|
||||
*/
|
||||
final readonly class RuleWarning
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $params Paramètres de la règle pour le frontend
|
||||
*/
|
||||
public function __construct(
|
||||
public string $ruleType,
|
||||
public string $message,
|
||||
public array $params = [],
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{ruleType: string, message: string, params: array<string, mixed>}
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'ruleType' => $this->ruleType,
|
||||
'message' => $this->message,
|
||||
'params' => $this->params,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user