A social knowledge tool for researchers built on ATProto
1import { AggregateRoot } from 'src/shared/domain/AggregateRoot';
2import { UniqueEntityID } from 'src/shared/domain/UniqueEntityID';
3import { Guard, IGuardArgument } from 'src/shared/core/Guard';
4import { err, ok, Result } from 'src/shared/core/Result';
5import { DID } from './value-objects/DID';
6import { Handle } from './value-objects/Handle';
7
8export interface UserProps {
9 did: DID;
10 handle?: Handle;
11 linkedAt: Date;
12 lastLoginAt: Date;
13}
14
15export class User extends AggregateRoot<UserProps> {
16 get userId(): UniqueEntityID {
17 return this._id;
18 }
19
20 get did(): DID {
21 return this.props.did;
22 }
23
24 get handle(): Handle | undefined {
25 return this.props.handle;
26 }
27
28 get linkedAt(): Date {
29 return this.props.linkedAt;
30 }
31
32 get lastLoginAt(): Date {
33 return this.props.lastLoginAt;
34 }
35
36 public updateHandle(handle: Handle): void {
37 this.props.handle = handle;
38 }
39
40 public recordLogin(): void {
41 this.props.lastLoginAt = new Date();
42 }
43
44 private constructor(props: UserProps, id?: UniqueEntityID) {
45 super(props, id);
46 }
47
48 public static create(props: UserProps, id?: UniqueEntityID): Result<User> {
49 const guardArgs: IGuardArgument[] = [
50 { argument: props.did, argumentName: 'did' },
51 { argument: props.linkedAt, argumentName: 'linkedAt' },
52 { argument: props.lastLoginAt, argumentName: 'lastLoginAt' },
53 ];
54
55 const guardResult = Guard.againstNullOrUndefinedBulk(guardArgs);
56
57 if (guardResult.isErr()) {
58 return err(new Error(guardResult.error));
59 }
60
61 const user = new User(props, id);
62
63 return ok(user);
64 }
65
66 public static createNew(did: DID, handle?: Handle): Result<User> {
67 const now = new Date();
68
69 return User.create({
70 did,
71 handle,
72 linkedAt: now,
73 lastLoginAt: now,
74 });
75 }
76}