Re_Backend/src/controllers/dashboard.controller.ts

265 lines
7.1 KiB
TypeScript

import { Request, Response } from 'express';
import { DashboardService } from '../services/dashboard.service';
import logger from '@utils/logger';
export class DashboardController {
private dashboardService: DashboardService;
constructor() {
this.dashboardService = new DashboardService();
}
/**
* Get all KPI metrics for dashboard
*/
async getKPIs(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const dateRange = req.query.dateRange as string | undefined;
const kpis = await this.dashboardService.getKPIs(userId, dateRange);
res.json({
success: true,
data: kpis
});
} catch (error) {
logger.error('[Dashboard] Error fetching KPIs:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch dashboard KPIs'
});
}
}
/**
* Get request volume and status statistics
*/
async getRequestStats(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const dateRange = req.query.dateRange as string | undefined;
const stats = await this.dashboardService.getRequestStats(userId, dateRange);
res.json({
success: true,
data: stats
});
} catch (error) {
logger.error('[Dashboard] Error fetching request stats:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch request statistics'
});
}
}
/**
* Get TAT efficiency metrics
*/
async getTATEfficiency(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const dateRange = req.query.dateRange as string | undefined;
const efficiency = await this.dashboardService.getTATEfficiency(userId, dateRange);
res.json({
success: true,
data: efficiency
});
} catch (error) {
logger.error('[Dashboard] Error fetching TAT efficiency:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch TAT efficiency metrics'
});
}
}
/**
* Get approver load statistics
*/
async getApproverLoad(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const dateRange = req.query.dateRange as string | undefined;
const load = await this.dashboardService.getApproverLoad(userId, dateRange);
res.json({
success: true,
data: load
});
} catch (error) {
logger.error('[Dashboard] Error fetching approver load:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch approver load statistics'
});
}
}
/**
* Get engagement and quality metrics
*/
async getEngagementStats(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const dateRange = req.query.dateRange as string | undefined;
const engagement = await this.dashboardService.getEngagementStats(userId, dateRange);
res.json({
success: true,
data: engagement
});
} catch (error) {
logger.error('[Dashboard] Error fetching engagement stats:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch engagement statistics'
});
}
}
/**
* Get AI insights and closure metrics
*/
async getAIInsights(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const dateRange = req.query.dateRange as string | undefined;
const insights = await this.dashboardService.getAIInsights(userId, dateRange);
res.json({
success: true,
data: insights
});
} catch (error) {
logger.error('[Dashboard] Error fetching AI insights:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch AI insights'
});
}
}
/**
* Get recent activity feed
*/
async getRecentActivity(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const limit = Number(req.query.limit || 10);
const activities = await this.dashboardService.getRecentActivity(userId, limit);
res.json({
success: true,
data: activities
});
} catch (error) {
logger.error('[Dashboard] Error fetching recent activity:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch recent activity'
});
}
}
/**
* Get critical/high priority requests
*/
async getCriticalRequests(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const criticalRequests = await this.dashboardService.getCriticalRequests(userId);
res.json({
success: true,
data: criticalRequests
});
} catch (error) {
logger.error('[Dashboard] Error fetching critical requests:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch critical requests'
});
}
}
/**
* Get upcoming deadlines
*/
async getUpcomingDeadlines(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const limit = Number(req.query.limit || 5);
const deadlines = await this.dashboardService.getUpcomingDeadlines(userId, limit);
res.json({
success: true,
data: deadlines
});
} catch (error) {
logger.error('[Dashboard] Error fetching upcoming deadlines:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch upcoming deadlines'
});
}
}
/**
* Get department-wise statistics
*/
async getDepartmentStats(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const dateRange = req.query.dateRange as string | undefined;
const stats = await this.dashboardService.getDepartmentStats(userId, dateRange);
res.json({
success: true,
data: stats
});
} catch (error) {
logger.error('[Dashboard] Error fetching department stats:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch department statistics'
});
}
}
/**
* Get priority distribution statistics
*/
async getPriorityDistribution(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?.userId;
const dateRange = req.query.dateRange as string | undefined;
const distribution = await this.dashboardService.getPriorityDistribution(userId, dateRange);
res.json({
success: true,
data: distribution
});
} catch (error) {
logger.error('[Dashboard] Error fetching priority distribution:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch priority distribution'
});
}
}
}