diff --git a/widget/controllers/widget.approvalrequests.controller.js b/widget/controllers/widget.approvalrequests.controller.js index 0fbfe4f..3da7711 100644 --- a/widget/controllers/widget.approvalrequests.controller.js +++ b/widget/controllers/widget.approvalrequests.controller.js @@ -19,7 +19,7 @@ ApprovalRequests.isRedeemItemsLoading = false; ApprovalRequests.currentLoggedInUser = null ApprovalRequests.strings = $rootScope.strings; - ApprovalRequests.Settings = null; + ApprovalRequests.Settings = null; ApprovalRequests.drawerItems = [ { text: ApprovalRequests.strings["staffApproval.approve"], @@ -31,7 +31,7 @@ } ] var breadCrumbFlag = true; - + buildfire.history.get('pluginBreadcrumbsOnly', function (err, result) { if (result && result.length) { result.forEach(function (breadCrumb) { @@ -74,11 +74,11 @@ redeemslistView.onItemActionClicked = (item) => { var success = function (result) { - ApprovalRequests.RedeemItems = ApprovalRequests.RedeemItems.filter(x => x.id != item.id ) + ApprovalRequests.RedeemItems = ApprovalRequests.RedeemItems.filter(x => x.id != item.id ) redeemslistView.clear(); redeemslistView.loadListViewItems(ApprovalRequests.RedeemItems) updateListViewDesign(); - + } , error = function (err) { if (err ) { @@ -99,7 +99,7 @@ } else { status = STATUS.Denied } - let selectedItem = ApprovalRequests.RedeemResult.find(x => x.id == item.id ) + let selectedItem = ApprovalRequests.RedeemResult.find(x => x.id == item.id ) Transactions.updateRequestedRedeemStatus(selectedItem, status, ApprovalRequests.currentLoggedInUser.displayName ? ApprovalRequests.currentLoggedInUser.displayName : ApprovalRequests.currentLoggedInUser.email ).then(success, error).then(x =>{ if(status == STATUS.Approved){ @@ -108,7 +108,7 @@ type: "success" }); LoyaltyAPI.redeemPoints(selectedItem.data.createdBy._id, selectedItem.data.createdBy.userToken, ApprovalRequests.context.instanceId, selectedItem.data.item._id).then(); - + buildfire.notifications.pushNotification.schedule( { title: "Points Approved", @@ -145,7 +145,7 @@ pointslistView.onItemActionClicked = (item) => { var success = function (result) { - + } , error = function (err) { if (err ) { @@ -154,8 +154,8 @@ }; var successLoyaltyPoints = function(result){ - var selectedItem = ApprovalRequests.PointItems.find(x => x.id == item.id ) - ApprovalRequests.PointItems = ApprovalRequests.PointItems.filter(x => x.id != item.id ) + var selectedItem = ApprovalRequests.PointItems.find(x => x.id == item.id ) + ApprovalRequests.PointItems = ApprovalRequests.PointItems.filter(x => x.id != item.id ) pointslistView.clear(); pointslistView.loadListViewItems(ApprovalRequests.PointItems) @@ -165,10 +165,11 @@ message: selectedItem.data.points + " points approved for " + selectedItem.title, type: "success" }); + var title = updatedItem.data.items ? updatedItem.data.items.map(el => el.title).join(",") : updatedItem.data.item ? updatedItem.data.item.title : "Unknown"; buildfire.notifications.pushNotification.schedule( { title: "Reward Approved", - text: ( (updatedItem.data.item.title && updatedItem.data.item.title == '') ? "BUY POINTS" : updatedItem.data.item.title) + " has been approved", + text: !title ? "BUY POINTS" : title + " has been approved", users: [updatedItem.data.createdBy._id] , at: new Date() }, @@ -176,6 +177,7 @@ if (err) return console.error(err); } ); + $rootScope.$broadcast('POINTS_ADDED', { points: selectedItem.data.points, userId: updatedItem.data.createdBy._id }); }, errorLoyaltyPoints = function (err) { if (err ) { console.error('Error Add ing Loyalty Points', err); @@ -195,8 +197,8 @@ } else { status = STATUS.Denied } - let selectedItem = ApprovalRequests.PointResult.find(x => x.id == item.id ) - + let selectedItem = ApprovalRequests.PointResult.find(x => x.id == item.id ) + if(ApprovalRequests.Settings == null || !ApprovalRequests.Settings.redemptionPasscode){ buildfire.dialog.toast({ message: "Please Add A Redemption Code", @@ -233,25 +235,25 @@ pointslistView.onItemClicked = item => { if(item.data.isClickable){ - let selectedItem = ApprovalRequests.PointResult.find(x => x.id == item.id ) + let selectedItem = ApprovalRequests.PointResult.find(x => x.id == item.id ) ViewStack.push({ template: 'items_purchased', item: selectedItem }); } - + }; buildfire.appearance.getAppTheme((err, appTheme) => { ApprovalRequests.appTheme = appTheme; let tab = document.querySelector(".tab"); let tabButtons = document.querySelectorAll(".tablinks") - + tab.style.backgroundColor = appTheme.colors.titleBar tabButtons.forEach(el => { el.style.color = appTheme.colors.titleBarTextAndIcons }) - + }) function updateListViewDesign(){ @@ -271,7 +273,7 @@ }) glyphicons.forEach(el => { el.style.color = ApprovalRequests.appTheme.colors.icons - + }) } @@ -320,7 +322,7 @@ let items = [] result.forEach(element => { - + items.push({ id: element.id, title: element.data.createdBy.displayName ? element.data.createdBy.displayName : element.data.createdBy.username , @@ -360,10 +362,10 @@ }) subTitle = (element.data.items.length == 1 ? element.data.items.length + " item" : element.data.items.length + " items") + " • " + element.data.pointsEarned + " Points" isClickable = true; - } else if(element.data.item){ + } else if(element.data.item){ title = element.data.item.title; subTitle = (element.data.item.title == "POINTS PURCHASE" ? element.data.purchaseAmount + "$" : element.data.item.title) + " • " + element.data.pointsEarned + " Points" - } + } items.push({ id: element.id, title: element.data.createdBy.displayName ? element.data.createdBy.displayName : element.data.createdBy.username , @@ -379,7 +381,7 @@ icon: 'material-icons material-inject--more' } }) - + }); ApprovalRequests.PointItems = items; pointslistView.loadListViewItems(items) @@ -392,7 +394,7 @@ getCurrentUser(); Transactions.getRequestedRedeems().then(successRequestedRedeems, errorRequestedRedeems); Transactions.getRequestedPoints().then(successRequestedPoints, errorRequestedPoints); - + }; @@ -400,12 +402,12 @@ buildfire.auth.getCurrentUser(function (err, user) { if (user) { ApprovalRequests.currentLoggedInUser = user; - - + + } Context.getContext(function (ctx) { ApprovalRequests.context = ctx; - + buildfire.datastore.get(TAG_NAMES.LOYALTY_INFO,function(err,result){ if(result && result.data && result.data.settings){ ApprovalRequests.Settings = result.data.settings @@ -427,12 +429,12 @@ if(currentDate.toDateString() === selectedDate.toDateString()){ return "Today " + time; } - + currentDate.setDate(currentDate.getDate() - 1); if (currentDate.toDateString() === selectedDate.toDateString()) { return "Yesterday " + time;; } - + return selectedDate.toLocaleTimeString([], {year: 'numeric', month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit'}); } ApprovalRequests.listeners['GOTO_HOME'] = $rootScope.$on('GOTO_HOME', function (e) { @@ -456,7 +458,7 @@ $scope.$destroy(); } }); - + init(); ApprovalRequests.listeners['CHANGED'] = $rootScope.$on('VIEW_CHANGED', function (e, type, view) { diff --git a/widget/controllers/widget.code.controller.js b/widget/controllers/widget.code.controller.js index 50fab0e..957ac42 100644 --- a/widget/controllers/widget.code.controller.js +++ b/widget/controllers/widget.code.controller.js @@ -37,12 +37,12 @@ WidgetCode.context = Context.getContext(); if (RewardCache.getApplication()) { WidgetCode.application = RewardCache.getApplication(); - } + } WidgetCode.addPoints = function () { var success = function (result) { Buildfire.spinner.hide(); - $rootScope.$broadcast('POINTS_ADDED', (currentView.amount * WidgetCode.application.pointsPerDollar) + WidgetCode.application.pointsPerVisit); + $rootScope.$broadcast('POINTS_ADDED', { points: (currentView.amount * WidgetCode.application.pointsPerDollar) + WidgetCode.application.pointsPerVisit }); var pointsAwarded = (currentView.amount * WidgetCode.application.pointsPerDollar) + WidgetCode.application.pointsPerVisit; ViewStack.push({ template: 'Awarded', @@ -52,7 +52,7 @@ Transactions.buyPoints(currentView.amount, pointsAwarded, $rootScope.loyaltyPoints, WidgetCode.currentLoggedInUser, currentView.title, currentView.iconUrl); } else if(currentView.type === 'buyProducts') { Transactions.buyProducts(currentView.items, $rootScope.loyaltyPoints, WidgetCode.currentLoggedInUser); - } else + } else return; } @@ -83,7 +83,7 @@ }); Transactions.redeemReward(currentView.reward, currentView.pointsToRedeem, $rootScope.loyaltyPoints, WidgetCode.currentLoggedInUser); }; - + var redeemFailure = function (error) { Buildfire.spinner.hide(); if (error && error.code == 2103) { @@ -113,7 +113,7 @@ } else { buildfire.auth.login({}, function () { - + }); } @@ -151,7 +151,7 @@ }; - + LoyaltyAPI.validatePasscode(WidgetCode.currentLoggedInUser.userToken, WidgetCode.context.instanceId, WidgetCode.passcode).then(success, error); }; @@ -179,4 +179,4 @@ }); }]) -})(window.angular, window.buildfire); \ No newline at end of file +})(window.angular, window.buildfire); diff --git a/widget/controllers/widget.home.controller.js b/widget/controllers/widget.home.controller.js index b33bd45..522340f 100644 --- a/widget/controllers/widget.home.controller.js +++ b/widget/controllers/widget.home.controller.js @@ -11,7 +11,7 @@ WidgetHome.isClient = false; WidgetHome.approvalRequestsTab = 0 WidgetHome.tags = null; - + WidgetHome.strings = { "general.loginOrRegister": "Login or register", "general.toGetPoints": "to get points", @@ -51,7 +51,7 @@ } var features = [] - + $window.strings.getLanguage(function(err, response){ const obj = response[0] ? response[0].data : $window.strings._data; const strings = {}; @@ -147,12 +147,12 @@ let newPoints; if(type === "Redeemed" && WidgetHome.data.settings.deductLeaderboardPoints){ newPoints = oldPoints === 0 ? negative(points) : oldPoints - points; - } + } else if(type === "Added"){ newPoints = oldPoints + points; } // If points are redeemed and deductLearboardPoints is off -> No changes - if(newPoints){ + if(newPoints){ buildfire.appData.searchAndUpdate( { userId: { $eq: userId} }, { $set: { totalPoints, newPoints } }, @@ -164,7 +164,7 @@ let data = { userId: userId, totalPoints: totalPoints, - newPoints: totalPoints + newPoints: totalPoints } buildfire.appData.insert( data, @@ -208,7 +208,7 @@ var reward=WidgetHome.loyaltyRewards.find(el=>el._id==data.id); if(reward) WidgetHome.openReward(reward,-1); - else + else buildfire.dialog.toast({ message: WidgetHome.strings["deeplink.deeplinkRewardNotFound"] }); @@ -305,8 +305,8 @@ ); } } - - } + + } else { ViewStack.push({ template: 'Amount', @@ -341,10 +341,31 @@ /** * This event listener is bound for "POINTS_ADDED" event broadcast */ - WidgetHome.listeners['POINTS_ADDED'] = $rootScope.$on('POINTS_ADDED', function (e, points) { - if (points) - $rootScope.loyaltyPoints = $rootScope.loyaltyPoints + points; + WidgetHome.listeners['POINTS_ADDED'] = $rootScope.$on('POINTS_ADDED', function (e, { points, userId }) { + // no userId means points should be assigned to the logged-in user + if (!userId) { + if (points) $rootScope.loyaltyPoints = $rootScope.loyaltyPoints + points; saveLoyaltyPointsInAppData(WidgetHome.currentLoggedInUser._id, $rootScope.loyaltyPoints, points, "Added") + } else { + // defined users, get their totalPoints first + buildfire.appData.search( + { + filter: { + $or: [ + { "$json.userId": userId }, + ], + }, + }, + "userLoyaltyPoints", + (err, res) => { + if (err) return console.error("there was a problem retrieving your data"); + const totalPoints = res && res.length && res[0].data.totalPoints ? + (res[0].data.totalPoints + points) + : points; + saveLoyaltyPointsInAppData(userId, totalPoints, points, "Added") + } + ); + } }); WidgetHome.listeners['POINTS_WAITING_APPROVAL_ADDED'] = $rootScope.$on('POINTS_WAITING_APPROVAL_ADDED', function (e, points) { @@ -476,7 +497,7 @@ limit: 1 }, "freeTextQuestionnaireSubmissions_" + element.instanceId, - (err, res) => { + (err, res) => { if(res && res.length > 0 && !res[0].data.isEarnedPoints){ let selectedFTQ = res[0].data; selectedFTQ.isEarnedPoints = true; @@ -486,7 +507,7 @@ "freeTextQuestionnaireSubmissions_" + element.instanceId, (err, result) => { if (err) return console.error("Error while inserting your data", err); - let score = 0 + let score = 0 selectedFTQ.answers.forEach(answer => { score += answer && answer.score ? answer.score : 0 }); @@ -527,7 +548,7 @@ var init = function () { var success = function (result) { - + if(result && result.data){ console.log('BUILDFIRE GET--------------------------LOYALTY---------RESULT',result); WidgetHome.data = result.data; @@ -640,7 +661,7 @@ return; } }); - + if(WidgetHome.isEmployer == false){ WidgetHome.isClient = true; buildfire.notifications.pushNotification.subscribe( @@ -661,8 +682,8 @@ ); } } - - + + } var checkIfEmployerOrUser = function(){ diff --git a/widget/controllers/widget.itemspurchased.controller.js b/widget/controllers/widget.itemspurchased.controller.js index 1524253..01c442f 100644 --- a/widget/controllers/widget.itemspurchased.controller.js +++ b/widget/controllers/widget.itemspurchased.controller.js @@ -16,7 +16,7 @@ ItemsPurchased.currentLoggedInUser = null ItemsPurchased.Settings = null; ItemsPurchased.strings = $rootScope.strings; - ItemsPurchased.Settings = null; + ItemsPurchased.Settings = null; ItemsPurchased.drawerItems = [ { text: ItemsPurchased.strings["staffApproval.approve"], @@ -44,13 +44,13 @@ ItemsPurchased.appTheme = appTheme; let tab = document.querySelector(".tab"); let tabButtons = document.querySelectorAll(".tablinks") - + tab.style.backgroundColor = appTheme.colors.titleBar tabButtons.forEach(el => { el.style.color = appTheme.colors.titleBarTextAndIcons }) - - + + }) ItemsPurchased.Update = (status) => { @@ -72,12 +72,13 @@ if (err) return console.error(err); } ); + $rootScope.$broadcast('POINTS_ADDED', { points: ItemsPurchased.TotalPoints }); } else { buildfire.dialog.toast({ message: ItemsPurchased.TotalPoints + " points denied for " + (ItemsPurchased.Item.data.createdBy.displayName ? ItemsPurchased.Item.data.createdBy.displayName : ItemsPurchased.Item.data.createdBy.email), type: "danger" }); - + buildfire.notifications.pushNotification.schedule( { title: "Reward Denied", @@ -121,7 +122,7 @@ listViewItemTitle.forEach(el => { el.style.color = ItemsPurchased.appTheme.colors.headerText }) - + btnApproved.style.backgroundColor = ItemsPurchased.appTheme.colors.primaryTheme + " !important" listViewItemSubtitle.forEach(el => { @@ -165,7 +166,7 @@ title: element.title, imageUrl: element.listImage, subtitle: element.pointsPerItem + " Points", - description: "Quantity: " + element.quantity + description: "Quantity: " + element.quantity }) }) itemsPurchasedListView.loadListViewItems(items) @@ -180,7 +181,7 @@ } Context.getContext(function (ctx) { ItemsPurchased.context = ctx; - + buildfire.datastore.get(TAG_NAMES.LOYALTY_INFO,function(err,result){ if(result && result.data && result.data.settings){ ItemsPurchased.Settings = result.data.settings @@ -204,7 +205,7 @@ } }); - + init(); ItemsPurchased.listeners['CHANGED'] = $rootScope.$on('VIEW_CHANGED', function (e, type, view) { if (ViewStack.getCurrentView().template == "items_purchased") {