Wallet/src/js/controllers/amazon.js

118 lines
3.8 KiB
JavaScript
Raw Normal View History

'use strict';
2016-07-21 11:18:48 -03:00
angular.module('copayApp.controllers').controller('amazonController',
2016-12-22 12:27:43 -03:00
function($scope, $timeout, $ionicModal, $log, $ionicScrollDelegate, lodash, amazonService, platformInfo, externalLinkService, popupService, ongoingProcess) {
2016-09-23 13:05:50 -03:00
$scope.network = amazonService.getEnvironment();
2016-12-12 17:29:11 -03:00
$scope.openExternalLink = function(url) {
externalLinkService.open(url);
};
var initAmazon = function() {
2016-07-21 11:18:48 -03:00
amazonService.getPendingGiftCards(function(err, gcds) {
if (err) {
2016-12-22 12:27:43 -03:00
popupService.showAlert('Error', err);
return;
}
2016-07-21 11:18:48 -03:00
$scope.giftCards = lodash.isEmpty(gcds) ? null : gcds;
2016-05-19 20:20:06 -03:00
$timeout(function() {
$scope.$digest();
});
if ($scope.cardClaimCode) {
2016-12-12 17:29:11 -03:00
var card = lodash.find($scope.giftCards, {
claimCode: $scope.cardClaimCode
});
if (lodash.isEmpty(card)) {
2016-12-22 12:27:43 -03:00
popupService.showAlert('Error', 'Card not found');
return;
}
$scope.openCardModal(card);
}
});
$scope.updatePendingGiftCards();
};
2016-07-21 11:18:48 -03:00
$scope.updatePendingGiftCards = lodash.debounce(function() {
2016-12-22 12:27:43 -03:00
ongoingProcess.set('updateGiftCards', true);
2016-07-21 11:18:48 -03:00
amazonService.getPendingGiftCards(function(err, gcds) {
2016-12-22 12:27:43 -03:00
if (lodash.isEmpty(gcds)) {
$timeout(function() {
ongoingProcess.set('updateGiftCards', false);
}, 1000);
}
$timeout(function() {
2016-12-22 12:27:43 -03:00
$scope.giftCards = lodash.isEmpty(gcds) ? null : gcds;
$scope.$digest();
});
2016-12-22 12:27:43 -03:00
var index = 0;
2016-07-21 11:18:48 -03:00
lodash.forEach(gcds, function(dataFromStorage) {
2016-12-22 12:27:43 -03:00
if (++index == Object.keys(gcds).length) {
$timeout(function() {
ongoingProcess.set('updateGiftCards', false);
}, 1000);
}
2016-07-21 11:18:48 -03:00
if (dataFromStorage.status == 'PENDING') {
$log.debug("creating gift card");
amazonService.createGiftCard(dataFromStorage, function(err, giftCard) {
if (err) {
2016-12-22 12:27:43 -03:00
popupService.showAlert('Error', err);
2016-07-21 11:18:48 -03:00
return;
}
if (giftCard.status != 'PENDING') {
var newData = {};
lodash.merge(newData, dataFromStorage, giftCard);
if (newData.status == 'expired') {
amazonService.savePendingGiftCard(newData, {
remove: true
}, function(err) {
return;
});
}
amazonService.savePendingGiftCard(newData, null, function(err) {
$log.debug("Saving new gift card");
amazonService.getPendingGiftCards(function(err, gcds) {
if (err) {
2016-12-22 12:27:43 -03:00
popupService.showAlert('Error', err);
2016-07-21 11:18:48 -03:00
return;
}
2016-12-22 12:27:43 -03:00
$scope.giftCards = lodash.isEmpty(gcds) ? null : gcds;
2016-07-21 11:18:48 -03:00
$timeout(function() {
$scope.$digest();
2016-12-22 12:32:43 -03:00
$ionicScrollDelegate.resize();
}, 10);
2016-07-21 11:18:48 -03:00
});
});
} else $log.debug("pending gift card not available yet");
});
}
});
});
2016-12-22 12:27:43 -03:00
}, 1000, {
'leading': true
});
$scope.openCardModal = function(card) {
$scope.card = card;
$ionicModal.fromTemplateUrl('views/modals/amazon-card-details.html', {
scope: $scope
}).then(function(modal) {
$scope.amazonCardDetailsModal = modal;
$scope.amazonCardDetailsModal.show();
});
2016-12-22 12:27:43 -03:00
$scope.$on('modal.hidden', function() {
$scope.updatePendingGiftCards();
});
};
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.cardClaimCode = data.stateParams.cardClaimCode;
initAmazon();
});
});