2021-02-12 16:13:56 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
|
|
|
|
const MemberPaymentEvent = ghostBookshelf.Model.extend({
|
|
|
|
tableName: 'members_payment_events',
|
2021-02-16 19:14:20 +03:00
|
|
|
|
|
|
|
member() {
|
|
|
|
return this.belongsTo('Member', 'member_id', 'id');
|
|
|
|
},
|
|
|
|
|
2021-02-12 16:13:56 +03:00
|
|
|
customQuery(qb, options) {
|
|
|
|
if (options.aggregatePaymentVolume) {
|
|
|
|
if (options.limit || options.filter) {
|
2021-12-01 14:22:14 +03:00
|
|
|
throw new errors.IncorrectUsageError({message: 'aggregatePaymentVolume does not work when passed a filter or limit'});
|
2021-02-12 16:13:56 +03:00
|
|
|
}
|
|
|
|
const knex = ghostBookshelf.knex;
|
|
|
|
return qb.clear('select')
|
|
|
|
.select(knex.raw('DATE(created_at) as date'))
|
2021-02-15 21:48:17 +03:00
|
|
|
.select(knex.raw('SUM(amount) as volume_delta'))
|
2021-02-12 16:13:56 +03:00
|
|
|
.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() {
|
2021-12-01 14:22:14 +03:00
|
|
|
throw new errors.IncorrectUsageError({message: 'Cannot edit MemberPaymentEvent'});
|
2021-02-12 16:13:56 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
async destroy() {
|
2021-12-01 14:22:14 +03:00
|
|
|
throw new errors.IncorrectUsageError({message: 'Cannot destroy MemberPaymentEvent'});
|
2021-02-12 16:13:56 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const MemberPaymentEvents = ghostBookshelf.Collection.extend({
|
|
|
|
model: MemberPaymentEvent
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
MemberPaymentEvent: ghostBookshelf.model('MemberPaymentEvent', MemberPaymentEvent),
|
|
|
|
MemberPaymentEvents: ghostBookshelf.collection('MemberPaymentEvents', MemberPaymentEvents)
|
|
|
|
};
|
|
|
|
|