From 41ec7afec095af401ad0f7a974587b8411816785 Mon Sep 17 00:00:00 2001
From: migudel <miguel.moras@estudiantes.uva.es>
Date: Sun, 3 Nov 2024 20:52:39 +0100
Subject: [PATCH] user status update after booking delete

---
 angular/RestClient/angular.json               |  3 ++
 .../user-booking-list.component.ts            | 54 +++++++++++++++++++
 .../src/app/shared/booking.service.ts         |  1 +
 3 files changed, 58 insertions(+)

diff --git a/angular/RestClient/angular.json b/angular/RestClient/angular.json
index 2755b4c..b9017eb 100644
--- a/angular/RestClient/angular.json
+++ b/angular/RestClient/angular.json
@@ -102,5 +102,8 @@
         }
       }
     }
+  },
+  "cli": {
+    "analytics": false
   }
 }
diff --git a/angular/RestClient/src/app/core/features/user/user-booking-list/user-booking-list.component.ts b/angular/RestClient/src/app/core/features/user/user-booking-list/user-booking-list.component.ts
index 535ff56..5f1b3f1 100644
--- a/angular/RestClient/src/app/core/features/user/user-booking-list/user-booking-list.component.ts
+++ b/angular/RestClient/src/app/core/features/user/user-booking-list/user-booking-list.component.ts
@@ -79,10 +79,64 @@ export class UserBookingListComponent {
     this.bookingClient.deleteBooking(bookingId).subscribe({
       next: () => {
         this.updateBookings();
+        this.updateUserStatus();
       },
       error: (err) => {
         console.error('Error al eliminar una reserva', err);
       },
     });
   }
+
+  updateUserStatus() {
+    this.client.getUserBookings(this.userId).subscribe({
+      next: (bookings) => {
+        const withActive = bookings.find(
+          (booking) => this.genBookingState(booking) === 'Reserva activa'
+        );
+        const withInactive = bookings.find(
+          (booking) => this.genBookingState(booking) === 'Reserva inactiva'
+        );
+        if (withActive) {
+          this.client
+            .alterUserStatus(this.userId, 'WITH_ACTIVE_BOOKINGS')
+            .subscribe({
+              next: (response) => {
+                console.log('Cambio de estado en el usuario a activo correcto');
+              },
+              error: (err) => {
+                console.error('Error al cambiar de estado al usuario a activo');
+              },
+            });
+        } else if (withInactive) {
+          this.client
+            .alterUserStatus(this.userId, 'WITH_INACTIVE_BOOKINGS')
+            .subscribe({
+              next: (response) => {
+                console.log(
+                  'Cambio de estado en el usuario a inactivo correcto'
+                );
+              },
+              error: (err) => {
+                console.error(
+                  'Error al cambiar de estado al usuario a inactivo'
+                );
+              },
+            });
+        } else {
+          this.client.alterUserStatus(this.userId, 'NO_BOOKINGS').subscribe({
+            next: (response) => {
+              console.log(
+                'Cambio de estado en el usuario a sin reservas correcto'
+              );
+            },
+            error: (err) => {
+              console.error(
+                'Error al cambiar de estado al usuario sin reservas'
+              );
+            },
+          });
+        }
+      },
+    });
+  }
 }
diff --git a/angular/RestClient/src/app/shared/booking.service.ts b/angular/RestClient/src/app/shared/booking.service.ts
index 0800e99..535f4d5 100644
--- a/angular/RestClient/src/app/shared/booking.service.ts
+++ b/angular/RestClient/src/app/shared/booking.service.ts
@@ -4,6 +4,7 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
 import { Observable } from 'rxjs';
 
 import { Booking } from '../../types/Booking'; // Ajusta la ruta a tu modelo Booking
+import { User, UserState } from '../../types';
 
 @Injectable({
   providedIn: 'root', // Esto hace que el servicio esté disponible en toda la aplicación
-- 
GitLab