Ghost/core/server/models/member-payment-event.js
Fabien O'Carroll a8ee271336 Added MemberPaymentEvent model
refs https://github.com/TryGhost/Ghost/issues/12602

- Adds the aggregatePaymentVolume option to the `findAll` method,
allowing us to calculate volume over time
2021-02-12 13:55:14 +00:00

48 lines
1.6 KiB
JavaScript

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)
};