mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 03:44:29 +03:00
Added MemberPaidSubscriptionEvent model
refs https://github.com/TryGhost/Ghost/issues/12602 - Adds the aggregateMRRDeltas option to the `findAll` method, allowing us to calculate MRR over time
This commit is contained in:
parent
a68d037cef
commit
7cf0252392
@ -29,6 +29,7 @@ const models = [
|
||||
'mobiledoc-revision',
|
||||
'member',
|
||||
'member-subscribe-event',
|
||||
'member-paid-subscription-event',
|
||||
'member-login-event',
|
||||
'member-email-change-event',
|
||||
'member-payment-event',
|
||||
|
46
core/server/models/member-paid-subscription-event.js
Normal file
46
core/server/models/member-paid-subscription-event.js
Normal file
@ -0,0 +1,46 @@
|
||||
const errors = require('@tryghost/errors');
|
||||
const ghostBookshelf = require('./base');
|
||||
|
||||
const MemberPaidSubscriptionEvent = ghostBookshelf.Model.extend({
|
||||
tableName: 'members_paid_subscription_events',
|
||||
customQuery(qb, options) {
|
||||
if (options.aggregateMRRDeltas) {
|
||||
if (options.limit || options.filter) {
|
||||
throw new errors.IncorrectUsageError('aggregateMRRDeltas does not work when passed a filter or limit');
|
||||
}
|
||||
const knex = ghostBookshelf.knex;
|
||||
return qb.clear('select')
|
||||
.select(knex.raw('DATE(created_at) as date'))
|
||||
.select(knex.raw('SUM(mrr_delta) as mrr_delta'))
|
||||
.select('currency')
|
||||
.groupByRaw('currency, DATE(created_at)')
|
||||
.orderByRaw('DATE(created_at)');
|
||||
}
|
||||
}
|
||||
}, {
|
||||
permittedOptions(methodName) {
|
||||
const options = ghostBookshelf.Model.permittedOptions.call(this, methodName);
|
||||
|
||||
if (methodName === 'findAll') {
|
||||
return options.concat('aggregateMRRDeltas');
|
||||
}
|
||||
|
||||
return options;
|
||||
},
|
||||
async edit() {
|
||||
throw new errors.IncorrectUsageError('Cannot edit MemberPaidSubscriptionEvent');
|
||||
},
|
||||
|
||||
async destroy() {
|
||||
throw new errors.IncorrectUsageError('Cannot destroy MemberPaidSubscriptionEvent');
|
||||
}
|
||||
});
|
||||
|
||||
const MemberPaidSubscriptionEvents = ghostBookshelf.Collection.extend({
|
||||
model: MemberPaidSubscriptionEvent
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
MemberPaidSubscriptionEvent: ghostBookshelf.model('MemberPaidSubscriptionEvent', MemberPaidSubscriptionEvent),
|
||||
MemberPaidSubscriptionEvents: ghostBookshelf.collection('MemberPaidSubscriptionEvents', MemberPaidSubscriptionEvents)
|
||||
};
|
@ -170,6 +170,7 @@ function createApiInstance(config) {
|
||||
StripeCustomerSubscription: models.StripeCustomerSubscription,
|
||||
Member: models.Member,
|
||||
MemberSubscribeEvent: models.MemberSubscribeEvent,
|
||||
MemberPaidSubscriptionEvent: models.MemberPaidSubscriptionEvent,
|
||||
MemberLoginEvent: models.MemberLoginEvent,
|
||||
MemberEmailChangeEvent: models.MemberEmailChangeEvent,
|
||||
MemberPaymentEvent: models.MemberPaymentEvent,
|
||||
|
Loading…
Reference in New Issue
Block a user