2021-10-08 13:23:40 +03:00
|
|
|
const ValueObject = require('./shared/ValueObject');
|
2021-10-08 13:31:11 +03:00
|
|
|
const InvalidOfferCurrency = require('../errors').InvalidOfferCurrency;
|
2021-10-07 18:33:26 +03:00
|
|
|
|
|
|
|
/** @extends ValueObject<string> */
|
|
|
|
class OfferCurrency extends ValueObject {
|
|
|
|
/** @param {unknown} currency */
|
|
|
|
static create(currency) {
|
|
|
|
if (typeof currency !== 'string') {
|
|
|
|
throw new InvalidOfferCurrency({
|
|
|
|
message: 'Offer `currency` must be a string.'
|
|
|
|
});
|
|
|
|
}
|
2021-11-04 20:52:20 +03:00
|
|
|
// Check currency is a 3 character string consisting of only letters (case insensitive)
|
|
|
|
if (!currency.match(/^[A-Z]{3}$/i)) {
|
|
|
|
throw new InvalidOfferCurrency({
|
|
|
|
message: 'Offer `currency` must be an ISO currency code.'
|
|
|
|
});
|
|
|
|
}
|
2021-10-07 18:33:26 +03:00
|
|
|
// TODO: Validate it is a country code we support?
|
2021-11-04 20:52:20 +03:00
|
|
|
return new OfferCurrency(currency.toUpperCase());
|
2021-10-07 18:33:26 +03:00
|
|
|
}
|
2021-11-04 20:52:20 +03:00
|
|
|
|
|
|
|
static InvalidOfferCurrency = InvalidOfferCurrency;
|
2021-10-07 18:33:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OfferCurrency;
|