models/UserId.js

import SDK from "../sdk";

export default class UserId {
    /**
     * Creates a UserId
     *
     * @constructor UserId
     * @param {string} id - User ID.
     * @param {string} providerId - Provider ID.
     */
    constructor(id, providerId) {
        this.id = id || null;
        this.providerId = providerId || null;
    }

    /**
     * Creates a single GetSocial user identifier.
     *
     * @memberof UserId
     * @param {string} id - GetSocial user ID.
     * @return {UserId} New identifier.
     */
    static create(id) {
        return new UserId(id);
    }

    /**
     * Creates a single identity user identifier for giver provider ID.
     *
     * @memberOf UserId
     * @param {string} providerId custom identity provider ID.
     * @param {string} id identity user ID.
     * @return {UserId} new identifier.
     */
    static createWithProvider(providerId, id) {
        return new UserId(id, providerId);
    }

    /**
     * Creates a single GetSocial user identifier for the app.
     *
     * @memberof UserId
     * @return {UserId} New identifier
     */
     static createForApp() {
        return new UserId('app');
    }

    /**
     * Returns current user's ID
     *
     * @memberof UserId
     * @return {UserId} Current user ID.
     */
    static currentUser() {
        const user = SDK.getCurrentUser();

        return new UserId(user
            ? user.id
            : null);
    }

    /**
     * Creates a string for the Hades requests
     *
     * @ignore
     * @memberof UserId
     * @return {string} ID as string
     *
     */
    toString() {
        return this.providerId
            ? `${this.providerId}:${this.id}`
            : this.id;
    }
}