Fix and refactor emailService for notifications by email

This commit is contained in:
Gustavo Maximiliano Cortez 2017-05-14 19:21:12 -03:00
commit 3700845449
No known key found for this signature in database
GPG key ID: 15EDAD8D9F2EB1AF
5 changed files with 72 additions and 64 deletions

View file

@ -1,34 +1,62 @@
'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 = {};
root.enableEmailNotifications = function(opts) {
root.updateEmail = function(opts) {
opts = opts || {};
if (!opts.email) return;
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({
emailFor: config.emailFor
emailFor: null, // Backward compatibility
emailNotifications: {
enabled: opts.enabled,
email: opts.enabled ? opts.email : null
}
}, 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
});
}
});
};

View file

@ -763,11 +763,13 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
};
// 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 = config.emailNotifications.email;
prefs.language = uxLanguage.getCurrentLanguage();
prefs.unit = config.unitCode;
prefs.unit = walletSettings.unitCode;
updateRemotePreferencesFor(lodash.clone(clients), prefs, function(err) {
if (err) return cb(err);