56 lines
1.2 KiB
TypeScript
56 lines
1.2 KiB
TypeScript
import type { IValueObject } from '@core/shared/domain';
|
|
import { IdentityDomainValidationError } from '../errors/IdentityDomainError';
|
|
|
|
export interface RatingDeltaProps {
|
|
value: number;
|
|
}
|
|
|
|
export class RatingDelta implements IValueObject<RatingDeltaProps> {
|
|
readonly value: number;
|
|
|
|
private constructor(value: number) {
|
|
this.value = value;
|
|
}
|
|
|
|
static create(value: number): RatingDelta {
|
|
if (typeof value !== 'number' || isNaN(value)) {
|
|
throw new IdentityDomainValidationError('Rating delta must be a valid number');
|
|
}
|
|
|
|
if (value < -500 || value > 500) {
|
|
throw new IdentityDomainValidationError(
|
|
`Rating delta must be between -500 and 500, got: ${value}`
|
|
);
|
|
}
|
|
|
|
return new RatingDelta(value);
|
|
}
|
|
|
|
get props(): RatingDeltaProps {
|
|
return { value: this.value };
|
|
}
|
|
|
|
equals(other: IValueObject<RatingDeltaProps>): boolean {
|
|
return this.value === other.props.value;
|
|
}
|
|
|
|
toNumber(): number {
|
|
return this.value;
|
|
}
|
|
|
|
toString(): string {
|
|
return this.value.toString();
|
|
}
|
|
|
|
isPositive(): boolean {
|
|
return this.value > 0;
|
|
}
|
|
|
|
isNegative(): boolean {
|
|
return this.value < 0;
|
|
}
|
|
|
|
isZero(): boolean {
|
|
return this.value === 0;
|
|
}
|
|
} |