1 import { Injectable } from '@angular/core'
2 import { HttpClient, HttpParams } from '@angular/common/http'
3 import { RestExtractor, RestService } from '../rest'
4 import { catchError, map, tap } from 'rxjs/operators'
5 import { environment } from '../../../environments/environment'
6 import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '../../../../../shared'
7 import { UserNotification } from './user-notification.model'
8 import { AuthService } from '../../core'
9 import { ComponentPagination } from '../rest/component-pagination.model'
10 import { User } from '../users/user.model'
11 import { UserNotificationSocket } from '@app/core/notification/user-notification-socket.service'
14 export class UserNotificationService {
15 static BASE_NOTIFICATIONS_URL = environment.apiUrl + '/api/v1/users/me/notifications'
16 static BASE_NOTIFICATION_SETTINGS = environment.apiUrl + '/api/v1/users/me/notification-settings'
19 private auth: AuthService,
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor,
22 private restService: RestService,
23 private userNotificationSocket: UserNotificationSocket
26 listMyNotifications (pagination: ComponentPagination, unread?: boolean, ignoreLoadingBar = false) {
27 let params = new HttpParams()
28 params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination))
30 if (unread) params = params.append('unread', `${unread}`)
32 const headers = ignoreLoadingBar ? { ignoreLoadingBar: '' } : undefined
34 return this.authHttp.get<ResultList<UserNotification>>(UserNotificationService.BASE_NOTIFICATIONS_URL, { params, headers })
36 map(res => this.restExtractor.convertResultListDateToHuman(res)),
37 map(res => this.restExtractor.applyToResultListData(res, this.formatNotification.bind(this))),
38 catchError(err => this.restExtractor.handleError(err))
42 countUnreadNotifications () {
43 return this.listMyNotifications({ currentPage: 1, itemsPerPage: 0 }, true)
44 .pipe(map(n => n.total))
47 markAsRead (notification: UserNotification) {
48 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read'
50 const body = { ids: [ notification.id ] }
51 const headers = { ignoreLoadingBar: '' }
53 return this.authHttp.post(url, body, { headers })
55 map(this.restExtractor.extractDataBool),
56 tap(() => this.userNotificationSocket.dispatch('read')),
57 catchError(res => this.restExtractor.handleError(res))
62 const url = UserNotificationService.BASE_NOTIFICATIONS_URL + '/read-all'
63 const headers = { ignoreLoadingBar: '' }
65 return this.authHttp.post(url, {}, { headers })
67 map(this.restExtractor.extractDataBool),
68 tap(() => this.userNotificationSocket.dispatch('read-all')),
69 catchError(res => this.restExtractor.handleError(res))
73 updateNotificationSettings (user: User, settings: UserNotificationSetting) {
74 const url = UserNotificationService.BASE_NOTIFICATION_SETTINGS
76 return this.authHttp.put(url, settings)
78 map(this.restExtractor.extractDataBool),
79 catchError(res => this.restExtractor.handleError(res))
83 private formatNotification (notification: UserNotificationServer) {
84 return new UserNotification(notification)