From 06ee0178d0433d4a216598a2c48ff8c102e6d9f0 Mon Sep 17 00:00:00 2001
From: Etienne Loupias <eloupias@grandlyon.com>
Date: Mon, 9 Dec 2024 17:15:12 +0100
Subject: [PATCH] Check if it is the user's own offer

---
 src/app/profile/services/profile.service.ts        | 14 +++++++++-----
 .../structure-edition-summary.component.ts         |  4 ++--
 src/app/services/auth.service.ts                   | 10 ++++++++++
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/app/profile/services/profile.service.ts b/src/app/profile/services/profile.service.ts
index b1baf54e7..511ad60d9 100644
--- a/src/app/profile/services/profile.service.ts
+++ b/src/app/profile/services/profile.service.ts
@@ -1,6 +1,5 @@
 import { HttpClient } from '@angular/common/http';
 import { Injectable } from '@angular/core';
-import decode from 'jwt-decode';
 import { Observable, lastValueFrom } from 'rxjs';
 import { catchError, map } from 'rxjs/operators';
 import { Employer } from '../../models/employer.model';
@@ -23,6 +22,14 @@ export class ProfileService {
     private notificationService: NotificationService,
   ) {}
 
+  public getId(): string {
+    if (this.authService.isLoggedIn()) {
+      const tokenPayload = this.authService.tokenPayload;
+      return tokenPayload?.id;
+    }
+    return null;
+  }
+
   public async getProfile(): Promise<User> {
     if (this.authService.isLoggedIn()) {
       const profile = await lastValueFrom(this.http.get<User>(`${this.baseUrl}/profile`));
@@ -55,10 +62,7 @@ export class ProfileService {
 
   public isAdmin(): boolean {
     if (this.authService.isLoggedIn()) {
-      const user = this.authService.userValue;
-      const token = user.accessToken;
-      // decode the token to get its payload
-      const tokenPayload: User = decode(token);
+      const tokenPayload = this.authService.tokenPayload;
       if (tokenPayload.role === UserRole.admin) {
         return true;
       }
diff --git a/src/app/profile/structure-edition-summary/structure-edition-summary.component.ts b/src/app/profile/structure-edition-summary/structure-edition-summary.component.ts
index 1a7468287..6430bef42 100644
--- a/src/app/profile/structure-edition-summary/structure-edition-summary.component.ts
+++ b/src/app/profile/structure-edition-summary/structure-edition-summary.component.ts
@@ -101,8 +101,8 @@ export class StructureEditionSummaryComponent implements OnInit {
 
           // Get categories labels
           if (personalOffer) {
-            // TODO : recuperer si c'est le user connecté en utilisant le profileService, mais a priori le profileService a pas l'id du user...
-            const isMyself = true;
+            // Check if it is the user's own offer
+            const isMyself = this.profileService.getId() === member._id;
 
             const offerHolder = {
               user: member,
diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts
index 6e4382da4..afe7b881a 100644
--- a/src/app/services/auth.service.ts
+++ b/src/app/services/auth.service.ts
@@ -1,5 +1,6 @@
 import { HttpClient } from '@angular/common/http';
 import { Injectable } from '@angular/core';
+import decode from 'jwt-decode';
 import { DateTime } from 'luxon';
 import { BehaviorSubject, Observable } from 'rxjs';
 import { map } from 'rxjs/operators';
@@ -33,6 +34,15 @@ export class AuthService {
     return null;
   }
 
+  public get tokenPayload(): User {
+    const token = this.token;
+    if (!token) return null;
+
+    // decode the token to get its payload
+    const tokenPayload: User = decode(token);
+    return tokenPayload;
+  }
+
   public logout(): void {
     localStorage.removeItem('user');
     this.userSubject.next(null);
-- 
GitLab