Source

classes/basicUser.ts

import IBasicUser, { UserType } from "../types/basicUser";
import Snowflake from "./snowflake";
import Language from "../types/language";
import File from "./file";
import { RequestContext } from "./requestContext";

/**
 * Holds user data without any methods
 */
class BasicUser {
    id: Snowflake;
    ctx: RequestContext;
    name: string;
    email?: string;
    avatar?: File;
    description: string;
    password: string;
    auth: string;
    language: Language;
    type: UserType;
    lastLogedIn?: Date;

    /**
     * Creates basic user data holed
     * @param {object} data
     */
    constructor({
        id,
        ctx,
        name,
        description = "",
        password = "",
        auth = "",
        language = Language.EN,
        type = UserType.MEMBER,
        email = "",
        avatar,
        lastLogedIn
    }: {
        id: Snowflake,
        ctx: RequestContext,
        name: string,
        description?: string,
        password?: string,
        auth?: string,
        language?: Language,
        type?: UserType,
        email?: string,
        avatar?: File,
        lastLogedIn?: Date
    }) {
        this.id = id;
        this.ctx = ctx;
        this.name = name;
        this.description = description;
        this.password = password;
        this.auth = auth;
        this.language = language;
        this.type = type;
        this.email = email;
        this.avatar = avatar;
        this.lastLogedIn = lastLogedIn;
    }

    /**
     * Returns JSON-compatible object to be used in stringify
     * @return {Object}
     */
    toJSON() {
        return {
            id: this.id.getSnowflake(),
            name: this.name,
            language: this.language,
            registered: this.id.time,
            type: this.type,
            lastLogedIn: this.lastLogedIn
        };
    }
}

export default BasicUser;