models/UserUpdate.js

export default class UserUpdate {
    /**
     * Creates a new UserUpdate instance from the provider parameters.
     *
     * @constructor UserUpdate
     * @param {Object} userMap - User update parameters
     * @param {string} [userMap.displayName] - User Display name
     * @param {File} [userMap.avatarFile] - User Avatar File
     * @param {string} [userMap.avatarUrl] - User Avatar URL
     * @param {Object<string, string>} [userMap.publicProperties={}] - User public properties
     * @param {Object<string, string>} [userMap.privateProperties={}] - User private properties
     */
    constructor(userMap) {
        userMap = userMap || {};
        this.displayName = userMap.displayName || null;
        this.avatarFile = userMap.avatarFile || null;
        this.avatarUrl = userMap.avatarUrl || null;
        this.publicProperties = userMap.publicProperties || {};
        this.privateProperties = userMap.privateProperties || {};
    }

    /**
     * Set the display name.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} displayName - New display name.
     * @return same instance.
     */
     updateDisplayName(displayName) {
        this.displayName = displayName;
        return this;
    }

    /**
     * Set the avatar URL.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} avatarUrl - New avatar URL.
     * @return same instance.
     */
    updateAvatarUrl(avatarUrl) {
        this.avatarUrl = avatarUrl;
        return this;
    }

    /**
     * Set the avatar with a file.
     * If set, it will overwrite the value for avatarUrl on update
     *
     * @memberof UserUpdate
     * @instance
     * @param {File} avatarFile - New avatar.
     * @return same instance.
     */
    updateAvatarWithFile(avatarFile) {
        this.avatarFile = avatarFile;
        return this;
    }

    /**
     * Set a public property with the provided key-value pair.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} key - Property key
     * @param {string} value - Property value
     * @return same instance.
     */
    setPublicProperty(key, value) {
        this.publicProperties[key] = value;
        return this;
    }

    /**
     * Remove public property with the given key.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} key - Property key
     * @return same instance.
     */
    removePublicProperty(key) {
        this.publicProperties[key] = UserUpdate.REMOVE_VALUE;
        return this;
    }

    /**
     * Set a private property with the provided key-value pair.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} key - Property key
     * @param {string} value - Property value
     * @return same instance.
     */
    setPrivateProperty(key, value) {
        this.privateProperties[key] = value;
        return this;
    }

    /**
     * Remove private property with the given key.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} key - Property key
     * @return same instance.
     */
    removePrivateProperty(key) {
        this.privateProperties[key] = UserUpdate.REMOVE_VALUE;
        return this;
    }

    /**
     * Increment a public property with the provided value.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} key - Property key
     * @param {number} increment - Positive number
     * @return same instance.
     */
    incrementPublicProperty(key, increment) {
        this.publicProperties[key] = UserUpdate.SUM_PLACEHOLDER + increment;
        return this;
    }

    /**
     * Decrement a public property with the provided value.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} key - Property key
     * @param {number} decrement - Positive number
     * @return same instance.
     */
    decrementPublicProperty(key, decrement) {
        this.publicProperties[key] = UserUpdate.SUM_PLACEHOLDER +
            (decrement * -1);
        return this;
    }

    /**
     * Increment a private property with the provided value.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} key - Property key
     * @param {number} increment - Positive number
     * @return same instance.
     */
    incrementPrivateProperty(key, increment) {
        this.privateProperties[key] = UserUpdate.SUM_PLACEHOLDER + increment;
        return this;
    }

    /**
     * Decrement a private property with the provided value.
     *
     * @memberof UserUpdate
     * @instance
     * @param {string} key - Property key
     * @param {number} decrement - Positive number
     * @return same instance.
     */
    decrementPrivateProperty(key, decrement) {
        this.privateProperties[key] = UserUpdate.SUM_PLACEHOLDER +
            (decrement * -1);
        return this;
    }
}

UserUpdate.REMOVE_VALUE = '';
UserUpdate.SUM_PLACEHOLDER = '$SUM:';