Fix admin dashboard LINQ crash, revise unique constraint, and mutation response data

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.
This commit is contained in:
Adam Moussa 2026-05-20 18:08:50 -04:00
parent 0b1af71166
commit d00c552497
2 changed files with 19 additions and 7 deletions

View file

@ -29,11 +29,14 @@ public class AdminController : ControllerBase
var approvedThisWeek = await _db.Proposals
.CountAsync(p => p.ApprovedAt >= weekStart, ct);
var avgTurnaround = await _db.Proposals
var approvedTimes = await _db.Proposals
.Where(p => p.ApprovedAt.HasValue)
.Select(p => (p.ApprovedAt!.Value - p.SubmittedAt).TotalHours)
.DefaultIfEmpty(0)
.AverageAsync(ct);
.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);

View file

@ -135,7 +135,11 @@ public class ProposalService : IProposalService
public async Task<ProposalResponse> UpdateAsync(Guid id, UpdateProposalRequest request, CancellationToken ct = default)
{
var proposal = await _db.Proposals.FindAsync(new object[] { id }, ct)
var proposal = await _db.Proposals
.Include(p => p.SubmittedBy)
.Include(p => p.AssignedAdmin)
.Include(p => p.ApprovedBy)
.FirstOrDefaultAsync(p => p.Id == id, ct)
?? throw new KeyNotFoundException($"Proposal {id} not found");
if (request.RefinedScope != null)
@ -163,6 +167,8 @@ public class ProposalService : IProposalService
{
var proposal = await _db.Proposals
.Include(p => p.LineItems)
.Include(p => p.SubmittedBy)
.Include(p => p.ApprovedBy)
.FirstOrDefaultAsync(p => p.Id == id, ct)
?? throw new KeyNotFoundException($"Proposal {id} not found");
@ -189,7 +195,10 @@ public class ProposalService : IProposalService
public async Task<ProposalResponse> MarkSentAsync(Guid id, CancellationToken ct = default)
{
var proposal = await _db.Proposals.FindAsync(new object[] { id }, ct)
var proposal = await _db.Proposals
.Include(p => p.SubmittedBy)
.Include(p => p.ApprovedBy)
.FirstOrDefaultAsync(p => p.Id == id, ct)
?? throw new KeyNotFoundException($"Proposal {id} not found");
if (proposal.Status == ProposalStatus.Sent)
@ -231,7 +240,7 @@ public class ProposalService : IProposalService
var revision = new Proposal
{
Id = Guid.NewGuid(),
ProposalNumber = proposal.ProposalNumber,
ProposalNumber = $"{proposal.ProposalNumber}-R{proposal.CurrentRevision + 1}",
WorkOrderNumber = proposal.WorkOrderNumber,
CustomerName = proposal.CustomerName,
CustomerAddress = proposal.CustomerAddress,