fix: false draft-restored message

This commit is contained in:
Björn Fromme
2026-08-31 09:31:00 +02:00
parent bfad61f1dd
commit 81b724c11a
4 changed files with 244 additions and 5 deletions
@@ -10,6 +10,7 @@ use App\Entity\User;
use App\Form\BookingParticipantType;
use App\Form\Model\BookingDto;
use App\Htmx\HxTrait;
use App\Service\BookingChangeTracker;
use App\Service\BookingConfigurator;
use App\Service\BookingEditContextFactory;
use App\Service\BookingEditDataLoader;
@@ -37,6 +38,7 @@ class ParticipantController extends AbstractController
private readonly BookingEditDraftManager $draftService,
private readonly BookingEditContextFactory $editContextFactory,
private readonly BookingConfigurator $bookingConfigurator,
private readonly BookingChangeTracker $changeTracker,
private readonly BookingSessionManager $bookingSessionService,
private readonly ParticipantDataPrefiller $prepopulationService,
private readonly ParticipantFormSupport $participantFormSupportService,
@@ -99,7 +101,7 @@ class ParticipantController extends AbstractController
$this->prepopulationService->fillDummyParticipant($bookingDto->participants[$index], $index);
$this->bookingSessionService->saveBookingDto($request, $bookingDto, BookingDto::MODE_EDIT);
$this->draftService->saveDraft($user, $bookingId, $bookingDto);
$this->persistDraftState($user, $bookingId, $bookingDto);
$form = $this->createParticipantForm($bookingDto, $index);
@@ -110,7 +112,7 @@ class ParticipantController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$this->bookingSessionService->saveBookingDto($request, $bookingDto, BookingDto::MODE_EDIT);
$this->draftService->saveDraft($user, $bookingId, $bookingDto);
$this->persistDraftState($user, $bookingId, $bookingDto);
$this->addNotificationsAsFlashMessages($notifications);
return $this->redirectToRoute('app_booking_edit', ['bookingId' => $bookingId]);
@@ -196,6 +198,24 @@ class ParticipantController extends AbstractController
return $response;
}
/**
* Persists the draft only while there is something to draft.
*
* Saving a draft that reproduces the API state exactly would make the next
* overview load report a restored draft for edits the user never made.
* Such a draft is obsolete, so it is removed instead.
*/
private function persistDraftState(User $user, int $bookingId, BookingDto $bookingDto): void
{
if (true === $this->changeTracker->isDirty($bookingDto)) {
$this->draftService->saveDraft($user, $bookingId, $bookingDto);
return;
}
$this->draftService->deleteDraft($user, $bookingId);
}
/**
* @param array<array{type: string, message: string}> $notifications
*/
+5 -3
View File
@@ -172,9 +172,11 @@ class BookingEditDataLoader
$draft = $this->draftService->findDraft($user, $bookingId);
if (null !== $draft) {
$applied = $this->draftService->applyDraftToDto($draft, $formData, $travelData);
if (true === $applied) {
$this->draftRestored = true;
}
// A draft that reproduces the API state exactly is not a restore. Reporting
// it would tell the user their draft was restored for edits they never made.
$this->draftRestored = true === $applied
&& $formData->originalFingerprint !== $this->fingerprintService->generateFingerprint($formData);
}
$this->bookingSessionService->saveBookingDto($request, $formData, BookingDto::MODE_EDIT);