Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/src/dashboard/dashboard.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Controller, Get, Query } from '@nestjs/common';
import { DashboardService } from './dashboard.service';
import { DashboardMetricsDto } from './dto/dashboard-metrics.dto';


@Controller('dashboard')
export class DashboardController {
constructor(private readonly service: DashboardService) {}


/**
* GET /dashboard/metrics
* Optional query: lowStockThreshold (number) — threshold to consider "low stock"
*/
@Get('metrics')
async getMetrics(@Query('lowStockThreshold') lowStockThreshold?: string): Promise<DashboardMetricsDto> {
const threshold = lowStockThreshold ? Number(lowStockThreshold) : undefined;
return this.service.getMetrics({ lowStockThreshold: threshold });
}
}
40 changes: 40 additions & 0 deletions backend/src/dashboard/dashboard.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export interface StatusGroup {
status: string;
count: number;
}


export interface LowStockLocation {
location: string | null;
totalQuantity: number;
}


export class DashboardMetricsDto {
// total number of asset rows
totalCount: number;


// sum of `quantity` across all assets (useful for stock units)
totalQuantity: number;


// assets marked as disposed
disposedCount: number;


// counts per status
statusGroups: StatusGroup[];


// number of assets under the low-stock threshold
lowStockCount: number;


// top few locations (or groups) with smallest aggregated quantity
lowStockLocations: LowStockLocation[];


// the threshold used for low-stock calculation
lowStockThreshold?: number;
}
32 changes: 32 additions & 0 deletions backend/src/dashboard/dashboard.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';


@Entity('assets')
export class Asset {
@PrimaryGeneratedColumn('uuid')
id: string;


@Column({ length: 50, default: 'active' })
status: string; // e.g. 'active' | 'disposed' | 'maintenance'


@Column({ type: 'int', nullable: true })
quantity: number | null;


@Column({ type: 'timestamptz', nullable: true })
disposedAt: Date | null;


@Column({ length: 100, nullable: true })
location: string | null;


@CreateDateColumn()
createdAt: Date;


@UpdateDateColumn()
updatedAt: Date;
}
14 changes: 14 additions & 0 deletions backend/src/dashboard/dashboard.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DashboardService } from './dashboard.service';
import { DashboardController } from './dashboard.controller';
import { Asset } from '../assets/entities/asset.entity';


@Module({
imports: [TypeOrmModule.forFeature([Asset])],
providers: [DashboardService],
controllers: [DashboardController],
exports: [DashboardService],
})
export class DashboardModule {}
76 changes: 76 additions & 0 deletions backend/src/dashboard/dashboard.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Injectable } from '@nestjs/common';
.select('COUNT(asset.id)', 'disposedCount')
.where('asset.status = :disposed', { disposed: 'disposed' });


// 3) Grouped by status (counts)
const statusGroupQuery = this.assetRepo
.createQueryBuilder('asset')
.select('asset.status', 'status')
.addSelect('COUNT(asset.id)', 'count')
.groupBy('asset.status');


// 4) Low stock items (count and list limited)
const lowStockCountQuery = this.assetRepo
.createQueryBuilder('asset')
.select('COUNT(asset.id)', 'lowStockCount')
.where('asset.quantity IS NOT NULL')
.andWhere('asset.quantity <= :threshold', { threshold: lowStockThreshold });


// 5) Example: get top 5 locations with lowest total quantity
const lowStockLocationsQuery = this.assetRepo
.createQueryBuilder('asset')
.select('asset.location', 'location')
.addSelect('COALESCE(SUM(asset.quantity),0)', 'totalQuantity')
.groupBy('asset.location')
.orderBy('totalQuantity', 'ASC')
.limit(5);


// Execute queries in parallel
const [totalRaw, disposedRaw, statusGroupsRaw, lowStockCountRaw, lowStockLocationsRaw] =
await Promise.all([
totalQuery.getRawOne(),
disposedQuery.getRawOne(),
statusGroupQuery.getRawMany(),
lowStockCountQuery.getRawOne(),
lowStockLocationsQuery.getRawMany(),
]);


const totalCount = Number(totalRaw?.totalCount ?? 0);
const totalQuantity = Number(totalRaw?.totalQuantity ?? 0);
const disposedCount = Number(disposedRaw?.disposedCount ?? 0);


const statusGroups: StatusGroup[] = (statusGroupsRaw ?? []).map((r: any) => ({
status: r.status,
count: Number(r.count),
}));


const lowStockCount = Number(lowStockCountRaw?.lowStockCount ?? 0);


const lowStockLocations = (lowStockLocationsRaw ?? []).map((r: any) => ({
location: r.location,
totalQuantity: Number(r.totalQuantity),
}));


const dto: DashboardMetricsDto = {
totalCount,
totalQuantity,
disposedCount,
statusGroups,
lowStockCount,
lowStockLocations,
lowStockThreshold,
};


return dto;
}
}