Skip to content

Commit 0d17f25

Browse files
hbaltypYassine
authored andcommitted
feat(admin): ajout d'un toast pour confirmer le bon déroulement de l'elevation de droit
1 parent f3414d4 commit 0d17f25

File tree

6 files changed

+72
-22
lines changed

6 files changed

+72
-22
lines changed

packages/backend/src/_common/mocks/USER_STRUCTURE_AUTHENTIFICATED.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const USER_STRUCTURE_AUTH: UserStructureAuthenticated = {
77
nom: "Admin",
88
prenom: "Test",
99
email: "admin@test.com",
10-
role: "admin",
10+
role: "simple",
1111
structureId: 1,
1212
verified: true,
1313
createdAt: new Date(),

packages/backend/src/modules/portail-admin/controllers/admin-users/admin-users.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export class AdminUsersController {
7979
uuid,
8080
});
8181

82+
if (user.role === "admin") throw new Error("User is already and admin");
83+
8284
await userStructureRepository.update(
8385
{
8486
uuid: userToElevate.uuid,

packages/portail-admins/src/app/modules/structure/components/users/users.component.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,16 @@ <h2>Utilisateurs enregistrés: {{ users.length }} utilisateurs</h2>
138138
>
139139
<button
140140
class="btn btn-outline-primary m-1"
141-
(click)="openConfirmationModdal(user)"
141+
(click)="
142+
openConfirmationModal(user, MODAL_ACTION.REINIT_USER_PASSWORD)
143+
"
142144
type="button"
143145
>
144146
Réinitialiser mot de passe
145147
</button>
146148
<button
147149
*ngIf="user.role !== 'admin'"
148-
(click)="doElevateRole(user)"
150+
(click)="openConfirmationModal(user, MODAL_ACTION.PROMOTE_USER)"
149151
class="btn btn-outline-primary mr-2"
150152
type="button"
151153
>
@@ -173,9 +175,9 @@ <h4 class="modal-title">Confirmation</h4>
173175
</div>
174176
<div class="modal-body">
175177
<p>
176-
Êtes-vous sûr de vouloir réinitialiser le mot de passe de l'utilisateur:
178+
Êtes-vous sûr de vouloir {{ confirmModalContext.actionText }}:
177179
<span class="fw-bold">
178-
{{ userForPasswordReinit.email }}
180+
{{ userForConfirmModal.email }}
179181
</span>
180182
?
181183
</p>
@@ -184,14 +186,14 @@ <h4 class="modal-title">Confirmation</h4>
184186
<button
185187
type="button"
186188
class="btn btn-lg btn-outline-dark"
187-
(click)="modal.dismiss()"
189+
(click)="confirmModalContext = null; modal.dismiss()"
188190
>
189191
Annuler
190192
</button>
191193
<button
192194
type="button"
193195
class="btn btn-primary"
194-
(click)="doResetPassword(userForPasswordReinit.email)"
196+
(click)="confirmModalContext.action(userForConfirmModal)"
195197
>
196198
Confirmer
197199
</button>

packages/portail-admins/src/app/modules/structure/components/users/users.component.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { TableHeadSortComponent } from "../../../shared/components/table-head-so
88
import { SortArrayPipe } from "../../../shared/pipes/sort-array.pipe";
99
import { StructureService } from "../../services/structure.service";
1010
import { STRUCTURE_MOCK } from "../../../../mocks/STRUCTURE_MOCK.mock";
11+
import { CustomToastService } from "../../../shared/services";
1112

1213
describe("UsersComponent", () => {
1314
let component: UsersComponent;
@@ -23,7 +24,7 @@ describe("UsersComponent", () => {
2324
HttpClientTestingModule,
2425
RouterModule.forRoot([]),
2526
],
26-
providers: [StructureService],
27+
providers: [StructureService, CustomToastService],
2728
}).compileComponents();
2829

2930
fixture = TestBed.createComponent(UsersComponent);

packages/portail-admins/src/app/modules/structure/components/users/users.component.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ import { environment } from "../../../../../environments/environment";
2222
import { subMonths } from "date-fns";
2323
import { NgbModal, NgbModalRef } from "@ng-bootstrap/ng-bootstrap";
2424
import { faPersonArrowUpFromLine } from "@fortawesome/free-solid-svg-icons";
25+
import { CustomToastService } from "../../../shared/services";
26+
27+
export enum MODAL_ACTION {
28+
PROMOTE_USER = "PROMOTE_USER",
29+
REINIT_USER_PASSWORD = "REINIT_USER_PASSWORD",
30+
}
31+
32+
export interface ConfirmModalContext {
33+
actionText: string;
34+
action: (user: UserStructureWithSecurity) => void;
35+
}
2536

2637
@Component({
2738
selector: "app-users",
@@ -37,6 +48,7 @@ export class UsersComponent implements OnInit, OnDestroy {
3748
public readonly frontendUrl = environment.frontendUrl;
3849
public readonly USER_FONCTION = UserFonction;
3950
public readonly _USER_FONCTION_LABELS = USER_FONCTION_LABELS;
51+
public readonly MODAL_ACTION = MODAL_ACTION;
4052
public readonly personArrowUp = faPersonArrowUpFromLine;
4153
public readonly USER_ROLES_LABELS: { [key in UserStructureRole]: string } = {
4254
admin: "Administrateur",
@@ -48,12 +60,14 @@ export class UsersComponent implements OnInit, OnDestroy {
4860
private subscription = new Subscription();
4961
public searching = true;
5062
@ViewChild("confirmPasswordReinit", { static: true })
51-
public confirmPasswordReinit!: TemplateRef<NgbModalRef>;
63+
public confirmModal!: TemplateRef<NgbModalRef>;
64+
public confirmModalContext?: ConfirmModalContext;
5265

53-
public userForPasswordReinit?: UserStructureWithSecurity;
66+
public userForConfirmModal?: UserStructureWithSecurity;
5467
constructor(
5568
private readonly structureService: StructureService,
56-
private readonly modalService: NgbModal
69+
private readonly modalService: NgbModal,
70+
private readonly toastService: CustomToastService
5771
) {}
5872

5973
ngOnInit(): void {
@@ -80,20 +94,44 @@ export class UsersComponent implements OnInit, OnDestroy {
8094
);
8195
}
8296

83-
public openConfirmationModdal(user: UserStructureWithSecurity): void {
84-
this.userForPasswordReinit = user;
85-
this.modalService.open(this.confirmPasswordReinit, {
97+
public openConfirmationModal(
98+
user: UserStructureWithSecurity,
99+
modalAction: MODAL_ACTION
100+
): void {
101+
this.setConfirmModalContext(user, modalAction);
102+
this.userForConfirmModal = user;
103+
this.modalService.open(this.confirmModal, {
86104
size: "s",
87105
centered: true,
88106
});
89107
}
90108

91-
public doResetPassword(email: string): void {
92-
if (!email) return;
109+
public setConfirmModalContext(
110+
user: UserStructureWithSecurity,
111+
modalAction: MODAL_ACTION
112+
) {
113+
const isPromoteUser = modalAction === MODAL_ACTION.PROMOTE_USER;
114+
this.confirmModalContext = {
115+
actionText: isPromoteUser
116+
? "transmettre les droits administrateur à"
117+
: `réinitialiser le mot de passe de l'utilisateur`,
118+
action: isPromoteUser
119+
? () => this.doElevateRole(user)
120+
: () => this.doResetPassword(user),
121+
};
122+
}
123+
124+
public doResetPassword(user: UserStructureWithSecurity): void {
125+
if (!user) return;
93126
this.subscription.add(
94-
this.structureService
95-
.resetStructureAdminPassword(email)
96-
.subscribe(() => this.reloadUsersSubject$.next())
127+
this.structureService.resetStructureAdminPassword(user.email).subscribe({
128+
next: () => {
129+
this.reloadUsersSubject$.next();
130+
this.toastService.success(
131+
"Le lien de réinitialisation de mot de passe admin a été généré avec succès "
132+
);
133+
},
134+
})
97135
);
98136

99137
this.modalService.dismissAll();
@@ -104,9 +142,16 @@ export class UsersComponent implements OnInit, OnDestroy {
104142
this.structureService.elevateUserRole(user.uuid).subscribe({
105143
next: () => {
106144
this.reloadUsersSubject$.next();
145+
this.toastService.success(
146+
"L'utilisateur a été promu au rôle admin avec succès"
147+
);
148+
},
149+
error: () => {
150+
this.toastService.error("Une erreur serveur est survenue");
107151
},
108152
})
109153
);
154+
this.modalService.dismissAll();
110155
}
111156

112157
ngOnDestroy(): void {

packages/portail-admins/src/app/modules/structure/services/structure.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ export class StructureService {
3838

3939
public resetStructureAdminPassword(email: string): Observable<void> {
4040
return this.http.post<void>(`${RESET_PASSWORD_URL}`, {
41-
email
41+
email,
4242
});
4343
}
4444

45-
public elevateUserRole(uuid: string): Observable<void> {
45+
public elevateUserRole(uuid: string): Observable<void> {
4646
return this.http.patch<void>(`${USER_ADMIN_BASE_URL}/elevate-user-role`, {
47-
uuid
47+
uuid,
4848
});
4949
}
5050

0 commit comments

Comments
 (0)