src/helpers/user/Profile.js
import ProfileModel from '../../models/user/Profile';
import UserService from '../../services/UserService';
import C6Image from '../Image';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
/**
* Helper class representing a user profile.
*/
class Profile extends aggregate(ProfileModel, Helper) {
/**
* Constructor for helper class representing a Profile
*
* @param {Object} json - json api response from server
*/
constructor(json = {}) {
super(json);
this.deserializeRelationshipStubs(json);
this.syncRelationships(json);
const _response = json.data || json; // if json api is passed in directly
const attributes = _response.attributes || _response; // if json api is passed in directly
if (attributes.avatar) {
// console.log(attributes.avatar.avatar);
// console.log(this.avatar);
this.avatar = new C6Image(attributes.avatar);
}
}
/** @type {String} - The type */
static get type() {
return 'profiles';
}
/** @type {Language} */
get language() {
if (!this._relationships) {
this._relationships = {};
}
return this._relationships.language;
}
/** @type {Language} */
set language(language) {
if (!this._relationships) {
this._relationships = {};
}
/** @type {Language} */
this._relationships.language = language;
}
/** @type {User} */
get profileable() {
return this._relationships.profileable;
}
/** @type {User} */
set profileable(profileable) {
/** @type {User} */
this._relationships.profileable = profileable;
}
/** @type {User} */
get user() {
return this._relationships.profileable;
}
/** @type {User} */
set user(user) {
/** @type {User} */
this._relationships.profileable = user;
}
/**
* Returns the full name of the user.
*
* @return {String} The user's full name
*
* @example
* client.getProfile().then(profile => {
* console.log('Full Name', profile.getFullName());
* });
*
* @memberOf Profile
*/
getFullName() {
return this.firstName + this.middleInitial + this.lastName;
}
/**
* Returns the full address of the user
*
* @return {String} The full address
*
* @example
* client.getProfile().then(profile => {
* console.log('Full Address', profile.getFullAddress());
* });
*
* @memberOf Profile
*/
getFullAddress() {
return `${this.street} ${this.city}, ${this.state} ${this.zipCode}`;
}
/**
* Updates the profile information for the current instance of profile. This
* method should be called after making changes to the profile object. I.e.
* profile.age = 25 then call profile.save() to save this change.
*
*
* @return {Promise} Promise object that returns success or failure
*
* @example
* client.getProfile().then(profile => {
* profile.zip_code = '12345';
* profile.save().then((response) => {
* console.log(response);
* });
* });
*
* @memberOf Profile
*/
save() {
return (new UserService()).saveProfile(this);
}
/**
* @override
* Overrides the existing toJSON to adjust for image attributes
*/
toJSON() {
const obj = super.toJSON();
if (this.avatar) {
obj.attributes.avatar = { avatar: this.avatar.toJSON() };
}
return obj;
}
}
export default Profile;