🐛 Fixed incorrect member count on sidebar (#15330)

refshttps://github.com/TryGhost/Team/issues/1738

- Changed sidebar member count to use new API endpoint data.
- Added separate function for getting member count.
- changed `_fetchCountsTask` to use new `/stats/member_count/` endpoint
- updated @task to calculate total members from endpoint data.
This commit is contained in:
Ronald Langeveld 2022-09-06 12:08:16 +02:00 committed by GitHub
parent d0c45cbe9e
commit dac3fff90f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 4 deletions

View File

@ -110,12 +110,12 @@ export default class Main extends Component.extend(ShortcutsMixin) {
@task(function* () {
try {
this.set('memberCountLoading', true);
const stats = yield this.membersStats.fetchCounts();
const stats = yield this.membersStats.fetchMemberCount();
this.set('memberCountLoading', false);
if (stats) {
const statsDateObj = this.membersStats.fillCountDates(stats.data) || {};
const dateValues = Object.values(statsDateObj);
this.set('memberCount', dateValues.length ? dateValues[dateValues.length - 1].total : 0);
const {free, paid, comped} = stats.meta.totals;
const total = free + paid + comped || 0;
this.set('memberCount', total);
}
} catch (e) {
return false;

View File

@ -16,6 +16,7 @@ export default class MembersStatsService extends Service {
@tracked countStats = null;
@tracked mrrStats = null;
@tracked newsletterStats = null;
@tracked totalMemberCount = null;
fetch() {
let daysChanged = this._lastFetchedDays !== this.days;
@ -50,6 +51,22 @@ export default class MembersStatsService extends Service {
return this._fetchCountsTask.perform();
}
fetchMemberCount() {
let staleData = this._lastFetchedMemberCounts && (new Date() - this._lastFetchedMemberCounts) > ONE_MINUTE;
// return an already in-progress promise unless params have changed
if (this._fetchMemberCountsTask.isRunning) {
return this._fetchMemberCountsTask.last;
}
// return existing stats unless data is > 1 min old
if (this.totalMemberCount && !this._forceRefresh && !staleData) {
return Promise.resolve(this.totalMemberCount);
}
return this._fetchMemberCountsTask.perform();
}
fetchNewsletterStats() {
let staleData = this._lastFetchedNewsletterStats && (new Date() - this._lastFetchedNewsletterStats) > ONE_MINUTE;
@ -196,6 +213,16 @@ export default class MembersStatsService extends Service {
return stats;
}
@task
*_fetchMemberCountsTask() {
this._lastFetchedMemberCounts = new Date();
let statsUrl = this.ghostPaths.url.api('stats/member_count/');
let stats = yield this.ajax.request(statsUrl);
this.totalMemberCount = stats;
return stats;
}
@task
*_fetchMRRTask() {
this._lastFetchedMRR = new Date();