fix(giving): map duplicate-date race to 409 + return zelle/paypal refs in session detail
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user