File size: 2,066 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

import { omnilog } from '../core/OmniLog';
import { Settings } from '../core/Settings';
import { DBObject, type IDBObjectLink } from './DBObject';

enum EUserStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive'
}

class User extends DBObject {
  static readonly modelName = 'User';

  email: string | null;
  username: string;
  status: string;
  externalId: string | undefined;
  authType: string | undefined;
  credit: number;
  organisation: IDBObjectLink | null;
  tier: IDBObjectLink | null;

  password: string | null;
  salt: string | null;
  tags: string[];

  settings: Settings;

  tosAccepted: number;

  constructor(id: string, username: string) {
    super(id);
    this._id = `user:${this.id}`;
    this.email = null;
    this.username = username;
    this.status = EUserStatus.ACTIVE;
    this.credit = 0;
    this.organisation = null;
    this.tier = null;
    this.password = null;
    this.salt = null;
    this.tags = [];
    this.settings = new Settings(this.id);
    this.tosAccepted = 0;
  }

  // @deprecated
  isAdmin() {
    // Moving this to AuthIntegration to optimize db calls
    omnilog.warn('User.isAdmin() is deprecated. Use AuthIntegration.isAdmin() instead');
    return this.tags.some((tag) => tag === 'admin');
  }

  static fromJSON(json: any): User {
    const result = new User(json.id, json.username);
    result._id = json._id;
    result._rev = json._rev;
    result.id = json.id;
    result.createdAt = json.createdAt;
    result.lastUpdated = json.lastUpdated;
    result.email = json.email;
    result.username = json.username;
    result.status = json.status;
    result.externalId = json.externalId;
    result.authType = json.authType;
    result.credit = json.credit;
    result.organisation = json.organisation;
    result.tier = json.tier;
    result.password = json.password;
    result.salt = json.salt;
    result.tags = json.tags;
    result.tosAccepted = json.tosAccepted;
    return result;
  }
}

export { User, EUserStatus };