Skip to content

Commit

Permalink
New restriction list (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchois authored Aug 12, 2024
1 parent 70124b2 commit 202c665
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 231 deletions.
1 change: 0 additions & 1 deletion src/Application/Regulation/Query/GetRegulationsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
public function __construct(
public int $pageSize,
public int $page,
public bool $isPermanent,
public ?array $organizationUuids = null,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function __invoke(GetRegulationsQuery $query): Pagination
$rows = $this->repository->findAllRegulations(
$query->pageSize,
$query->page,
$query->isPermanent,
$query->organizationUuids,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function findOneUuidByIdentifierInOrganization(string $identifier, Organi
public function findAllRegulations(
int $maxItemsPerPage,
int $page,
bool $isPermanent,
?array $organizationUuids = null,
): array;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,19 @@ public function __invoke(Request $request, string $uuid): Response
// The regulation may have been deleted before.
// Don't fail, as DELETE is an idempotent method (see RFC 9110, 9.2.2).
return new RedirectResponse(
url: $this->router->generate('app_regulations_list', [
'tab' => 'temporary',
]),
url: $this->router->generate('app_regulations_list'),
status: Response::HTTP_SEE_OTHER,
);
}

// Get before the entity gets deleted.
$endDate = $regulationOrderRecord->getRegulationOrder()->getEndDate();

try {
$this->commandBus->handle(new DeleteRegulationCommand($user->getOrganizationUuids(), $regulationOrderRecord));
} catch (RegulationOrderRecordCannotBeDeletedException) {
throw new AccessDeniedHttpException();
}

return new RedirectResponse(
url: $this->router->generate('app_regulations_list', [
'tab' => $endDate ? 'temporary' : 'permanent',
]),
url: $this->router->generate('app_regulations_list'),
status: Response::HTTP_SEE_OTHER,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __invoke(Request $request): Response
{
/** @var SymfonyUser|null */
$user = $this->security->getUser();
$tab = $request->query->get('tab', 'temporary');
$pageSize = min($request->query->getInt('pageSize', 20), 100);
$page = $request->query->getInt('page', 1);

Expand All @@ -46,29 +45,18 @@ public function __invoke(Request $request): Response

$organizationUserUuids = $user?->getOrganizationUuids();

$temporaryRegulations = $this->queryBus->handle(
$regulations = $this->queryBus->handle(
new GetRegulationsQuery(
pageSize: $pageSize,
page: $tab === 'temporary' ? $page : 1,
isPermanent: false,
organizationUuids: $organizationUserUuids,
),
);
$permanentRegulations = $this->queryBus->handle(
new GetRegulationsQuery(
pageSize: $pageSize,
page: $tab === 'permanent' ? $page : 1,
isPermanent: true,
page: $page,
organizationUuids: $organizationUserUuids,
),
);

return new Response($this->twig->render(
name: 'regulation/index.html.twig',
context: [
'temporaryRegulations' => $temporaryRegulations,
'permanentRegulations' => $permanentRegulations,
'tab' => $tab,
'regulations' => $regulations,
'pageSize' => $pageSize,
'page' => $page,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
final class RegulationOrderFixture extends Fixture
{
public const TYPICAL_IDENTIFIER = 'FO1/2023';
public const NUM_TEMPORARY = 9;
public const NUM_PERMANENT = 1;
public const IDENTIFIER_CIFS = 'F/CIFS/2023';

public function load(ObjectManager $manager): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function __construct(
public function findAllRegulations(
int $maxItemsPerPage,
int $page,
bool $isPermanent,
?array $organizationUuids = null,
): array {
$query = $this->createQueryBuilder('roc')
Expand All @@ -83,7 +82,7 @@ public function findAllRegulations(

$query
->innerJoin('roc.organization', 'o')
->innerJoin('roc.regulationOrder', 'ro', 'WITH', $isPermanent ? 'ro.endDate IS NULL' : 'ro.endDate IS NOT NULL')
->innerJoin('roc.regulationOrder', 'ro')
->orderBy('ro.startDate', 'DESC')
->addOrderBy('ro.identifier', 'ASC')
->addGroupBy('ro, roc, o')
Expand Down
2 changes: 1 addition & 1 deletion templates/regulation/_items.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<th scope="col" class="app-regulation-table__identifier fr-hidden fr-x-revert-sm">{{ 'regulation.organization'|trans }}</th>
<th scope="col">{{ 'regulation.locations'|trans }}</th>
<th scope="col" class="fr-hidden fr-x-revert-sm">{{ 'regulation.period'|trans }}</th>
<th scope="col" class="fr-hidden fr-x-revert-sm">{{ 'regulation.status'|trans }}</th>
<th scope="col" class="fr-hidden fr-x-revert-sm app-table__actions">{{ 'regulation.status'|trans }}</th>
<th scope="col" class="app-table__actions">{{ 'common.actions'|trans }}</th>
</tr>
</thead>
Expand Down
3 changes: 1 addition & 2 deletions templates/regulation/detail.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@
</aside>
</div>
<div class="fr-mt-22v">
{% set tab = generalInfo.startDate and generalInfo.endDate ? 'temporary' : 'permanent' %}
<a href="{{ path('app_regulations_list', { tab }) }}" class="fr-link fr-fi-arrow-left-line fr-link--icon-left">
<a href="{{ path('app_regulations_list') }}" class="fr-link fr-fi-arrow-left-line fr-link--icon-left">
{{ 'regulation.back'|trans }}
</a>
</div>
Expand Down
50 changes: 7 additions & 43 deletions templates/regulation/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

{% block body %}
<section class="fr-container fr-py-5w" aria-labelledby="regulation-list">
{% set isTemporaryTab = tab == 'temporary' %}
{% set isPermanent = tab == 'permanent' %}
<div class="fr-grid-row">
<h3 id="regulation-list" class="fr-col fr-mb-0">{{ 'regulation.list.title'|trans }}</h3>
{% if app.user %}
Expand All @@ -16,47 +14,13 @@
</a>
{% endif %}
</div>
<div class="fr-tabs fr-mt-2w">
<ul class="fr-tabs__list" role="tablist" aria-label="{{ 'regulation.list.title'|trans }}">
<li role="presentation">
<button id="temporary" class="fr-tabs__tab" tabindex="0" role="tab" aria-selected="{{ isTemporaryTab ? "true" : "false" }}" aria-controls="temporary-panel">
{{ 'regulation.list.total_temporary'|trans({'%count%': temporaryRegulations.totalItems}) }}
</button>
</li>
<li role="presentation">
<button id="permanent" class="fr-tabs__tab" tabindex="1" role="tab" aria-selected="{{ isPermanent ? "true" : "false" }}" aria-controls="permanent-panel">
{{ 'regulation.list.total_permanent'|trans({'%count%': permanentRegulations.totalItems}) }}
</button>
</li>
</ul>
<div
id="temporary-panel"
class="fr-tabs__panel {% if isTemporaryTab %}fr-tabs__panel--selected{% else %}fr-tabs__panel--direction-start{% endif %}"
role="tabpanel"
aria-labelledby="temporary"
tabindex="0"
>
{% include "regulation/_items.html.twig" with { pagination: temporaryRegulations } only %}
{% include "common/pagination.html.twig" with {
pagination: temporaryRegulations,
currentPage: isTemporaryTab ? app.request.get('page', 1) : 1,
queryParams: app.request.query.all|merge({ tab: 'temporary' }),
} only %}
</div>
<div
id="permanent-panel"
class="fr-tabs__panel {% if isPermanent %}fr-tabs__panel--selected{% else %}fr-tabs__panel--direction-end{% endif %}"
role="tabpanel"
aria-labelledby="permanent"
tabindex="1"
>
{% include "regulation/_items.html.twig" with { pagination: permanentRegulations } only %}
{% include "common/pagination.html.twig" with {
pagination: permanentRegulations,
currentPage: isPermanent ? app.request.get('page', 1) : 1,
queryParams: app.request.query.all|merge({ tab: 'permanent' }),
} only %}
</div>
<div class="fr-card fr-mt-2w fr-p-2w">
{% include "regulation/_items.html.twig" with { pagination: regulations } only %}
{% include "common/pagination.html.twig" with {
pagination: regulations,
currentPage: app.request.get('page', 1),
queryParams: app.request.query.all(),
} only %}
</div>
</section>
{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,27 @@ final class DeleteRegulationControllerTest extends AbstractWebTestCase
{
use SessionHelper;

private function countRows($crawler)
private function countRows($crawler): int
{
$numTemporary = $crawler->filter('#temporary-panel tbody > tr:not([data-testid=empty-row])')->count();
$numPermanent = $crawler->filter('#permanent-panel tbody > tr:not([data-testid=empty-row])')->count();
$num = $crawler->filter('.app-regulation-table tbody > tr:not([data-testid=empty-row])')->count();

return [$numTemporary, $numPermanent];
return $num;
}

public function testDeleteTemporary(): void
public function testDelete(): void
{
$client = $this->login();

$crawler = $client->request('GET', '/regulations');
[$numTemporary, $numPermanent] = $this->countRows($crawler);
$num = $this->countRows($crawler);

$client->request('DELETE', '/regulations/' . RegulationOrderRecordFixture::UUID_TYPICAL, [
'token' => $this->generateCsrfToken($client, 'delete-regulation'),
]);
$this->assertResponseRedirects('/regulations?tab=temporary', 303);
$this->assertResponseRedirects('/regulations', 303);
$crawler = $client->followRedirect();
// Doesn't appear in list of temporary regulations anymore.
$this->assertEquals([$numTemporary - 1, $numPermanent], $this->countRows($crawler));
}

public function testDeletePermanent(): void
{
$client = $this->login();

$client->request('GET', '/regulations/' . RegulationOrderRecordFixture::UUID_PERMANENT);
$this->assertResponseStatusCodeSame(200);

$crawler = $client->request('GET', '/regulations');
[$numTemporary, $numPermanent] = $this->countRows($crawler);

$client->request('DELETE', '/regulations/' . RegulationOrderRecordFixture::UUID_PERMANENT, [
'token' => $this->generateCsrfToken($client, 'delete-regulation'),
]);
$this->assertResponseRedirects('/regulations?tab=permanent', 303);
$crawler = $client->followRedirect();

// Doesn't appear in list of permanent regulations anymore.
$this->assertSame([$numTemporary, $numPermanent - 1], $this->countRows($crawler), $crawler->html());

// Detail page doesn't exist anymore.
$client->request('GET', '/regulations/' . RegulationOrderRecordFixture::UUID_PERMANENT);
$this->assertResponseStatusCodeSame(404);
$this->assertEquals($num - 1, $this->countRows($crawler));
}

public function testCannotDeleteBecauseDifferentOrganization(): void
Expand All @@ -76,7 +51,7 @@ public function testRegulationOrderRecordNotFound(): void
$client->request('DELETE', '/regulations/' . RegulationOrderRecordFixture::UUID_DOES_NOT_EXIST, [
'token' => $this->generateCsrfToken($client, 'delete-regulation'),
]);
$this->assertResponseRedirects('/regulations?tab=temporary', 303);
$this->assertResponseRedirects('/regulations', 303);
}

public function testInvalidCsrfToken(): void
Expand Down
Loading

0 comments on commit 202c665

Please sign in to comment.