Fix and refactor emailService for notifications by email
This commit is contained in:
parent
82879cca20
commit
3700845449
5 changed files with 72 additions and 64 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
angular.module('copayApp.controllers').controller('collectEmailController', function($scope, $state, $log, $timeout, $http, $httpParamSerializer, $ionicConfig, profileService, configService, walletService, appConfigService) {
|
angular.module('copayApp.controllers').controller('collectEmailController', function($scope, $state, $log, $timeout, $http, $httpParamSerializer, $ionicConfig, profileService, configService, walletService, appConfigService, emailService) {
|
||||||
|
|
||||||
var wallet, walletId;
|
var wallet, walletId;
|
||||||
$scope.data = {};
|
$scope.data = {};
|
||||||
|
|
@ -48,22 +48,18 @@ angular.module('copayApp.controllers').controller('collectEmailController', func
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.save = function() {
|
$scope.save = function() {
|
||||||
var opts = {
|
var enabled = true; // Set enabled email: true
|
||||||
emailFor: {}
|
|
||||||
};
|
emailService.updateEmail({
|
||||||
opts.emailFor[walletId] = $scope.data.email;
|
enabled: enabled,
|
||||||
walletService.updateRemotePreferences(wallet, {
|
email: enabled ? $scope.data.email : null
|
||||||
email: $scope.data.email,
|
|
||||||
}, function(err) {
|
|
||||||
if (err) return;
|
|
||||||
configService.set(opts, function(err) {
|
|
||||||
if (err) $log.warn(err);
|
|
||||||
if ($scope.data.accept) collectEmail();
|
|
||||||
$timeout(function() {
|
|
||||||
$scope.goNextView();
|
|
||||||
}, 200);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if ($scope.data.accept) collectEmail();
|
||||||
|
|
||||||
|
$timeout(function() {
|
||||||
|
$scope.goNextView();
|
||||||
|
}, 200);
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.goNextView = function() {
|
$scope.goNextView = function() {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
angular.module('copayApp.controllers').controller('preferencesNotificationsController', function($scope, $log, $timeout, appConfigService, lodash, configService, platformInfo, pushNotificationsService, profileService, emailService) {
|
angular.module('copayApp.controllers').controller('preferencesNotificationsController', function($scope, $log, $timeout, appConfigService, lodash, configService, platformInfo, pushNotificationsService, emailService) {
|
||||||
var updateConfig = function() {
|
var updateConfig = function() {
|
||||||
var config = configService.getSync();
|
var config = configService.getSync();
|
||||||
$scope.appName = appConfigService.nameCase;
|
$scope.appName = appConfigService.nameCase;
|
||||||
|
|
@ -13,7 +13,7 @@ angular.module('copayApp.controllers').controller('preferencesNotificationsContr
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.latestEmail = {
|
$scope.latestEmail = {
|
||||||
value: getLatestEmailConfig()
|
value: emailService.getEmailIfEnabled()
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.newEmail = lodash.clone($scope.latestEmail);
|
$scope.newEmail = lodash.clone($scope.latestEmail);
|
||||||
|
|
@ -44,33 +44,19 @@ angular.module('copayApp.controllers').controller('preferencesNotificationsContr
|
||||||
|
|
||||||
$scope.emailNotificationsChange = function() {
|
$scope.emailNotificationsChange = function() {
|
||||||
var opts = {
|
var opts = {
|
||||||
emailNotifications: {
|
enabled: $scope.emailNotifications.value,
|
||||||
enabled: $scope.emailNotifications.value
|
email: $scope.newEmail.value
|
||||||
}
|
|
||||||
};
|
};
|
||||||
configService.set(opts, function(err) {
|
|
||||||
if (err) $log.debug(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
$scope.latestEmail = {
|
$scope.latestEmail = {
|
||||||
value: getLatestEmailConfig()
|
value: emailService.getEmailIfEnabled()
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.newEmail = lodash.clone($scope.latestEmail);
|
emailService.updateEmail(opts);
|
||||||
|
|
||||||
if (!$scope.emailNotifications.value) {
|
|
||||||
emailService.enableEmailNotifications({
|
|
||||||
enabled: $scope.emailNotifications.value,
|
|
||||||
email: null
|
|
||||||
});
|
|
||||||
}
|
|
||||||
$timeout(function() {
|
|
||||||
$scope.$apply();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.save = function() {
|
$scope.save = function() {
|
||||||
emailService.enableEmailNotifications({
|
emailService.updateEmail({
|
||||||
enabled: $scope.emailNotifications.value,
|
enabled: $scope.emailNotifications.value,
|
||||||
email: $scope.newEmail.value
|
email: $scope.newEmail.value
|
||||||
});
|
});
|
||||||
|
|
@ -84,11 +70,6 @@ angular.module('copayApp.controllers').controller('preferencesNotificationsContr
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function getLatestEmailConfig() {
|
|
||||||
var config = configService.getSync();
|
|
||||||
return config.emailFor ? lodash.values(config.emailFor)[0] : null;
|
|
||||||
};
|
|
||||||
|
|
||||||
$scope.$on("$ionicView.enter", function(event, data) {
|
$scope.$on("$ionicView.enter", function(event, data) {
|
||||||
updateConfig();
|
updateConfig();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1111,7 +1111,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.run(function($rootScope, $state, $location, $log, $timeout, startupService, fingerprintService, $ionicHistory, $ionicPlatform, $window, appConfigService, lodash, platformInfo, profileService, uxLanguage, gettextCatalog, openURLService, storageService, scannerService, configService, /* plugins START HERE => */ coinbaseService, glideraService, amazonService, bitpayCardService, applicationService) {
|
.run(function($rootScope, $state, $location, $log, $timeout, startupService, fingerprintService, $ionicHistory, $ionicPlatform, $window, appConfigService, lodash, platformInfo, profileService, uxLanguage, gettextCatalog, openURLService, storageService, scannerService, configService, emailService, /* plugins START HERE => */ coinbaseService, glideraService, amazonService, bitpayCardService, applicationService) {
|
||||||
|
|
||||||
uxLanguage.init();
|
uxLanguage.init();
|
||||||
|
|
||||||
|
|
@ -1224,8 +1224,9 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
|
||||||
applicationService.appLockModal('check');
|
applicationService.appLockModal('check');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
// After everything have been loaded, initialize handler URL
|
// After everything have been loaded
|
||||||
$timeout(function() {
|
$timeout(function() {
|
||||||
|
emailService.init(); // Update email subscription if necessary
|
||||||
openURLService.init();
|
openURLService.init();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,62 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
angular.module('copayApp.services').factory('emailService', function($log, configService, profileService, lodash, walletService) {
|
angular.module('copayApp.services').factory('emailService', function($log, configService, lodash, walletService, profileService) {
|
||||||
var root = {};
|
var root = {};
|
||||||
|
|
||||||
root.enableEmailNotifications = function(opts) {
|
root.updateEmail = function(opts) {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
if (!opts.email) return;
|
||||||
|
|
||||||
var wallets = profileService.getWallets();
|
var wallets = profileService.getWallets();
|
||||||
var keys = lodash.map(wallets, function(w) {
|
|
||||||
return w.credentials.walletId;
|
|
||||||
});
|
|
||||||
|
|
||||||
walletService.updateRemotePreferences(wallets, {
|
|
||||||
email: opts.enabled ? opts.email : null
|
|
||||||
});
|
|
||||||
|
|
||||||
var config = configService.getSync();
|
|
||||||
if (!config.emailFor)
|
|
||||||
config.emailFor = {};
|
|
||||||
|
|
||||||
lodash.each(keys, function(k) {
|
|
||||||
config.emailFor[k] = opts.email;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!opts.enabled) return;
|
|
||||||
|
|
||||||
configService.set({
|
configService.set({
|
||||||
emailFor: config.emailFor
|
emailFor: null, // Backward compatibility
|
||||||
|
emailNotifications: {
|
||||||
|
enabled: opts.enabled,
|
||||||
|
email: opts.enabled ? opts.email : null
|
||||||
|
}
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (err) $log.debug(err);
|
if (err) $log.warn(err);
|
||||||
|
walletService.updateRemotePreferences(wallets);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
root.getEmailIfEnabled = function(config) {
|
||||||
|
config = config || configService.getSync();
|
||||||
|
|
||||||
|
if (config.emailNotifications) {
|
||||||
|
if (!config.emailNotifications.enabled) return;
|
||||||
|
|
||||||
|
if (config.emailNotifications.email)
|
||||||
|
return config.emailNotifications.email;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lodash.isEmpty(config.emailFor)) return;
|
||||||
|
|
||||||
|
// Backward compatibility
|
||||||
|
var emails = lodash.values(config.emailFor);
|
||||||
|
for(var i = 0; i < emails.length; i++) {
|
||||||
|
if (emails[i] !== null && typeof emails[i] !== 'undefined') {
|
||||||
|
return emails[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
root.init = function() {
|
||||||
|
configService.whenAvailable(function(config) {
|
||||||
|
|
||||||
|
if (config.emailNotifications && config.emailNotifications.enabled) {
|
||||||
|
|
||||||
|
// If email already set
|
||||||
|
if (config.emailNotifications.email) return;
|
||||||
|
|
||||||
|
var currentEmail = root.getEmailIfEnabled(config);
|
||||||
|
|
||||||
|
root.updateEmail({
|
||||||
|
enabled: currentEmail ? true : false,
|
||||||
|
email: currentEmail
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -763,11 +763,13 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
|
||||||
};
|
};
|
||||||
|
|
||||||
// Update this JIC.
|
// Update this JIC.
|
||||||
var config = configService.getSync().wallet.settings;
|
var config = configService.getSync();
|
||||||
|
var walletSettings = config.wallet.settings;
|
||||||
|
|
||||||
//prefs.email (may come from arguments)
|
//prefs.email (may come from arguments)
|
||||||
|
prefs.email = config.emailNotifications.email;
|
||||||
prefs.language = uxLanguage.getCurrentLanguage();
|
prefs.language = uxLanguage.getCurrentLanguage();
|
||||||
prefs.unit = config.unitCode;
|
prefs.unit = walletSettings.unitCode;
|
||||||
|
|
||||||
updateRemotePreferencesFor(lodash.clone(clients), prefs, function(err) {
|
updateRemotePreferencesFor(lodash.clone(clients), prefs, function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue