fix(giving): map duplicate-date race to 409 + return zelle/paypal refs in session detail

This commit is contained in:
Chris Chen
2026-05-28 16:53:24 -07:00
parent e04776460d
commit 86041c0d05
3 changed files with 43 additions and 2 deletions
@@ -57,6 +57,10 @@ public class OfferingSessionService : IOfferingSessionService
public async Task<bool> DateExistsAsync(DateOnly date)
=> await _db.OfferingSessions.AnyAsync(s => s.SessionDate == date);
// Distinguishes a unique-index collision on SessionDate (concurrent insert) from other DB errors.
private async Task<bool> DateExistsConcurrentlyAsync(DateOnly date, int excludeId)
=> await _db.OfferingSessions.AsNoTracking().AnyAsync(s => s.SessionDate == date && s.Id != excludeId);
public async Task<OfferingSessionDto?> GetByIdAsync(int id)
{
var s = await _db.OfferingSessions.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
@@ -84,7 +88,10 @@ public class OfferingSessionService : IOfferingSessionService
GivingCategoryId = l.GivingCategoryId,
CategoryName = catNames.TryGetValue(l.GivingCategoryId, out var cn) ? cn : "",
Amount = l.Amount, PaymentMethod = l.PaymentMethod,
CheckNumber = l.CheckNumber, IsAnonymous = l.IsAnonymous, Notes = l.Notes,
CheckNumber = l.CheckNumber,
ZelleReferenceCode = l.ZelleReferenceCode,
PayPalTransactionId = l.PayPalTransactionId,
IsAnonymous = l.IsAnonymous, Notes = l.Notes,
}).ToList(),
};
}
@@ -106,7 +113,16 @@ public class OfferingSessionService : IOfferingSessionService
Givings = r.Givings.Select(line => MapLine(line, r.SessionDate)).ToList(),
};
_db.OfferingSessions.Add(session);
await _db.SaveChangesAsync();
try
{
await _db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (await DateExistsConcurrentlyAsync(r.SessionDate, session.Id))
throw new InvalidOperationException($"An offering session for {r.SessionDate:yyyy-MM-dd} already exists.");
throw;
}
return session.Id;
}