30 lines
780 B
TypeScript
30 lines
780 B
TypeScript
import { NullOr } from "../../../../utilities";
|
|
import { MoneyValue } from "./MoneyValue";
|
|
import { Result } from "./Result";
|
|
|
|
export interface IUnitPriceProps {
|
|
amount: NullOr<number | string>;
|
|
currencyCode?: string;
|
|
scale: number;
|
|
}
|
|
|
|
export const DEFAULT_UNIT_PRICE_SCALE = 2;
|
|
|
|
export class UnitPrice extends MoneyValue {
|
|
public static create(props: IUnitPriceProps) {
|
|
const { amount, currencyCode, scale = DEFAULT_UNIT_PRICE_SCALE } = props;
|
|
const _unitPriceOrError = MoneyValue.create({
|
|
amount,
|
|
currencyCode,
|
|
scale,
|
|
});
|
|
if (_unitPriceOrError.isFailure) {
|
|
return _unitPriceOrError;
|
|
}
|
|
|
|
const _unitPrice = _unitPriceOrError.object.convertScale(DEFAULT_UNIT_PRICE_SCALE);
|
|
|
|
return Result.ok<UnitPrice>(_unitPrice);
|
|
}
|
|
}
|