Wallet/src/js/controllers/modals/amazonCardDetails.js

83 lines
2.6 KiB
JavaScript
Raw Normal View History

'use strict';
2016-12-22 12:27:43 -03:00
angular.module('copayApp.controllers').controller('amazonCardDetailsController', function($scope, $log, $timeout, $ionicScrollDelegate, bwcError, amazonService, lodash, ongoingProcess, popupService, externalLinkService) {
2016-08-03 10:05:20 -03:00
$scope.cancelGiftCard = function() {
2016-12-22 12:27:43 -03:00
ongoingProcess.set('cancelGiftCard', true);
2016-08-03 10:05:20 -03:00
amazonService.cancelGiftCard($scope.card, function(err, data) {
2016-12-22 12:27:43 -03:00
ongoingProcess.set('cancelGiftCard', false);
2016-08-03 10:05:20 -03:00
if (err) {
2016-12-22 12:27:43 -03:00
popupService.showAlert('Error', bwcError.msg(err));
2016-08-03 10:05:20 -03:00
return;
}
$scope.card.cardStatus = data.cardStatus;
2016-12-22 12:27:43 -03:00
$timeout(function() {
$ionicScrollDelegate.resize();
$ionicScrollDelegate.scrollTop();
}, 10);
amazonService.savePendingGiftCard($scope.card, null, function(err) {
2016-12-22 12:27:43 -03:00
$scope.refreshGiftCard();
});
2016-08-03 10:05:20 -03:00
});
};
$scope.remove = function() {
2016-07-21 11:18:48 -03:00
amazonService.savePendingGiftCard($scope.card, {
remove: true
}, function(err) {
$scope.cancel();
});
};
$scope.refreshGiftCard = function() {
2016-12-22 12:27:43 -03:00
ongoingProcess.set('updateGiftCard', 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('updateGiftCard', false);
}, 1000);
}
if (err) {
2016-12-22 12:27:43 -03:00
popupService.showAlert('Error', err);
return;
}
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('updateGiftCard', false);
}, 1000);
}
2016-07-21 11:18:48 -03:00
if (dataFromStorage.status == 'PENDING' && dataFromStorage.invoiceId == $scope.card.invoiceId) {
$log.debug("creating gift card");
amazonService.createGiftCard(dataFromStorage, function(err, giftCard) {
if (err) {
2016-12-22 12:27:43 -03:00
popupService.showAlert('Error', bwcError.msg(err));
2016-07-21 11:18:48 -03:00
return;
}
if (!lodash.isEmpty(giftCard)) {
var newData = {};
lodash.merge(newData, dataFromStorage, giftCard);
amazonService.savePendingGiftCard(newData, null, function(err) {
$log.debug("Saving new gift card");
$scope.card = newData;
$timeout(function() {
$scope.$digest();
});
});
} else $log.debug("pending gift card not available yet");
});
}
});
});
};
$scope.cancel = function() {
$scope.amazonCardDetailsModal.hide();
};
2016-12-12 17:29:11 -03:00
$scope.openExternalLink = function(url) {
externalLinkService.open(url);
};
});