mirror of
https://github.com/Sea-Haven-Industries/proposal-system.git
synced 2026-10-02 23:53:18 +00:00
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:
parent
0b1af71166
commit
d00c552497
2 changed files with 19 additions and 7 deletions
|
|
@ -29,11 +29,14 @@ public class AdminController : ControllerBase
|
||||||
var approvedThisWeek = await _db.Proposals
|
var approvedThisWeek = await _db.Proposals
|
||||||
.CountAsync(p => p.ApprovedAt >= weekStart, ct);
|
.CountAsync(p => p.ApprovedAt >= weekStart, ct);
|
||||||
|
|
||||||
var avgTurnaround = await _db.Proposals
|
var approvedTimes = await _db.Proposals
|
||||||
.Where(p => p.ApprovedAt.HasValue)
|
.Where(p => p.ApprovedAt.HasValue)
|
||||||
.Select(p => (p.ApprovedAt!.Value - p.SubmittedAt).TotalHours)
|
.Select(p => new { p.ApprovedAt, p.SubmittedAt })
|
||||||
.DefaultIfEmpty(0)
|
.ToListAsync(ct);
|
||||||
.AverageAsync(ct);
|
|
||||||
|
var avgTurnaround = approvedTimes.Count > 0
|
||||||
|
? approvedTimes.Average(p => (p.ApprovedAt!.Value - p.SubmittedAt).TotalHours)
|
||||||
|
: 0;
|
||||||
|
|
||||||
var totalProposals = await _db.Proposals.CountAsync(ct);
|
var totalProposals = await _db.Proposals.CountAsync(ct);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,11 @@ public class ProposalService : IProposalService
|
||||||
|
|
||||||
public async Task<ProposalResponse> UpdateAsync(Guid id, UpdateProposalRequest request, CancellationToken ct = default)
|
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");
|
?? throw new KeyNotFoundException($"Proposal {id} not found");
|
||||||
|
|
||||||
if (request.RefinedScope != null)
|
if (request.RefinedScope != null)
|
||||||
|
|
@ -163,6 +167,8 @@ public class ProposalService : IProposalService
|
||||||
{
|
{
|
||||||
var proposal = await _db.Proposals
|
var proposal = await _db.Proposals
|
||||||
.Include(p => p.LineItems)
|
.Include(p => p.LineItems)
|
||||||
|
.Include(p => p.SubmittedBy)
|
||||||
|
.Include(p => p.ApprovedBy)
|
||||||
.FirstOrDefaultAsync(p => p.Id == id, ct)
|
.FirstOrDefaultAsync(p => p.Id == id, ct)
|
||||||
?? throw new KeyNotFoundException($"Proposal {id} not found");
|
?? 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)
|
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");
|
?? throw new KeyNotFoundException($"Proposal {id} not found");
|
||||||
|
|
||||||
if (proposal.Status == ProposalStatus.Sent)
|
if (proposal.Status == ProposalStatus.Sent)
|
||||||
|
|
@ -231,7 +240,7 @@ public class ProposalService : IProposalService
|
||||||
var revision = new Proposal
|
var revision = new Proposal
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid(),
|
Id = Guid.NewGuid(),
|
||||||
ProposalNumber = proposal.ProposalNumber,
|
ProposalNumber = $"{proposal.ProposalNumber}-R{proposal.CurrentRevision + 1}",
|
||||||
WorkOrderNumber = proposal.WorkOrderNumber,
|
WorkOrderNumber = proposal.WorkOrderNumber,
|
||||||
CustomerName = proposal.CustomerName,
|
CustomerName = proposal.CustomerName,
|
||||||
CustomerAddress = proposal.CustomerAddress,
|
CustomerAddress = proposal.CustomerAddress,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue