|
| 1 | +import { Injectable } from '@nestjs/common' |
| 2 | +import { OrderRepository } from '@order/repositories/order.repository' |
| 3 | +import { ProductRepository } from '@product/repositories/product.repository' |
| 4 | +import { CustomerRepository } from '@customer/repositories/customer.repository' |
| 5 | +import { PaymentRepository } from '@payment/repositories/payment.repository' |
| 6 | +import { OrderStatus, ProductStatus, Status, TransactionStatus } from '@common/contracts/constant' |
| 7 | +import type { Moment } from 'moment' |
| 8 | +import * as moment from 'moment' |
| 9 | +import { QueryStatisticAnalyticDto } from '@analytic/dto/analytic.dto' |
| 10 | + |
| 11 | +@Injectable() |
| 12 | +export class AnalyticService { |
| 13 | + constructor( |
| 14 | + private readonly orderRepository: OrderRepository, |
| 15 | + private readonly productRepository: ProductRepository, |
| 16 | + private readonly customerRepository: CustomerRepository, |
| 17 | + private readonly paymentRepository: PaymentRepository |
| 18 | + ) {} |
| 19 | + |
| 20 | + public async getOrderCount(current: Moment, startOfCurrentPeriod: Moment, startOfPreviousPeriod: Moment) { |
| 21 | + const filter = { |
| 22 | + orderStatus: { |
| 23 | + $in: [OrderStatus.PENDING, OrderStatus.CONFIRMED, OrderStatus.DELIVERING, OrderStatus.COMPLETED] |
| 24 | + }, |
| 25 | + transactionStatus: { |
| 26 | + $in: [TransactionStatus.CAPTURED, TransactionStatus.DRAFT, TransactionStatus.ERROR] |
| 27 | + } |
| 28 | + } |
| 29 | + const [total, previousTotal] = await Promise.all([ |
| 30 | + this.orderRepository.model.countDocuments({ |
| 31 | + ...filter, |
| 32 | + createdAt: { $lte: current, $gte: startOfCurrentPeriod } |
| 33 | + }), |
| 34 | + this.orderRepository.model.countDocuments({ |
| 35 | + ...filter, |
| 36 | + createdAt: { $lte: startOfCurrentPeriod, $gte: startOfPreviousPeriod } |
| 37 | + }) |
| 38 | + ]) |
| 39 | + const percent = previousTotal !== 0 ? Math.round(((total - previousTotal) / previousTotal) * 100 * 100) / 100 : 0 |
| 40 | + return { total, previousTotal, percent } |
| 41 | + } |
| 42 | + |
| 43 | + public async getSalesSum(current: Date, startOfCurrentPeriod: Date, startOfPreviousPeriod: Date) { |
| 44 | + const filter = { |
| 45 | + transactionStatus: TransactionStatus.CAPTURED |
| 46 | + } |
| 47 | + const [sumTotal, previousSumTotal] = await Promise.all([ |
| 48 | + this.paymentRepository.model.aggregate([ |
| 49 | + { $match: { ...filter, createdAt: { $lte: current, $gte: startOfCurrentPeriod } } }, |
| 50 | + { |
| 51 | + $group: { _id: null, amount: { $sum: '$amount' } } |
| 52 | + } |
| 53 | + ]), |
| 54 | + this.paymentRepository.model.aggregate([ |
| 55 | + { $match: { ...filter, createdAt: { $lte: startOfCurrentPeriod, $gte: startOfPreviousPeriod } } }, |
| 56 | + { |
| 57 | + $group: { _id: null, amount: { $sum: '$amount' } } |
| 58 | + } |
| 59 | + ]) |
| 60 | + ]) |
| 61 | + const total = sumTotal[0]?.amount || 0 |
| 62 | + const previousTotal = previousSumTotal[0]?.amount || 0 |
| 63 | + const percent = previousTotal !== 0 ? Math.round(((total - previousTotal) / previousTotal) * 100 * 100) / 100 : 0 |
| 64 | + return { total, previousTotal, percent } |
| 65 | + } |
| 66 | + |
| 67 | + public async getProductCount(current: Moment, startOfCurrentPeriod: Moment, startOfPreviousPeriod: Moment) { |
| 68 | + const filter = { |
| 69 | + status: { $ne: ProductStatus.DELETED } |
| 70 | + } |
| 71 | + const [total, previousTotal] = await Promise.all([ |
| 72 | + this.productRepository.model.countDocuments({ |
| 73 | + ...filter, |
| 74 | + createdAt: { $lte: current, $gte: startOfCurrentPeriod } |
| 75 | + }), |
| 76 | + this.productRepository.model.countDocuments({ |
| 77 | + ...filter, |
| 78 | + createdAt: { $lte: startOfCurrentPeriod, $gte: startOfPreviousPeriod } |
| 79 | + }) |
| 80 | + ]) |
| 81 | + const percent = previousTotal !== 0 ? Math.round(((total - previousTotal) / previousTotal) * 100 * 100) / 100 : 0 |
| 82 | + return { total, previousTotal, percent } |
| 83 | + } |
| 84 | + |
| 85 | + public async getCustomerCount(current: Moment, startOfCurrentPeriod: Moment, startOfPreviousPeriod: Moment) { |
| 86 | + const filter = { |
| 87 | + status: { $ne: Status.DELETED } |
| 88 | + } |
| 89 | + const [total, previousTotal] = await Promise.all([ |
| 90 | + this.customerRepository.model.countDocuments({ |
| 91 | + ...filter, |
| 92 | + createdAt: { $lte: current, $gte: startOfCurrentPeriod } |
| 93 | + }), |
| 94 | + this.customerRepository.model.countDocuments({ |
| 95 | + ...filter, |
| 96 | + createdAt: { $lte: startOfCurrentPeriod, $gte: startOfPreviousPeriod } |
| 97 | + }) |
| 98 | + ]) |
| 99 | + const percent = previousTotal !== 0 ? Math.round(((total - previousTotal) / previousTotal) * 100 * 100) / 100 : 0 |
| 100 | + return { total, previousTotal, percent } |
| 101 | + } |
| 102 | + |
| 103 | + public async getSalesStatistic(queryStatisticAnalyticDto: QueryStatisticAnalyticDto) { |
| 104 | + const year = queryStatisticAnalyticDto.year |
| 105 | + const filter = { |
| 106 | + transactionStatus: TransactionStatus.CAPTURED |
| 107 | + } |
| 108 | + const operations = [] |
| 109 | + for (let i = 0; i < 12; i++) { |
| 110 | + const startOfMonth = moment([year, i]).startOf('month') |
| 111 | + const endOfMonth = startOfMonth.clone().endOf('month') |
| 112 | + operations.push( |
| 113 | + this.paymentRepository.model.aggregate([ |
| 114 | + { $match: { ...filter, createdAt: { $gte: startOfMonth.toDate(), $lte: endOfMonth.toDate() } } }, |
| 115 | + { |
| 116 | + $group: { _id: null, amount: { $sum: '$amount' } } |
| 117 | + } |
| 118 | + ]) |
| 119 | + ) |
| 120 | + } |
| 121 | + const result = await Promise.all(operations) |
| 122 | + const statistic = result.map((sumTotal) => sumTotal[0]?.amount || 0) |
| 123 | + return { statistic } |
| 124 | + } |
| 125 | +} |
0 commit comments