Add transition handling for signature state changes and waiting step redirection.

- Updated `SignatureStepStateChanger` to handle transitions and return the next workflow step.
- Added a new controller action to support waiting for signature state changes.
- Introduced a Twig template for displaying the waiting step page.
- Updated translations to include a label for "waiting for" state.
This commit is contained in:
2025-10-01 16:19:41 +02:00
parent 3de3f65ae3
commit e6f3112b1c
4 changed files with 110 additions and 20 deletions

View File

@@ -44,7 +44,7 @@ final readonly class WorkflowSignatureStateChangeController
$signature, $signature,
$request, $request,
EntityWorkflowStepSignatureVoter::CANCEL, EntityWorkflowStepSignatureVoter::CANCEL,
function (EntityWorkflowStepSignature $signature) {$this->signatureStepStateChanger->markSignatureAsCanceled($signature); }, function (EntityWorkflowStepSignature $signature): string { return $this->signatureStepStateChanger->markSignatureAsCanceled($signature); },
'@ChillMain/WorkflowSignature/cancel.html.twig', '@ChillMain/WorkflowSignature/cancel.html.twig',
); );
} }
@@ -56,11 +56,32 @@ final readonly class WorkflowSignatureStateChangeController
$signature, $signature,
$request, $request,
EntityWorkflowStepSignatureVoter::REJECT, EntityWorkflowStepSignatureVoter::REJECT,
function (EntityWorkflowStepSignature $signature) {$this->signatureStepStateChanger->markSignatureAsRejected($signature); }, function (EntityWorkflowStepSignature $signature): string { return $this->signatureStepStateChanger->markSignatureAsRejected($signature); },
'@ChillMain/WorkflowSignature/reject.html.twig', '@ChillMain/WorkflowSignature/reject.html.twig',
); );
} }
#[Route('/{_locale}/main/workflow/signature/{id}/wait/{expectedStep}', name: 'chill_main_workflow_signature_wait', methods: ['GET'])]
public function waitForSignatureChange(EntityWorkflowStepSignature $signature, string $expectedStep, Request $request): Response
{
if ($signature->getStep()->getEntityWorkflow()->getStep() === $expectedStep) {
return new RedirectResponse(
$this->chillUrlGenerator->returnPathOr('chill_main_workflow_show', ['id' => $signature->getStep()->getEntityWorkflow()->getId()])
);
}
return new Response(
$this->twig->render('@ChillMain/WorkflowSignature/waiting.html.twig', ['signature' => $signature, 'expectedStep' => $expectedStep])
);
}
/**
* @param callable(EntityWorkflowStepSignature): string $markSignature
*
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
private function markSignatureAction( private function markSignatureAction(
EntityWorkflowStepSignature $signature, EntityWorkflowStepSignature $signature,
Request $request, Request $request,
@@ -79,12 +100,17 @@ final readonly class WorkflowSignatureStateChangeController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->wrapInTransaction(function () use ($signature, $markSignature) { $expectedStep = $this->entityManager->wrapInTransaction(function () use ($signature, $markSignature) {
$markSignature($signature); dump(__METHOD__);
return dump($markSignature($signature));
}); });
return new RedirectResponse( return new RedirectResponse(
$this->chillUrlGenerator->returnPathOr('chill_main_workflow_show', ['id' => $signature->getStep()->getEntityWorkflow()->getId()]) $this->chillUrlGenerator->forwardReturnPath(
'chill_main_workflow_signature_wait',
['id' => $signature->getId(), 'expectedStep' => $expectedStep]
)
); );
} }

View File

@@ -0,0 +1,10 @@
{% extends '@ChillMain/layout.html.twig' %}
{% block title %}{{ 'workflow.signature.waiting_for'|trans }}{% endblock %}
{% block content %}
<h1>{{ block('title') }}</h1>
{% endblock %}

View File

@@ -22,6 +22,7 @@ use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Workflow\Registry; use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Transition;
/** /**
* Handles state changes for signature steps within a workflow. * Handles state changes for signature steps within a workflow.
@@ -50,8 +51,10 @@ class SignatureStepStateChanger
* *
* @param EntityWorkflowStepSignature $signature the signature entity to be marked as signed * @param EntityWorkflowStepSignature $signature the signature entity to be marked as signed
* @param int|null $atIndex optional index position for the signature within the zone * @param int|null $atIndex optional index position for the signature within the zone
*
* @return string The expected new workflow's step, after transition is applyied
*/ */
public function markSignatureAsSigned(EntityWorkflowStepSignature $signature, ?int $atIndex): void public function markSignatureAsSigned(EntityWorkflowStepSignature $signature, ?int $atIndex): string
{ {
$this->entityManager->refresh($signature, LockMode::PESSIMISTIC_WRITE); $this->entityManager->refresh($signature, LockMode::PESSIMISTIC_WRITE);
@@ -60,7 +63,14 @@ class SignatureStepStateChanger
->setZoneSignatureIndex($atIndex) ->setZoneSignatureIndex($atIndex)
->setStateDate($this->clock->now()); ->setStateDate($this->clock->now());
$this->logger->info(self::LOG_PREFIX.'Mark signature entity as signed', ['signatureId' => $signature->getId(), 'index' => (string) $atIndex]); $this->logger->info(self::LOG_PREFIX.'Mark signature entity as signed', ['signatureId' => $signature->getId(), 'index' => (string) $atIndex]);
['transition' => $transition, 'futureUser' => $futureUser] = $this->decideTransition($signature);
$this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId())); $this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId()));
if (null === $transition) {
return $signature->getStep()->getEntityWorkflow()->getStep();
}
return $transition->getTos()[0];
} }
/** /**
@@ -71,8 +81,10 @@ class SignatureStepStateChanger
* *
* This method updates the signature state to 'canceled' and logs the action. * This method updates the signature state to 'canceled' and logs the action.
* It also dispatches a message to notify about the state change. * It also dispatches a message to notify about the state change.
*
* @return string The expected new workflow's step, after transition is applyied
*/ */
public function markSignatureAsCanceled(EntityWorkflowStepSignature $signature): void public function markSignatureAsCanceled(EntityWorkflowStepSignature $signature): string
{ {
$this->entityManager->refresh($signature, LockMode::PESSIMISTIC_WRITE); $this->entityManager->refresh($signature, LockMode::PESSIMISTIC_WRITE);
@@ -80,7 +92,15 @@ class SignatureStepStateChanger
->setState(EntityWorkflowSignatureStateEnum::CANCELED) ->setState(EntityWorkflowSignatureStateEnum::CANCELED)
->setStateDate($this->clock->now()); ->setStateDate($this->clock->now());
$this->logger->info(self::LOG_PREFIX.'Mark signature entity as canceled', ['signatureId' => $signature->getId()]); $this->logger->info(self::LOG_PREFIX.'Mark signature entity as canceled', ['signatureId' => $signature->getId()]);
['transition' => $transition, 'futureUser' => $futureUser] = $this->decideTransition($signature);
$this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId())); $this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId()));
if (null === $transition) {
return $signature->getStep()->getEntityWorkflow()->getStep();
}
return $transition->getTos()[0];
} }
/** /**
@@ -93,8 +113,10 @@ class SignatureStepStateChanger
* a state change has occurred. * a state change has occurred.
* *
* @param EntityWorkflowStepSignature $signature the signature entity to be marked as rejected * @param EntityWorkflowStepSignature $signature the signature entity to be marked as rejected
*
* @return string The expected new workflow's step, after transition is applyied
*/ */
public function markSignatureAsRejected(EntityWorkflowStepSignature $signature): void public function markSignatureAsRejected(EntityWorkflowStepSignature $signature): string
{ {
$this->entityManager->refresh($signature, LockMode::PESSIMISTIC_WRITE); $this->entityManager->refresh($signature, LockMode::PESSIMISTIC_WRITE);
@@ -102,7 +124,16 @@ class SignatureStepStateChanger
->setState(EntityWorkflowSignatureStateEnum::REJECTED) ->setState(EntityWorkflowSignatureStateEnum::REJECTED)
->setStateDate($this->clock->now()); ->setStateDate($this->clock->now());
$this->logger->info(self::LOG_PREFIX.'Mark signature entity as rejected', ['signatureId' => $signature->getId()]); $this->logger->info(self::LOG_PREFIX.'Mark signature entity as rejected', ['signatureId' => $signature->getId()]);
['transition' => $transition, 'futureUser' => $futureUser] = $this->decideTransition($signature);
$this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId())); $this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId()));
if (null === $transition) {
return $signature->getStep()->getEntityWorkflow()->getStep();
}
return $transition->getTos()[0];
} }
/** /**
@@ -117,10 +148,35 @@ class SignatureStepStateChanger
{ {
$this->entityManager->refresh($signature, LockMode::PESSIMISTIC_READ); $this->entityManager->refresh($signature, LockMode::PESSIMISTIC_READ);
['transition' => $transition, 'futureUser' => $futureUser] = $this->decideTransition($signature);
if (null === $transition) {
return;
}
$entityWorkflow = $signature->getStep()->getEntityWorkflow();
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
$transitionDto = new WorkflowTransitionContextDTO($entityWorkflow);
$transitionDto->futureDestUsers[] = $futureUser;
$workflow->apply($entityWorkflow, $transition->getName(), [
'context' => $transitionDto,
'transitionAt' => $this->clock->now(),
'transition' => $transition->getName(),
]);
$this->logger->info(self::LOG_PREFIX.'Transition automatically applied', ['signatureId' => $signature->getId()]);
}
/**
* @return array{transition: Transition|null, futureUser: User|null}
*/
private function decideTransition(EntityWorkflowStepSignature $signature): array
{
if (!EntityWorkflowStepSignature::isAllSignatureNotPendingForStep($signature->getStep())) { if (!EntityWorkflowStepSignature::isAllSignatureNotPendingForStep($signature->getStep())) {
$this->logger->info(self::LOG_PREFIX.'This is not the last signature, skipping transition to another place', ['signatureId' => $signature->getId()]); $this->logger->info(self::LOG_PREFIX.'This is not the last signature, skipping transition to another place', ['signatureId' => $signature->getId()]);
return; return ['transition' => null, 'futureUser' => null];
} }
$this->logger->debug(self::LOG_PREFIX.'Continuing the process to find a transition', ['signatureId' => $signature->getId()]); $this->logger->debug(self::LOG_PREFIX.'Continuing the process to find a transition', ['signatureId' => $signature->getId()]);
@@ -144,7 +200,7 @@ class SignatureStepStateChanger
if (null === $transition) { if (null === $transition) {
$this->logger->info(self::LOG_PREFIX.'The transition is not configured, will not apply a transition', ['signatureId' => $signature->getId()]); $this->logger->info(self::LOG_PREFIX.'The transition is not configured, will not apply a transition', ['signatureId' => $signature->getId()]);
return; return ['transition' => null, 'futureUser' => null];
} }
if ('person' === $signature->getSignerKind()) { if ('person' === $signature->getSignerKind()) {
@@ -156,19 +212,16 @@ class SignatureStepStateChanger
if (null === $futureUser) { if (null === $futureUser) {
$this->logger->info(self::LOG_PREFIX.'No previous user, will not apply a transition', ['signatureId' => $signature->getId()]); $this->logger->info(self::LOG_PREFIX.'No previous user, will not apply a transition', ['signatureId' => $signature->getId()]);
return; return ['transition' => null, 'futureUser' => null];
} }
$transitionDto = new WorkflowTransitionContextDTO($entityWorkflow); foreach ($workflow->getDefinition()->getTransitions() as $transitionObj) {
$transitionDto->futureDestUsers[] = $futureUser; if ($transitionObj->getName() === $transition) {
return ['transition' => $transitionObj, 'futureUser' => $futureUser];
}
}
$workflow->apply($entityWorkflow, $transition, [ throw new \RuntimeException('Transition not found');
'context' => $transitionDto,
'transitionAt' => $this->clock->now(),
'transition' => $transition,
]);
$this->logger->info(self::LOG_PREFIX.'Transition automatically applied', ['signatureId' => $signature->getId()]);
} }
private function getPreviousSender(EntityWorkflowStep $entityWorkflowStep): ?User private function getPreviousSender(EntityWorkflowStep $entityWorkflowStep): ?User

View File

@@ -666,6 +666,7 @@ workflow:
cancel_are_you_sure: Êtes-vous sûr de vouloir annuler la signature de %signer% cancel_are_you_sure: Êtes-vous sûr de vouloir annuler la signature de %signer%
reject_signature_of: Rejet de la signature de %signer% reject_signature_of: Rejet de la signature de %signer%
reject_are_you_sure: Êtes-vous sûr de vouloir rejeter la signature de %signer% reject_are_you_sure: Êtes-vous sûr de vouloir rejeter la signature de %signer%
waiting_for: En attente de modification de l'état de la signature
attachments: attachments:
title: Pièces jointes title: Pièces jointes