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

refactor: create route + page for edit moderation action #740

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions cypress/e2e/join_org_with_verified_domain/index.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("join organizations", () => {
);

// Try to change org
cy.get('a[href^="/help"]')
cy.get('a[href^="/edit-moderation"]')
.contains("J’ai fait une erreur dans ma demande")
.click();
cy.get('[action^="/users/cancel-moderation-and-redirect-to-join-org/"]')
Expand All @@ -64,7 +64,7 @@ describe("join organizations", () => {
cy.contains("Notre équipe étudie votre demande");

// Try to change email
cy.get('a[href^="/help"]')
cy.get('a[href^="/edit-moderation"]')
.contains("J’ai fait une erreur dans ma demande")
.click();
cy.get('[action^="/users/cancel-moderation-and-redirect-to-sign-in/"]')
Expand Down
48 changes: 48 additions & 0 deletions src/controllers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,51 @@ export const getHelpController = async (
next(error);
}
};

export const getEditModerationController = async (
req: Request,
res: Response,
next: NextFunction,
) => {
try {
let email: string | undefined;
let user: User | undefined;
let cached_libelle: string | null | undefined;

if (isWithinAuthenticatedSession(req.session)) {
user = getUserFromAuthenticatedSession(req);
email = user.email;
}

const schema = z.object({
moderation_id: z.union([idSchema(), z.undefined()]),
});
let { moderation_id } = await schema.parseAsync(req.query);

if (!isEmpty(user) && moderation_id) {
try {
const organization = await getOrganizationFromModeration({
user,
moderation_id,
});
cached_libelle = organization.cached_libelle;
} catch (e) {
if (!(e instanceof NotFoundError || e instanceof ForbiddenError)) {
return next(e);
}

moderation_id = undefined;
}
}

return res.render("edit-moderation", {
pageTitle: "Modifier une demande de rattachement en cours",
email,
csrfToken: email && csrfToken(req),
organization_label: cached_libelle,
moderation_id,
});
} catch (error) {
next(error);
}
};
9 changes: 9 additions & 0 deletions src/routers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type Express, Router, urlencoded } from "express";
import nocache from "nocache";
import {
getConnectionAndAccountController,
getEditModerationController,
getHelpController,
getHomeController,
getManageOrganizationsController,
Expand Down Expand Up @@ -152,6 +153,14 @@ export const mainRouter = (app: Express) => {
getHelpController,
);

mainRouter.get(
"/edit-moderation",
urlencoded({ extended: false }),
ejsLayoutMiddlewareFactory(app, true),
csrfProtectionMiddleware,
getEditModerationController,
);

return mainRouter;
};

Expand Down
10 changes: 10 additions & 0 deletions src/routers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ export const userRouter = () => {
),
);

userRouter.post(
"/cancel-moderation-and-redirect-to-personal-information/:moderation_id",
rateLimiterMiddleware,
checkUserHasPersonalInformationsMiddleware,
csrfProtectionMiddleware,
postCancelModerationAndRedirectControllerFactory(
"/users/personal-information",
),
);

userRouter.get(
"/select-organization",
checkUserHasAtLeastOneOrganizationMiddleware,
Expand Down
83 changes: 83 additions & 0 deletions src/views/edit-moderation.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<div class="page-container">
<div>
<h1 class="fr-h2">Modifier une demande de rattachement en cours</h1>
<p>
Vous avez demandé à être rattaché à <%= organization_label; %> avec votre
adresse <%= email; %> :
</p>
<ul>
<li class="fr-mb-3w">
Vous voulez utiliser une autre adresse email<br />
💡 <b>
<form
class="inline-form"
autocomplete="off"
action="/users/cancel-moderation-and-redirect-to-sign-in/<%= moderation_id; %>"
method="post"
>
<input
type="hidden"
name="_csrf"
value="<%= csrfToken; %>"
autocomplete="off"
/>
<input
aria-label="Corriger l’email <%= email; %>"
class="fr-link"
type="submit"
value="Annuler la demande en cours et utiliser une autre adresse email"
/>
</form>
</b>
</li>
<li class="fr-mb-3w">
Vous voulez être rattaché à une autre organisation<br />
💡 <b>
<form
class="inline-form"
autocomplete="off"
action="/users/cancel-moderation-and-redirect-to-join-org/<%= moderation_id; %>"
method="post"
>
<input
type="hidden"
name="_csrf"
value="<%= csrfToken; %>"
autocomplete="off"
/>
<input
aria-label="Corriger l’organisation <%= organization_label; %>"
class="fr-link"
type="submit"
value="Annuler la demande en cours et sélectionner une organisation différente"
/>
</form>
</b>
</li>
<li class="fr-mb-3w">
Vous voulez modifier vos informations personnelles<br />
💡 <b>
<form
class="inline-form"
autocomplete="off"
action="/cancel-moderation-and-redirect-to-personal-information/<%= moderation_id; %>"
method="post"
>
<input
type="hidden"
name="_csrf"
value="<%= csrfToken; %>"
autocomplete="off"
/>
<input
aria-label="Corriger ses informations personnelles"
class="fr-link"
type="submit"
value="Annuler la demande en cours et corriger ses informations personnelles"
/>
</form>
</b>
</li>
</ul>
</div>
</div>
2 changes: 1 addition & 1 deletion src/views/user/unable-to-auto-join-organization.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div>
<a
class="fr-link fr-icon-question-line fr-link--icon-left"
href="/help?moderation_id=<%= moderation_id; %>#modifier-moderation"
href="/edit-moderation"
>
J’ai fait une erreur dans ma demande
</a>
Expand Down
Loading