Presupuestador_web/shared/lib/contexts/common/domain/entities/UnitPrice.ts

30 lines
780 B
TypeScript
Raw Normal View History

2024-07-09 16:21:21 +00:00
import { NullOr } from "../../../../utilities";
import { MoneyValue } from "./MoneyValue";
import { Result } from "./Result";
2024-04-23 15:29:38 +00:00
export interface IUnitPriceProps {
amount: NullOr<number | string>;
currencyCode?: string;
2024-07-15 15:41:42 +00:00
scale: number;
2024-04-23 15:29:38 +00:00
}
2024-10-25 09:49:10 +00:00
export const DEFAULT_UNIT_PRICE_SCALE = 2;
2024-04-23 15:29:38 +00:00
export class UnitPrice extends MoneyValue {
public static create(props: IUnitPriceProps) {
2024-10-25 09:49:10 +00:00
const { amount, currencyCode, scale = DEFAULT_UNIT_PRICE_SCALE } = props;
2024-04-23 15:29:38 +00:00
const _unitPriceOrError = MoneyValue.create({
amount,
currencyCode,
2024-07-15 15:41:42 +00:00
scale,
2024-04-23 15:29:38 +00:00
});
if (_unitPriceOrError.isFailure) {
return _unitPriceOrError;
}
2024-10-25 09:49:10 +00:00
const _unitPrice = _unitPriceOrError.object.convertScale(DEFAULT_UNIT_PRICE_SCALE);
2024-04-23 15:29:38 +00:00
return Result.ok<UnitPrice>(_unitPrice);
}
}