Ghost/core/server/models/member-status-event.js
Fabien O'Carroll 5880edd722 Replaced members 'comped' status with 'paid'
refs https://github.com/TryGhost/Team/issues/693

Since we've got rid of the concept of Complimentary with the Custom
Prices work, we're removing the 'comped' status from members. This
involves a migration for existing members, a schema update for the
validation, and a bump to members-api to no longer use the 'comped'
status for new members.

We also update the aggregation of the MemberStatusEvent to consider the
'comped' status as 'paid', and that there are 0 'comped' status events
in the database.

We can consider a migration for this data in the future, either adding
new status events moving from 'comped' to 'paid', or by modifying
existing status events. However both of these are very difficulty to
write a down migration for, and might be best saved for a major version.

- @tryghost/members-api@1.7.0 is the version that includes the required
  changes, however we have already bumped to 1.8.0 in Ghost
2021-05-19 18:49:18 +01:00

62 lines
2.1 KiB
JavaScript

const errors = require('@tryghost/errors');
const ghostBookshelf = require('./base');
const MemberStatusEvent = ghostBookshelf.Model.extend({
tableName: 'members_status_events',
member() {
return this.belongsTo('Member', 'member_id', 'id');
},
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 to_status IN ('paid', 'comped') THEN 1
WHEN from_status IN ('paid', 'comped') THEN -1
ELSE 0
END
) as paid_delta`))
.select(knex.raw(`0 as comped_delta`))
.select(knex.raw(`SUM(
CASE WHEN to_status='free' THEN 1
WHEN from_status='free' THEN -1
ELSE 0 END
) as free_delta`))
.groupByRaw('DATE(created_at)')
.orderByRaw('DATE(created_at)');
}
}
}, {
permittedOptions(methodName) {
const options = ghostBookshelf.Model.permittedOptions.call(this, methodName);
if (methodName === 'findAll') {
return options.concat('aggregateStatusCounts');
}
return options;
},
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)
};