wip
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
* Represents a monetary amount with currency and platform fee calculation
|
||||
*/
|
||||
|
||||
import { RacingDomainValidationError } from '../errors/RacingDomainError';
|
||||
|
||||
export type Currency = 'USD' | 'EUR' | 'GBP';
|
||||
|
||||
export class Money {
|
||||
@@ -18,10 +20,10 @@ export class Money {
|
||||
|
||||
static create(amount: number, currency: Currency = 'USD'): Money {
|
||||
if (amount < 0) {
|
||||
throw new Error('Money amount cannot be negative');
|
||||
throw new RacingDomainValidationError('Money amount cannot be negative');
|
||||
}
|
||||
if (!Number.isFinite(amount)) {
|
||||
throw new Error('Money amount must be a finite number');
|
||||
throw new RacingDomainValidationError('Money amount must be a finite number');
|
||||
}
|
||||
return new Money(amount, currency);
|
||||
}
|
||||
@@ -47,7 +49,7 @@ export class Money {
|
||||
*/
|
||||
add(other: Money): Money {
|
||||
if (this.currency !== other.currency) {
|
||||
throw new Error('Cannot add money with different currencies');
|
||||
throw new RacingDomainValidationError('Cannot add money with different currencies');
|
||||
}
|
||||
return new Money(this.amount + other.amount, this.currency);
|
||||
}
|
||||
@@ -57,11 +59,11 @@ export class Money {
|
||||
*/
|
||||
subtract(other: Money): Money {
|
||||
if (this.currency !== other.currency) {
|
||||
throw new Error('Cannot subtract money with different currencies');
|
||||
throw new RacingDomainValidationError('Cannot subtract money with different currencies');
|
||||
}
|
||||
const result = this.amount - other.amount;
|
||||
if (result < 0) {
|
||||
throw new Error('Subtraction would result in negative amount');
|
||||
throw new RacingDomainValidationError('Subtraction would result in negative amount');
|
||||
}
|
||||
return new Money(result, this.currency);
|
||||
}
|
||||
@@ -71,7 +73,7 @@ export class Money {
|
||||
*/
|
||||
isGreaterThan(other: Money): boolean {
|
||||
if (this.currency !== other.currency) {
|
||||
throw new Error('Cannot compare money with different currencies');
|
||||
throw new RacingDomainValidationError('Cannot compare money with different currencies');
|
||||
}
|
||||
return this.amount > other.amount;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user