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

Enregistre la date de création de l'organisation #1127

Merged
merged 1 commit into from
Dec 19, 2024
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
Expand Up @@ -50,10 +50,12 @@ public function __invoke(ConvertAccessRequestToUserCommand $command): void

$organization = $this->organizationRepository->findOneBySiret($accessRequest->getSiret());
$organizationRole = OrganizationRolesEnum::ROLE_ORGA_CONTRIBUTOR->value; // Default organization role
$now = $this->dateUtils->getNow();

if (!$organization) {
$organizationRole = OrganizationRolesEnum::ROLE_ORGA_ADMIN->value; // The first user in an organization becomes an admin
$organization = (new Organization($this->idFactory->make()))
->setCreatedAt($now)
->setSiret($accessRequest->getSiret())
->setName($accessRequest->getOrganization());
$this->organizationRepository->add($organization);
Expand All @@ -64,7 +66,7 @@ public function __invoke(ConvertAccessRequestToUserCommand $command): void
->setPassword($accessRequest->getPassword())
->setEmail($accessRequest->getEmail())
->setRoles([UserRolesEnum::ROLE_USER->value])
->setRegistrationDate($this->dateUtils->getNow());
->setRegistrationDate($now);

$organizationUser = (new OrganizationUser($this->idFactory->make()))
->setUser($user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Application\User\Command;

use App\Application\DateUtilsInterface;
use App\Application\IdFactoryInterface;
use App\Domain\User\Exception\SiretAlreadyExistException;
use App\Domain\User\Organization;
Expand All @@ -13,6 +14,7 @@ final class SaveOrganizationCommandHandler
{
public function __construct(
private IdFactoryInterface $idFactory,
private DateUtilsInterface $dateUtils,
private OrganizationRepositoryInterface $organizationRepository,
) {
}
Expand All @@ -21,6 +23,7 @@ public function __invoke(SaveOrganizationCommand $command): Organization
{
if (!$command->organization) {
$organization = (new Organization($this->idFactory->make()))
->setCreatedAt($this->dateUtils->getNow())
->setSiret($command->siret)
->setName($command->name);

Expand Down
13 changes: 13 additions & 0 deletions src/Domain/User/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Organization
{
private \DateTimeInterface $createdAt;
private string $name;
private ?string $siret;
private ?string $logo;
Expand All @@ -20,6 +21,18 @@ public function getUuid(): string
return $this->uuid;
}

public function getCreatedAt(): \DateTimeInterface
{
return $this->createdAt;
}

public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;

return $this;
}

public function getName(): string
{
return $this->name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Domain\User\Organization;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

final class OrganizationCrudController extends AbstractCrudController
Expand All @@ -29,6 +30,9 @@ public function configureFields(string $pageName): iterable
{
return [
TextField::new('name')->setLabel('Nom de l\'organisation'),
DateField::new('createdAt')
->setLabel('Date de création')
->setDisabled($pageName === Crud::PAGE_EDIT),
TextField::new('siret')->setLabel('Siret'),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ public function load(ObjectManager $manager): void
{
$mainOrg = (new Organization(self::MAIN_ORG_ID))
->setName(self::MAIN_ORG_NAME)
->setCreatedAt(new \DateTimeImmutable('2022-11-01'))
->setLogo('/path/to/logo.jpeg');

$otherOrg = (new Organization(self::OTHER_ORG_ID))
->setName('Mairie de Savenay')
->setCreatedAt(new \DateTimeImmutable('2023-02-13'))
->setSiret('12345678909876');

$otherOrg2 = (new Organization(self::OTHER_ORG_ID_2))
->setName('Mairie de Saint Ouen')
->setCreatedAt(new \DateTimeImmutable('2023-06-24'))
->setSiret('67876540989876');

$organizationUser1 = new OrganizationUser('53aede0c-1ff3-4873-9e3d-132950dfb893');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<field name="name" type="string" column="name" nullable="false"/>
<field name="siret" type="string" length="14" nullable="true"/>
<field name="logo" type="string" length="200" nullable="true"/>
<field name="createdAt" type="datetimetz" nullable="false">
<options>
<option name="default">CURRENT_TIMESTAMP</option>
</options>
</field>
<unique-constraints>
<unique-constraint columns="siret" name="organization_siret" />
</unique-constraints>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Infrastructure\Persistence\Doctrine\Migrations;

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

final class Version20241219094151 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add column organization.created_at';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE organization ADD created_at TIMESTAMP(0) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE organization DROP created_at');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public function testConvertWithSiretNotLinkedToAnOrganization(): void
->setRegistrationDate($date);

$organization = (new Organization('d145a0e3-e397-412c-ba6a-90b150f7aec2'))
->setCreatedAt($date)
->setName('Fairness')
->setSiret('82050375300015');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Tests\Unit\Application\User\Command;

use App\Application\DateUtilsInterface;
use App\Application\IdFactoryInterface;
use App\Application\User\Command\SaveOrganizationCommand;
use App\Application\User\Command\SaveOrganizationCommandHandler;
Expand All @@ -15,22 +16,31 @@
final class SaveOrganizationCommandHandlerTest extends TestCase
{
private $idFactory;
private $dateUtils;
private $organizationRepository;

public function setUp(): void
{
$this->idFactory = $this->createMock(IdFactoryInterface::class);
$this->dateUtils = $this->createMock(DateUtilsInterface::class);
$this->organizationRepository = $this->createMock(OrganizationRepositoryInterface::class);
}

public function testCreate(): void
{
$date = new \DateTimeImmutable('2024-05-07');
$this->dateUtils
->expects(self::once())
->method('getNow')
->willReturn($date);

$this->idFactory
->expects(self::once())
->method('make')
->willReturn('7fb74c5d-069b-4027-b994-7545bb0942d0');

$createdOrganization = (new Organization(uuid: '7fb74c5d-069b-4027-b994-7545bb0942d0'))
->setCreatedAt($date)
->setSiret('21930027400012')
->setName('Commune de La Courneuve');

Expand All @@ -45,6 +55,7 @@ public function testCreate(): void

$handler = new SaveOrganizationCommandHandler(
$this->idFactory,
$this->dateUtils,
$this->organizationRepository,
);

Expand Down Expand Up @@ -87,6 +98,7 @@ public function testUpdateWithSameSiret(): void

$handler = new SaveOrganizationCommandHandler(
$this->idFactory,
$this->dateUtils,
$this->organizationRepository,
);

Expand Down Expand Up @@ -131,6 +143,7 @@ public function testUpdateWithDifferentSiret(): void

$handler = new SaveOrganizationCommandHandler(
$this->idFactory,
$this->dateUtils,
$this->organizationRepository,
);

Expand Down Expand Up @@ -177,6 +190,7 @@ public function testSiretAlreadyExist(): void

$handler = new SaveOrganizationCommandHandler(
$this->idFactory,
$this->dateUtils,
$this->organizationRepository,
);

Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Domain/User/OrganizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ final class OrganizationTest extends TestCase
{
public function testGetters(): void
{
$date = new \DateTimeImmutable('2024-05-07');

$organization = (new Organization('6598fd41-85cb-42a6-9693-1bc45f4dd392'))
->setCreatedAt($date)
->setName('Mairie de Savenay')
->setSiret('21440195200129')
->setLogo('/path/to/logo.jpg');

$this->assertSame('6598fd41-85cb-42a6-9693-1bc45f4dd392', $organization->getUuid());
$this->assertEquals($date, $organization->getCreatedAt());
$this->assertSame('Mairie de Savenay', $organization->getName());
$this->assertSame('21440195200129', $organization->getSiret());
$this->assertSame('Mairie de Savenay', (string) $organization);
Expand Down
Loading