0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00
ghost/core/server/models/member-status-event.js
Fabien O'Carroll b5c1b9e4fa Added customQuery for MemberStatusEvent
refs https://github.com/TryGhost/Ghost/issues/12602

This allows us to build up a count of member statuses over time
2021-02-12 16:38:57 +00:00

38 lines
1.5 KiB
JavaScript

const errors = require('@tryghost/errors');
const ghostBookshelf = require('./base');
const MemberStatusEvent = ghostBookshelf.Model.extend({
tableName: 'members_status_events',
customQuery(qb, options) {
if (options.aggregateStatusCounts) {
if (options.limit || options.filter) {
throw new errors.IncorrectUsageError('aggregateStatusCounts 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(CASE WHEN status='paid' THEN 1 ELSE 0 END) as paid_delta`))
.select(knex.raw(`SUM(CASE WHEN status='comped' THEN 1 ELSE 0 END) as comped_delta`))
.select(knex.raw(`SUM(CASE WHEN status='free' THEN 1 ELSE 0 END) as free_delta`))
.groupByRaw('DATE(created_at)')
.orderByRaw('DATE(created_at)');
}
}
}, {
async edit() {
throw new errors.IncorrectUsageError('Cannot edit MemberStatusEvent');
},
async destroy() {
throw new errors.IncorrectUsageError('Cannot destroy MemberStatusEvent');
}
});
const MemberStatusEvents = ghostBookshelf.Collection.extend({
model: MemberStatusEvent
});
module.exports = {
MemberStatusEvent: ghostBookshelf.model('MemberStatusEvent', MemberStatusEvent),
MemberStatusEvents: ghostBookshelf.collection('MemberStatusEvents', MemberStatusEvents)
};