chore: approve invoice, reports for admin dashboard

This commit is contained in:
Matin
2025-02-22 16:04:26 +03:30
parent e711ffd3ee
commit 3b0120d338
12 changed files with 448 additions and 8 deletions
@@ -284,4 +284,140 @@ export class SubscriptionsService {
await queryRunner.release();
}
}
//************************************ */
async getPlanRevenueAverage() {
const currentYear = new Date().getFullYear();
const startOfYear = new Date(currentYear, 0, 1);
const endOfYear = new Date(currentYear, 11, 31);
const result = await this.userSubscriptionsRepository
.createQueryBuilder("userSubscription")
.leftJoin("userSubscription.plan", "plan")
.select("AVG(plan.price)", "averageRevenue")
.where("userSubscription.startDate >= :startDate", { startDate: startOfYear })
.andWhere("userSubscription.startDate <= :endDate", { endDate: endOfYear })
.andWhere("userSubscription.status = :status", { status: SubscriptionStatus.ACTIVE })
.getRawOne();
return Number(result.averageRevenue) || 0;
}
async getPlansSalesCount() {
const currentYear = new Date().getFullYear();
const startOfYear = new Date(currentYear, 0, 1);
const endOfYear = new Date(currentYear, 11, 31);
return await this.userSubscriptionsRepository
.createQueryBuilder("userSubscription")
.where("userSubscription.startDate >= :startDate", { startDate: startOfYear })
.andWhere("userSubscription.startDate <= :endDate", { endDate: endOfYear })
.andWhere("userSubscription.status = :status", { status: SubscriptionStatus.ACTIVE })
.getCount();
}
async getPlansSalesCountByMonthsInYear() {
const now = new Date();
const startOfYear = new Date(now.getFullYear(), 0, 1);
const endOfYear = new Date(now.getFullYear(), 11, 31);
const sales = await this.userSubscriptionsRepository
.createQueryBuilder("userSubscription")
.select("EXTRACT(MONTH FROM userSubscription.startDate)", "month")
.addSelect("COUNT(*)", "count")
.where("userSubscription.startDate >= :startDate", { startDate: startOfYear })
.andWhere("userSubscription.startDate <= :endDate", { endDate: endOfYear })
.andWhere("userSubscription.status = :status", { status: SubscriptionStatus.ACTIVE })
.groupBy("EXTRACT(MONTH FROM userSubscription.startDate)")
.orderBy("month", "ASC")
.getRawMany();
const salesByMonth = Array(12).fill(0);
sales.forEach((sale) => {
const month = parseInt(sale.month) - 1;
salesByMonth[month] = parseInt(sale.count);
});
return salesByMonth;
}
async getPlansSalesCountByDaysInMonth() {
const now = new Date();
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
const endOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);
const sales = await this.userSubscriptionsRepository
.createQueryBuilder("userSubscription")
.select("DATE(userSubscription.startDate)", "date")
.addSelect("COUNT(*)", "count")
.where("userSubscription.startDate >= :startDate", { startDate: startOfMonth })
.andWhere("userSubscription.startDate <= :endDate", { endDate: endOfMonth })
.andWhere("userSubscription.status = :status", { status: SubscriptionStatus.ACTIVE })
.groupBy("DATE(userSubscription.startDate)")
.orderBy("date", "ASC")
.getRawMany();
const daysInMonth = endOfMonth.getDate();
const salesByDay = Array(daysInMonth).fill(0);
sales.forEach((sale) => {
const day = new Date(sale.date).getDate() - 1;
salesByDay[day] = parseInt(sale.count);
});
return salesByDay;
}
async getPlansSalesCountByDaysInWeek() {
const now = new Date();
const daysSinceStart = now.getDay(); // Remove the +1 and % 7
const startOfWeek = new Date(now);
startOfWeek.setDate(now.getDate() - daysSinceStart);
const endOfWeek = new Date(startOfWeek);
endOfWeek.setDate(startOfWeek.getDate() + 6);
const sales = await this.userSubscriptionsRepository
.createQueryBuilder("userSubscription")
.select("DATE(userSubscription.startDate)", "date")
.addSelect("COUNT(*)", "count")
.where("userSubscription.startDate >= :startDate", { startDate: startOfWeek })
.andWhere("userSubscription.startDate <= :endDate", { endDate: endOfWeek })
.andWhere("userSubscription.status = :status", { status: SubscriptionStatus.ACTIVE })
.groupBy("DATE(userSubscription.startDate)")
.orderBy("date", "ASC")
.getRawMany();
const salesByDay = Array(7).fill(0);
sales.forEach((sale) => {
const day = new Date(sale.date).getDay(); // Remove the +1 and % 7
salesByDay[day] = parseInt(sale.count);
});
return salesByDay;
}
async getSubscriptionGrowthRate() {
const currentYear = new Date().getFullYear();
const lastYear = currentYear - 1;
const currentYearCount = await this.userSubscriptionsRepository
.createQueryBuilder("userSubscription")
.where("EXTRACT(YEAR FROM userSubscription.startDate) = :year", { year: currentYear })
.andWhere("userSubscription.status = :status", { status: SubscriptionStatus.ACTIVE })
.getCount();
const lastYearCount = await this.userSubscriptionsRepository
.createQueryBuilder("userSubscription")
.where("EXTRACT(YEAR FROM userSubscription.startDate) = :year", { year: lastYear })
.andWhere("userSubscription.status = :status", { status: SubscriptionStatus.ACTIVE })
.getCount();
if (lastYearCount === 0) return 100;
const growthRate = ((currentYearCount - lastYearCount) / lastYearCount) * 100;
return Math.round(growthRate * 100) / 100;
}
}