]>
Commit | Line | Data |
---|---|---|
2f1548fd | 1 | import { catchError, map, tap } from 'rxjs/operators' |
67ed6552 C |
2 | import { HttpClient, HttpParams } from '@angular/common/http' |
3 | import { Injectable } from '@angular/core' | |
4 | import { ComponentPaginationLight, RestExtractor, RestService, User, UserNotificationSocket } from '@app/core' | |
5 | import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '@shared/models' | |
6 | import { environment } from '../../../../environments/environment' | |
9a39392a | 7 | import { UserNotification } from './user-notification.model' |
654a188f | 8 | import { SortMeta } from 'primeng/api' |
2f1548fd C |
9 | |
10 | @Injectable() | |
11 | export class UserNotificationService { | |
12 | static BASE_NOTIFICATIONS_URL = environment.apiUrl + '/api/v1/users/me/notifications' | |
13 | static BASE_NOTIFICATION_SETTINGS = environment.apiUrl + '/api/v1/users/me/notification-settings' | |
14 | ||
2f1548fd | 15 | constructor ( |
2f1548fd C |
16 | private authHttp: HttpClient, |
17 | private restExtractor: RestExtractor, | |
9a39392a C |
18 | private restService: RestService, |
19 | private userNotificationSocket: UserNotificationSocket | |
2f1548fd C |
20 | ) {} |
21 | ||
654a188f RK |
22 | listMyNotifications (parameters: { |
23 | pagination: ComponentPaginationLight | |
24 | ignoreLoadingBar?: boolean | |
25 | unread?: boolean, | |
26 | sort?: SortMeta | |
27 | }) { | |
28 | const { pagination, ignoreLoadingBar, unread, sort } = parameters | |
29 | ||
2f1548fd | 30 | let params = new HttpParams() |
654a188f | 31 | params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination), sort) |
2f1548fd C |
32 | |
33 | if (unread) params = params.append('unread', `${unread}`) | |
34 | ||
35 | const headers = ignoreLoadingBar ? { ignoreLoadingBar: '' } : undefined | |
36 | ||
37 | return this.authHttp.get<ResultList<UserNotification>>(UserNotificationService.BASE_NOTIFICATIONS_URL, { params, headers }) | |
38 | .pipe( | |
39 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
40 | map(res => this.restExtractor.applyToResultListData(res, this.formatNotification.bind(this))), | |
41 | catchError(err => this.restExtractor.handleError(err)) | |
42 | ) | |
43 | } | |
44 | ||
45 | countUnreadNotifications () { | |
654a188f | 46 | return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true }) |
2f1548fd C |
47 | .pipe(map(n => n.total)) |
48 | } | |
49 | ||
2f1548fd C |
50 | markAsRead (notification: UserNotification) { |
51 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read' | |
52 | ||
53 | const body = { ids: [ notification.id ] } | |
54 | const headers = { ignoreLoadingBar: '' } | |
55 | ||
56 | return this.authHttp.post(url, body, { headers }) | |
57 | .pipe( | |
58 | map(this.restExtractor.extractDataBool), | |
9a39392a | 59 | tap(() => this.userNotificationSocket.dispatch('read')), |
2f1548fd C |
60 | catchError(res => this.restExtractor.handleError(res)) |
61 | ) | |
62 | } | |
63 | ||
64 | markAllAsRead () { | |
65 | const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all' | |
66 | const headers = { ignoreLoadingBar: '' } | |
67 | ||
68 | return this.authHttp.post(url, {}, { headers }) | |
69 | .pipe( | |
70 | map(this.restExtractor.extractDataBool), | |
9a39392a | 71 | tap(() => this.userNotificationSocket.dispatch('read-all')), |
2f1548fd C |
72 | catchError(res => this.restExtractor.handleError(res)) |
73 | ) | |
74 | } | |
75 | ||
76 | updateNotificationSettings (user: User, settings: UserNotificationSetting) { | |
77 | const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS | |
78 | ||
79 | return this.authHttp.put(url, settings) | |
80 | .pipe( | |
81 | map(this.restExtractor.extractDataBool), | |
82 | catchError(res => this.restExtractor.handleError(res)) | |
83 | ) | |
84 | } | |
85 | ||
2f1548fd C |
86 | private formatNotification (notification: UserNotificationServer) { |
87 | return new UserNotification(notification) | |
88 | } | |
89 | } |