2016-05-13 11:48:48 -03:00
|
|
|
'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-05-13 11:48:48 -03:00
|
|
|
|
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);
|
2016-08-24 19:00:50 -03:00
|
|
|
};
|
|
|
|
|
|
2016-09-22 01:47:39 -03:00
|
|
|
var initAmazon = function() {
|
2016-07-21 11:18:48 -03:00
|
|
|
amazonService.getPendingGiftCards(function(err, gcds) {
|
2016-05-13 11:48:48 -03:00
|
|
|
if (err) {
|
2016-12-22 12:27:43 -03:00
|
|
|
popupService.showAlert('Error', err);
|
2016-05-13 11:48:48 -03:00
|
|
|
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-11-28 17:01:07 -03:00
|
|
|
if ($scope.cardClaimCode) {
|
2016-12-12 17:29:11 -03:00
|
|
|
var card = lodash.find($scope.giftCards, {
|
|
|
|
|
claimCode: $scope.cardClaimCode
|
|
|
|
|
});
|
2016-11-28 17:01:07 -03:00
|
|
|
if (lodash.isEmpty(card)) {
|
2016-12-22 12:27:43 -03:00
|
|
|
popupService.showAlert('Error', 'Card not found');
|
2016-11-28 17:01:07 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$scope.openCardModal(card);
|
|
|
|
|
}
|
2016-05-13 11:48:48 -03:00
|
|
|
});
|
2016-09-22 01:47:39 -03:00
|
|
|
$scope.updatePendingGiftCards();
|
|
|
|
|
};
|
2016-07-21 11:18:48 -03:00
|
|
|
|
2016-09-22 01:47:39 -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);
|
|
|
|
|
}
|
2016-11-28 17:01:07 -03:00
|
|
|
$timeout(function() {
|
2016-12-22 12:27:43 -03:00
|
|
|
$scope.giftCards = lodash.isEmpty(gcds) ? null : gcds;
|
2016-11-28 17:01:07 -03:00
|
|
|
$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
|
|
|
|
|
});
|
2016-05-13 11:48:48 -03:00
|
|
|
|
2016-09-22 01:47:39 -03:00
|
|
|
$scope.openCardModal = function(card) {
|
2016-06-13 15:00:17 -03:00
|
|
|
$scope.card = card;
|
2016-05-13 11:48:48 -03:00
|
|
|
|
2016-06-13 15:00:17 -03:00
|
|
|
$ionicModal.fromTemplateUrl('views/modals/amazon-card-details.html', {
|
|
|
|
|
scope: $scope
|
|
|
|
|
}).then(function(modal) {
|
|
|
|
|
$scope.amazonCardDetailsModal = modal;
|
|
|
|
|
$scope.amazonCardDetailsModal.show();
|
2016-05-13 11:48:48 -03:00
|
|
|
});
|
2016-06-14 00:36:41 -03:00
|
|
|
|
2016-12-22 12:27:43 -03:00
|
|
|
$scope.$on('modal.hidden', function() {
|
|
|
|
|
$scope.updatePendingGiftCards();
|
2016-06-14 00:36:41 -03:00
|
|
|
});
|
2016-05-13 11:48:48 -03:00
|
|
|
};
|
2016-09-22 01:47:39 -03:00
|
|
|
|
2016-10-12 15:29:06 -04:00
|
|
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
2016-11-28 17:01:07 -03:00
|
|
|
$scope.cardClaimCode = data.stateParams.cardClaimCode;
|
2016-09-22 01:47:39 -03:00
|
|
|
initAmazon();
|
|
|
|
|
});
|
2016-05-13 11:48:48 -03:00
|
|
|
});
|