mirror of
https://github.com/Sea-Haven-Industries/proposal-system.git
synced 2026-10-05 07:12:01 +00:00
43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using ProposalSystem.Application.DTOs;
|
||
|
|
using ProposalSystem.Domain.Entities;
|
||
|
|
using ProposalSystem.Infrastructure.Data;
|
||
|
|
|
||
|
|
namespace ProposalSystem.Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Route("api/admin")]
|
||
|
|
[Authorize(Roles = "admins,sysadmins")]
|
||
|
|
public class AdminController : ControllerBase
|
||
|
|
{
|
||
|
|
private readonly ProposalDbContext _db;
|
||
|
|
|
||
|
|
public AdminController(ProposalDbContext db)
|
||
|
|
{
|
||
|
|
_db = db;
|
||
|
|
}
|
||
|
|
|
||
|
|
[HttpGet("dashboard")]
|
||
|
|
public async Task<ActionResult<DashboardResponse>> GetDashboard(CancellationToken ct)
|
||
|
|
{
|
||
|
|
var pendingCount = await _db.Proposals
|
||
|
|
.CountAsync(p => p.Status == ProposalStatus.InReview, ct);
|
||
|
|
|
||
|
|
var weekStart = DateTime.UtcNow.AddDays(-7);
|
||
|
|
var approvedThisWeek = await _db.Proposals
|
||
|
|
.CountAsync(p => p.ApprovedAt >= weekStart, ct);
|
||
|
|
|
||
|
|
var avgTurnaround = await _db.Proposals
|
||
|
|
.Where(p => p.ApprovedAt.HasValue)
|
||
|
|
.Select(p => (p.ApprovedAt!.Value - p.SubmittedAt).TotalHours)
|
||
|
|
.DefaultIfEmpty(0)
|
||
|
|
.AverageAsync(ct);
|
||
|
|
|
||
|
|
var totalProposals = await _db.Proposals.CountAsync(ct);
|
||
|
|
|
||
|
|
return Ok(new DashboardResponse(pendingCount, approvedThisWeek, avgTurnaround, totalProposals));
|
||
|
|
}
|
||
|
|
}
|