From 541949e7d81788912061345cd9c402aab43604e5 Mon Sep 17 00:00:00 2001
From: Etienne Loupias <eloupias@grandlyon.com>
Date: Mon, 17 Mar 2025 08:53:36 +0100
Subject: [PATCH] improve test patch

---
 .../orientation-details.component.ts          | 44 ++++++++++++-------
 1 file changed, 28 insertions(+), 16 deletions(-)

diff --git a/src/app/profile/dashboard/orientation-details/orientation-details.component.ts b/src/app/profile/dashboard/orientation-details/orientation-details.component.ts
index f8bc43c44..d0e4ccc41 100644
--- a/src/app/profile/dashboard/orientation-details/orientation-details.component.ts
+++ b/src/app/profile/dashboard/orientation-details/orientation-details.component.ts
@@ -1,7 +1,9 @@
 import { Component, OnInit } from '@angular/core';
 import { ActivatedRoute } from '@angular/router';
+import { User } from '../../../models/user.model';
 import { NotificationService } from '../../../services/notification.service';
 import { OrientationService } from '../../../services/orientation.service';
+import { ProfileService } from '../../services/profile.service';
 
 @Component({
   selector: 'app-orientation-details',
@@ -13,10 +15,12 @@ export class OrientationDetailsComponent implements OnInit {
     private route: ActivatedRoute,
     public orientationService: OrientationService,
     private notificationService: NotificationService,
+    private profileService: ProfileService,
   ) {}
 
   public orientation;
   public isModalOpenned = false;
+  public userProfile: User;
 
   ngOnInit(): void {
     this.route.params.subscribe((urlParams) => {
@@ -27,6 +31,9 @@ export class OrientationDetailsComponent implements OnInit {
         });
       }
     });
+    this.profileService.getProfile().then((profile) => {
+      this.userProfile = new User(profile);
+    });
   }
 
   public goBack(): void {
@@ -40,29 +47,34 @@ export class OrientationDetailsComponent implements OnInit {
   public closeOrientation(event): void {
     if (event) {
       // close orientation
-
       // wip test patch
-      this.orientationService.patchOrientation(this.orientation._id, { status: 'completed' }).subscribe({
-        next: (orientation) => {
-          this.notificationService.showSuccess("L'orientation a bien été mise à jour.");
-          // Update the status displayed on the orientation details page
-          this.orientation.status = orientation.status;
-          // After the successful update, close the modal
-          this.isModalOpenned = false;
-        },
-        error: () => {
-          this.notificationService.showErrorPleaseRetry(
-            "Une erreur s'est produite lors de la mise à jour de l'orientation.",
-          );
-        },
+      this.patchOrientation({
+        status: 'completed',
+        closingComment: 'closing comment',
+        closingUser: this.userProfile._id,
+        closingDate: new Date(),
       });
-    } else {
-      this.isModalOpenned = false;
     }
+    this.isModalOpenned = false;
   }
 
   public setOrientationStatus(status): void {
     // set status
     console.log(status);
   }
+
+  private patchOrientation(updatedFields): void {
+    this.orientationService.patchOrientation(this.orientation._id, updatedFields).subscribe({
+      next: (orientation) => {
+        this.notificationService.showSuccess("L'orientation a bien été mise à jour.");
+        // Display the updated values on the orientation details page
+        this.orientation = orientation;
+      },
+      error: () => {
+        this.notificationService.showErrorPleaseRetry(
+          "Une erreur s'est produite lors de la mise à jour de l'orientation.",
+        );
+      },
+    });
+  }
 }
-- 
GitLab