From 7cf0252392a7cb40400a6cb895a59f2cb41fb340 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Fri, 12 Feb 2021 13:15:47 +0000 Subject: [PATCH] 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 --- core/server/models/index.js | 1 + .../models/member-paid-subscription-event.js | 46 +++++++++++++++++++ core/server/services/members/api.js | 1 + 3 files changed, 48 insertions(+) create mode 100644 core/server/models/member-paid-subscription-event.js diff --git a/core/server/models/index.js b/core/server/models/index.js index 6c22bcdc2b..dd2f4809af 100644 --- a/core/server/models/index.js +++ b/core/server/models/index.js @@ -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', diff --git a/core/server/models/member-paid-subscription-event.js b/core/server/models/member-paid-subscription-event.js new file mode 100644 index 0000000000..072b6a45b6 --- /dev/null +++ b/core/server/models/member-paid-subscription-event.js @@ -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) +}; diff --git a/core/server/services/members/api.js b/core/server/services/members/api.js index 8929daa50c..9b8071094e 100644 --- a/core/server/services/members/api.js +++ b/core/server/services/members/api.js @@ -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,