mirror of
https://github.com/Sea-Haven-Industries/proposal-system.git
synced 2026-10-06 18:12:00 +00:00
AdminController: Rewrite avgTurnaround query to fetch approved times to
memory before computing TotalHours — EF Core/Npgsql cannot translate
TimeSpan.TotalHours to SQL, causing a 409 on every dashboard load.
ProposalService.ReviseAsync: Append -R{n} suffix to revision's
ProposalNumber so it doesn't violate the unique index. Previously copied
the parent's number verbatim, causing a DbUpdateException (500).
ProposalService Update/Approve/MarkSent: Add .Include(p => p.SubmittedBy)
(and ApprovedBy where relevant) so MapToResponse returns submittedByName
instead of null. GetByIdAsync already had these includes.
45 lines
1.4 KiB
C#
45 lines
1.4 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 approvedTimes = await _db.Proposals
|
|
.Where(p => p.ApprovedAt.HasValue)
|
|
.Select(p => new { p.ApprovedAt, p.SubmittedAt })
|
|
.ToListAsync(ct);
|
|
|
|
var avgTurnaround = approvedTimes.Count > 0
|
|
? approvedTimes.Average(p => (p.ApprovedAt!.Value - p.SubmittedAt).TotalHours)
|
|
: 0;
|
|
|
|
var totalProposals = await _db.Proposals.CountAsync(ct);
|
|
|
|
return Ok(new DashboardResponse(pendingCount, approvedThisWeek, avgTurnaround, totalProposals));
|
|
}
|
|
}
|