Home Reference Source

src/session/User.js

/**
 * Represents a user. This class must not be directly instantiated. Instead,
 * instances of this class are obtained by calling to {@link session#getUser}
 */
export class User {
	/**
	 * @protected
	 * @return {User}
	 */
	static newInstance(wacProxy) {
		return new User(wacProxy);
	}

	/** @private */
	constructor(wacProxy) {
		this._wacProxy = wacProxy;
		this._initializing = null;
	}

	/** @protected */
	init() {
		if (!this._initializing) {
			this._id = this._wacProxy.getCurrentSession().userObj.id;
			this._initializing = this._wacProxy.getUser(this._id).then((data) => {
				this._update(data);
			});
		}
		return this._initializing;
	}

	/**
	 * The unique identifier of this user
	 * @type {string}
	 */
	get id() {
		return this._id;
	}

	/**
	 * The domain of this user
	 * @type {string}
	 */
	get domain() {
		return this._domain;
	}

	/**
	 * The username of this user
	 * @type {string}
	 */
	get username() {
		return this._username;
	}

	/**
	 * Returns the address of this user as <user>@<domain>
	 * @return {string} The user address
	 */
	getAddress() {
		return `${this._username}@${this._domain}`;
	}

	/**
	 * User's email
	 * @type {string}
	 */
	get email() {
		return this._email;
	}

	/**
	 * When the user was created (milliseconds)
	 * @type {number}
	 */
	get created() {
		return this._created;
	}

	/**
	 * User's lastLogin
	 * @type {number}
	 */
	get lastLogin() {
		return this._lastLogin;
	}

	/**
	 * User's role. One of them: "anonymous", "user", "admin"
	 * @type {string}
	 */
	get role() {
		return this._role;
	}

	/**
	 * User's capabilities
	 * @type {Array}
	 */
	get capabilities() {
		return this._capabilities;
	}

	/**
	 * User's mobile phone
	 * @type {Array}
	 */
	get mobilePhone() {
		return this._mobilePhone;
	}

	/**
	 * User's landLineNumber
	 * @type {string}
	 */
	get landLineNumber() {
		return this._landLineNumber;
	}

	/**
	 * User's alias
	 * @type {string}
	 */
	get alias() {
		return this._alias;
	}

	/**
	 * Update an user
	 * @param {Object} objToUpdate Data that will be used to update an user
	 * @param {String} [objToUpdate.email]
	 * @param {Array} [objToUpdate.mobilePhone]
	 * @param {String} [objToUpdate.landLineNumber]
	 * @param {String} [objToUpdate.alias]
	 * @example
	 * // returns this
	 * this.update({
	 *    "email": "user@demo.quobis.com",
	 *    "mobilePhone": ['phone1', 'phone2'],
	 *    "landLineNumber": "landLineNumber"
	 *  });
	 *
	 * @return {Promise<User>}
	 */
	update(objToUpdate) {
		return this._wacProxy.updateUser(this._id, objToUpdate).then((oUpdated) => {
			return this._update(oUpdated);
		});
	}

	/**
	 * Check if a code is right to update a phone
	 *
	 * @param  {String} code Random string introduced by the user
	 * @param  {String} phone Phone that it wants to update
	 * @example
	 * // returns this
	 * this.validatePhone('uifs8wr5', '4564879564');
	 *
	 * @return {Promise<User>}
	 */
	validatePhone(code, phone) {
		return this._wacProxy.validatePhone(this._id, code, phone).then((oUpdated) => {
			return this._update(oUpdated);
		});
	}

	/**
	 * Updates the contact with the new data
	 * @protected
	 * @param {Object} data
	 * @param {String} data.id
	 * @param {String} data.domain
	 * @param {String} data.username
	 * @param {String} data.email
	 * @param {Number} data.created
	 * @param {Number} data.lastLogin
	 * @param {String} data.role
	 * @param {Array}  data.capabilities
	 * @param {Array}  data.mobilePhone
	 * @param {String} data.landLineNumber
	 * @param {String} data.alias
	 */
	_update(data) {
		this._id = data.id;
		this._domain = data.domain;
		this._username = data.username;
		this._email = data.email;
		this._created = data.created;
		this._lastLogin = data.lastLogin;
		this._role = data.role;
		this._capabilities = data.capabilities;
		this._mobilePhone = data.mobilePhone;
		this._landLineNumber = data.landLineNumber;
		this._alias = data.alias;
		return this;
	}

	/**
	 * Returns a JS object with the JSON serialization representation of this object
	 * @return {Object}
	 * @property {String} id
	 * @property {String} domain
	 * @property {String} username
	 * @property {String} email
	 * @property {Number} created
	 * @property {Number} lastLogin
	 * @property {String} role
	 * @property {Array}  capabilities
	 * @property {Array}  mobilePhone
	 * @property {String} landLineNumber
	 * @property {String} alias
	 */
	toJSON() {
		return {
			id: this._id,
			domain: this._domain,
			username: this._username,
			email: this._email,
			created: this._created,
			lastLogin: this._lastLogin,
			role: this._role,
			capabilities: this._capabilities,
			mobilePhone: this._mobilePhone,
			landLineNumer: this._landLineNumer,
			alias: this._alias,
		};
	}
}