Skip to content

Commit

Permalink
Enregistre une date de dernière activité de l'utilisateur (#998)
Browse files Browse the repository at this point in the history
* Enregistre une date de dernière activité de l'utilisateur

* Renomme
  • Loading branch information
florimondmanca authored Oct 9, 2024
1 parent 2c68845 commit 88e8aef
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/Application/User/Command/MarkUserAsActiveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Application\User\Command;

use App\Application\CommandInterface;
use App\Domain\User\User;

final class MarkUserAsActiveCommand implements CommandInterface
{
public function __construct(
public readonly User $user,
) {
}
}
21 changes: 21 additions & 0 deletions src/Application/User/Command/MarkUserAsActiveCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Application\User\Command;

use App\Application\DateUtilsInterface;

final class MarkUserAsActiveCommandHandler
{
public function __construct(
private DateUtilsInterface $dateUtils,
) {
}

public function __invoke(MarkUserAsActiveCommand $command): void
{
$command->user
->setLastActiveAt($this->dateUtils->getNow());
}
}
11 changes: 11 additions & 0 deletions src/Domain/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class User
private string $password;
private array $roles = [];
private \DateTimeInterface $registrationDate;
private ?\DateTimeInterface $lastActiveAt;

public function __construct(
private string $uuid,
Expand Down Expand Up @@ -82,6 +83,16 @@ public function setRegistrationDate(\DateTimeInterface $date): self
return $this;
}

public function getLastActiveAt(): ?\DateTimeInterface
{
return $this->lastActiveAt;
}

public function setLastActiveAt(\DateTimeInterface $date): void
{
$this->lastActiveAt = $date;
}

public function __toString(): string
{
return \sprintf('%s (%s)', $this->fullName, $this->email);
Expand Down
3 changes: 3 additions & 0 deletions src/Infrastructure/Controller/Admin/UserCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function configureFields(string $pageName): iterable
DateField::new('registrationDate')
->setLabel('Date d\'inscription')
->setDisabled($pageName === Crud::PAGE_EDIT),
DateField::new('lastActiveAt')
->setLabel('Dernière activité')
->setDisabled(true),
ChoiceField::new('roles')
->setLabel('Rôles')
->allowMultipleChoices()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use App\Application\CommandBusInterface;
use App\Application\DateUtilsInterface;
use App\Application\Regulation\Command\SaveRegulationGeneralInfoCommand;
use App\Application\User\Command\MarkUserAsActiveCommand;
use App\Infrastructure\Form\Regulation\GeneralInfoFormType;
use App\Infrastructure\Security\AuthenticatedUser;
use App\Infrastructure\Security\SymfonyUser;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Form\FormFactoryInterface;
Expand All @@ -21,6 +23,7 @@ final class AddRegulationController
{
public function __construct(
private \Twig\Environment $twig,
private AuthenticatedUser $authenticatedUser,
private FormFactoryInterface $formFactory,
private Security $security,
private RouterInterface $router,
Expand Down Expand Up @@ -60,6 +63,10 @@ public function __invoke(Request $request): Response
if ($form->isSubmitted() && $form->isValid()) {
$regulationOrderRecord = $this->commandBus->handle($command);

// User just created a regulation order, this is a sign of activity.
$user = $this->authenticatedUser->getUser();
$this->commandBus->handle(new MarkUserAsActiveCommand($user));

return new RedirectResponse(
url: $this->router->generate('app_regulation_detail', [
'uuid' => $regulationOrderRecord->getUuid(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<option name="default">CURRENT_TIMESTAMP</option>
</options>
</field>
<field name="lastActiveAt" type="datetimetz" column="last_active_at" nullable="true" />
<unique-constraints>
<unique-constraint columns="email" name="user_email" />
</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 Version20241008120435 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add user.last_active_at';
}

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

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE "user" DROP last_active_at');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
namespace App\Tests\Integration\Infrastructure\Controller\Regulation;

use App\Domain\Regulation\Enum\RegulationOrderCategoryEnum;
use App\Domain\User\Repository\UserRepositoryInterface;
use App\Infrastructure\Persistence\Doctrine\Fixtures\OrganizationFixture;
use App\Infrastructure\Persistence\Doctrine\Fixtures\RegulationOrderFixture;
use App\Infrastructure\Persistence\Doctrine\Fixtures\UserFixture;
use App\Tests\Integration\Infrastructure\Controller\AbstractWebTestCase;

final class AddRegulationControllerTest extends AbstractWebTestCase
{
public function testAdd(): void
{
$client = $this->login();
$email = UserFixture::MAIN_ORG_USER_EMAIL;
$client = $this->login($email);
$crawler = $client->request('GET', '/regulations/add');

$this->assertResponseStatusCodeSame(200);
Expand All @@ -37,8 +40,13 @@ public function testAdd(): void
$form['general_info_form[startDate]'] = '2023-02-14';
$form['general_info_form[category]'] = RegulationOrderCategoryEnum::OTHER->value;
$form['general_info_form[otherCategoryText]'] = 'Trou en formation';

/** @var UserRepositoryInterface */
$userRepository = static::getContainer()->get(UserRepositoryInterface::class);
$this->assertNull($userRepository->findOneByEmail($email)->getLastActiveAt());
$client->submit($form);
$this->assertResponseStatusCodeSame(303);
$this->assertEquals(new \DateTimeImmutable('2023-06-09'), $userRepository->findOneByEmail($email)->getLastActiveAt());

$client->followRedirect();
$this->assertResponseStatusCodeSame(200);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

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

use App\Application\DateUtilsInterface;
use App\Application\User\Command\MarkUserAsActiveCommand;
use App\Application\User\Command\MarkUserAsActiveCommandHandler;
use App\Domain\User\User;
use PHPUnit\Framework\TestCase;

final class MarkUserActiveCommandHandlerTest extends TestCase
{
public function testHandler(): void
{
$user = $this->createMock(User::class);
$dateUtils = $this->createMock(DateUtilsInterface::class);

$now = new \DateTimeImmutable('2024-10-01');

$dateUtils
->expects(self::once())
->method('getNow')
->willReturn($now);

$command = new MarkUserAsActiveCommand($user);
$handler = new MarkUserAsActiveCommandHandler($dateUtils);
$handler($command);
}
}

0 comments on commit 88e8aef

Please sign in to comment.