2022-02-14 21:26:21 +00:00
|
|
|
import {UserOptions} from 'app/common/UserAPI';
|
|
|
|
import {nativeValues} from 'app/gen-server/lib/values';
|
2022-10-03 14:45:44 +00:00
|
|
|
import {makeId} from 'app/server/lib/idUtils';
|
|
|
|
import {BaseEntity, BeforeInsert, Column, Entity, JoinTable, ManyToMany, OneToMany, OneToOne,
|
2020-07-21 13:20:51 +00:00
|
|
|
PrimaryGeneratedColumn} from "typeorm";
|
|
|
|
|
|
|
|
import {Group} from "./Group";
|
|
|
|
import {Login} from "./Login";
|
|
|
|
import {Organization} from "./Organization";
|
2023-04-11 05:00:28 +00:00
|
|
|
import {Pref} from './Pref';
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
@Entity({name: 'users'})
|
|
|
|
export class User extends BaseEntity {
|
|
|
|
|
|
|
|
@PrimaryGeneratedColumn()
|
|
|
|
public id: number;
|
|
|
|
|
2023-05-23 19:17:28 +00:00
|
|
|
@Column({type: String})
|
2020-07-21 13:20:51 +00:00
|
|
|
public name: string;
|
|
|
|
|
|
|
|
@Column({name: 'api_key', type: String, nullable: true})
|
|
|
|
// Found how to make a type nullable in this discussion: https://github.com/typeorm/typeorm/issues/2567
|
|
|
|
// todo: adds constraint for api_key not to equal ''
|
|
|
|
public apiKey: string | null;
|
|
|
|
|
|
|
|
@Column({name: 'picture', type: String, nullable: true})
|
|
|
|
public picture: string | null;
|
|
|
|
|
|
|
|
@Column({name: 'first_login_at', type: Date, nullable: true})
|
|
|
|
public firstLoginAt: Date | null;
|
|
|
|
|
|
|
|
@OneToOne(type => Organization, organization => organization.owner)
|
|
|
|
public personalOrg: Organization;
|
|
|
|
|
|
|
|
@OneToMany(type => Login, login => login.user)
|
|
|
|
public logins: Login[];
|
|
|
|
|
2023-04-11 05:00:28 +00:00
|
|
|
@OneToMany(type => Pref, pref => pref.user)
|
|
|
|
public prefs: Pref[];
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
@ManyToMany(type => Group)
|
|
|
|
@JoinTable({
|
|
|
|
name: 'group_users',
|
|
|
|
joinColumn: {name: 'user_id'},
|
|
|
|
inverseJoinColumn: {name: 'group_id'}
|
|
|
|
})
|
|
|
|
public groups: Group[];
|
|
|
|
|
2023-05-23 19:17:28 +00:00
|
|
|
@Column({name: 'is_first_time_user', type: Boolean, default: false})
|
2020-07-21 13:20:51 +00:00
|
|
|
public isFirstTimeUser: boolean;
|
|
|
|
|
2022-02-14 21:26:21 +00:00
|
|
|
@Column({name: 'options', type: nativeValues.jsonEntityType, nullable: true})
|
|
|
|
public options: UserOptions | null;
|
|
|
|
|
2022-05-18 10:25:14 +00:00
|
|
|
@Column({name: 'connect_id', type: String, nullable: true})
|
|
|
|
public connectId: string | null;
|
|
|
|
|
2022-10-03 14:45:44 +00:00
|
|
|
/**
|
|
|
|
* Unique reference for this user. Primarily used as an ownership key in a cell metadata (comments).
|
|
|
|
*/
|
|
|
|
@Column({name: 'ref', type: String, nullable: false})
|
|
|
|
public ref: string;
|
|
|
|
|
|
|
|
@BeforeInsert()
|
|
|
|
public async beforeInsert() {
|
|
|
|
if (!this.ref) {
|
|
|
|
this.ref = makeId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
/**
|
|
|
|
* Get user's email. Returns undefined if logins has not been joined, or no login
|
|
|
|
* is available
|
|
|
|
*/
|
|
|
|
public get loginEmail(): string|undefined {
|
|
|
|
const login = this.logins && this.logins[0];
|
|
|
|
if (!login) { return undefined; }
|
|
|
|
return login.email;
|
|
|
|
}
|
|
|
|
}
|