Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traçabilité des modifications sur un arrêté #1147

Merged
merged 17 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Application\Regulation\Command;

use App\Application\CommandInterface;
use App\Domain\Regulation\RegulationOrder;

final class CreateRegulationOrderHistoryCommand implements CommandInterface
{
public function __construct(
public RegulationOrder $regulationOrder,
public string $action,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

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\Security\AuthenticatedUser;

final class CreateRegulationOrderHistoryCommandHandler
{
public function __construct(
private IdFactoryInterface $idFactory,
private RegulationOrderHistoryRepositoryInterface $regulationOrderHistoryRepository,
private DateUtilsInterface $dateUtils,
private AuthenticatedUser $authenticatedUser,
) {
}

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;
}
Lealefoulon marked this conversation as resolved.
Show resolved Hide resolved

$this->regulationOrderHistoryRepository->add(
new RegulationOrderHistory(
uuid: $this->idFactory->make(),
regulationOrderUuid: $command->regulationOrder->getUuid(),
userUuid: $this->authenticatedUser->getUser()->getUuid(),
action: $command->action,
date: $this->dateUtils->getNow(),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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;
Expand All @@ -13,17 +15,21 @@ final class DeleteRegulationCommandHandler
public function __construct(
private RegulationOrderRepositoryInterface $regulationOrderRepository,
private CanOrganizationAccessToRegulation $canOrganizationAccessToRegulation,
private CommandBusInterface $commandBus,
) {
}

public function __invoke(DeleteRegulationCommand $command): void
{
$regulationOrderRecord = $command->regulationOrderRecord;
$regulationOrder = $regulationOrderRecord->getRegulationOrder();

if (false === $this->canOrganizationAccessToRegulation->isSatisfiedBy($regulationOrderRecord, $command->userOrganizationUuids)) {
throw new RegulationOrderRecordCannotBeDeletedException();
}

$this->regulationOrderRepository->delete($regulationOrderRecord->getRegulationOrder());
$this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, ActionTypeEnum::DELETE->value));

$this->regulationOrderRepository->delete($regulationOrder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

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;
Expand All @@ -12,6 +14,7 @@ final class PublishRegulationCommandHandler
{
public function __construct(
private CanRegulationOrderRecordBePublished $canRegulationOrderRecordBePublished,
private CommandBusInterface $commandBus,
) {
}

Expand All @@ -21,6 +24,10 @@ public function __invoke(PublishRegulationCommand $command): void
throw new RegulationOrderRecordCannotBePublishedException();
}

$regulationOrder = $command->regulationOrderRecord->getRegulationOrder();

$this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, ActionTypeEnum::PUBLISH->value));

$command->regulationOrderRecord->updateStatus(RegulationOrderRecordStatusEnum::PUBLISHED->value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

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;
Expand All @@ -21,6 +23,7 @@ public function __construct(
private RegulationOrderRecordRepositoryInterface $regulationOrderRecordRepository,
private \DateTimeInterface $now,
private QueryBusInterface $queryBus,
private CommandBusInterface $commandBus,
) {
}

Expand Down Expand Up @@ -57,11 +60,16 @@ public function __invoke(SaveRegulationGeneralInfoCommand $command): RegulationO
),
);

$this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, ActionTypeEnum::CREATE->value));

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,
Expand All @@ -72,6 +80,8 @@ public function __invoke(SaveRegulationGeneralInfoCommand $command): RegulationO
visaModel: $visaModel,
);

$this->commandBus->handle(new CreateRegulationOrderHistoryCommand($regulationOrder, ActionTypeEnum::UPDATE->value));

return $command->regulationOrderRecord;
}
}
13 changes: 13 additions & 0 deletions src/Domain/Regulation/Enum/ActionTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Domain\Regulation\Enum;

enum ActionTypeEnum: string
{
case CREATE = 'create';
case UPDATE = 'update';
case PUBLISH = 'publish';
case DELETE = 'delete';
}
42 changes: 42 additions & 0 deletions src/Domain/Regulation/RegulationOrderHistory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace App\Domain\Regulation;

class RegulationOrderHistory
{
public function __construct(
private string $uuid,
private string $regulationOrderUuid,
private string $userUuid,
private string $action,
private \DateTimeInterface $date,
) {
}

public function getUuid(): string
{
return $this->uuid;
}

public function getRegulationOrderUuid(): string
{
return $this->regulationOrderUuid;
}

public function getUserUuid(): string
{
return $this->userUuid;
}

public function getAction(): string
{
return $this->action;
}

public function getDate(): \DateTimeInterface
{
return $this->date;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Domain\Regulation\Repository;

use App\Domain\Regulation\RegulationOrderHistory;

interface RegulationOrderHistoryRepositoryInterface
{
public function add(RegulationOrderHistory $regulationOrderHistory): RegulationOrderHistory;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity
name="App\Domain\Regulation\RegulationOrderHistory"
table="regulation_order_history">
<indexes>
<index columns="regulation_order_uuid"/>
<index columns="user_uuid"/>
</indexes>
<id name="uuid" type="guid" column="uuid"/>
<field name="regulationOrderUuid" type="string" column="regulation_order_uuid" length="50" nullable="false"/>
<field name="userUuid" type="string" column="user_uuid" length="50" nullable="false"/>
<field name="action" type="string" column="action" length="20" nullable="false"/>
<field name="date" type="datetimetz" column="date" nullable="false">
<options>
<option name="default">CURRENT_TIMESTAMP</option>
</options>
</field>
</entity>
</doctrine-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Infrastructure\Persistence\Doctrine\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250116114330 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Infrastructure\Persistence\Doctrine\Repository\Regulation;

use App\Domain\Regulation\RegulationOrderHistory;
use App\Domain\Regulation\Repository\RegulationOrderHistoryRepositoryInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

final class RegulationOrderHistoryRepository extends ServiceEntityRepository implements RegulationOrderHistoryRepositoryInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, RegulationOrderHistory::class);
}

public function add(RegulationOrderHistory $regulationOrderHistory): RegulationOrderHistory
{
$this->getEntityManager()->persist($regulationOrderHistory);

return $regulationOrderHistory;
}
}
Loading
Loading