2017-01-24 01:23:57 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('amazonCardsController',
|
|
|
|
|
function($scope, $timeout, $ionicModal, $log, $ionicScrollDelegate, lodash, amazonService, platformInfo, externalLinkService, popupService, ongoingProcess) {
|
|
|
|
|
|
|
|
|
|
$scope.openExternalLink = function(url) {
|
|
|
|
|
externalLinkService.open(url);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var updateGiftCards = function(cb) {
|
|
|
|
|
amazonService.getPendingGiftCards(function(err, gcds) {
|
|
|
|
|
if (err) {
|
|
|
|
|
popupService.showAlert('Could not get gift cards', err);
|
|
|
|
|
if (cb) return cb();
|
|
|
|
|
else return;
|
|
|
|
|
}
|
|
|
|
|
$scope.giftCards = gcds;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
$ionicScrollDelegate.resize();
|
|
|
|
|
if (cb) return cb();
|
|
|
|
|
}, 100);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.updatePendingGiftCards = lodash.debounce(function() {
|
|
|
|
|
$scope.updatingPending = {};
|
|
|
|
|
updateGiftCards(function() {
|
|
|
|
|
var index = 0;
|
|
|
|
|
var gcds = $scope.giftCards;
|
|
|
|
|
lodash.forEach(gcds, function(dataFromStorage) {
|
2017-04-13 16:58:42 -03:00
|
|
|
if (dataFromStorage.status == 'PENDING' || dataFromStorage.status == 'invalid') {
|
|
|
|
|
$log.debug("Creating / Updating gift card");
|
2017-01-24 01:23:57 -03:00
|
|
|
$scope.updatingPending[dataFromStorage.invoiceId] = true;
|
2017-04-13 16:58:42 -03:00
|
|
|
|
2017-01-24 01:23:57 -03:00
|
|
|
amazonService.createGiftCard(dataFromStorage, function(err, giftCard) {
|
2017-04-13 16:58:42 -03:00
|
|
|
|
2017-01-24 01:23:57 -03:00
|
|
|
$scope.updatingPending[dataFromStorage.invoiceId] = false;
|
|
|
|
|
if (err) {
|
|
|
|
|
popupService.showAlert('Error creating gift card', err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-04-13 16:58:42 -03:00
|
|
|
|
2017-01-24 01:23:57 -03:00
|
|
|
if (giftCard.status != 'PENDING') {
|
|
|
|
|
var newData = {};
|
|
|
|
|
|
|
|
|
|
lodash.merge(newData, dataFromStorage, giftCard);
|
|
|
|
|
|
|
|
|
|
if (newData.status == 'expired') {
|
|
|
|
|
amazonService.savePendingGiftCard(newData, {
|
|
|
|
|
remove: true
|
|
|
|
|
}, function(err) {
|
|
|
|
|
updateGiftCards();
|
|
|
|
|
});
|
2017-07-11 00:41:10 -03:00
|
|
|
return;
|
2017-01-24 01:23:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
amazonService.savePendingGiftCard(newData, null, function(err) {
|
|
|
|
|
$log.debug("Saving new gift card");
|
|
|
|
|
updateGiftCards();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}, 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();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.$on('modal.hidden', function() {
|
|
|
|
|
$scope.updatePendingGiftCards();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
2017-07-11 00:41:10 -03:00
|
|
|
$scope.invoiceId = data.stateParams.invoiceId;
|
2017-01-24 01:23:57 -03:00
|
|
|
updateGiftCards(function() {
|
2017-07-11 00:41:10 -03:00
|
|
|
if ($scope.invoiceId) {
|
2017-01-24 01:23:57 -03:00
|
|
|
var card = lodash.find($scope.giftCards, {
|
2017-07-11 00:41:10 -03:00
|
|
|
invoiceId: $scope.invoiceId
|
2017-01-24 01:23:57 -03:00
|
|
|
});
|
|
|
|
|
if (lodash.isEmpty(card)) {
|
|
|
|
|
popupService.showAlert(null, 'Card not found');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$scope.openCardModal(card);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicView.afterEnter", function(event, data) {
|
|
|
|
|
$scope.updatePendingGiftCards();
|
|
|
|
|
});
|
|
|
|
|
});
|