import { Component, OnInit } from '@angular/core'; import { cloneDeep } from 'lodash'; import { Subscription } from 'rxjs'; import { STATUS_TOAST_TOP } from '../../../mockup'; import { UserConfigService } from '../../../utils/user-config.service'; import { ToastService } from '../../generic/toast/toast.service'; const moment = require('moment').default || require('moment'); @Component({ selector: 'app-alerts', templateUrl: './app-alerts.component.html', styleUrls: ['./app-alerts.component.scss'], }) export class AppAlertsComponent implements OnInit { private applicationCast$: Subscription; downTimeMessage; systemDown; i18n; appConfig; downTime; constructor(private userConfig: UserConfigService, private toastService: ToastService) {} ngOnInit(): void { const currentApp = this.userConfig.currentApp; this.userConfig.getAppConfig(currentApp); this.userConfig.cast.subscribe(response => { if (response['configJson']) { this.i18n = response['i18n']; this.appConfig = response['configJson']['app-alerts'] ? response['configJson']['app-alerts'] : response['configJson']; } }); this.checkForAlerts(); } checkForAlerts(): void { this.applicationCast$ = this.userConfig.applicationcast.subscribe(response => { if (response['userConfig']['appAlerts']) { this.applicationCast$.unsubscribe(); const toastConfigArr = []; const appAlerts = response['userConfig']['appAlerts']; const currentAppId = String(response['appId']); if (appAlerts !== undefined) { //now is current time without format const now = moment(new Date()); //sorting all alert in ASC order appAlerts[currentAppId]?.sort(function (alert1, alert2) { return moment.duration(moment(alert1.parameters[0]).diff(now)).asMinutes() - moment.duration(moment(alert2.parameters[0]).diff(now)).asMinutes() } ); const toastConfig = cloneDeep(STATUS_TOAST_TOP); if(appAlerts[currentAppId]?.length) { //taking latest alert to show const alert = appAlerts[currentAppId][0]; // fromDate and toDate are the formated times of start and end of maintenance msg const fromDate = moment(alert.parameters[0]).format('MM/DD/YYYY HH:mm:ss'); const toDate = moment(alert.parameters[1]).format('MM/DD/YYYY HH:mm:ss'); const currentDate = moment(); // fromDate and toDate are without formated times of start and end of maintenance msg const frmDateNoFormat = moment(alert.parameters[0]); const tilDateNoFormat = moment(alert.parameters[1]); // timeToOutage is difference of current time and the start time of maintenance(in Minutes). let timeToOutage = moment.duration(frmDateNoFormat.diff(now)).asMinutes(); // timeToOutage is difference of current time and the start time of maintenance(in Seconds). let timeToOutageSecound = moment.duration(frmDateNoFormat.diff(now)).asSeconds(); // timeToOutage is difference of current time and the end time of maintenance(in Seconds). let timeTilOutageSecound = moment.duration(tilDateNoFormat.diff(now)).asSeconds(); let remainingTime: string; let toastMessage: string; if (timeToOutage > 0) { // if current time cross lead hours time. if (timeToOutage < alert.displayLeadHours * 60) { toastMessage = alert.message; //if difference between current time and maintenance start time is less than 1 hour if (timeToOutage < 60) { remainingTime = `${timeToOutage.toFixed()} ${this.i18n['translations'][this.appConfig['minuteMinutes']]}`; if (timeToOutage < 1) { remainingTime = this.i18n['translations'][this.appConfig['lessThanAMinute']]; } } else { //if difference between current time and maintenance start time is greater than 1 hour const timeToOutageArr = (timeToOutage / 60).toFixed(2).split('.'); remainingTime = `${timeToOutageArr[0]} ${ Number(timeToOutageArr[0]) > 1 ? this.i18n['translations'][this.appConfig['hours']] : this.i18n['translations'][this.appConfig['hour']] } ${this.i18n['translations'][this.appConfig['and']]} ${Math.ceil(parseInt(timeToOutageArr[1]) * 0.6)} ${ Number(timeToOutageArr[1]) > 1 ? this.i18n['translations'][this.appConfig['minutes']] : this.i18n['translations'][this.appConfig['minute']] }`; } this.loadNextScreen(timeToOutageSecound); toastConfig.data.content.header = ''; toastConfig.data.onClose = this.closeToast.bind(this); toastConfig.data.content.message = toastMessage .replace('{0}', fromDate) .replace('{1}', toDate) .replace('{minutes}', remainingTime); this.toastService.addToast(toastConfig); toastConfigArr.push(toastConfig); } } else { // when current time reached to the maintenance start time, below code enable maintenance msg if (currentDate.isBetween(fromDate, toDate)) { this.systemDown = true; this.downTimeMessage = this.i18n['translations'][this.appConfig['downTimeMessage']]; this.downTime = `${fromDate} ${this.i18n['translations'][this.appConfig['to']]} ${toDate}`; this.loadNextScreen(timeTilOutageSecound); } } } if (this.systemDown) { this.toastService.deleteToasts(); this.userConfig.systemDown.next(true); } else { this.userConfig.systemDown.next(false); } } } }); } loadNextScreen(time): void { setTimeout(() => { window.location.reload(); }, time * 1000); } private closeToast(config) { this.toastService.deleteToast(config); } }