import { BaseModel } from '@verb/src/libs/shared/base/base.model'; export class StreamTransitionStatusModel implements BaseModel { public constructor(streamTransitionStatusSchema?: StreamTransitionStatusSchema) { if (streamTransitionStatusSchema) { this.fromSchema(streamTransitionStatusSchema); } } public get endedAt() { return this._endedAt; } public set endedAt(_endedAt: Date | null) { this._endedAt = _endedAt; } public get error() { return this._error; } public set error(_error: string | null) { this.finish(this._stepDescription); this._error = _error; } public get percentComplete() { return +(Math.min(this._stepNumber / Math.max(this._stepCount, 1), 100) * 100).toFixed(2); } public get startedAt() { return this._startedAt; } public set startedAt(_startedAt: Date | null) { this._startedAt = _startedAt; } public get stepCount() { return this._stepCount; } public set stepCount(_stepCount) { this._stepCount = _stepCount; } public get stepNumber() { return this._stepNumber; } public set stepNumber(_stepNumber: number) { this._stepNumber = _stepNumber; } public start(stepDescription: string, stepCount: number) { this._endedAt = null; this._error = null; this._stepCount = stepCount; this._stepDescription = stepDescription; this._stepNumber = 1; this._startedAt = new Date(); } public describe(stepDescription: string | null) { this._stepDescription = stepDescription; } public finish(stepDescription: string | null) { this._error = null; this._stepDescription = stepDescription; this._stepNumber = this._stepCount; this._endedAt = new Date(); } public fromSchema(streamTransitionStatusSchema: StreamTransitionStatusSchema) { this._endedAt = streamTransitionStatusSchema.ended_at ? new Date(streamTransitionStatusSchema.ended_at) : null; this._error = streamTransitionStatusSchema.error; this._startedAt = streamTransitionStatusSchema.started_at ? new Date(streamTransitionStatusSchema.started_at) : null; this._stepCount = streamTransitionStatusSchema.step_count; this._stepDescription = streamTransitionStatusSchema.step_description; this._stepNumber = streamTransitionStatusSchema.step_number; } public increment(stepDescription: string) { this.describe(stepDescription); this._stepNumber = Math.min(this._stepNumber + 1, this._stepCount); } public toSchema(): StreamTransitionStatusSchema { return { ended_at: this._endedAt ? this._endedAt.toUTCString() : null, error: this._error, percent_complete: this.percentComplete, started_at: this._startedAt ? this._startedAt.toUTCString() : null, step_count: this._stepCount, step_description: this._stepDescription, step_number: this._stepNumber, }; } private _error: string | null = null; private _endedAt: Date | null = null; private _startedAt: Date | null = null; private _stepCount = 0; private _stepDescription: string | null = null; private _stepNumber = 0; } export interface StreamTransitionStatusSchema { ended_at: string | null; error: string | null; percent_complete: number; started_at: string | null; step_count: number; step_description: string | null; step_number: number; }