var notificationApp = angular.module('notificationApp', ['commonsApp', 'angular-inview']); notificationApp.constant("notificationConstants", { 'FEED_COUNT': 10, 'FOLLOW_INSIGHT': 'FOLLOW_INSIGHT', 'GENERIC_MSG': 'GENERIC_MSG', 'PROMOTIONAL_MSG': 'PROMOTIONAL_MSG', 'TIME_INTRVAL_TO_FETCH_NOTIFICATIONS':180000 }); notificationApp.directive('notification',['$document','$interval','Constants','notificationConstants','commonsAppServices','notificationService','mixpanelService', function($document,$interval,Constants,notificationConstants,commonsAppServices,notificationService,mixpanelService) { return { restrict: "E", templateUrl: Constants.cdnFullPath+'scripts/angular/commons/modules/notification/notification.html', replace: true, link:function(scope, element, attrs) { scope.memberId = userId; scope.notificationConsts = notificationConstants; scope.appBaseURL = Constants.apiUrl; scope.enableNotification = false; scope.showNotifications = false; scope.showSeeMore = false; scope.notificationFeeds = []; scope.notificationFeedsCount = 0; scope.notificationCount = 0; scope.getNotifications = function(){ var promise = notificationService.getNotificationsForUser(scope.memberId, scope.notificationConsts.FEED_COUNT); //Get notification in certain time interval $interval(function () { scope.fetchFeedsUpdate(); },scope.notificationConsts.TIME_INTRVAL_TO_FETCH_NOTIFICATIONS); promise.then(function(response){ var data = response; if(data != null && data != undefined && Object.keys(data).length > 0){ if(data.feedcardResponse != null && data.feedcardResponse != undefined) { scope.notificationCount = data.notificationCount; scope.notificationFeeds = data.feedcardResponse; scope.notificationFeedsCount = scope.notificationFeeds.length; angular.forEach(scope.notificationFeeds, function(notification){ if(notification.interactionCount > 0){ notification.isInteracted = true; } else { notification.isInteracted = false; } notification.isSeenByUser = false; }); scope.handleShowingSeeMore(data.feedcardResponse.length); scope.isNotificationAvailable = true; } else { scope.isNotificationAvailable = false; } } else { scope.isNotificationAvailable = false; } scope.enableNotification = true; }, function(error) { scope.enableNotification = false; }); }; scope.getMoreNotifications = function () { var promise = notificationService.getPrevNotificationsForUser(scope.memberId, scope.notificationConsts.FEED_COUNT, scope.notificationFeeds[scope.notificationFeedsCount-1].id); promise.then(function(response){ var data = response.data; if(data != null && data != undefined && Object.keys(data).length > 0){ if(data.feedcardResponse != null && data.feedcardResponse != undefined) { var feedcardResponse = data.feedcardResponse; angular.forEach(feedcardResponse, function(notification){ if(notification.interactionCount > 0){ notification.isInteracted = true; } else { notification.isInteracted = false; } notification.isSeenByUser = false; scope.notificationFeeds.push(notification); }); scope.notificationFeedsCount = scope.notificationFeeds.length; scope.handleShowingSeeMore(feedcardResponse.length); } } else { scope.showSeeMore = false; } }); }; scope.showNotificationPopup = function() { if(scope.showNotifications){ scope.showNotifications = false; } else { scope.showNotifications = true; notificationService.resetNotificationAlertCount(); scope.notificationCount = 0; //For Amplitude Tracking mixpanelService.trackEvent("Notification Icon Clicked"); } }; scope.hideNotificationPopup = function(){ scope.showNotifications = false; }; scope.handleUserInteraction = function(notificationFeed){ if(!notificationFeed.isInteracted) { var listOfFeedIds = []; listOfFeedIds.push(notificationFeed.id); notificationService.updateNotificationInteractCount(listOfFeedIds, scope.memberId); notificationFeed.isInteracted = true; } }; scope.followCompany = function (notificationFeed, isFollowing) { commonsAppServices.followCompany(notificationFeed.additionalData.company_id, isFollowing, 'Notification Feed').then(function (result) { if (result.data == "true" || result.data == true) { notificationFeed.additionalData.is_followed = 'true'; } }); }; scope.userNotificationSeen = function (notificationFeed) { if(!notificationFeed.isSeenByUser) { var listOfFeedIds = []; listOfFeedIds.push(notificationFeed.id); notificationFeed.isSeenByUser = true; notificationService.updateNotificationSeenCount(listOfFeedIds, scope.memberId); //For Amplitude Tracking if(notificationFeed.template == scope.notificationConsts.FOLLOW_INSIGHT){ var prop={}; prop["Type"] = notificationFeed.template; mixpanelService.trackEvent("Notification Feed Shown", prop); } } }; scope.fetchFeedsUpdate = function(){ if(scope.notificationFeeds.length > 0){ var promise = notificationService.getNotificationsForUser(scope.memberId, scope.notificationConsts.FEED_COUNT, scope.notificationFeeds[0].id); } else { var promise = notificationService.getNotificationsForUser(scope.memberId, scope.notificationConsts.FEED_COUNT); } promise.then(function(response){ var data = response; if(data != null && data != undefined && Object.keys(data).length > 0){ if(data.feedcardResponse != null && data.feedcardResponse != undefined) { var feedcardResponse = data.feedcardResponse; angular.forEach(feedcardResponse, function(notification){ if(notification.interactionCount > 0){ notification.isInteracted = true; } else { notification.isInteracted = false; } notification.isSeenByUser = false; scope.notificationFeeds.unshift(notification); }); scope.notificationCount = data.notificationCount; scope.notificationFeedsCount = scope.notificationFeeds.length; var element = angular.element(document.getElementById('notification-dropdown-list')); element[0].scrollTop = 0; } } }, function(error) { }); }; scope.handleShowingSeeMore = function(feedLength){ if(scope.notificationConsts.FEED_COUNT == feedLength){ scope.showSeeMore = true; } else { scope.showSeeMore = false; } }; //Get initial Notifications scope.getNotifications(); } }; }]); notificationApp.service("notificationService", [ "$http", "$q", "Constants", function($http, $q, Constants) { var baseUrl = Constants.apiUrl; var marketingURL = Constants.marketingUrl; this.getNotificationsForUser = function(memberId, feedCount, lastId) { var url = ''; if(lastId != undefined){ url = marketingURL + '/a/v1/pr/getNotificationsForUser?memberId='+ memberId + '&feedCount=' + feedCount + '&lastId=' + lastId; } else { url = marketingURL + '/a/v1/pr/getNotificationsForUser?memberId='+ memberId + '&feedCount=' + feedCount; } var deferred = $q.defer(); var promise = $http.get(url).success(function(data) { deferred.resolve(data); }).error(deferred.reject); return deferred.promise; }; this.getPrevNotificationsForUser = function(memberId, feedCount, lastId){ var promise = $http.get(marketingURL + '/a/v1/pr/getPrevNotificationsForUser?memberId='+ memberId + '&feedCount=' + feedCount + '&lastId=' + lastId); return promise; }; this.resetNotificationAlertCount = function() { var promise = $http.post(marketingURL + "/a/v1/pr/resetNotificationAlertCount"); return promise; }; this.updateNotificationInteractCount = function(feedId, memberId) { var params = { "feedIds": feedId, "memberId": memberId }; var promise = $http.post(marketingURL + "/a/v1/pr/updateNotificationInteractCount", params); return promise; }; this.updateNotificationSeenCount = function(feedId, memberId) { var params = { "feedIds": feedId, "memberId": memberId }; var promise = $http.post(marketingURL + "/a/v1/pr/updateNotificationSeenCount", params); return promise; }; } ]);