diff --git a/core/server/models/index.js b/core/server/models/index.js index df0ed58fde..0b66e5ff62 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-payment-event', 'member-status-event', 'posts-meta', 'member-stripe-customer', diff --git a/core/server/models/member-payment-event.js b/core/server/models/member-payment-event.js new file mode 100644 index 0000000000..5e56af0919 --- /dev/null +++ b/core/server/models/member-payment-event.js @@ -0,0 +1,47 @@ +const errors = require('@tryghost/errors'); +const ghostBookshelf = require('./base'); + +const MemberPaymentEvent = ghostBookshelf.Model.extend({ + tableName: 'members_payment_events', + customQuery(qb, options) { + if (options.aggregatePaymentVolume) { + if (options.limit || options.filter) { + throw new errors.IncorrectUsageError('aggregatePaymentVolume 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(amount) as gross_volume')) + .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('aggregatePaymentVolume'); + } + + return options; + }, + async edit() { + throw new errors.IncorrectUsageError('Cannot edit MemberPaymentEvent'); + }, + + async destroy() { + throw new errors.IncorrectUsageError('Cannot destroy MemberPaymentEvent'); + } +}); + +const MemberPaymentEvents = ghostBookshelf.Collection.extend({ + model: MemberPaymentEvent +}); + +module.exports = { + MemberPaymentEvent: ghostBookshelf.model('MemberPaymentEvent', MemberPaymentEvent), + MemberPaymentEvents: ghostBookshelf.collection('MemberPaymentEvents', MemberPaymentEvents) +}; + diff --git a/core/server/services/members/api.js b/core/server/services/members/api.js index 333016a8ed..4bec7e8d3b 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, + MemberPaymentEvent: models.MemberPaymentEvent, MemberStatusEvent: models.MemberStatusEvent }, logger: logging