From 6d2b731379fb1e99c5d3918eefd375e44ce6c112 Mon Sep 17 00:00:00 2001 From: Rish Date: Thu, 18 Feb 2021 18:11:50 +0530 Subject: [PATCH] Fixed member status events query refs https://github.com/TryGhost/Ghost/issues/12602 - Adds aggregate option to permitted options - Cleans up SQL query to fetch delta values --- core/server/models/member-status-event.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/core/server/models/member-status-event.js b/core/server/models/member-status-event.js index c5c3bc88de..b0e04d8c4e 100644 --- a/core/server/models/member-status-event.js +++ b/core/server/models/member-status-event.js @@ -18,17 +18,17 @@ const MemberStatusEvent = ghostBookshelf.Model.extend({ .select(knex.raw('DATE(created_at) as date')) .select(knex.raw(`SUM( CASE WHEN to_status='paid' THEN 1 - CASE WHEN from_status='paid' THEN -1 + WHEN from_status='paid' THEN -1 ELSE 0 END ) as paid_delta`)) .select(knex.raw(`SUM( CASE WHEN to_status='comped' THEN 1 - CASE WHEN from_status='comped' THEN -1 + WHEN from_status='comped' THEN -1 ELSE 0 END ) as comped_delta`)) .select(knex.raw(`SUM( CASE WHEN to_status='free' THEN 1 - CASE WHEN from_status='free' THEN -1 + WHEN from_status='free' THEN -1 ELSE 0 END ) as free_delta`)) .groupByRaw('DATE(created_at)') @@ -36,6 +36,15 @@ const MemberStatusEvent = ghostBookshelf.Model.extend({ } } }, { + 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'); },