Skip to content

Commit

Permalink
feat: CGU ref #563
Browse files Browse the repository at this point in the history
  • Loading branch information
pprev94 committed Nov 27, 2024
1 parent bb5bc49 commit 618f4bb
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 13 deletions.
2 changes: 1 addition & 1 deletion assets/entrepot/api/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const getMembers = (communityId: string, otherOptions: RequestInit = {}) => {
* @returns
*/
const updateMember = (communityId: string, formData: object) => {
const url = SymfonyRouting.generate("cartesgouvfr_api_community_add_member", { communityId });
const url = SymfonyRouting.generate("cartesgouvfr_api_community_update_member", { communityId });
return jsonFetch<UserRightsResponseDto>(
url,
{
Expand Down
9 changes: 8 additions & 1 deletion assets/espaceco/api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ const search = (search: string, signal: AbortSignal) => {
});
};

const user = { getMe, search, getSharedThemes };
const checkMeCGU = (signal: AbortSignal) => {
const url = SymfonyRouting.generate("cartesgouvfr_api_espaceco_user_check_cgu");
return jsonFetch<boolean>(url, {
method: "GET",
signal: signal,
});
};
const user = { getMe, search, getSharedThemes, checkMeCGU };

export default user;
37 changes: 35 additions & 2 deletions assets/espaceco/pages/communities/MemberInvitation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { routes } from "../../../router/router";
import api from "../../api";

import "../../../../assets/sass/pages/espaceco/member_invitation.scss";
import { useApiEspaceCoStore } from "../../../stores/ApiEspaceCoStore";

type MemberInvitationProps = {
communityId: number;
Expand All @@ -27,18 +28,32 @@ const MemberInvitation: FC<MemberInvitationProps> = ({ communityId }) => {
const { t } = useTranslation("MemberInvitation");
const { t: tBreadcrumb } = useTranslation("Breadcrumb");

const apiEspaceCoUrl = useApiEspaceCoStore((state) => state.api_espaceco_url);
const espaceCoUrl = useMemo(() => (apiEspaceCoUrl ? apiEspaceCoUrl.replace("/gcms/api", "/login") : ""), [apiEspaceCoUrl]);

const navItems = useMemo(() => datastoreNavItems(), []);

const checkCGUQuery = useQuery<boolean>({
queryKey: RQKeys.checkMeCGU(),
queryFn: ({ signal }) => api.user.checkMeCGU(signal),
staleTime: 60000,
});
const cguAccepted = useMemo(() => {
return checkCGUQuery.data;
}, [checkCGUQuery.data]);

const query = useQuery<CommunityResponseDTO, CartesApiException>({
queryKey: RQKeys.community(communityId),
queryFn: () => api.community.getCommunity(communityId),
staleTime: 3600000,
enabled: cguAccepted,
});

const meQuery = useQuery<UserMe>({
queryKey: RQKeys.getMe(),
queryFn: ({ signal }) => api.user.getMe(signal),
staleTime: 3600000,
enabled: cguAccepted,
});

const myRole = useMemo<Role | undefined>(() => {
Expand Down Expand Up @@ -111,7 +126,9 @@ const MemberInvitation: FC<MemberInvitationProps> = ({ communityId }) => {
</Wait>
)}

{community && myRole === "invited" ? (
{cguAccepted === false && espaceCoUrl ? (
t("espaceco_accept_cgu", { url: espaceCoUrl })
) : community && myRole === "invited" ? (
<div>
<CallOut
title={
Expand Down Expand Up @@ -150,7 +167,7 @@ const MemberInvitation: FC<MemberInvitationProps> = ({ communityId }) => {
) : myRole !== undefined ? (
<p>{t("already_member")}</p>
) : (
<p>{t("no_invitation")}</p>
<Alert severity={"warning"} closable title={t("no_invitation")} />
)}
</AppLayout>
);
Expand All @@ -168,6 +185,7 @@ export const { i18n } = declareComponentKeys<
| { K: "community_name"; P: { name: string }; R: JSX.Element }
| { K: "community_description"; P: { description: string | null }; R: JSX.Element }
| { K: "invitation"; R: JSX.Element }
| { K: "espaceco_accept_cgu"; P: { url: string }; R: JSX.Element }
| "already_member"
| "no_invitation"
| "accept"
Expand All @@ -192,6 +210,20 @@ export const MemberInvitationFrTranslations: Translations<"fr">["MemberInvitatio
return description ? <p dangerouslySetInnerHTML={{ __html: description }} /> : <p>Aucune description</p>;
},
invitation: <p>Vous avez reçu une invitation à rejoindre le guichet :</p>,
espaceco_accept_cgu: ({ url }) => (
<div>
<p>{"Bonjour, vous n'avez pas encore accepté les conditions générales d'utilisation de l'espace collaboratif."}</p>
<p>
{"Veuillez vous connecter avec votre nom d'utilisateur sur "}
<a href={url} target="_blank" rel="noreferrer">
{"l'espace collaboratif"}
</a>
{" et accepter les CGU."}
<br />
{"Vous pourrez ensuite continuer la navigation sur cartes.gouv.fr."}
</p>
</div>
),
already_member: "Vous êtes déjà membre de ce guichet",
no_invitation: "Vous n'avez pas reçu d'invitation de ce guichet",
accept: "Accepter et rejoindre le guichet",
Expand All @@ -214,6 +246,7 @@ export const MemberInvitationEnTranslations: Translations<"en">["MemberInvitatio
),
community_description: undefined,
invitation: undefined,
espaceco_accept_cgu: ({ url }) => <p>{url}</p>,
already_member: undefined,
no_invitation: undefined,
accept: undefined,
Expand Down
12 changes: 9 additions & 3 deletions assets/espaceco/pages/communities/management/CommunityLogo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ const CommunityLogo: FC<CommunityLogoProps> = ({ communityId, logoUrl }) => {
</div>
)}
</div>
{updateLogoMutation.isError && (
<Alert severity="error" closable title={tCommon("error")} description={updateLogoMutation.error.message} className={fr.cx("fr-my-3w")} />
)}
{removeLogoMutation.isError && (
<Alert severity="error" closable title={tCommon("error")} description={removeLogoMutation.error.message} className={fr.cx("fr-my-3w")} />
)}
Expand All @@ -234,6 +231,15 @@ const CommunityLogo: FC<CommunityLogoProps> = ({ communityId, logoUrl }) => {
{createPortal(
<AddLogoModal.Component title={t("modal.logo.title")} buttons={AddModalButtons}>
<div className={fr.cx("fr-grid-row")}>
{updateLogoMutation.isError && (
<Alert
severity="error"
closable
title={tCommon("error")}
description={updateLogoMutation.error.message}
className={fr.cx("fr-my-3w")}
/>
)}
<div className={fr.cx("fr-col-9")}>
<Upload
hint={t("modal.logo.file_hint")}
Expand Down
1 change: 1 addition & 0 deletions assets/modules/espaceco/RQKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const RQKeys = {
searchAddress: (search: string): string[] => ["searchAddress", search],
searchGrids: (text: string): string[] => ["searchGrids", text],
getMe: (): string[] => ["espaceco", "users", "me"],
checkMeCGU: (): string[] => ["espaceco", "cgu", "me"],
searchUsers: (text: string): string[] => ["searchUsers", text],
tables: (communityId: number): string[] => ["feature_types", communityId.toString()],
emailPlanners: (communityId: number): string[] => ["emailplanners", communityId.toString()],
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Entrepot/CommunityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getMembers(string $communityId): JsonResponse
}
}

#[Route('/update_member', name: 'add_member', methods: ['PUT'],
#[Route('/update_member', name: 'update_member', methods: ['PUT'],
options: ['expose' => true],
condition: 'request.isXmlHttpRequest()')
]
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/EspaceCo/CommunityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function updateLogo(int $communityId, Request $request): JsonResponse
$this->communityApiService->updateLogo($communityId, $tempFilePath);
$this->fs->remove($tempFileDir);

$community = $this->communityApiService->getCommunity($communityId, ['fields' => ['logo_url']]);
$community = $this->communityApiService->getCommunity($communityId, ['logo_url']);

return new JsonResponse(['logo_url' => $community['logo_url']]);
} catch (ApiException $ex) {
Expand Down
18 changes: 16 additions & 2 deletions src/Controller/EspaceCo/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
}

#[Route('/me', name: 'me')]
public function getCurrentUser(): JsonResponse
public function getMe(): JsonResponse
{
$me = $this->userApiService->getMe();

Expand All @@ -32,7 +32,7 @@ public function getCurrentUser(): JsonResponse

#[Route('/search', name: 'search')]
public function search(
#[MapQueryParameter] string $search
#[MapQueryParameter] string $search,
): JsonResponse {
$users = $this->userApiService->search($search);

Expand All @@ -46,4 +46,18 @@ public function getSharedThemes(): JsonResponse

return $this->json($me);
}

#[Route('/me/check_cgu', name: 'check_cgu')]
public function checkMeCGUser(): JsonResponse
{
$cguAccepted = false;

$response = $this->userApiService->checkMeCGU();
$contentType = $response['headers']['content-type'][0];
if (preg_match("/^application\/json/", $contentType)) {
$cguAccepted = (null !== json_decode($response['content']));
}

return new JsonResponse($cguAccepted);
}
}
6 changes: 4 additions & 2 deletions src/Services/EspaceCoApi/CommunityApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public function getCommunitiesName(): array
*/
public function getCommunity(int $communityId, array $fields = []): array
{
return $this->request('GET', "communities/$communityId", [], ['fields' => $fields]);
$query = empty($fields) ? [] : ['fields' => implode(',', $fields)];

return $this->request('GET', "communities/$communityId", [], $query);
}

/**
Expand Down Expand Up @@ -137,7 +139,7 @@ private function _transformGrids(array $grids): array
foreach ($grids as $name) {
$grid = $this->gridApiService->getGrid($name);
if (!$grid['deleted']) {
$result[] = $grid;
$grids[] = $grid;
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Services/EspaceCoApi/UserApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ public function search(string $search): array
{
return $this->request('GET', 'users', [], ['search' => $search, 'fields' => ['id', 'username', 'firstname', 'surname']]);
}

/**
* On essaye de récupérer les détails de l'utilisateur courant. Si celui-ci n'a pas encore
* accepter les conditions générales de l'espace collaboratif, la route retourne un XML.
*/
public function checkMeCGU(): array|string
{
return $this->request('GET', 'users/me', [], [], [], false, false, true);
}
}

0 comments on commit 618f4bb

Please sign in to comment.