From 177b323b2372a0dfa5220a62e9380b57164b000e Mon Sep 17 00:00:00 2001
From: migudel <miguel.moras@estudiantes.uva.es>
Date: Sun, 29 Dec 2024 18:37:42 +0100
Subject: [PATCH] cambios angular

---
 angular/RestClient/src/app/app.routes.ts      | 12 +++---
 .../src/app/core/guards/rol.guard.ts          |  5 ++-
 .../app/features/bookings/bookings.routes.ts  |  3 --
 .../src/app/features/users/admin.routes.ts    |  4 +-
 .../users/user-form/user-form.component.html  |  4 +-
 .../users/user-form/user-form.component.ts    | 38 ++++++++-----------
 .../src/app/features/users/users.routes.ts    |  6 ++-
 docker-compose.yml                            | 32 ++++++++--------
 8 files changed, 49 insertions(+), 55 deletions(-)

diff --git a/angular/RestClient/src/app/app.routes.ts b/angular/RestClient/src/app/app.routes.ts
index 8a24a51..c889563 100644
--- a/angular/RestClient/src/app/app.routes.ts
+++ b/angular/RestClient/src/app/app.routes.ts
@@ -16,14 +16,12 @@ export const routes: AppRoute[] = [
   // Usuario
   {
     path: 'me',
-    canActivate: [rolGuard],
     canActivateChild: [rolGuardChild],
     loadChildren: () => import('./features/users').then((m) => m.USERS_ROUTES),
   },
   // Administrador
   {
     path: 'admin',
-    canActivate: [rolGuard],
     canActivateChild: [rolGuardChild],
     data: { expectedRole: 'ADMIN' },
     loadChildren: () => import('./features/admin').then((m) => m.ADMIN_ROUTES),
@@ -33,9 +31,9 @@ export const routes: AppRoute[] = [
     path: 'unauthorized',
     component: UnauthorizedComponent,
   },
-  // {
-  //   path: '**',
-  //   redirectTo: '/login',
-  //   pathMatch: 'full',
-  // },
+  {
+    path: '**',
+    redirectTo: '/login',
+    pathMatch: 'full',
+  },
 ];
diff --git a/angular/RestClient/src/app/core/guards/rol.guard.ts b/angular/RestClient/src/app/core/guards/rol.guard.ts
index f63b9bc..4688794 100644
--- a/angular/RestClient/src/app/core/guards/rol.guard.ts
+++ b/angular/RestClient/src/app/core/guards/rol.guard.ts
@@ -34,7 +34,7 @@ function verifyRol(expectedRole: UserRol) {
 
   if (!session) {
     console.log('no session');
-    router.navigate(['/login']);
+    router.navigateByUrl(`/login?redirect=${router.url}`);
     return false;
   }
 
@@ -75,7 +75,8 @@ export const rolGuard: CanActivateFn = (route, state) => {
 export const rolGuardChild: CanActivateChildFn = (childRoute, state) => {
   // Obtener el rol de la ruta hija si está especificado y en caso
   // de no especificarse se busca en el/los padres
-  let requiredRol = childRoute.data['rol'] ?? getInheritedRole(childRoute);
+  let requiredRol =
+    childRoute.data['expectedRole'] ?? getInheritedRole(childRoute);
 
   // Si no hay rol especificado se supone libre de verificación
   if (!requiredRol) return true;
diff --git a/angular/RestClient/src/app/features/bookings/bookings.routes.ts b/angular/RestClient/src/app/features/bookings/bookings.routes.ts
index a35688a..50991f2 100644
--- a/angular/RestClient/src/app/features/bookings/bookings.routes.ts
+++ b/angular/RestClient/src/app/features/bookings/bookings.routes.ts
@@ -7,16 +7,13 @@ export const CLIENT_BOOKINGS_ROUTES: AppRoute[] = [
   {
     path: '',
     component: UserBookingListComponent,
-    data: { expectedRole: 'CLIENT' },
   },
   {
     path: ':id',
     component: BookingComponent,
-    data: { expectedRole: 'CLIENT' },
   },
   {
     path: 'new',
     component: BookingComponent,
-    data: { expectedRole: 'CLIENT' },
   },
 ];
diff --git a/angular/RestClient/src/app/features/users/admin.routes.ts b/angular/RestClient/src/app/features/users/admin.routes.ts
index 26ec328..8441e25 100644
--- a/angular/RestClient/src/app/features/users/admin.routes.ts
+++ b/angular/RestClient/src/app/features/users/admin.routes.ts
@@ -2,7 +2,7 @@ import { AppRoute } from '@core/models/Route.interface';
 import { MainPageComponent } from 'app/features/users/main-page/main-page.component';
 import { UserFormRoute } from 'app/features/users/types/UserFormData';
 import { UserFormComponent } from 'app/features/users/user-form/user-form.component';
-import { USERS_ROUTES } from 'app/features/users/users.routes';
+import { COMMON_USERS_ROUTES } from 'app/features/users/users.routes';
 
 function changeToAdminScope(routes: UserFormRoute[]) {
   return routes.map((r) => {
@@ -40,7 +40,7 @@ export const ADMIN_ROUTES: AppRoute[] = [
       },
       {
         path: ':id',
-        children: changeToAdminScope(USERS_ROUTES),
+        children: changeToAdminScope(COMMON_USERS_ROUTES),
       },
     ],
   },
diff --git a/angular/RestClient/src/app/features/users/user-form/user-form.component.html b/angular/RestClient/src/app/features/users/user-form/user-form.component.html
index 0e32cc8..052533f 100644
--- a/angular/RestClient/src/app/features/users/user-form/user-form.component.html
+++ b/angular/RestClient/src/app/features/users/user-form/user-form.component.html
@@ -129,9 +129,9 @@
         } @if (!isViewUser) { @if (isAuth) {
         <p class="text-right">
           @if (isLogin) {
-          <a [routerLink]="['/register']">¿No tienes cuenta?</a>
+          <a [routerLink]="[getRegisterUrl()]">¿No tienes cuenta?</a>
           }@else {
-          <a [routerLink]="['/login']">¿Ya tienes cuenta?</a>
+          <a [routerLink]="[getLoginUrl()]">¿Ya tienes cuenta?</a>
           }
         </p>
         }
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 2f310e1..2a0e8f3 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
@@ -75,6 +75,8 @@ export class UserFormComponent implements OnInit {
   isManager = false;
   isAdmin = false;
 
+  redirect?: string;
+
   constructor(
     private fb: FormBuilder,
     private sessionService: SessionService,
@@ -86,13 +88,8 @@ export class UserFormComponent implements OnInit {
 
   ngOnInit(): void {
     this.setUp();
-
-    // const auth = this.session.getSession();
-    // this.user = auth;
-    // this.userForm.patchValue({
-    //   name: this.user.name,
-    //   email: this.user.email,
-    // });
+    const { redirect } = this.route.snapshot.queryParams;
+    this.redirect = redirect;
   }
 
   private initializeForm(): void {
@@ -112,14 +109,6 @@ export class UserFormComponent implements OnInit {
     const nameNotRequired = emailNotRequired || this.isLogin;
     // Solicitar rol
     const rolNotRequired = !this.isRegister;
-    // console.log({
-    //   confirmIdentity,
-    //   isChangePassword,
-    //   confirmPassword,
-    //   emailNotRequired,
-    //   nameNotRequired,
-    //   rolNotRequired,
-    // });
 
     this.userForm = this.fb.group({
       name: [{ value: '', disabled: nameNotRequired }, Validators.required],
@@ -204,6 +193,14 @@ export class UserFormComponent implements OnInit {
     }
   }
 
+  getRegisterUrl() {
+    return this.redirect ? `/register?redirect=${this.redirect}` : '/register';
+  }
+
+  getLoginUrl() {
+    return this.redirect ? `/login?redirect=${this.redirect}` : '/login';
+  }
+
   getHotelsUri() {
     const basePath = getBasePath(this.router.url); // Obtener la base: '/me' o '/users/:id'
     return `${basePath}/hotels`;
@@ -326,11 +323,11 @@ export class UserFormComponent implements OnInit {
   private login(email: string, password: string) {
     this.sessionService.login(email, password).subscribe({
       next: (r: any) => {
-        this.router.navigateByUrl(r.mainPage);
+        if (this.redirect) this.router.navigateByUrl(this.redirect);
+        else this.router.navigateByUrl(r.mainPage);
       },
       error: (error) => {
         console.error(error);
-        // this.toastr.error('Invalid email or password');
       },
     });
   }
@@ -344,11 +341,11 @@ export class UserFormComponent implements OnInit {
     console.log({ name, email, password, rol });
     this.sessionService.register(name, email, password, rol).subscribe({
       next: (r: any) => {
-        this.router.navigateByUrl(r.mainPage);
+        if (this.redirect) this.router.navigateByUrl(this.redirect);
+        else this.router.navigateByUrl(r.mainPage);
       },
       error: (error) => {
         console.error(error);
-        // this.toastr.error('Invalid email or password');
       },
     });
   }
@@ -360,7 +357,6 @@ export class UserFormComponent implements OnInit {
       },
       error: (error) => {
         console.error(error);
-        // this.toastr.error('Invalid email or password');
       },
     });
   }
@@ -376,7 +372,6 @@ export class UserFormComponent implements OnInit {
         },
         error: (error) => {
           console.error(error);
-          // this.toastr.error('Invalid email or password');
         },
       });
   }
@@ -399,7 +394,6 @@ export class UserFormComponent implements OnInit {
         },
         error: (error) => {
           console.error(error);
-          // this.toastr.error('Invalid email or password');
         },
       });
   }
diff --git a/angular/RestClient/src/app/features/users/users.routes.ts b/angular/RestClient/src/app/features/users/users.routes.ts
index b227130..8ed107a 100644
--- a/angular/RestClient/src/app/features/users/users.routes.ts
+++ b/angular/RestClient/src/app/features/users/users.routes.ts
@@ -1,7 +1,7 @@
 import { UserFormComponent } from './user-form/user-form.component';
 import { UserFormRoute } from './types/UserFormData';
 
-export const USERS_ROUTES: UserFormRoute[] = [
+export const COMMON_USERS_ROUTES: UserFormRoute[] = [
   // Common
   {
     path: '',
@@ -30,6 +30,10 @@ export const USERS_ROUTES: UserFormRoute[] = [
     },
     component: UserFormComponent,
   },
+];
+
+export const USERS_ROUTES: UserFormRoute[] = [
+  ...COMMON_USERS_ROUTES,
   {
     // Usuario administrador de hoteles
     path: 'hotels',
diff --git a/docker-compose.yml b/docker-compose.yml
index 2244d32..522252a 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -11,22 +11,22 @@ services:
   #######################################
   # Cliente Angular
   #######################################
-  # RoomsBooking-Web:
-  #   image: roomsbooking-web-image
-  #   build:
-  #     context: ./angular/RestClient
-  #     dockerfile: ./Dockerfile
-  #   restart: unless-stopped
-  #   ports:
-  #     - 4200:80
-  #   networks:
-  #     - kong-net
-  #   depends_on:
-  #     - kong
-  #     - Auth-API
-  #     - Users-API
-  #     - Hotels-API
-  #     - Bookings-API
+  RoomsBooking-Web:
+    image: roomsbooking-web-image
+    build:
+      context: ./angular/RestClient
+      dockerfile: ./Dockerfile
+    restart: unless-stopped
+    ports:
+      - 4200:80
+    networks:
+      - kong-net
+    depends_on:
+      - kong
+      - Auth-API
+      - Users-API
+      - Hotels-API
+      - Bookings-API
 
   #######################################
   # Kong
-- 
GitLab