2021-10-07 17:42:56 +03:00
|
|
|
const ValueObject = require('../../shared/ValueObject');
|
|
|
|
const InvalidOfferAmount = require('../../errors').InvalidOfferAmount;
|
|
|
|
|
|
|
|
/** @extends ValueObject<number> */
|
|
|
|
class OfferAmount extends ValueObject {}
|
|
|
|
|
|
|
|
class OfferPercentageAmount extends OfferAmount {
|
|
|
|
/** @param {unknown} amount */
|
|
|
|
static create(amount) {
|
|
|
|
if (typeof amount !== 'number') {
|
|
|
|
throw new InvalidOfferAmount({
|
|
|
|
message: 'Offer `amount` must be an integer between 0 and 100.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!Number.isInteger(amount)) {
|
|
|
|
throw new InvalidOfferAmount({
|
|
|
|
message: 'Offer `amount` must be an integer between 0 and 100.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (amount < 0 || amount > 100) {
|
|
|
|
throw new InvalidOfferAmount({
|
|
|
|
message: 'Offer `amount` must be an integer between 0 and 100.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return new OfferPercentageAmount(amount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 18:29:56 +03:00
|
|
|
class OfferFixedAmount extends OfferAmount {
|
2021-10-07 17:42:56 +03:00
|
|
|
/** @param {unknown} amount */
|
|
|
|
static create(amount) {
|
|
|
|
if (typeof amount !== 'number') {
|
|
|
|
throw new InvalidOfferAmount({
|
|
|
|
message: 'Offer `amount` must be a number greater than 0.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (amount < 0) {
|
|
|
|
throw new InvalidOfferAmount({
|
|
|
|
message: 'Offer `amount` must be a number greater than 0.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const withTwoDecimalPlaces = +amount.toFixed(2);
|
|
|
|
return new OfferPercentageAmount(withTwoDecimalPlaces);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OfferAmount;
|
|
|
|
module.exports.OfferPercentageAmount = OfferPercentageAmount;
|
2021-10-07 18:29:56 +03:00
|
|
|
module.exports.OfferFixedAmount = OfferFixedAmount;
|