|
| 1 | +import { SingletonAppServiceContract, AppService, Priority, Logger, LogLevel } from '@kephas/core'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Notification service. |
| 5 | + * |
| 6 | + * @export |
| 7 | + * @class NotificationService |
| 8 | + */ |
| 9 | +@AppService({ overridePriority: Priority.Low }) |
| 10 | +@SingletonAppServiceContract() |
| 11 | +export class NotificationService { |
| 12 | + /** |
| 13 | + * Gets or sets the logger. |
| 14 | + * |
| 15 | + * @protected |
| 16 | + * @type {Logger} |
| 17 | + * @memberof NotificationService |
| 18 | + */ |
| 19 | + protected logger: Logger; |
| 20 | + |
| 21 | + /** |
| 22 | + * Creates an instance of Notification. |
| 23 | + * @param {Logger} [logger] Optional. The logger. |
| 24 | + * @memberof NotificationService |
| 25 | + */ |
| 26 | + constructor(logger?: Logger) { |
| 27 | + this.logger = logger || new Logger(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Notifies the message at error level. |
| 32 | + * |
| 33 | + * @param {*} data The information to be displayed. |
| 34 | + * @memberof NotificationService |
| 35 | + */ |
| 36 | + public notifyError(data: any) { |
| 37 | + // debugger; |
| 38 | + const severityString = data.severity || LogLevel[LogLevel.Error]; |
| 39 | + const severity = severityString === LogLevel[LogLevel.Warning] |
| 40 | + ? LogLevel.Warning |
| 41 | + : LogLevel.Error; |
| 42 | + this.show(this.formatData(data), severity); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Notifies the message at warning level. |
| 47 | + * |
| 48 | + * @param {*} data The information to be displayed. |
| 49 | + * @memberof NotificationService |
| 50 | + */ |
| 51 | + public notifyWarning(data: any) { |
| 52 | + // debugger; |
| 53 | + const severityString = data.severity || LogLevel[LogLevel.Warning]; |
| 54 | + const severity = severityString === LogLevel[LogLevel.Warning] |
| 55 | + ? LogLevel.Warning |
| 56 | + : LogLevel.Error; |
| 57 | + this.show(this.formatData(data), severity); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Notifies the message at information level. |
| 62 | + * |
| 63 | + * @param {*} data The information to be displayed. |
| 64 | + * @memberof NotificationService |
| 65 | + */ |
| 66 | + public notifyInfo(data: any) { |
| 67 | + this.show(this.formatData(data), LogLevel.Info); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Shows the notification. |
| 72 | + * |
| 73 | + * @protected |
| 74 | + * @param {*} formattedData The formatted data. |
| 75 | + * @param {LogLevel} severity The severity. |
| 76 | + * @memberof NotificationService |
| 77 | + */ |
| 78 | + protected show(formattedData: any, severity: LogLevel) { |
| 79 | + this.logger.log(severity, null, formattedData); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Formats the data. By default it returns a formatted string. |
| 84 | + * |
| 85 | + * @protected |
| 86 | + * @param {*} data The information to be formatted. |
| 87 | + * @returns {string} |
| 88 | + * @memberof NotificationService |
| 89 | + */ |
| 90 | + protected formatData(data: any): any { |
| 91 | + if (!data) { |
| 92 | + return 'Unknown error. Please check the client and server logs for more information.'; |
| 93 | + } |
| 94 | + |
| 95 | + if (data.message && data.url) { |
| 96 | + return `${data.message} (url: ${data.url}).`; |
| 97 | + } |
| 98 | + |
| 99 | + if (typeof (data) === 'object') { |
| 100 | + if (data.error) { |
| 101 | + // this is the case of Kendo data objects. |
| 102 | + if (typeof data.error === 'object') { |
| 103 | + if (data.error.responseStatus) { |
| 104 | + return data.error.responseStatus.message; |
| 105 | + } |
| 106 | + } |
| 107 | + return data.error; |
| 108 | + } |
| 109 | + |
| 110 | + if (data.message) { |
| 111 | + return data.message; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return data; |
| 116 | + } |
| 117 | +} |
0 commit comments