From 91a6935ae2724602b9af2f89f6102e999ac39401 Mon Sep 17 00:00:00 2001
From: migudel <miguel.moras@estudiantes.uva.es>
Date: Sun, 29 Dec 2024 14:16:31 +0100
Subject: [PATCH] =?UTF-8?q?mejora=20de=20eliminaci=C3=B3n=20de=20usuarios?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../users/user-form/user-form.component.ts    | 40 ++++++++++---------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/angular/RestClient/src/app/features/users/user-form/user-form.component.ts b/angular/RestClient/src/app/features/users/user-form/user-form.component.ts
index 055e9da..2f310e1 100644
--- a/angular/RestClient/src/app/features/users/user-form/user-form.component.ts
+++ b/angular/RestClient/src/app/features/users/user-form/user-form.component.ts
@@ -316,13 +316,7 @@ export class UserFormComponent implements OnInit {
         this.updatePassword(data.currentPassword, data.newPassword);
         break;
       case 'VIEW':
-        const password = this.isAdmin
-          ? confirm('Desea eliminar el usuario')
-            ? 'password'
-            : undefined
-          : prompt('Confirma tu contraseña actual');
-        if (!!password && password.trim().length != 0)
-          this.deleteUser(this.user.id, password);
+        this.deleteUser(this.user.id);
         break;
       default:
         break;
@@ -387,16 +381,26 @@ export class UserFormComponent implements OnInit {
       });
   }
 
-  private deleteUser(userId: number, password: string) {
-    this.authService.deleteUser(userId, password).subscribe({
-      next: () => {
-        if (this.isAdmin) this.router.navigate(['/admin', 'users']);
-        else this.sessionService.logout();
-      },
-      error: (error) => {
-        console.error(error);
-        // this.toastr.error('Invalid email or password');
-      },
-    });
+  private deleteUser(userId: number) {
+    const isAdmin = this.isAdmin;
+    const isOwner = this.user.id == userId;
+    const adminDel = isAdmin && !isOwner;
+
+    const password = adminDel
+      ? confirm('Desea eliminar el usuario')
+        ? 'password'
+        : undefined
+      : prompt('Confirma tu contraseña actual');
+    if (!!password && password.trim().length != 0)
+      this.authService.deleteUser(userId, password).subscribe({
+        next: () => {
+          if (adminDel) this.router.navigate(['/admin', 'users']);
+          else this.sessionService.logout();
+        },
+        error: (error) => {
+          console.error(error);
+          // this.toastr.error('Invalid email or password');
+        },
+      });
   }
 }
-- 
GitLab