Wallet/src/js/controllers/amazon.js

87 lines
2.7 KiB
JavaScript
Raw Normal View History

'use strict';
2016-07-21 11:18:48 -03:00
angular.module('copayApp.controllers').controller('amazonController',
2016-09-05 14:59:11 -03:00
function($scope, $timeout, $ionicModal, $log, lodash, bwcError, amazonService, platformInfo, externalLinkService, popupService) {
$scope.openExternalLink = function(url, target) {
2016-09-05 14:59:11 -03:00
externalLinkService.open(url, target);
};
this.init = function() {
var self = this;
$scope.network = amazonService.getEnvironment();
2016-07-21 11:18:48 -03:00
amazonService.getPendingGiftCards(function(err, gcds) {
if (err) {
2016-08-30 20:47:55 -03:00
popupService.showAlert(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();
});
});
2016-07-21 11:18:48 -03:00
this.updatePendingGiftCards();
}
this.updatePendingGiftCards = lodash.debounce(function() {
2016-07-29 15:47:45 -03:00
var self = this;
2016-07-21 11:18:48 -03:00
amazonService.getPendingGiftCards(function(err, gcds) {
lodash.forEach(gcds, function(dataFromStorage) {
if (dataFromStorage.status == 'PENDING') {
$log.debug("creating gift card");
amazonService.createGiftCard(dataFromStorage, function(err, giftCard) {
if (err) {
2016-08-30 20:47:55 -03:00
popupService.showAlert(bwcError.msg(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-08-30 20:47:55 -03:00
popupService.showAlert(err);
2016-07-21 11:18:48 -03:00
return;
}
$scope.giftCards = gcds;
$timeout(function() {
$scope.$digest();
});
});
});
} else $log.debug("pending gift card not available yet");
});
}
});
});
}, 1000);
this.openCardModal = function(card) {
var self = this;
$scope.card = card;
$ionicModal.fromTemplateUrl('views/modals/amazon-card-details.html', {
scope: $scope
}).then(function(modal) {
$scope.amazonCardDetailsModal = modal;
$scope.amazonCardDetailsModal.show();
});
$scope.$on('UpdateAmazonList', function(event) {
self.init();
});
};
});