This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

semble / src / modules / user / domain / Follow.ts
1.9 kB 76 lines
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 { FollowTargetType } from './value-objects/FollowTargetType'; 7 8export interface FollowProps { 9 followerId: DID; 10 targetId: string; 11 targetType: FollowTargetType; 12 createdAt: Date; 13} 14 15export class Follow extends AggregateRoot<FollowProps> { 16 get followId(): UniqueEntityID { 17 return this._id; 18 } 19 20 get followerId(): DID { 21 return this.props.followerId; 22 } 23 24 get targetId(): string { 25 return this.props.targetId; 26 } 27 28 get targetType(): FollowTargetType { 29 return this.props.targetType; 30 } 31 32 get createdAt(): Date { 33 return this.props.createdAt; 34 } 35 36 private constructor(props: FollowProps, id?: UniqueEntityID) { 37 super(props, id); 38 } 39 40 public static create( 41 props: FollowProps, 42 id?: UniqueEntityID, 43 ): Result<Follow> { 44 const guardArgs: IGuardArgument[] = [ 45 { argument: props.followerId, argumentName: 'followerId' }, 46 { argument: props.targetId, argumentName: 'targetId' }, 47 { argument: props.targetType, argumentName: 'targetType' }, 48 { argument: props.createdAt, argumentName: 'createdAt' }, 49 ]; 50 51 const guardResult = Guard.againstNullOrUndefinedBulk(guardArgs); 52 53 if (guardResult.isErr()) { 54 return err(new Error(guardResult.error)); 55 } 56 57 const follow = new Follow(props, id); 58 59 return ok(follow); 60 } 61 62 public static createNew( 63 followerId: DID, 64 targetId: string, 65 targetType: FollowTargetType, 66 ): Result<Follow> { 67 const now = new Date(); 68 69 return Follow.create({ 70 followerId, 71 targetId, 72 targetType, 73 createdAt: now, 74 }); 75 } 76}