Project

General

Profile

1 61381 k.triantaf
import {Injectable} from "@angular/core";
2
import {HttpClient} from "@angular/common/http";
3
import {properties} from "../../../environments/environment";
4
import {Observable} from "rxjs";
5
import {Notification, NotificationUser} from "./notifications";
6
import {CustomOptions} from "../services/servicesUtils/customOptions.class";
7
import {not} from "rxjs/internal-compatibility";
8
import {map} from "rxjs/operators";
9
import {StringUtils} from '../utils/string-utils.class';
10
11
@Injectable({
12
  providedIn: "root"
13
})
14
export class NotificationService {
15
16
17
  constructor(private httpClient: HttpClient) {
18
  }
19
20
  public getAllNotifications(): Observable<Notification[]> {
21
    return this.httpClient.get<Notification[]>(properties.notificationsAPIURL + 'all', CustomOptions.registryOptions()).pipe(map(notifications => {
22
      notifications.forEach(notification => {
23
        this.formatNotification(notification);
24
      })
25
      return notifications;
26
    }));
27
  }
28
29
  public getNotifications(service: string): Observable<Notification[]> {
30
    return this.httpClient.get<Notification[]>(properties.notificationsAPIURL + encodeURIComponent(service), CustomOptions.registryOptions()).pipe(map(notifications => {
31
      notifications.forEach(notification => {
32
        this.formatNotification(notification);
33
      })
34
      return notifications;
35
    }));
36
  }
37
38
  public sendNotification(notification: Notification): Observable<Notification> {
39
    return this.httpClient.post<Notification>(properties.notificationsAPIURL + 'save', notification, CustomOptions.registryOptions());
40
  }
41
42
  public readNotification(id: string): Observable<NotificationUser> {
43
    return this.httpClient.put<NotificationUser>(properties.notificationsAPIURL + encodeURIComponent(id), null, CustomOptions.registryOptions());
44
  }
45
46
  private formatNotification(notification: Notification): Notification {
47
    if (notification.title) {
48
      notification.preview = notification.title;
49
    } else {
50
      notification.preview = StringUtils.HTMLToString(notification.message);
51
    }
52
    return notification;
53
  }
54
}