ghost
Version:
The professional publishing platform
64 lines (52 loc) • 1.82 kB
JavaScript
module.exports = class MilestoneQueries {
/** @type {number} */
constructor(deps) {
this.
this.
}
/**
* @returns {Promise<number>}
*/
async getMembersCount() {
const [membersCount] = await this.
return membersCount?.count || 0;
}
/**
* @returns {Promise<Array>}
*/
async getARR() {
const currentARR = await this.
.select(this.
.groupBy('stripe.currency');
return currentARR;
}
/**
* @returns {Promise<boolean>}
*/
async hasImportedMembersInPeriod() {
const importedThreshold = new Date();
importedThreshold.setDate(importedThreshold.getDate() - this.
const [hasImportedMembers] = await this.
.count('id as count')
.where('source', '=', 'import')
.where('created_at', '>=', importedThreshold);
return hasImportedMembers?.count > 0;
}
/**
* @returns {Promise<string>}
*/
async getDefaultCurrency() {
const currentARR = await this.getARR();
// Set the default currency as the one with the highest value
if (currentARR.length > 1) {
const highestValues = currentARR.sort((a, b) => b.arr - a.arr);
return highestValues?.[0]?.currency;
} else if (currentARR?.[0]?.currency) {
return currentARR[0].currency;
} else {
return 'usd';
}
}
};