All files / app/services contact.service.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 8/8
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                                                                                                                                                  1x 18x 18x 18x           2x             2x                   3x                   2x                   1x                     2x                   2x                     3x              
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '../config/environment';
import { AuthService } from './auth.service';
 
export interface ContactMessage {
  id: number;
  name: string;
  email: string;
  message: string;
  read: boolean;
  createdAt: string;
  readAt?: string;
}
 
export interface ContactRequest {
  name: string;
  email: string;
  message: string;
}
 
export interface ContactResponse {
  message: string;
}
 
export interface PageContactMessage {
  content: ContactMessage[];
  totalElements: number;
  totalPages: number;
  size: number;
  number: number;
  first: boolean;
  last: boolean;
}
 
export interface UnreadCountResponse {
  count: number;
}
 
export interface ReplyRequest {
  subject: string;
  message: string;
}
 
export interface ReplyResponse {
  message: string;
  replyId?: number;
}
 
export interface ConversationReply {
  id: number;
  subject: string;
  message: string;
  isFromUser: boolean;
  createdAt: string;
}
 
export interface Conversation {
  originalMessage: {
    id: number;
    name: string;
    email: string;
    message: string;
    createdAt: string;
    isFromUser: boolean;
  };
  replies: ConversationReply[];
}
 
@Injectable({
  providedIn: 'root'
})
export class ContactService {
  private http = inject(HttpClient);
  private authService = inject(AuthService);
  private readonly API_URL = `${environment.apiUrl}/contact`;
 
  /**
   * Envoyer un message de contact
   */
  sendMessage(request: ContactRequest): Observable<ContactResponse> {
    return this.http.post<ContactResponse>(`${this.API_URL}/send`, request);
  }
 
  /**
   * Récupérer tous les messages (paginé) - Admin uniquement
   */
  getAllMessages(page: number = 0, size: number = 20): Observable<PageContactMessage> {
    return this.http.get<PageContactMessage>(
      `${this.API_URL}/messages?page=${page}&size=${size}`,
      { headers: this.authService.getAuthHeaders() }
    );
  }
 
  /**
   * Récupérer les messages non lus - Admin uniquement
   */
  getUnreadMessages(): Observable<ContactMessage[]> {
    return this.http.get<ContactMessage[]>(
      `${this.API_URL}/messages/unread`,
      { headers: this.authService.getAuthHeaders() }
    );
  }
 
  /**
   * Compter les messages non lus - Admin uniquement
   */
  getUnreadCount(): Observable<UnreadCountResponse> {
    return this.http.get<UnreadCountResponse>(
      `${this.API_URL}/messages/unread/count`,
      { headers: this.authService.getAuthHeaders() }
    );
  }
 
  /**
   * Marquer un message comme lu - Admin uniquement
   */
  markAsRead(messageId: number): Observable<{ message: string }> {
    return this.http.patch<{ message: string }>(
      `${this.API_URL}/messages/${messageId}/read`,
      {},
      { headers: this.authService.getAuthHeaders() }
    );
  }
 
  /**
   * Récupérer un message par ID - Admin uniquement
   */
  getMessage(messageId: number): Observable<ContactMessage> {
    return this.http.get<ContactMessage>(
      `${this.API_URL}/messages/${messageId}`,
      { headers: this.authService.getAuthHeaders() }
    );
  }
 
  /**
   * Répondre à un message de contact - Admin et Employee uniquement
   */
  replyToMessage(messageId: number, request: ReplyRequest): Observable<ReplyResponse> {
    return this.http.post<ReplyResponse>(
      `${this.API_URL}/messages/${messageId}/reply`,
      request,
      { headers: this.authService.getAuthHeaders() }
    );
  }
 
  /**
   * Récupérer la conversation complète (message original + réponses) - Admin et Employee uniquement
   */
  getConversation(messageId: number): Observable<Conversation> {
    return this.http.get<Conversation>(
      `${this.API_URL}/messages/${messageId}/conversation`,
      { headers: this.authService.getAuthHeaders() }
    );
  }
}