[CORD-1858] Avoid event duplication and skipping notification for parent models

Change-Id: I8815de31b0adb731226ae26bedbe3598bde04aca
(cherry picked from commit 31daa80018ef5014691dd49d9ad71f8c5638d416)
diff --git a/src/app/core/header/header.spec.ts b/src/app/core/header/header.spec.ts
index 13f8b5b..eb236b1 100644
--- a/src/app/core/header/header.spec.ts
+++ b/src/app/core/header/header.spec.ts
@@ -51,11 +51,26 @@
 const infoNotification = {
   model: 'TestModel',
   msg: {
-    changed_fields: ['backend_status'],
+    changed_fields: ['backend_status', 'backend_code'],
     pk: 1,
     object: {
       name: 'TestName',
-      backend_status: '0 - In Progress'
+      backend_status: 'In Progress',
+      backend_code: 0
+    }
+  }
+};
+
+const noNotification = {
+  model: 'TestModel',
+  skip_notification: true,
+  msg: {
+    changed_fields: ['backend_status', 'backend_code'],
+    pk: 1,
+    object: {
+      name: 'TestName',
+      backend_status: 'In Progress',
+      backend_code: 0
     }
   }
 };
@@ -74,6 +89,9 @@
       .value('toastrConfig', MockToastrConfig)
       .value('AuthService', MockAuth)
       .value('XosNavigationService', {})
+      .value('ConfigHelpers', {
+        stateWithParamsForJs: () => null
+      })
       .value('XosKeyboardShortcut', MockXosKeyboardShortcut)
       .value('StyleConfig', {
         logo: 'cord-logo.png',
@@ -92,6 +110,7 @@
 
     // clear notifications
     isolatedScope.notifications = [];
+    MockToastr.info.calls.reset();
   }));
 
   it('should render the appropriate logo', () => {
@@ -116,6 +135,8 @@
   });
 
   it('should configure toastr', () => {
+    delete MockToastrConfig['onTap'];
+
     expect(MockToastrConfig).toEqual({
       newestOnTop: false,
       positionClass: 'toast-top-right',
@@ -129,7 +150,14 @@
     sendEvent(infoNotification);
     scope.$digest();
 
-    expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel');
+    expect(MockToastr.info).toHaveBeenCalledWith('Synchronization started for: TestName', 'TestModel', {extraData: {dest: null}});
+  });
+
+  it('should not display a toastr for a new event that use skip_notification', () => {
+    sendEvent(noNotification);
+    scope.$digest();
+
+    expect(MockToastr.info).not.toHaveBeenCalled();
   });
 
   // TODO test error and success toaster call
diff --git a/src/app/core/header/header.ts b/src/app/core/header/header.ts
index a89199d..a465f8f 100644
--- a/src/app/core/header/header.ts
+++ b/src/app/core/header/header.ts
@@ -26,13 +26,29 @@
 import {IXosStyleConfig} from '../../../index';
 import {IXosSearchService, IXosSearchResult} from '../../datasources/helpers/search.service';
 import {IXosKeyboardShortcutService} from '../services/keyboard-shortcut';
+import {Subscription} from 'rxjs';
+import {IXosConfigHelpersService} from '../services/helpers/config.helpers';
 
 export interface INotification extends IWSEvent {
   viewed?: boolean;
 }
 
 class HeaderController {
-  static $inject = ['$scope', '$rootScope', '$state', 'AuthService', 'SynchronizerStore', 'toastr', 'toastrConfig', 'XosNavigationService', 'StyleConfig', 'SearchService', 'XosKeyboardShortcut'];
+  static $inject = [
+    '$log',
+    '$scope',
+    '$rootScope',
+    '$state',
+    'AuthService',
+    'SynchronizerStore',
+    'toastr',
+    'toastrConfig',
+    'XosNavigationService',
+    'StyleConfig',
+    'SearchService',
+    'XosKeyboardShortcut',
+    'ConfigHelpers'
+  ];
   public notifications: INotification[] = [];
   public newNotifications: INotification[] = [];
   public version: string;
@@ -42,7 +58,10 @@
   public query: string;
   public search: (query: string) => any[];
 
+  private syncStoreSubscription: Subscription;
+
   constructor(
+    private $log: ng.ILogService,
     private $scope: angular.IScope,
     private $rootScope: ng.IScope,
     private $state: IStateService,
@@ -53,8 +72,14 @@
     private NavigationService: IXosNavigationService,
     private StyleConfig: IXosStyleConfig,
     private SearchService: IXosSearchService,
-    private XosKeyboardShortcut: IXosKeyboardShortcutService
+    private XosKeyboardShortcut: IXosKeyboardShortcutService,
+    private ConfigHelpers: IXosConfigHelpersService
   ) {
+
+  }
+
+  $onInit() {
+    this.$log.info('[XosHeader] Setup');
     this.version = require('../../../../package.json').version;
     angular.extend(this.toastrConfig, {
       newestOnTop: false,
@@ -62,10 +87,9 @@
       preventDuplicates: false,
       preventOpenDuplicates: false,
       progressBar: true,
-      // autoDismiss: false,
-      // closeButton: false,
-      // timeOut: 0,
-      // tapToDismiss: false
+      onTap: (toast) => {
+        this.$state.go(toast.scope.extraData.dest.name, toast.scope.extraData.dest.params);
+      }
     });
 
     this.search = (query: string) => {
@@ -94,27 +118,43 @@
 
     this.userEmail = this.authService.getUser() ? this.authService.getUser().email : '';
 
-    this.syncStore.query()
+    this.syncStoreSubscription = this.syncStore.query()
       .subscribe(
         (event: IWSEvent) => {
-          $scope.$evalAsync(() => {
+          this.$scope.$evalAsync(() => {
+
+            if (event.model === 'Diag') {
+              // NOTE skip notifications for Diag model
+              return;
+            }
+
             let toastrMsg: string;
             let toastrLevel: string;
-            if (event.msg.object.backend_status.indexOf('0') > -1) {
+            if (event.msg.object.backend_code === 0) {
               toastrMsg = 'Synchronization started for:';
               toastrLevel = 'info';
             }
-            else if (event.msg.object.backend_status.indexOf('1') > -1) {
+            else if (event.msg.object.backend_code === 1) {
               toastrMsg = 'Synchronization succedeed for:';
               toastrLevel = 'success';
             }
-            else if (event.msg.object.backend_status.indexOf('2') > -1) {
+            else if (event.msg.object.backend_code === 2) {
               toastrMsg = 'Synchronization failed for:';
               toastrLevel = 'error';
             }
 
             if (toastrLevel && toastrMsg) {
-              this.toastr[toastrLevel](`${toastrMsg} ${event.msg.object.name}`, event.model);
+              let modelName = event.msg.object.name;
+              let modelClassName = event.model;
+              if (angular.isUndefined(event.msg.object.name) || event.msg.object.name === null) {
+                modelName = `${event.msg.object.leaf_model_name} [${event.msg.object.id}]`;
+              }
+
+              const dest = this.ConfigHelpers.stateWithParamsForJs(modelClassName, event.msg.object);
+
+              if (!event.skip_notification) {
+                this.toastr[toastrLevel](`${toastrMsg} ${modelName}`, modelClassName, {extraData: {dest: dest}});
+              }
             }
             // this.notifications.unshift(event);
             // this.newNotifications = this.getNewNotifications(this.notifications);
@@ -123,6 +163,11 @@
       );
   }
 
+  $onDestroy() {
+    this.$log.info('[XosHeader] Teardown');
+    this.syncStoreSubscription.unsubscribe();
+  }
+
   public getLogo(): string {
     return require(`../../images/brand/${this.StyleConfig.logo}`);
   }