All files / src/lib/types user.ts

94.74% Statements 18/19
83.33% Branches 5/6
100% Functions 2/2
94.74% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 8689x 89x                                                     89x 492x                                                           492x     492x 491x 491x   491x 491x 491x 491x 491x 491x 491x 491x       491x             89x  
import gravatarUrl from 'gravatar-url';
import Joi from 'joi';
 
export interface UserData {
    id: number;
    name?: string;
    username?: string;
    email?: string;
    imageUrl?: string;
    seenAt?: Date;
    loginAttempts?: number;
    createdAt?: Date;
}
 
export interface IUser {
    id: number;
    name?: string;
    username?: string;
    email?: string;
    inviteLink?: string;
    seenAt?: Date;
    createdAt: Date;
    permissions: string[];
    loginAttempts: number;
    isAPI: boolean;
    imageUrl: string;
}
 
export default class User implements IUser {
    isAPI: boolean = false;
 
    id: number;
 
    name: string;
 
    username: string;
 
    email: string;
 
    permissions: string[];
 
    imageUrl: string;
 
    seenAt: Date;
 
    loginAttempts: number;
 
    createdAt: Date;
 
    constructor({
        id,
        name,
        email,
        username,
        imageUrl,
        seenAt,
        loginAttempts,
        createdAt,
    }: UserData) {
        Iif (!id) {
            throw new TypeError('Id is required');
        }
        Joi.assert(email, Joi.string().email(), 'Email');
        Joi.assert(username, Joi.string(), 'Username');
        Joi.assert(name, Joi.string(), 'Name');
 
        this.id = id;
        this.name = name;
        this.username = username;
        this.email = email;
        this.imageUrl = imageUrl || this.generateImageUrl();
        this.seenAt = seenAt;
        this.loginAttempts = loginAttempts;
        this.createdAt = createdAt;
    }
 
    generateImageUrl(): string {
        return gravatarUrl(this.email || this.username || '' + this.id, {
            size: 42,
            default: 'retro',
        });
    }
}
 
module.exports = User;