src/modules/identity-user.js
/**
* Identity user
*
* This will have a selection of properties defined by the active {@link IdentityConfig#properties}.
*
* For a list of standard properties see: {@link IdentityProperty.standardMap}.
*/
class IdentityUser {
/**
* Map from user property keys used by the SDK to those used by the server
*
* - id => _id
* - revisionId => _rev
* - birthday => DOB
* - firstName => givenName
* - email => mail
* - address => postalAddress
* - zip => serviceZip
* - zipCode => serviceZip
* - lastName => sn
* - state => stateProvince
* - phone => telephoneNumber
* - tcDate => termsAndConditionsAgreementDate
* - username => userName
*
* @return {Map<string, string>}
*/
static get serverKeyMap() {
return {
id: '_id',
revisionId: '_rev',
email: 'mail',
// facebookId: 'facebookId',
// googleId: 'googleId',
username: 'userName',
password: 'password',
firstName: 'givenName',
lastName: 'sn',
phone: 'telephoneNumber',
address: 'postalAddress',
address2: 'address2',
// city: 'city',
// country: 'country',
zip: 'serviceZip',
zipCode: 'serviceZip',
state: 'stateProvince',
// gender: 'gender',
// ageRange: 'ageRange',
// episodeCount: 'episodeCount',
// episodeArray: 'episodeArray',
birthday: 'DOB',
tcDate: 'termsAndConditionsAgreementDate',
};
}
/**
* IdentityUser constructor
*
* Only properties specified in the config will be copied to this instance.
*
* The {@link IdentityUser.serverKeyMap} will be automatically applied to change property keys as necessary.
* E.g: If you provide {givenName: 'foo'} this will be equivalent to providing {firstName: 'foo'}
*
* @param {IdentityConfig} config - Identity config
* @param {Object} [properties={}] - Object with property values
*/
constructor(config, properties = {}) {
const map = IdentityUser.serverKeyMap;
Object.entries(config.properties).forEach(([key, property]) => {
const mappedKey = map[key];
/**
* Save properties to the IdentityUser object
*
* @type {Object}
*/
this[key] = mappedKey && mappedKey in properties ? properties[mappedKey] :
key in properties ? properties[key] :
property.value; // fallback to configured default value
});
// Disable property modification
Object.freeze(this);
}
}
export default IdentityUser;