import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, MoreThan } from 'typeorm'; import { LcCustomer } from '../../entities/lc-customer.entity'; import { LcLicense } from '../../entities/lc-license.entity'; @Injectable() export class DashboardService { constructor( @InjectRepository(LcCustomer) private readonly customerRepo: Repository, @InjectRepository(LcLicense) private readonly licenseRepo: Repository, ) {} async stats() { const customerCount = await this.customerRepo.count(); const activeLicenseCount = await this.licenseRepo.count({ where: { status: 'active' } }); const thirtyDaysLater = new Date(); thirtyDaysLater.setDate(thirtyDaysLater.getDate() + 30); const expiringSoonCount = await this.licenseRepo.count({ where: { status: 'active', expiresAt: MoreThan(new Date()), }, }); const expiring = await this.licenseRepo .createQueryBuilder('l') .where('l.status = :status', { status: 'active' }) .andWhere('l.expires_at <= :date', { date: thirtyDaysLater }) .andWhere('l.expires_at > NOW()') .getCount(); return { customerCount, activeLicenseCount, expiringSoonCount: expiring, totalLicenses: await this.licenseRepo.count(), }; } }