2021-02-12 16:13:16 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
|
|
|
|
const MemberStatusEvent = ghostBookshelf.Model.extend({
|
2021-02-12 17:55:27 +03:00
|
|
|
tableName: 'members_status_events',
|
2021-02-16 19:14:20 +03:00
|
|
|
|
|
|
|
member() {
|
|
|
|
return this.belongsTo('Member', 'member_id', 'id');
|
|
|
|
},
|
|
|
|
|
2021-02-12 17:55:27 +03:00
|
|
|
customQuery(qb, options) {
|
|
|
|
if (options.aggregateStatusCounts) {
|
|
|
|
if (options.limit || options.filter) {
|
2021-12-01 14:22:14 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 'aggregateStatusCounts does not work when passed a filter or limit'
|
|
|
|
});
|
2021-02-12 17:55:27 +03:00
|
|
|
}
|
|
|
|
const knex = ghostBookshelf.knex;
|
|
|
|
return qb.clear('select')
|
|
|
|
.select(knex.raw('DATE(created_at) as date'))
|
2021-02-16 13:38:36 +03:00
|
|
|
.select(knex.raw(`SUM(
|
2021-07-15 17:17:51 +03:00
|
|
|
CASE WHEN to_status='paid' THEN 1
|
|
|
|
WHEN from_status='paid' THEN -1
|
|
|
|
ELSE 0 END
|
2021-02-16 13:38:36 +03:00
|
|
|
) as paid_delta`))
|
2021-07-15 17:17:51 +03:00
|
|
|
.select(knex.raw(`SUM(
|
|
|
|
CASE WHEN to_status='comped' THEN 1
|
|
|
|
WHEN from_status='comped' THEN -1
|
|
|
|
ELSE 0 END
|
|
|
|
) as comped_delta`))
|
2021-02-16 13:38:36 +03:00
|
|
|
.select(knex.raw(`SUM(
|
|
|
|
CASE WHEN to_status='free' THEN 1
|
2021-02-18 15:41:50 +03:00
|
|
|
WHEN from_status='free' THEN -1
|
2021-02-16 13:38:36 +03:00
|
|
|
ELSE 0 END
|
|
|
|
) as free_delta`))
|
2021-02-12 17:55:27 +03:00
|
|
|
.groupByRaw('DATE(created_at)')
|
|
|
|
.orderByRaw('DATE(created_at)');
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 16:13:16 +03:00
|
|
|
}, {
|
2021-02-18 15:41:50 +03:00
|
|
|
permittedOptions(methodName) {
|
|
|
|
const options = ghostBookshelf.Model.permittedOptions.call(this, methodName);
|
|
|
|
|
|
|
|
if (methodName === 'findAll') {
|
|
|
|
return options.concat('aggregateStatusCounts');
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
},
|
2021-02-12 16:13:16 +03:00
|
|
|
async edit() {
|
2021-12-01 14:22:14 +03:00
|
|
|
throw new errors.IncorrectUsageError({message: 'Cannot edit MemberStatusEvent'});
|
2021-02-12 16:13:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
async destroy() {
|
2021-12-01 14:22:14 +03:00
|
|
|
throw new errors.IncorrectUsageError({message: 'Cannot destroy MemberStatusEvent'});
|
2021-02-12 16:13:16 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const MemberStatusEvents = ghostBookshelf.Collection.extend({
|
|
|
|
model: MemberStatusEvent
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
MemberStatusEvent: ghostBookshelf.model('MemberStatusEvent', MemberStatusEvent),
|
|
|
|
MemberStatusEvents: ghostBookshelf.collection('MemberStatusEvents', MemberStatusEvents)
|
|
|
|
};
|