From b7d8d4985e9a76149e2bf9c9deff0d2a6d8709a1 Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Tue, 14 Jan 2025 16:32:06 +0100 Subject: [PATCH 01/10] new table regulationOrderHistory --- .../CreateRegulationOrderHistoryCommand.php | 19 ++++++++ ...teRegulationOrderHistoryCommandHandler.php | 36 +++++++++++++++ ...aveRegulationGeneralInfoCommandHandler.php | 19 +++++++- src/Domain/Regulation/Enum/ActionTypeEnum.php | 12 +++++ .../Regulation/RegulationOrderHistory.php | 44 +++++++++++++++++++ ...ulationOrderHistoryRepositoryInterface.php | 12 +++++ .../Regulation.RegulationOrderHistory.orm.xml | 24 ++++++++++ .../Migrations/Version20250114151655.php | 36 +++++++++++++++ .../RegulationOrderHistoryRepository.php | 25 +++++++++++ 9 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 src/Application/Regulation/Command/CreateRegulationOrderHistoryCommand.php create mode 100644 src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php create mode 100644 src/Domain/Regulation/Enum/ActionTypeEnum.php create mode 100644 src/Domain/Regulation/RegulationOrderHistory.php create mode 100644 src/Domain/Regulation/Repository/RegulationOrderHistoryRepositoryInterface.php create mode 100644 src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml create mode 100644 src/Infrastructure/Persistence/Doctrine/Migrations/Version20250114151655.php create mode 100644 src/Infrastructure/Persistence/Doctrine/Repository/Regulation/RegulationOrderHistoryRepository.php diff --git a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommand.php b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommand.php new file mode 100644 index 000000000..3fdbd653a --- /dev/null +++ b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommand.php @@ -0,0 +1,19 @@ +regulationOrderHistoryRepository->add( + new RegulationOrderHistory( + uuid: $this->idFactory->make(), + regulationOrder: $command->regulationOrder, + user: $command->user, + action: $command->action, + date: $this->now, + ), + ); + + return $regulationOrderHistory; + } +} diff --git a/src/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandler.php b/src/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandler.php index 2a565d53e..dc5f0f7e9 100644 --- a/src/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandler.php +++ b/src/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandler.php @@ -4,14 +4,17 @@ namespace App\Application\Regulation\Command; +use App\Application\CommandBusInterface; use App\Application\IdFactoryInterface; use App\Application\Organization\VisaModel\Query\GetVisaModelQuery; use App\Application\QueryBusInterface; +use App\Domain\Regulation\Enum\ActionTypeEnum; use App\Domain\Regulation\Enum\RegulationOrderRecordStatusEnum; use App\Domain\Regulation\RegulationOrder; use App\Domain\Regulation\RegulationOrderRecord; use App\Domain\Regulation\Repository\RegulationOrderRecordRepositoryInterface; use App\Domain\Regulation\Repository\RegulationOrderRepositoryInterface; +use App\Infrastructure\Security\AuthenticatedUser; final class SaveRegulationGeneralInfoCommandHandler { @@ -21,11 +24,14 @@ public function __construct( private RegulationOrderRecordRepositoryInterface $regulationOrderRecordRepository, private \DateTimeInterface $now, private QueryBusInterface $queryBus, + private CommandBusInterface $commandBus, + private AuthenticatedUser $authenticatedUser, ) { } public function __invoke(SaveRegulationGeneralInfoCommand $command): RegulationOrderRecord { + $user = $this->authenticatedUser->getUser(); $command->cleanOtherCategoryText(); $visaModel = $command->visaModelUuid ? $this->queryBus->handle(new GetVisaModelQuery($command->visaModelUuid)) @@ -57,11 +63,18 @@ public function __invoke(SaveRegulationGeneralInfoCommand $command): RegulationO ), ); + $action = ActionTypeEnum::CREATE->value; + + $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action)); + return $regulationOrderRecord; } $command->regulationOrderRecord->updateOrganization($command->organization); - $command->regulationOrderRecord->getRegulationOrder()->update( + + $regulationOrder = $command->regulationOrderRecord->getRegulationOrder(); + + $regulationOrder->update( identifier: $command->identifier, category: $command->category, subject: $command->subject, @@ -72,6 +85,10 @@ public function __invoke(SaveRegulationGeneralInfoCommand $command): RegulationO visaModel: $visaModel, ); + $action = ActionTypeEnum::UPDATE->value; + + $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action)); + return $command->regulationOrderRecord; } } diff --git a/src/Domain/Regulation/Enum/ActionTypeEnum.php b/src/Domain/Regulation/Enum/ActionTypeEnum.php new file mode 100644 index 000000000..a39d03b99 --- /dev/null +++ b/src/Domain/Regulation/Enum/ActionTypeEnum.php @@ -0,0 +1,12 @@ +uuid; + } + + public function getRegulationOrder(): RegulationOrder + { + return $this->regulationOrder; + } + + public function getUser(): User + { + return $this->user; + } + + public function getAction(): string + { + return $this->action; + } + + public function getDate(): \DateTimeInterface + { + return $this->date; + } +} diff --git a/src/Domain/Regulation/Repository/RegulationOrderHistoryRepositoryInterface.php b/src/Domain/Regulation/Repository/RegulationOrderHistoryRepositoryInterface.php new file mode 100644 index 000000000..5e18a0c7f --- /dev/null +++ b/src/Domain/Regulation/Repository/RegulationOrderHistoryRepositoryInterface.php @@ -0,0 +1,12 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250114151655.php b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250114151655.php new file mode 100644 index 000000000..19d16a157 --- /dev/null +++ b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250114151655.php @@ -0,0 +1,36 @@ +addSql('CREATE TABLE regulation_order_history (uuid UUID NOT NULL, regulation_order_uuid UUID NOT NULL, user_uuid UUID NOT NULL, action VARCHAR(20) NOT NULL, date TIMESTAMP(0) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY(uuid))'); + $this->addSql('CREATE INDEX IDX_E47F415B267E0D5E ON regulation_order_history (regulation_order_uuid)'); + $this->addSql('CREATE INDEX IDX_E47F415BABFE1C6F ON regulation_order_history (user_uuid)'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415B267E0D5E FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415BABFE1C6F FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415B267E0D5E'); + $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415BABFE1C6F'); + $this->addSql('DROP TABLE regulation_order_history'); + } +} diff --git a/src/Infrastructure/Persistence/Doctrine/Repository/Regulation/RegulationOrderHistoryRepository.php b/src/Infrastructure/Persistence/Doctrine/Repository/Regulation/RegulationOrderHistoryRepository.php new file mode 100644 index 000000000..fb1608463 --- /dev/null +++ b/src/Infrastructure/Persistence/Doctrine/Repository/Regulation/RegulationOrderHistoryRepository.php @@ -0,0 +1,25 @@ +getEntityManager()->persist($regulationOrderHistory); + + return $regulationOrderHistory; + } +} From 64e26d1b76c51bc06fc7b2141319794bfaea8b08 Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Wed, 15 Jan 2025 18:04:02 +0100 Subject: [PATCH 02/10] supprime on delete cascade --- ...teRegulationOrderHistoryCommandHandler.php | 2 - .../DeleteRegulationCommandHandler.php | 13 +++++- .../Regulation.RegulationOrderHistory.orm.xml | 4 +- .../Migrations/Version20250115104546.php | 36 +++++++++++++++++ .../Migrations/Version20250115164924.php | 40 +++++++++++++++++++ 5 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115104546.php create mode 100644 src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115164924.php diff --git a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php index e238f5106..3bd04bd1c 100644 --- a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php +++ b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php @@ -4,7 +4,6 @@ namespace App\Application\Regulation\Command; -use App\Application\CommandBusInterface; use App\Domain\Regulation\RegulationOrderHistory; use App\Domain\Regulation\Repository\RegulationOrderHistoryRepositoryInterface; use App\Infrastructure\Adapter\IdFactory; @@ -14,7 +13,6 @@ final class CreateRegulationOrderHistoryCommandHandler public function __construct( private IdFactory $idFactory, private RegulationOrderHistoryRepositoryInterface $regulationOrderHistoryRepository, - private CommandBusInterface $commandBus, private \DateTimeInterface $now, ) { } diff --git a/src/Application/Regulation/Command/DeleteRegulationCommandHandler.php b/src/Application/Regulation/Command/DeleteRegulationCommandHandler.php index 950a04adb..039091c52 100644 --- a/src/Application/Regulation/Command/DeleteRegulationCommandHandler.php +++ b/src/Application/Regulation/Command/DeleteRegulationCommandHandler.php @@ -4,26 +4,37 @@ namespace App\Application\Regulation\Command; +use App\Application\CommandBusInterface; +use App\Domain\Regulation\Enum\ActionTypeEnum; use App\Domain\Regulation\Exception\RegulationOrderRecordCannotBeDeletedException; use App\Domain\Regulation\Repository\RegulationOrderRepositoryInterface; use App\Domain\Regulation\Specification\CanOrganizationAccessToRegulation; +use App\Infrastructure\Security\AuthenticatedUser; final class DeleteRegulationCommandHandler { public function __construct( private RegulationOrderRepositoryInterface $regulationOrderRepository, private CanOrganizationAccessToRegulation $canOrganizationAccessToRegulation, + private CommandBusInterface $commandBus, + private AuthenticatedUser $authenticatedUser, ) { } public function __invoke(DeleteRegulationCommand $command): void { $regulationOrderRecord = $command->regulationOrderRecord; + $regulationOrder = $regulationOrderRecord->getRegulationOrder(); + $user = $this->authenticatedUser->getUser(); if (false === $this->canOrganizationAccessToRegulation->isSatisfiedBy($regulationOrderRecord, $command->userOrganizationUuids)) { throw new RegulationOrderRecordCannotBeDeletedException(); } - $this->regulationOrderRepository->delete($regulationOrderRecord->getRegulationOrder()); + $action = ActionTypeEnum::DELETE->value; + + $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action)); + + $this->regulationOrderRepository->delete($regulationOrder); } } diff --git a/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml b/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml index 52c345518..9a34530de 100644 --- a/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml +++ b/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml @@ -12,12 +12,12 @@ - + - + diff --git a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115104546.php b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115104546.php new file mode 100644 index 000000000..e14cfa55c --- /dev/null +++ b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115104546.php @@ -0,0 +1,36 @@ +addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415B267E0D5E'); + $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415BABFE1C6F'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415B267E0D5E FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415BABFE1C6F FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) NOT DEFERRABLE INITIALLY IMMEDIATE'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT fk_e47f415b267e0d5e'); + $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT fk_e47f415babfe1c6f'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT fk_e47f415b267e0d5e FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT fk_e47f415babfe1c6f FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + } +} diff --git a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115164924.php b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115164924.php new file mode 100644 index 000000000..92e4087fd --- /dev/null +++ b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115164924.php @@ -0,0 +1,40 @@ +addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415B267E0D5E'); + $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415BABFE1C6F'); + $this->addSql('ALTER TABLE regulation_order_history ALTER regulation_order_uuid DROP NOT NULL'); + $this->addSql('ALTER TABLE regulation_order_history ALTER user_uuid DROP NOT NULL'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415B267E0D5E FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415BABFE1C6F FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT fk_e47f415b267e0d5e'); + $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT fk_e47f415babfe1c6f'); + $this->addSql('ALTER TABLE regulation_order_history ALTER regulation_order_uuid SET NOT NULL'); + $this->addSql('ALTER TABLE regulation_order_history ALTER user_uuid SET NOT NULL'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT fk_e47f415b267e0d5e FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT fk_e47f415babfe1c6f FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) NOT DEFERRABLE INITIALLY IMMEDIATE'); + } +} From 951e1f575c6057e220268d798a1ae0ebc4ab5e11 Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Thu, 16 Jan 2025 12:52:49 +0100 Subject: [PATCH 03/10] delete foreign key --- ...teRegulationOrderHistoryCommandHandler.php | 4 +- .../Regulation/RegulationOrderHistory.php | 14 +++---- .../Regulation.RegulationOrderHistory.orm.xml | 12 +----- .../Migrations/Version20250114151655.php | 36 ----------------- .../Migrations/Version20250115104546.php | 36 ----------------- .../Migrations/Version20250115164924.php | 40 ------------------- .../Migrations/Version20250116114330.php | 32 +++++++++++++++ 7 files changed, 42 insertions(+), 132 deletions(-) delete mode 100644 src/Infrastructure/Persistence/Doctrine/Migrations/Version20250114151655.php delete mode 100644 src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115104546.php delete mode 100644 src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115164924.php create mode 100644 src/Infrastructure/Persistence/Doctrine/Migrations/Version20250116114330.php diff --git a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php index 3bd04bd1c..801c9bad3 100644 --- a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php +++ b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php @@ -22,8 +22,8 @@ public function __invoke(CreateRegulationOrderHistoryCommand $command): Regulati $regulationOrderHistory = $this->regulationOrderHistoryRepository->add( new RegulationOrderHistory( uuid: $this->idFactory->make(), - regulationOrder: $command->regulationOrder, - user: $command->user, + regulationOrderUuid: $command->regulationOrder->getUuid(), + userUuid: $command->user->getUuid(), action: $command->action, date: $this->now, ), diff --git a/src/Domain/Regulation/RegulationOrderHistory.php b/src/Domain/Regulation/RegulationOrderHistory.php index d16edd545..e8e78e684 100644 --- a/src/Domain/Regulation/RegulationOrderHistory.php +++ b/src/Domain/Regulation/RegulationOrderHistory.php @@ -4,14 +4,12 @@ namespace App\Domain\Regulation; -use App\Domain\User\User; - class RegulationOrderHistory { public function __construct( private string $uuid, - private RegulationOrder $regulationOrder, - private User $user, + private string $regulationOrderUuid, + private string $userUuid, private string $action, private \DateTimeInterface $date, ) { @@ -22,14 +20,14 @@ public function getUuid(): string return $this->uuid; } - public function getRegulationOrder(): RegulationOrder + public function getRegulationOrderUuid(): string { - return $this->regulationOrder; + return $this->regulationOrderUuid; } - public function getUser(): User + public function getUserUuid(): string { - return $this->user; + return $this->userUuid; } public function getAction(): string diff --git a/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml b/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml index 9a34530de..972e544a8 100644 --- a/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml +++ b/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml @@ -4,21 +4,13 @@ name="App\Domain\Regulation\RegulationOrderHistory" table="regulation_order_history"> + + - - - - - - - - - - diff --git a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250114151655.php b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250114151655.php deleted file mode 100644 index 19d16a157..000000000 --- a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250114151655.php +++ /dev/null @@ -1,36 +0,0 @@ -addSql('CREATE TABLE regulation_order_history (uuid UUID NOT NULL, regulation_order_uuid UUID NOT NULL, user_uuid UUID NOT NULL, action VARCHAR(20) NOT NULL, date TIMESTAMP(0) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY(uuid))'); - $this->addSql('CREATE INDEX IDX_E47F415B267E0D5E ON regulation_order_history (regulation_order_uuid)'); - $this->addSql('CREATE INDEX IDX_E47F415BABFE1C6F ON regulation_order_history (user_uuid)'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415B267E0D5E FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415BABFE1C6F FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); - } - - public function down(Schema $schema): void - { - $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415B267E0D5E'); - $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415BABFE1C6F'); - $this->addSql('DROP TABLE regulation_order_history'); - } -} diff --git a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115104546.php b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115104546.php deleted file mode 100644 index e14cfa55c..000000000 --- a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115104546.php +++ /dev/null @@ -1,36 +0,0 @@ -addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415B267E0D5E'); - $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415BABFE1C6F'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415B267E0D5E FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) NOT DEFERRABLE INITIALLY IMMEDIATE'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415BABFE1C6F FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) NOT DEFERRABLE INITIALLY IMMEDIATE'); - } - - public function down(Schema $schema): void - { - $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT fk_e47f415b267e0d5e'); - $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT fk_e47f415babfe1c6f'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT fk_e47f415b267e0d5e FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT fk_e47f415babfe1c6f FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); - } -} diff --git a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115164924.php b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115164924.php deleted file mode 100644 index 92e4087fd..000000000 --- a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250115164924.php +++ /dev/null @@ -1,40 +0,0 @@ -addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415B267E0D5E'); - $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT FK_E47F415BABFE1C6F'); - $this->addSql('ALTER TABLE regulation_order_history ALTER regulation_order_uuid DROP NOT NULL'); - $this->addSql('ALTER TABLE regulation_order_history ALTER user_uuid DROP NOT NULL'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415B267E0D5E FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT FK_E47F415BABFE1C6F FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE'); - } - - public function down(Schema $schema): void - { - $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT fk_e47f415b267e0d5e'); - $this->addSql('ALTER TABLE regulation_order_history DROP CONSTRAINT fk_e47f415babfe1c6f'); - $this->addSql('ALTER TABLE regulation_order_history ALTER regulation_order_uuid SET NOT NULL'); - $this->addSql('ALTER TABLE regulation_order_history ALTER user_uuid SET NOT NULL'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT fk_e47f415b267e0d5e FOREIGN KEY (regulation_order_uuid) REFERENCES regulation_order (uuid) NOT DEFERRABLE INITIALLY IMMEDIATE'); - $this->addSql('ALTER TABLE regulation_order_history ADD CONSTRAINT fk_e47f415babfe1c6f FOREIGN KEY (user_uuid) REFERENCES "user" (uuid) NOT DEFERRABLE INITIALLY IMMEDIATE'); - } -} diff --git a/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250116114330.php b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250116114330.php new file mode 100644 index 000000000..8b801ac73 --- /dev/null +++ b/src/Infrastructure/Persistence/Doctrine/Migrations/Version20250116114330.php @@ -0,0 +1,32 @@ +addSql('CREATE TABLE regulation_order_history (uuid UUID NOT NULL, regulation_order_uuid VARCHAR(50) NOT NULL, user_uuid VARCHAR(50) NOT NULL, action VARCHAR(20) NOT NULL, date TIMESTAMP(0) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY(uuid))'); + $this->addSql('CREATE INDEX IDX_E47F415B267E0D5E ON regulation_order_history (regulation_order_uuid)'); + $this->addSql('CREATE INDEX IDX_E47F415BABFE1C6F ON regulation_order_history (user_uuid)'); + } + + public function down(Schema $schema): void + { + $this->addSql('DROP TABLE regulation_order_history'); + } +} From 81ebbeaa489b4d6e75929cbf2cc9555e944dd007 Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Thu, 16 Jan 2025 15:18:53 +0100 Subject: [PATCH 04/10] publish action --- .../Command/PublishRegulationCommandHandler.php | 11 +++++++++++ src/Domain/Regulation/Enum/ActionTypeEnum.php | 1 + 2 files changed, 12 insertions(+) diff --git a/src/Application/Regulation/Command/PublishRegulationCommandHandler.php b/src/Application/Regulation/Command/PublishRegulationCommandHandler.php index eec785567..05f659102 100644 --- a/src/Application/Regulation/Command/PublishRegulationCommandHandler.php +++ b/src/Application/Regulation/Command/PublishRegulationCommandHandler.php @@ -4,14 +4,19 @@ namespace App\Application\Regulation\Command; +use App\Application\CommandBusInterface; +use App\Domain\Regulation\Enum\ActionTypeEnum; use App\Domain\Regulation\Enum\RegulationOrderRecordStatusEnum; use App\Domain\Regulation\Exception\RegulationOrderRecordCannotBePublishedException; use App\Domain\Regulation\Specification\CanRegulationOrderRecordBePublished; +use App\Infrastructure\Security\AuthenticatedUser; final class PublishRegulationCommandHandler { public function __construct( private CanRegulationOrderRecordBePublished $canRegulationOrderRecordBePublished, + private CommandBusInterface $commandBus, + private AuthenticatedUser $authenticatedUser, ) { } @@ -21,6 +26,12 @@ public function __invoke(PublishRegulationCommand $command): void throw new RegulationOrderRecordCannotBePublishedException(); } + $regulationOrder = $command->regulationOrderRecord->getRegulationOrder(); + $user = $this->authenticatedUser->getUser(); + $action = ActionTypeEnum::PUBLISH->value; + + $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action)); + $command->regulationOrderRecord->updateStatus(RegulationOrderRecordStatusEnum::PUBLISHED->value); } } diff --git a/src/Domain/Regulation/Enum/ActionTypeEnum.php b/src/Domain/Regulation/Enum/ActionTypeEnum.php index a39d03b99..6381e990c 100644 --- a/src/Domain/Regulation/Enum/ActionTypeEnum.php +++ b/src/Domain/Regulation/Enum/ActionTypeEnum.php @@ -8,5 +8,6 @@ enum ActionTypeEnum: string { case CREATE = 'create'; case UPDATE = 'update'; + case PUBLISH = 'publish'; case DELETE = 'delete'; } From 874c234f8e5c45e31f829679da6d76eb421ce385 Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Tue, 21 Jan 2025 11:05:59 +0100 Subject: [PATCH 05/10] test unit --- ...teRegulationOrderHistoryCommandHandler.php | 9 +- ...gulationOrderHistoryCommandHandlerTest.php | 91 +++++++++++++++++++ .../DeleteRegulationCommandHandlerTest.php | 20 +++- .../PublishRegulationCommandHandlerTest.php | 37 +++++++- ...egulationGeneralInfoCommandHandlerTest.php | 27 ++++++ 5 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php diff --git a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php index 801c9bad3..dd29f6e7e 100644 --- a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php +++ b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php @@ -4,16 +4,17 @@ namespace App\Application\Regulation\Command; +use App\Application\DateUtilsInterface; +use App\Application\IdFactoryInterface; use App\Domain\Regulation\RegulationOrderHistory; use App\Domain\Regulation\Repository\RegulationOrderHistoryRepositoryInterface; -use App\Infrastructure\Adapter\IdFactory; final class CreateRegulationOrderHistoryCommandHandler { public function __construct( - private IdFactory $idFactory, + private IdFactoryInterface $idFactory, private RegulationOrderHistoryRepositoryInterface $regulationOrderHistoryRepository, - private \DateTimeInterface $now, + private DateUtilsInterface $dateUtils, ) { } @@ -25,7 +26,7 @@ public function __invoke(CreateRegulationOrderHistoryCommand $command): Regulati regulationOrderUuid: $command->regulationOrder->getUuid(), userUuid: $command->user->getUuid(), action: $command->action, - date: $this->now, + date: $this->dateUtils->getNow(), ), ); diff --git a/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php b/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php new file mode 100644 index 000000000..5059a897c --- /dev/null +++ b/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php @@ -0,0 +1,91 @@ +idFactory = $this->createMock(IdFactoryInterface::class); + $this->regulationOrderHistoryRepository = $this->createMock(RegulationOrderHistoryRepositoryInterface::class); + $this->dateUtils = $this->createMock(DateUtilsInterface::class); + } + + public function testCreate(): void + { + $now = new \DateTimeImmutable('2023-06-13'); + + $this->dateUtils + ->expects(self::once()) + ->method('getNow') + ->willReturn($now); + + $this->idFactory + ->expects(self::once()) + ->method('make') + ->willReturn('d035fec0-30f3-4134-95b9-d74c68eb53e3'); + + $regulationOrder = $this->createMock(RegulationOrder::class); + $user = $this->createMock(User::class); + $action = ActionTypeEnum::PUBLISH->value; + + $regulationOrder + ->expects(self::once()) + ->method('getUuid') + ->willReturn('9ddd73e5-2162-4279-be73-183816e7f85b'); + + $user + ->expects(self::once()) + ->method('getUuid') + ->willReturn('3cc78eae-50ba-4805-9b75-7f64ca638caf'); + + $createdRegulationOrderHistory = $this->createMock(RegulationOrderHistory::class); + + $this->regulationOrderHistoryRepository + ->expects(self::once()) + ->method('add') + ->with( + $this->equalTo( + new RegulationOrderHistory( + uuid: 'd035fec0-30f3-4134-95b9-d74c68eb53e3', + regulationOrderUuid: '9ddd73e5-2162-4279-be73-183816e7f85b', + userUuid: '3cc78eae-50ba-4805-9b75-7f64ca638caf', + action: $action, + date: $now, + ), + ), + ) + ->willReturn($createdRegulationOrderHistory); + + $handler = new CreateRegulationOrderHistoryCommandHandler( + $this->idFactory, + $this->regulationOrderHistoryRepository, + $this->dateUtils, + ); + + $command = new CreateRegulationOrderHistoryCommand($regulationOrder, + $user, + $action); + + $result = $handler($command); + + $this->assertSame($createdRegulationOrderHistory, $result); + } +} diff --git a/tests/Unit/Application/Regulation/Command/DeleteRegulationCommandHandlerTest.php b/tests/Unit/Application/Regulation/Command/DeleteRegulationCommandHandlerTest.php index abb7866b0..7b472d101 100644 --- a/tests/Unit/Application/Regulation/Command/DeleteRegulationCommandHandlerTest.php +++ b/tests/Unit/Application/Regulation/Command/DeleteRegulationCommandHandlerTest.php @@ -4,6 +4,7 @@ namespace App\Tests\Unit\Application\Regulation\Command; +use App\Application\CommandBusInterface; use App\Application\Regulation\Command\DeleteRegulationCommand; use App\Application\Regulation\Command\DeleteRegulationCommandHandler; use App\Domain\Regulation\Exception\RegulationOrderRecordCannotBeDeletedException; @@ -12,6 +13,8 @@ use App\Domain\Regulation\Repository\RegulationOrderRepositoryInterface; use App\Domain\Regulation\Specification\CanOrganizationAccessToRegulation; use App\Domain\User\Organization; +use App\Domain\User\User; +use App\Infrastructure\Security\AuthenticatedUser; use PHPUnit\Framework\TestCase; final class DeleteRegulationCommandHandlerTest extends TestCase @@ -21,6 +24,8 @@ final class DeleteRegulationCommandHandlerTest extends TestCase private $regulationOrderRepository; private $regulationOrderRecord; private $regulationOrder; + private $commandBus; + private $authenticatedUser; protected function setUp(): void { @@ -29,11 +34,14 @@ protected function setUp(): void $this->regulationOrderRepository = $this->createMock(RegulationOrderRepositoryInterface::class); $this->regulationOrderRecord = $this->createMock(RegulationOrderRecord::class); $this->regulationOrder = $this->createMock(RegulationOrder::class); + $this->commandBus = $this->createMock(CommandBusInterface::class); + $this->authenticatedUser = $this->createMock(AuthenticatedUser::class); } public function testDelete(): void { $organization = $this->createMock(Organization::class); + $user = $this->createMock(User::class); $this->canOrganizationAccessToRegulation ->expects(self::once()) @@ -51,9 +59,15 @@ public function testDelete(): void ->method('getRegulationOrder') ->willReturn($this->regulationOrder); + $this->authenticatedUser + ->expects(self::once()) + ->method('getUser') + ->willReturn($user); + $handler = new DeleteRegulationCommandHandler( $this->regulationOrderRepository, - $this->canOrganizationAccessToRegulation, + $this->canOrganizationAccessToRegulation, $this->commandBus, + $this->authenticatedUser, ); $command = new DeleteRegulationCommand($this->organizationUuids, $this->regulationOrderRecord); @@ -77,12 +91,14 @@ public function testCannotDelete(): void ->method('delete'); $this->regulationOrderRecord - ->expects(self::never()) + ->expects(self::once()) ->method('getRegulationOrder'); $handler = new DeleteRegulationCommandHandler( $this->regulationOrderRepository, $this->canOrganizationAccessToRegulation, + $this->commandBus, + $this->authenticatedUser, ); $command = new DeleteRegulationCommand($this->organizationUuids, $this->regulationOrderRecord); diff --git a/tests/Unit/Application/Regulation/Command/PublishRegulationCommandHandlerTest.php b/tests/Unit/Application/Regulation/Command/PublishRegulationCommandHandlerTest.php index 7c4b875a8..9508ccdb6 100644 --- a/tests/Unit/Application/Regulation/Command/PublishRegulationCommandHandlerTest.php +++ b/tests/Unit/Application/Regulation/Command/PublishRegulationCommandHandlerTest.php @@ -4,37 +4,70 @@ namespace App\Tests\Unit\Application\Regulation\Command; +use App\Application\CommandBusInterface; +use App\Application\Regulation\Command\CreateRegulationOrderHistoryCommand; use App\Application\Regulation\Command\PublishRegulationCommand; use App\Application\Regulation\Command\PublishRegulationCommandHandler; +use App\Domain\Regulation\Enum\ActionTypeEnum; use App\Domain\Regulation\Exception\RegulationOrderRecordCannotBePublishedException; +use App\Domain\Regulation\RegulationOrder; +use App\Domain\Regulation\RegulationOrderHistory; use App\Domain\Regulation\RegulationOrderRecord; use App\Domain\Regulation\Specification\CanRegulationOrderRecordBePublished; +use App\Domain\User\User; +use App\Infrastructure\Security\AuthenticatedUser; use PHPUnit\Framework\TestCase; final class PublishRegulationCommandHandlerTest extends TestCase { private $canRegulationOrderRecordBePublished; + private $commandBus; + private $authenticatedUser; protected function setUp(): void { $this->canRegulationOrderRecordBePublished = $this->createMock(CanRegulationOrderRecordBePublished::class); + $this->commandBus = $this->createMock(CommandBusInterface::class); + $this->authenticatedUser = $this->createMock(AuthenticatedUser::class); } public function testPublish(): void { + $createdRegulationOrderHistory = $this->createMock(RegulationOrderHistory::class); + $regulationOrder = $this->createMock(RegulationOrder::class); + $user = $this->createMock(User::class); $regulationOrderRecord = $this->createMock(RegulationOrderRecord::class); + $regulationOrderRecord ->expects(self::once()) ->method('updateStatus') ->with('published'); + $regulationOrderRecord + ->expects(self::once()) + ->method('getRegulationOrder') + ->willReturn($regulationOrder); + + $this->authenticatedUser + ->expects(self::once()) + ->method('getUser') + ->willReturn($user); + $this->canRegulationOrderRecordBePublished ->expects(self::once()) ->method('isSatisfiedBy') ->willReturn(true); + $action = ActionTypeEnum::PUBLISH->value; + $regulationOrderHistoryCommand = new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action); + + $this->commandBus + ->expects(self::once()) + ->method('handle') + ->with($this->equalTo($regulationOrderHistoryCommand)); + $handler = new PublishRegulationCommandHandler( - $this->canRegulationOrderRecordBePublished, + $this->canRegulationOrderRecordBePublished, $this->commandBus, $this->authenticatedUser, ); $command = new PublishRegulationCommand($regulationOrderRecord); @@ -57,7 +90,7 @@ public function testRegulationCannotBePublished(): void ->willReturn(false); $handler = new PublishRegulationCommandHandler( - $this->canRegulationOrderRecordBePublished, + $this->canRegulationOrderRecordBePublished, $this->commandBus, $this->authenticatedUser, ); $command = new PublishRegulationCommand($regulationOrderRecord); diff --git a/tests/Unit/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandlerTest.php b/tests/Unit/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandlerTest.php index ae60202d7..0ab185883 100644 --- a/tests/Unit/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandlerTest.php +++ b/tests/Unit/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandlerTest.php @@ -4,12 +4,15 @@ namespace App\Tests\Unit\Application\Regulation\Command; +use App\Application\CommandBusInterface; use App\Application\IdFactoryInterface; use App\Application\Organization\VisaModel\Query\GetVisaModelQuery; use App\Application\QueryBusInterface; +use App\Application\Regulation\Command\CreateRegulationOrderHistoryCommand; use App\Application\Regulation\Command\SaveRegulationGeneralInfoCommand; use App\Application\Regulation\Command\SaveRegulationGeneralInfoCommandHandler; use App\Domain\Organization\VisaModel\VisaModel; +use App\Domain\Regulation\Enum\ActionTypeEnum; use App\Domain\Regulation\Enum\RegulationOrderCategoryEnum; use App\Domain\Regulation\Enum\RegulationOrderRecordSourceEnum; use App\Domain\Regulation\Enum\RegulationOrderRecordStatusEnum; @@ -19,6 +22,8 @@ use App\Domain\Regulation\Repository\RegulationOrderRecordRepositoryInterface; use App\Domain\Regulation\Repository\RegulationOrderRepositoryInterface; use App\Domain\User\Organization; +use App\Domain\User\User; +use App\Infrastructure\Security\AuthenticatedUser; use PHPUnit\Framework\TestCase; final class SaveRegulationGeneralInfoCommandHandlerTest extends TestCase @@ -29,6 +34,9 @@ final class SaveRegulationGeneralInfoCommandHandlerTest extends TestCase private $queryBus; private $organization; private $visaModel; + private $commandBus; + private $authenticatedUser; + private $user; public function setUp(): void { @@ -38,6 +46,14 @@ public function setUp(): void $this->queryBus = $this->createMock(QueryBusInterface::class); $this->organization = $this->createMock(Organization::class); $this->visaModel = $this->createMock(VisaModel::class); + $this->commandBus = $this->createMock(CommandBusInterface::class); + $this->authenticatedUser = $this->createMock(AuthenticatedUser::class); + $this->user = $this->createMock(User::class); + + $this->authenticatedUser + ->expects(self::once()) + ->method('getUser') + ->willReturn($this->user); } public function testCreate(): void @@ -98,6 +114,8 @@ public function testCreate(): void $this->regulationOrderRecordRepository, $now, $this->queryBus, + $this->commandBus, + $this->authenticatedUser, ); $command = new SaveRegulationGeneralInfoCommand(); @@ -165,12 +183,21 @@ public function testUpdate(): void ->method('updateOrganization') ->with($organization); + $action = ActionTypeEnum::UPDATE->value; + $regulationOrderHistoryCommand = new CreateRegulationOrderHistoryCommand($regulationOrder, $this->user, $action); + + $this->commandBus + ->expects(self::once()) + ->method('handle') + ->with($this->equalTo($regulationOrderHistoryCommand)); $handler = new SaveRegulationGeneralInfoCommandHandler( $this->idFactory, $this->regulationOrderRepository, $this->regulationOrderRecordRepository, $now, $this->queryBus, + $this->commandBus, + $this->authenticatedUser, ); $command = new SaveRegulationGeneralInfoCommand($regulationOrderRecord); From 7ff9170de6bef379b09ba801e200fbb2dead4c5a Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Tue, 21 Jan 2025 14:43:53 +0100 Subject: [PATCH 06/10] correction mapping --- .../Mapping/Regulation.RegulationOrderHistory.orm.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml b/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml index 972e544a8..27a2715c8 100644 --- a/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml +++ b/src/Infrastructure/Persistence/Doctrine/Mapping/Regulation.RegulationOrderHistory.orm.xml @@ -3,6 +3,10 @@ + + + + From 846c1bb316ea33a752257d819664d1d20ef67f5b Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Wed, 22 Jan 2025 15:31:08 +0100 Subject: [PATCH 07/10] correction after review --- .../CreateRegulationOrderHistoryCommand.php | 2 -- .../CreateRegulationOrderHistoryCommandHandler.php | 4 +++- .../Command/DeleteRegulationCommandHandler.php | 7 +------ .../Command/PublishRegulationCommandHandler.php | 6 +----- .../SaveRegulationGeneralInfoCommandHandler.php | 11 ++--------- ...ateRegulationOrderHistoryCommandHandlerTest.php | 13 ++++++++++--- .../Command/DeleteRegulationCommandHandlerTest.php | 10 ---------- .../PublishRegulationCommandHandlerTest.php | 14 +++----------- ...SaveRegulationGeneralInfoCommandHandlerTest.php | 13 +------------ 9 files changed, 21 insertions(+), 59 deletions(-) diff --git a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommand.php b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommand.php index 3fdbd653a..a7600367c 100644 --- a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommand.php +++ b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommand.php @@ -6,13 +6,11 @@ use App\Application\CommandInterface; use App\Domain\Regulation\RegulationOrder; -use App\Domain\User\User; final class CreateRegulationOrderHistoryCommand implements CommandInterface { public function __construct( public RegulationOrder $regulationOrder, - public User $user, public string $action, ) { } diff --git a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php index dd29f6e7e..9538e8b16 100644 --- a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php +++ b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php @@ -8,6 +8,7 @@ use App\Application\IdFactoryInterface; use App\Domain\Regulation\RegulationOrderHistory; use App\Domain\Regulation\Repository\RegulationOrderHistoryRepositoryInterface; +use App\Infrastructure\Security\AuthenticatedUser; final class CreateRegulationOrderHistoryCommandHandler { @@ -15,6 +16,7 @@ public function __construct( private IdFactoryInterface $idFactory, private RegulationOrderHistoryRepositoryInterface $regulationOrderHistoryRepository, private DateUtilsInterface $dateUtils, + private AuthenticatedUser $authenticatedUser, ) { } @@ -24,7 +26,7 @@ public function __invoke(CreateRegulationOrderHistoryCommand $command): Regulati new RegulationOrderHistory( uuid: $this->idFactory->make(), regulationOrderUuid: $command->regulationOrder->getUuid(), - userUuid: $command->user->getUuid(), + userUuid: $this->authenticatedUser->getUser()->getUuid(), action: $command->action, date: $this->dateUtils->getNow(), ), diff --git a/src/Application/Regulation/Command/DeleteRegulationCommandHandler.php b/src/Application/Regulation/Command/DeleteRegulationCommandHandler.php index 039091c52..cfab439b0 100644 --- a/src/Application/Regulation/Command/DeleteRegulationCommandHandler.php +++ b/src/Application/Regulation/Command/DeleteRegulationCommandHandler.php @@ -9,7 +9,6 @@ use App\Domain\Regulation\Exception\RegulationOrderRecordCannotBeDeletedException; use App\Domain\Regulation\Repository\RegulationOrderRepositoryInterface; use App\Domain\Regulation\Specification\CanOrganizationAccessToRegulation; -use App\Infrastructure\Security\AuthenticatedUser; final class DeleteRegulationCommandHandler { @@ -17,7 +16,6 @@ public function __construct( private RegulationOrderRepositoryInterface $regulationOrderRepository, private CanOrganizationAccessToRegulation $canOrganizationAccessToRegulation, private CommandBusInterface $commandBus, - private AuthenticatedUser $authenticatedUser, ) { } @@ -25,15 +23,12 @@ public function __invoke(DeleteRegulationCommand $command): void { $regulationOrderRecord = $command->regulationOrderRecord; $regulationOrder = $regulationOrderRecord->getRegulationOrder(); - $user = $this->authenticatedUser->getUser(); if (false === $this->canOrganizationAccessToRegulation->isSatisfiedBy($regulationOrderRecord, $command->userOrganizationUuids)) { throw new RegulationOrderRecordCannotBeDeletedException(); } - $action = ActionTypeEnum::DELETE->value; - - $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action)); + $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, ActionTypeEnum::DELETE->value)); $this->regulationOrderRepository->delete($regulationOrder); } diff --git a/src/Application/Regulation/Command/PublishRegulationCommandHandler.php b/src/Application/Regulation/Command/PublishRegulationCommandHandler.php index 05f659102..2bb1c56c2 100644 --- a/src/Application/Regulation/Command/PublishRegulationCommandHandler.php +++ b/src/Application/Regulation/Command/PublishRegulationCommandHandler.php @@ -9,14 +9,12 @@ use App\Domain\Regulation\Enum\RegulationOrderRecordStatusEnum; use App\Domain\Regulation\Exception\RegulationOrderRecordCannotBePublishedException; use App\Domain\Regulation\Specification\CanRegulationOrderRecordBePublished; -use App\Infrastructure\Security\AuthenticatedUser; final class PublishRegulationCommandHandler { public function __construct( private CanRegulationOrderRecordBePublished $canRegulationOrderRecordBePublished, private CommandBusInterface $commandBus, - private AuthenticatedUser $authenticatedUser, ) { } @@ -27,10 +25,8 @@ public function __invoke(PublishRegulationCommand $command): void } $regulationOrder = $command->regulationOrderRecord->getRegulationOrder(); - $user = $this->authenticatedUser->getUser(); - $action = ActionTypeEnum::PUBLISH->value; - $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action)); + $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, ActionTypeEnum::PUBLISH->value)); $command->regulationOrderRecord->updateStatus(RegulationOrderRecordStatusEnum::PUBLISHED->value); } diff --git a/src/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandler.php b/src/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandler.php index dc5f0f7e9..b4f51bd4d 100644 --- a/src/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandler.php +++ b/src/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandler.php @@ -14,7 +14,6 @@ use App\Domain\Regulation\RegulationOrderRecord; use App\Domain\Regulation\Repository\RegulationOrderRecordRepositoryInterface; use App\Domain\Regulation\Repository\RegulationOrderRepositoryInterface; -use App\Infrastructure\Security\AuthenticatedUser; final class SaveRegulationGeneralInfoCommandHandler { @@ -25,13 +24,11 @@ public function __construct( private \DateTimeInterface $now, private QueryBusInterface $queryBus, private CommandBusInterface $commandBus, - private AuthenticatedUser $authenticatedUser, ) { } public function __invoke(SaveRegulationGeneralInfoCommand $command): RegulationOrderRecord { - $user = $this->authenticatedUser->getUser(); $command->cleanOtherCategoryText(); $visaModel = $command->visaModelUuid ? $this->queryBus->handle(new GetVisaModelQuery($command->visaModelUuid)) @@ -63,9 +60,7 @@ public function __invoke(SaveRegulationGeneralInfoCommand $command): RegulationO ), ); - $action = ActionTypeEnum::CREATE->value; - - $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action)); + $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, ActionTypeEnum::CREATE->value)); return $regulationOrderRecord; } @@ -85,9 +80,7 @@ public function __invoke(SaveRegulationGeneralInfoCommand $command): RegulationO visaModel: $visaModel, ); - $action = ActionTypeEnum::UPDATE->value; - - $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action)); + $this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, ActionTypeEnum::UPDATE->value)); return $command->regulationOrderRecord; } diff --git a/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php b/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php index 5059a897c..3286168c7 100644 --- a/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php +++ b/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php @@ -13,6 +13,7 @@ use App\Domain\Regulation\RegulationOrderHistory; use App\Domain\Regulation\Repository\RegulationOrderHistoryRepositoryInterface; use App\Domain\User\User; +use App\Infrastructure\Security\AuthenticatedUser; use AsyncAws\Core\Test\TestCase; final class CreateRegulationOrderHistoryCommandHandlerTest extends TestCase @@ -20,12 +21,14 @@ final class CreateRegulationOrderHistoryCommandHandlerTest extends TestCase private $idFactory; private $regulationOrderHistoryRepository; private $dateUtils; + private $authenticatedUser; public function SetUp(): void { $this->idFactory = $this->createMock(IdFactoryInterface::class); $this->regulationOrderHistoryRepository = $this->createMock(RegulationOrderHistoryRepositoryInterface::class); $this->dateUtils = $this->createMock(DateUtilsInterface::class); + $this->authenticatedUser = $this->createMock(AuthenticatedUser::class); } public function testCreate(): void @@ -51,6 +54,11 @@ public function testCreate(): void ->method('getUuid') ->willReturn('9ddd73e5-2162-4279-be73-183816e7f85b'); + $this->authenticatedUser + ->expects(self::once()) + ->method('getUser') + ->willReturn($user); + $user ->expects(self::once()) ->method('getUuid') @@ -78,11 +86,10 @@ public function testCreate(): void $this->idFactory, $this->regulationOrderHistoryRepository, $this->dateUtils, + $this->authenticatedUser, ); - $command = new CreateRegulationOrderHistoryCommand($regulationOrder, - $user, - $action); + $command = new CreateRegulationOrderHistoryCommand($regulationOrder, $action); $result = $handler($command); diff --git a/tests/Unit/Application/Regulation/Command/DeleteRegulationCommandHandlerTest.php b/tests/Unit/Application/Regulation/Command/DeleteRegulationCommandHandlerTest.php index 7b472d101..d2847e584 100644 --- a/tests/Unit/Application/Regulation/Command/DeleteRegulationCommandHandlerTest.php +++ b/tests/Unit/Application/Regulation/Command/DeleteRegulationCommandHandlerTest.php @@ -14,7 +14,6 @@ use App\Domain\Regulation\Specification\CanOrganizationAccessToRegulation; use App\Domain\User\Organization; use App\Domain\User\User; -use App\Infrastructure\Security\AuthenticatedUser; use PHPUnit\Framework\TestCase; final class DeleteRegulationCommandHandlerTest extends TestCase @@ -25,7 +24,6 @@ final class DeleteRegulationCommandHandlerTest extends TestCase private $regulationOrderRecord; private $regulationOrder; private $commandBus; - private $authenticatedUser; protected function setUp(): void { @@ -35,7 +33,6 @@ protected function setUp(): void $this->regulationOrderRecord = $this->createMock(RegulationOrderRecord::class); $this->regulationOrder = $this->createMock(RegulationOrder::class); $this->commandBus = $this->createMock(CommandBusInterface::class); - $this->authenticatedUser = $this->createMock(AuthenticatedUser::class); } public function testDelete(): void @@ -59,15 +56,9 @@ public function testDelete(): void ->method('getRegulationOrder') ->willReturn($this->regulationOrder); - $this->authenticatedUser - ->expects(self::once()) - ->method('getUser') - ->willReturn($user); - $handler = new DeleteRegulationCommandHandler( $this->regulationOrderRepository, $this->canOrganizationAccessToRegulation, $this->commandBus, - $this->authenticatedUser, ); $command = new DeleteRegulationCommand($this->organizationUuids, $this->regulationOrderRecord); @@ -98,7 +89,6 @@ public function testCannotDelete(): void $this->regulationOrderRepository, $this->canOrganizationAccessToRegulation, $this->commandBus, - $this->authenticatedUser, ); $command = new DeleteRegulationCommand($this->organizationUuids, $this->regulationOrderRecord); diff --git a/tests/Unit/Application/Regulation/Command/PublishRegulationCommandHandlerTest.php b/tests/Unit/Application/Regulation/Command/PublishRegulationCommandHandlerTest.php index 9508ccdb6..f66ad31d5 100644 --- a/tests/Unit/Application/Regulation/Command/PublishRegulationCommandHandlerTest.php +++ b/tests/Unit/Application/Regulation/Command/PublishRegulationCommandHandlerTest.php @@ -15,20 +15,17 @@ use App\Domain\Regulation\RegulationOrderRecord; use App\Domain\Regulation\Specification\CanRegulationOrderRecordBePublished; use App\Domain\User\User; -use App\Infrastructure\Security\AuthenticatedUser; use PHPUnit\Framework\TestCase; final class PublishRegulationCommandHandlerTest extends TestCase { private $canRegulationOrderRecordBePublished; private $commandBus; - private $authenticatedUser; protected function setUp(): void { $this->canRegulationOrderRecordBePublished = $this->createMock(CanRegulationOrderRecordBePublished::class); $this->commandBus = $this->createMock(CommandBusInterface::class); - $this->authenticatedUser = $this->createMock(AuthenticatedUser::class); } public function testPublish(): void @@ -48,18 +45,13 @@ public function testPublish(): void ->method('getRegulationOrder') ->willReturn($regulationOrder); - $this->authenticatedUser - ->expects(self::once()) - ->method('getUser') - ->willReturn($user); - $this->canRegulationOrderRecordBePublished ->expects(self::once()) ->method('isSatisfiedBy') ->willReturn(true); $action = ActionTypeEnum::PUBLISH->value; - $regulationOrderHistoryCommand = new CreateRegulationOrderHistoryCommand($regulationOrder, $user, $action); + $regulationOrderHistoryCommand = new CreateRegulationOrderHistoryCommand($regulationOrder, $action); $this->commandBus ->expects(self::once()) @@ -67,7 +59,7 @@ public function testPublish(): void ->with($this->equalTo($regulationOrderHistoryCommand)); $handler = new PublishRegulationCommandHandler( - $this->canRegulationOrderRecordBePublished, $this->commandBus, $this->authenticatedUser, + $this->canRegulationOrderRecordBePublished, $this->commandBus, ); $command = new PublishRegulationCommand($regulationOrderRecord); @@ -90,7 +82,7 @@ public function testRegulationCannotBePublished(): void ->willReturn(false); $handler = new PublishRegulationCommandHandler( - $this->canRegulationOrderRecordBePublished, $this->commandBus, $this->authenticatedUser, + $this->canRegulationOrderRecordBePublished, $this->commandBus, ); $command = new PublishRegulationCommand($regulationOrderRecord); diff --git a/tests/Unit/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandlerTest.php b/tests/Unit/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandlerTest.php index 0ab185883..df93dc4de 100644 --- a/tests/Unit/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandlerTest.php +++ b/tests/Unit/Application/Regulation/Command/SaveRegulationGeneralInfoCommandHandlerTest.php @@ -23,7 +23,6 @@ use App\Domain\Regulation\Repository\RegulationOrderRepositoryInterface; use App\Domain\User\Organization; use App\Domain\User\User; -use App\Infrastructure\Security\AuthenticatedUser; use PHPUnit\Framework\TestCase; final class SaveRegulationGeneralInfoCommandHandlerTest extends TestCase @@ -35,7 +34,6 @@ final class SaveRegulationGeneralInfoCommandHandlerTest extends TestCase private $organization; private $visaModel; private $commandBus; - private $authenticatedUser; private $user; public function setUp(): void @@ -47,13 +45,7 @@ public function setUp(): void $this->organization = $this->createMock(Organization::class); $this->visaModel = $this->createMock(VisaModel::class); $this->commandBus = $this->createMock(CommandBusInterface::class); - $this->authenticatedUser = $this->createMock(AuthenticatedUser::class); $this->user = $this->createMock(User::class); - - $this->authenticatedUser - ->expects(self::once()) - ->method('getUser') - ->willReturn($this->user); } public function testCreate(): void @@ -115,7 +107,6 @@ public function testCreate(): void $now, $this->queryBus, $this->commandBus, - $this->authenticatedUser, ); $command = new SaveRegulationGeneralInfoCommand(); @@ -183,8 +174,7 @@ public function testUpdate(): void ->method('updateOrganization') ->with($organization); - $action = ActionTypeEnum::UPDATE->value; - $regulationOrderHistoryCommand = new CreateRegulationOrderHistoryCommand($regulationOrder, $this->user, $action); + $regulationOrderHistoryCommand = new CreateRegulationOrderHistoryCommand($regulationOrder, ActionTypeEnum::UPDATE->value); $this->commandBus ->expects(self::once()) @@ -197,7 +187,6 @@ public function testUpdate(): void $now, $this->queryBus, $this->commandBus, - $this->authenticatedUser, ); $command = new SaveRegulationGeneralInfoCommand($regulationOrderRecord); From cd006838ac58af5190401773ecd0113347d58d38 Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Thu, 23 Jan 2025 11:23:59 +0100 Subject: [PATCH 08/10] no regulation order history when integration --- .../CreateRegulationOrderHistoryCommandHandler.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php index 9538e8b16..1ff2618a4 100644 --- a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php +++ b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php @@ -20,9 +20,16 @@ public function __construct( ) { } - public function __invoke(CreateRegulationOrderHistoryCommand $command): RegulationOrderHistory + public function __invoke(CreateRegulationOrderHistoryCommand $command): void { - $regulationOrderHistory = $this->regulationOrderHistoryRepository->add( + // La seule possibilité d'exécuter cette commande sans être authentifié, c'est + // d'avoir lancé une commande Symfony en ligne de commande. + // Dans ce cas on n'a pas d'utilisateur connecté donc on ne stocke pas d'historique. + + if (!$this->authenticatedUser->getUser()) { + return; + } + $this->regulationOrderHistoryRepository->add( new RegulationOrderHistory( uuid: $this->idFactory->make(), regulationOrderUuid: $command->regulationOrder->getUuid(), @@ -31,7 +38,5 @@ public function __invoke(CreateRegulationOrderHistoryCommand $command): Regulati date: $this->dateUtils->getNow(), ), ); - - return $regulationOrderHistory; } } From 1743c4cc2650db71f33e6cd48a7145138c2262ab Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Thu, 23 Jan 2025 11:53:18 +0100 Subject: [PATCH 09/10] test --- ...CreateRegulationOrderHistoryCommandHandlerTest.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php b/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php index 3286168c7..aafcdb99e 100644 --- a/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php +++ b/tests/Unit/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandlerTest.php @@ -55,7 +55,7 @@ public function testCreate(): void ->willReturn('9ddd73e5-2162-4279-be73-183816e7f85b'); $this->authenticatedUser - ->expects(self::once()) + ->expects(self::exactly(2)) ->method('getUser') ->willReturn($user); @@ -64,8 +64,6 @@ public function testCreate(): void ->method('getUuid') ->willReturn('3cc78eae-50ba-4805-9b75-7f64ca638caf'); - $createdRegulationOrderHistory = $this->createMock(RegulationOrderHistory::class); - $this->regulationOrderHistoryRepository ->expects(self::once()) ->method('add') @@ -79,8 +77,7 @@ public function testCreate(): void date: $now, ), ), - ) - ->willReturn($createdRegulationOrderHistory); + ); $handler = new CreateRegulationOrderHistoryCommandHandler( $this->idFactory, @@ -91,8 +88,6 @@ public function testCreate(): void $command = new CreateRegulationOrderHistoryCommand($regulationOrder, $action); - $result = $handler($command); - - $this->assertSame($createdRegulationOrderHistory, $result); + $handler($command); } } From cdce4482e593c4f81c4b6a2c1c0a100c322bdf72 Mon Sep 17 00:00:00 2001 From: Lealefoulon Date: Thu, 23 Jan 2025 15:20:13 +0100 Subject: [PATCH 10/10] tests getter --- ...teRegulationOrderHistoryCommandHandler.php | 2 +- .../Regulation/RegulationOrderHistoryTest.php | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Domain/Regulation/RegulationOrderHistoryTest.php diff --git a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php index 1ff2618a4..ccf341b5a 100644 --- a/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php +++ b/src/Application/Regulation/Command/CreateRegulationOrderHistoryCommandHandler.php @@ -25,10 +25,10 @@ public function __invoke(CreateRegulationOrderHistoryCommand $command): void // La seule possibilité d'exécuter cette commande sans être authentifié, c'est // d'avoir lancé une commande Symfony en ligne de commande. // Dans ce cas on n'a pas d'utilisateur connecté donc on ne stocke pas d'historique. - if (!$this->authenticatedUser->getUser()) { return; } + $this->regulationOrderHistoryRepository->add( new RegulationOrderHistory( uuid: $this->idFactory->make(), diff --git a/tests/Unit/Domain/Regulation/RegulationOrderHistoryTest.php b/tests/Unit/Domain/Regulation/RegulationOrderHistoryTest.php new file mode 100644 index 000000000..e0db7de82 --- /dev/null +++ b/tests/Unit/Domain/Regulation/RegulationOrderHistoryTest.php @@ -0,0 +1,30 @@ +value, + date: $date, + ); + + $this->assertSame('6598fd41-85cb-42a6-9693-1bc45f4dd392', $regulationOrderHistory->getUuid()); + $this->assertSame('f518b777-aa4e-4e8e-852b-15a64c849198', $regulationOrderHistory->getRegulationOrderUuid()); + $this->assertSame('6659bfb5-1e51-4000-99de-68e3d9a90a69', $regulationOrderHistory->getUserUuid()); + $this->assertSame('create', $regulationOrderHistory->getAction()); + $this->assertSame($date, $regulationOrderHistory->getDate()); + } +}