import { Injectable , Injector } from '@angular/core' ; import { EntityDtoOfGuid , NotificationServiceProxy } from '@shared/service-proxies/service-proxies' ; import * as Push from 'push.js' ; import { NotificationSettingsModalComponent } from './notification-settings-modal.component' ; import { AppConsts } from '@shared/AppConsts' ; import { ModalHelper } from '@delon/theme' ; import { DateTimeService } from '@app/shared/common/timing/date-time.service' ; export interface IFormattedUserNotification { userNotificationId : string ; text : string ; time : string ; creationTime : Date ; icon : string ; state : String ; data : any ; url : string ; isUnread : boolean ; } @ Injectable ( ) export class UserNotificationHelper { constructor ( private _modalHelper , private _notificationService , private _dateTimeService ) { } getUrl ( userNotification ) { switch ( userNotification . notification . notificationName ) { case 'App.NewUserRegistered' : return '/app/admin/users?filterText=' + userNotification . notification . data . properties . emailAddress ; case 'App.NewTenantRegistered' : return '/app/admin/tenants?filterText=' + userNotification . notification . data . properties . tenancyName ; case 'App.GdprDataPrepared' : return AppConsts . remoteServiceBaseUrl + '/File/DownloadBinaryFile?id=' + userNotification . notification . data . properties . binaryObjectId + '&contentType=application/zip&fileName=collectedData.zip' ; } return '' ; } getUiIconBySeverity ( severity ) { switch ( severity ) { case abp . notifications . severity . SUCCESS : return 'check-circle-o' ; case abp . notifications . severity . WARN : return 'warning' ; case abp . notifications . severity . ERROR : return 'close-circle-o' ; case abp . notifications . severity . FATAL : return 'close-circle-o' ; case abp . notifications . severity . INFO : default : return 'info-circle-o' ; } } format ( userNotification , truncateText ? ) { let formatted = { userNotificationId : userNotification . id , text : abp . notifications . getFormattedMessageFromUserNotification ( userNotification ) , time : this . _dateTimeService . formatJSDate ( userNotification . notification . creationTime , 'yyyy-LL-dd HH:mm:ss' ) , creationTime : userNotification . notification . creationTime , icon : this . getUiIconBySeverity ( userNotification . notification . severity ) , state : abp . notifications . getUserNotificationStateAsString ( userNotification . state ) , data : userNotification . notification . data , url : this . getUrl ( userNotification ) , isUnread : userNotification . state === abp . notifications . userNotificationState . UNREAD } ; if ( truncateText || truncateText === undefined ) { formatted . text = abp . utils . truncateStringWithPostfix ( formatted . text , 100 ) ; } return formatted ; } show ( userNotification ) { abp . notifications . showUiNotifyForUserNotification ( userNotification , { 'onclick' : ( ) => { let url = this . getUrl ( userNotification ) ; if ( url ) { location . href = url ; } } } ) ; if ( Push . default . Permission . has ( ) ) { Push . default . create ( 'AbpZeroTemplate' , { body : this . format ( userNotification ) . text , icon : abp . appPath + 'assets/common/images/app-logo-small.png' , timeout : 6000 , onClick : function ( ) { window . focus ( ) ; this . close ( ) ; } } ) ; } } setAllAsRead ( callback ? ) { this . _notificationService . setAllNotificationsAsRead ( ) . subscribe ( ( ) => { abp . event . trigger ( 'app.notifications.refresh' ) ; if ( callback ) { callback ( ) ; } } ) ; } setAsRead ( userNotificationId , callback ? ) { const input = new EntityDtoOfGuid ( ) ; input . id = userNotificationId ; this . _notificationService . setNotificationAsRead ( input ) . subscribe ( ( ) => { abp . event . trigger ( 'app.notifications.read' , userNotificationId ) ; if ( callback ) { callback ( userNotificationId ) ; } } ) ; } openSettingsModal ( ) { this . _modalHelper . createStatic ( NotificationSettingsModalComponent , { } , { size : 'md' } ) . subscribe ( ( ) => { } ) ; } }