-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.ts
82 lines (67 loc) · 1.85 KB
/
User.ts
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
import { AggregateRoot } from '../../../shared/domain/AggregateRoot';
import { EntityID } from '../../../shared/domain/EntityID';
import { UserEmail } from './UserEmail';
import { UserCreatedEvent } from './events/UserCreatedEvent';
import { UserPassword } from './UserPassword';
import { UserName } from './UserName';
import { Alias } from './Alias';
interface UserProps {
email: UserEmail;
username: UserName;
password: UserPassword;
alias: Alias;
isEmailVerified?: boolean;
isAdminUser?: boolean;
isDeleted?: boolean;
}
export class User extends AggregateRoot<UserProps> {
get id(): EntityID {
return this._id;
}
get email(): UserEmail {
return this.props.email;
}
get password(): UserPassword {
return this.props.password;
}
get isEmailVerified(): boolean | undefined {
return this.props.isEmailVerified;
}
get username(): UserName {
return this.props.username;
}
get alias(): Alias {
return this.props.alias;
}
get isDeleted(): boolean | undefined {
return this.props.isDeleted;
}
get isAdminUser(): boolean | undefined {
return this.props.isAdminUser;
}
private constructor(props: UserProps, id?: EntityID) {
super(props, id);
}
public static create(props: UserProps, id?: EntityID): User {
const user = new User(
{
...props,
isDeleted: props.isDeleted ? props.isDeleted : false,
isEmailVerified: props.isEmailVerified ? props.isEmailVerified : false,
isAdminUser: props.isAdminUser ? props.isAdminUser : false,
},
id
);
const idWasProvided = !!id;
if (!idWasProvided) {
user.addDomainEvent(new UserCreatedEvent(user));
}
return user;
}
public delete(): void {
if (!this.props.isDeleted) {
// this.addDomainEvent(new UserDeleted(this)); TBI
this.props.isDeleted = true;
}
}
}