import { Injectable } from '@nestjs/common'; import { DataSource } from 'typeorm'; import { Warehouse } from './entities/warehouse.entity'; import { WarehouseInventory } from './entities/warehouse-inventory.entity'; export interface WarehouseDashboard { totalWarehouses: number; totalInventory: number; receivedToday: number; stored: number; reserved: number; readyForLoading: number; loaded: number; dispatched: number; } @Injectable() export class WarehouseDashboardService { constructor(private readonly dataSource: DataSource) {} async getDashboard(): Promise { const warehouseRepo = this.dataSource.getRepository(Warehouse); const inventoryRepo = this.dataSource.getRepository(WarehouseInventory); const startOfToday = new Date(); startOfToday.setHours(0, 0, 0, 0); const [totalWarehouses, totalInventory, stored, reserved, readyForLoading, loaded, dispatched, receivedToday] = await Promise.all([ warehouseRepo.count(), inventoryRepo.count(), inventoryRepo.count({ where: { status: 'STORED' } }), inventoryRepo.count({ where: { status: 'RESERVED' } }), inventoryRepo.count({ where: { status: 'READY_FOR_LOADING' } }), inventoryRepo.count({ where: { status: 'LOADED' } }), inventoryRepo.count({ where: { status: 'DISPATCHED' } }), inventoryRepo .createQueryBuilder('inv') .where('inv.arrived_at >= :start', { start: startOfToday }) .getCount(), ]); return { totalWarehouses, totalInventory, receivedToday, stored, reserved, readyForLoading, loaded, dispatched, }; } }