2021-10-05 17:24:47 +03:00
|
|
|
const DomainEvents = require('@tryghost/domain-events');
|
2021-10-08 13:21:27 +03:00
|
|
|
const OfferCodeChangeEvent = require('./domain/events/OfferCodeChange');
|
2021-10-07 17:42:56 +03:00
|
|
|
const Offer = require('./domain/models/Offer');
|
2021-10-05 12:15:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} OfferRepositoryOptions
|
|
|
|
* @prop {import('knex').Transaction} transacting
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {any} json
|
2021-10-07 18:34:10 +03:00
|
|
|
* @returns {Offer.OfferCreateProps}
|
2021-10-05 12:15:57 +03:00
|
|
|
*/
|
|
|
|
function toDomain(json) {
|
|
|
|
return {
|
|
|
|
id: json.id,
|
|
|
|
name: json.name,
|
|
|
|
code: json.code,
|
|
|
|
display_title: json.portal_title,
|
|
|
|
display_description: json.portal_description,
|
2021-10-07 18:29:56 +03:00
|
|
|
type: json.discount_type === 'amount' ? 'fixed' : 'percent',
|
2021-10-05 12:15:57 +03:00
|
|
|
amount: json.discount_amount,
|
|
|
|
cadence: json.interval,
|
|
|
|
currency: json.currency,
|
2021-10-07 18:10:44 +03:00
|
|
|
duration: json.duration,
|
2021-10-08 13:10:36 +03:00
|
|
|
duration_in_months: json.duration_in_months,
|
2021-10-06 16:13:08 +03:00
|
|
|
stripe_coupon_id: json.stripe_coupon_id,
|
2021-10-05 12:15:57 +03:00
|
|
|
tier: {
|
|
|
|
id: json.product.id,
|
|
|
|
name: json.product.name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class OfferRepository {
|
|
|
|
/**
|
|
|
|
* @param {{forge: (data: object) => import('bookshelf').Model<Offer.OfferProps>}} OfferModel
|
|
|
|
* @param {import('@tryghost/members-stripe-service')} stripeAPIService
|
|
|
|
*/
|
2021-10-06 15:58:55 +03:00
|
|
|
constructor(OfferModel, stripeAPIService) {
|
2021-10-05 12:15:57 +03:00
|
|
|
/** @private */
|
|
|
|
this.OfferModel = OfferModel;
|
|
|
|
/** @private */
|
|
|
|
this.stripeAPIService = stripeAPIService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {(t: import('knex').Transaction) => Promise<T>} cb
|
|
|
|
* @returns {Promise<T>}
|
|
|
|
*/
|
|
|
|
async createTransaction(cb) {
|
|
|
|
return this.OfferModel.transaction(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name
|
2021-10-07 17:42:56 +03:00
|
|
|
* @param {OfferRepositoryOptions} [options]
|
2021-10-05 12:15:57 +03:00
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
async existsByName(name, options) {
|
|
|
|
const model = await this.OfferModel.findOne({name}, options);
|
|
|
|
if (!model) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} code
|
2021-10-07 17:42:56 +03:00
|
|
|
* @param {OfferRepositoryOptions} [options]
|
2021-10-05 12:15:57 +03:00
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
|
|
|
async existsByCode(code, options) {
|
|
|
|
const model = await this.OfferModel.findOne({code}, options);
|
|
|
|
if (!model) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} id
|
2021-10-07 17:42:56 +03:00
|
|
|
* @param {OfferRepositoryOptions} [options]
|
2021-10-05 12:15:57 +03:00
|
|
|
* @returns {Promise<Offer>}
|
|
|
|
*/
|
|
|
|
async getById(id, options) {
|
|
|
|
const model = await this.OfferModel.findOne({id}, {
|
|
|
|
...options,
|
|
|
|
withRelated: ['product']
|
|
|
|
});
|
|
|
|
|
|
|
|
const json = model.toJSON();
|
|
|
|
|
|
|
|
return Offer.create(toDomain(json));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-07 17:42:56 +03:00
|
|
|
* @param {OfferRepositoryOptions} [options]
|
2021-10-05 12:15:57 +03:00
|
|
|
* @returns {Promise<Offer[]>}
|
|
|
|
*/
|
|
|
|
async getAll(options) {
|
|
|
|
const models = await this.OfferModel.findAll({
|
|
|
|
...options,
|
|
|
|
withRelated: ['product']
|
|
|
|
});
|
|
|
|
return Promise.all(models.toJSON().map(toDomain).map(Offer.create));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Offer} offer
|
2021-10-07 17:42:56 +03:00
|
|
|
* @param {OfferRepositoryOptions} [options]
|
2021-10-05 12:15:57 +03:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async save(offer, options) {
|
|
|
|
const model = this.OfferModel.forge({
|
|
|
|
id: offer.id,
|
2021-10-07 17:42:56 +03:00
|
|
|
name: offer.name.value,
|
|
|
|
code: offer.code.value,
|
|
|
|
portal_title: offer.displayTitle.value,
|
|
|
|
portal_description: offer.displayDescription.value,
|
2021-10-07 18:29:56 +03:00
|
|
|
discount_type: offer.type.value === 'fixed' ? 'amount' : 'percent',
|
2021-10-07 17:42:56 +03:00
|
|
|
discount_amount: offer.amount.value,
|
|
|
|
interval: offer.cadence.value,
|
2021-10-05 12:15:57 +03:00
|
|
|
product_id: offer.tier.id,
|
2021-10-08 13:10:36 +03:00
|
|
|
duration: offer.duration.value.type,
|
|
|
|
duration_in_months: offer.duration.value.type === 'repeating' ? offer.duration.value.months : null,
|
2021-10-07 19:13:17 +03:00
|
|
|
currency: offer.currency ? offer.currency.value : null
|
2021-10-05 12:15:57 +03:00
|
|
|
});
|
|
|
|
|
2021-10-05 14:14:20 +03:00
|
|
|
if (offer.codeChanged || offer.isNew) {
|
2021-10-05 17:24:47 +03:00
|
|
|
const event = OfferCodeChangeEvent.create({
|
|
|
|
offerId: offer.id,
|
|
|
|
previousCodes: offer.isNew ? null : offer.oldCodes,
|
|
|
|
currentCode: offer.code
|
2021-10-05 12:15:57 +03:00
|
|
|
});
|
2021-10-05 17:24:47 +03:00
|
|
|
DomainEvents.dispatch(event);
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (offer.isNew) {
|
|
|
|
/** @type {import('stripe').Stripe.CouponCreateParams} */
|
|
|
|
const coupon = {
|
2021-10-07 17:42:56 +03:00
|
|
|
name: offer.name.value,
|
2021-10-08 13:10:36 +03:00
|
|
|
duration: offer.duration.value.type
|
2021-10-05 12:15:57 +03:00
|
|
|
};
|
|
|
|
|
2021-10-08 13:10:36 +03:00
|
|
|
if (offer.duration.value.type === 'repeating') {
|
|
|
|
coupon.duration_in_months = offer.duration.value.months;
|
|
|
|
}
|
|
|
|
|
2021-10-07 17:42:56 +03:00
|
|
|
if (offer.type.value === 'percent') {
|
|
|
|
coupon.percent_off = offer.amount.value;
|
2021-10-07 18:33:26 +03:00
|
|
|
} else {
|
|
|
|
coupon.amount_off = offer.amount.value;
|
|
|
|
coupon.currency = offer.currency.value;
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const couponData = await this.stripeAPIService.createCoupon(coupon);
|
|
|
|
model.set('stripe_coupon_id', couponData.id);
|
|
|
|
await model.save(null, {method: 'insert', ...options});
|
|
|
|
} else {
|
|
|
|
await model.save(null, {method: 'update', ...options});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OfferRepository;
|