Wallet/src/js/models/profile.js

151 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
/**
* Profile
*
* credential: array of OBJECTS
*/
function Profile() {
this.version = '1.0.0';
};
2018-06-05 11:34:14 +12:00
Profile.create = function(appVersion) {
2015-03-06 12:00:10 -03:00
var x = new Profile();
2018-06-05 11:34:14 +12:00
x.appVersion = appVersion;
2015-03-06 12:00:10 -03:00
x.createdOn = Date.now();
2018-06-05 11:34:14 +12:00
x.credentials = [];
2018-02-02 12:27:32 -04:00
x.disclaimerAccepted = true;
x.checked = {};
2015-03-06 12:00:10 -03:00
return x;
};
Profile.fromObj = function(obj) {
var x = new Profile();
2015-05-13 11:58:19 -03:00
2018-06-05 11:34:14 +12:00
x.appVersion = obj.appVersion;
2015-03-06 12:00:10 -03:00
x.createdOn = obj.createdOn;
x.credentials = obj.credentials;
2015-12-07 12:52:42 -03:00
x.disclaimerAccepted = obj.disclaimerAccepted;
x.checked = obj.checked || {};
2016-06-06 18:26:45 -03:00
x.checkedUA = obj.checkedUA || {};
2015-05-13 11:58:19 -03:00
2015-03-06 12:00:10 -03:00
if (x.credentials[0] && typeof x.credentials[0] != 'object')
throw ("credentials should be an object");
2015-05-13 11:58:19 -03:00
2015-03-06 12:00:10 -03:00
return x;
};
Profile.fromString = function(str) {
return Profile.fromObj(JSON.parse(str));
};
Profile.prototype.toObj = function() {
2016-06-06 18:26:45 -03:00
delete this.dirty;
2015-03-06 12:00:10 -03:00
return JSON.stringify(this);
};
2016-06-06 12:21:15 -03:00
Profile.prototype.hasWallet = function(walletId) {
for (var i in this.credentials) {
var c = this.credentials[i];
if (c.walletId == walletId) return true;
};
return false;
};
Profile.prototype.isChecked = function(ua, walletId) {
2016-06-06 18:26:45 -03:00
return !!(this.checkedUA == ua && this.checked[walletId]);
2016-06-06 12:21:15 -03:00
};
2016-06-14 12:28:21 -03:00
Profile.prototype.isDeviceChecked = function(ua) {
return this.checkedUA == ua;
};
/**
*
* @param {Profile} other
*/
Profile.prototype.merge = function(other) {
2018-06-04 14:38:15 +12:00
var newCredentials = [];
2018-06-05 11:34:14 +12:00
var otherCredentialsLength = other.credentials.length;
var thisProfile = this;
2018-06-04 14:38:15 +12:00
other.credentials.forEach(function(otherCredential) {
var credentialExists = false;
2018-06-05 11:34:14 +12:00
thisProfile.credentials.forEach(function(thisCredential) {
2018-06-04 14:38:15 +12:00
if (otherCredential.walletId === thisCredential.walletId) {
credentialExists = true;
}
});
if (!credentialExists) {
newCredentials.push(otherCredential);
}
});
Array.prototype.push.apply(this.credentials, newCredentials);
};
2016-06-14 12:28:21 -03:00
2018-06-05 11:34:14 +12:00
/**
* It's a simple operation, but it means that all the profile logic stays
* in this file.
* @param {string} appVersion - ie "4.11.0"
*/
Profile.prototype.setAppVersion = function(appVersion) {
this.appVersion = appVersion;
}
2016-06-06 12:21:15 -03:00
Profile.prototype.setChecked = function(ua, walletId) {
if (this.checkedUA != ua) {
this.checkedUA = ua;
this.checked = {};
}
this.checked[walletId] = true;
2016-06-06 18:26:45 -03:00
this.dirty = true;
2016-06-06 12:21:15 -03:00
};
Profile.prototype.addWallet = function(credentials) {
2016-06-14 17:48:00 -03:00
if (!credentials.walletId)
throw 'credentials must have .walletId';
2016-06-06 12:21:15 -03:00
if (this.hasWallet(credentials.walletId))
return false;
this.credentials.push(credentials);
2016-06-06 18:26:45 -03:00
this.dirty = true;
return true;
};
Profile.prototype.updateWallet = function(credentials) {
2016-06-14 17:48:00 -03:00
if (!credentials.walletId)
throw 'credentials must have .walletId';
2016-06-06 18:26:45 -03:00
if (!this.hasWallet(credentials.walletId))
return false;
2016-06-14 17:48:00 -03:00
this.credentials = this.credentials.map(function(c) {
if(c.walletId != credentials.walletId ) {
return c;
} else {
return credentials
}
2016-06-06 18:26:45 -03:00
});
this.dirty = true;
2016-06-06 12:21:15 -03:00
return true;
};
Profile.prototype.deleteWallet = function(walletId) {
if (!this.hasWallet(walletId))
return false;
this.credentials = this.credentials.filter(function(c) {
return c.walletId != walletId;
});
2016-06-06 18:26:45 -03:00
this.dirty = true;
2016-06-06 12:21:15 -03:00
return true;
};