This repository has no description
0

Configure Feed

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

semble / src / modules / sync / domain / SyncStatus.ts
3.0 kB 112 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 'src/modules/user/domain/value-objects/DID'; 6import { SyncState } from './value-objects/SyncState'; 7 8export interface SyncStatusProps { 9 curatorId: DID; 10 syncState: SyncState; 11 lastSyncedAt?: Date; 12 lastSyncAttemptAt?: Date; 13 syncErrorMessage?: string; 14 recordsProcessed?: number; 15 createdAt: Date; 16 updatedAt: Date; 17} 18 19export class SyncStatus extends AggregateRoot<SyncStatusProps> { 20 get syncStatusId(): UniqueEntityID { 21 return this._id; 22 } 23 24 get curatorId(): DID { 25 return this.props.curatorId; 26 } 27 28 get syncState(): SyncState { 29 return this.props.syncState; 30 } 31 32 get lastSyncedAt(): Date | undefined { 33 return this.props.lastSyncedAt; 34 } 35 36 get lastSyncAttemptAt(): Date | undefined { 37 return this.props.lastSyncAttemptAt; 38 } 39 40 get syncErrorMessage(): string | undefined { 41 return this.props.syncErrorMessage; 42 } 43 44 get recordsProcessed(): number | undefined { 45 return this.props.recordsProcessed; 46 } 47 48 get createdAt(): Date { 49 return this.props.createdAt; 50 } 51 52 get updatedAt(): Date { 53 return this.props.updatedAt; 54 } 55 56 public markSyncInProgress(): void { 57 this.props.syncState = SyncState.inProgress(); 58 this.props.lastSyncAttemptAt = new Date(); 59 this.props.updatedAt = new Date(); 60 } 61 62 public markSyncCompleted(recordsProcessed: number): void { 63 this.props.syncState = SyncState.completed(); 64 this.props.lastSyncedAt = new Date(); 65 this.props.recordsProcessed = recordsProcessed; 66 this.props.syncErrorMessage = undefined; 67 this.props.updatedAt = new Date(); 68 } 69 70 public markSyncFailed(errorMessage: string): void { 71 this.props.syncState = SyncState.failed(); 72 this.props.syncErrorMessage = errorMessage; 73 this.props.updatedAt = new Date(); 74 } 75 76 private constructor(props: SyncStatusProps, id?: UniqueEntityID) { 77 super(props, id); 78 } 79 80 public static create( 81 props: SyncStatusProps, 82 id?: UniqueEntityID, 83 ): Result<SyncStatus> { 84 const guardArgs: IGuardArgument[] = [ 85 { argument: props.curatorId, argumentName: 'curatorId' }, 86 { argument: props.syncState, argumentName: 'syncState' }, 87 { argument: props.createdAt, argumentName: 'createdAt' }, 88 { argument: props.updatedAt, argumentName: 'updatedAt' }, 89 ]; 90 91 const guardResult = Guard.againstNullOrUndefinedBulk(guardArgs); 92 93 if (guardResult.isErr()) { 94 return err(new Error(guardResult.error)); 95 } 96 97 const syncStatus = new SyncStatus(props, id); 98 99 return ok(syncStatus); 100 } 101 102 public static createNew(curatorId: DID): Result<SyncStatus> { 103 const now = new Date(); 104 105 return SyncStatus.create({ 106 curatorId, 107 syncState: SyncState.notSynced(), 108 createdAt: now, 109 updatedAt: now, 110 }); 111 } 112}