2021-10-07 17:42:56 +03:00
|
|
|
const {slugify} = require('@tryghost/string');
|
2021-10-08 13:23:40 +03:00
|
|
|
const ValueObject = require('./shared/ValueObject');
|
2021-10-07 17:42:56 +03:00
|
|
|
|
2021-10-08 13:31:11 +03:00
|
|
|
const InvalidOfferCode = require('../errors').InvalidOfferCode;
|
2021-10-07 17:42:56 +03:00
|
|
|
|
|
|
|
/** @extends ValueObject<string> */
|
|
|
|
class OfferCode extends ValueObject {
|
|
|
|
/** @param {unknown} code */
|
|
|
|
static create(code) {
|
|
|
|
if (!code || typeof code !== 'string') {
|
|
|
|
throw new InvalidOfferCode({
|
|
|
|
message: 'Offer `code` must be a string.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const slugged = slugify(code);
|
|
|
|
|
|
|
|
if (slugged.length > 191) {
|
|
|
|
throw new InvalidOfferCode({
|
|
|
|
message: 'Offer `code` can be a maximum of 191 characters.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new OfferCode(slugged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OfferCode;
|