feat(giving): offering-session batch service with server-side totals + locking

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Chris Chen
2026-05-28 16:47:19 -07:00
parent 586551aec0
commit e04776460d
9 changed files with 374 additions and 0 deletions
@@ -0,0 +1,15 @@
using ROLAC.API.DTOs.Giving;
using ROLAC.API.DTOs.Shared;
namespace ROLAC.API.Services;
public interface IOfferingSessionService
{
Task<PagedResult<OfferingSessionListItemDto>> GetPagedAsync(
int page, int pageSize, DateOnly? from, DateOnly? to);
Task<OfferingSessionDto?> GetByIdAsync(int id);
Task<bool> DateExistsAsync(DateOnly date);
Task<int> CreateAsync(CreateOfferingSessionRequest request);
Task ReopenAsync(int id);
Task ReplaceAsync(int id, CreateOfferingSessionRequest request);
}
@@ -0,0 +1,158 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using ROLAC.API.Data;
using ROLAC.API.DTOs.Giving;
using ROLAC.API.DTOs.Shared;
using ROLAC.API.Entities;
namespace ROLAC.API.Services;
public class OfferingSessionService : IOfferingSessionService
{
private readonly AppDbContext _db;
private readonly IHttpContextAccessor _http;
public OfferingSessionService(AppDbContext db, IHttpContextAccessor http)
{
_db = db;
_http = http;
}
private string CurrentUserId =>
_http.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier) ?? "system";
public async Task<PagedResult<OfferingSessionListItemDto>> GetPagedAsync(
int page, int pageSize, DateOnly? from, DateOnly? to)
{
var query = _db.OfferingSessions.AsNoTracking().AsQueryable();
if (from.HasValue) query = query.Where(s => s.SessionDate >= from.Value);
if (to.HasValue) query = query.Where(s => s.SessionDate <= to.Value);
var total = await query.CountAsync();
var rows = await query
.OrderByDescending(s => s.SessionDate)
.Skip((page - 1) * pageSize).Take(pageSize)
.ToListAsync();
var ids = rows.Select(r => r.Id).ToList();
var counts = await _db.Givings.AsNoTracking()
.Where(g => g.OfferingSessionId != null && ids.Contains(g.OfferingSessionId.Value))
.GroupBy(g => g.OfferingSessionId!.Value)
.Select(grp => new { Id = grp.Key, Count = grp.Count() })
.ToDictionaryAsync(x => x.Id, x => x.Count);
var items = rows.Select(s => new OfferingSessionListItemDto
{
Id = s.Id, SessionDate = s.SessionDate.ToString("yyyy-MM-dd"), Status = s.Status,
CashTotal = s.CashTotal, CheckTotal = s.CheckTotal,
SystemTotal = s.SystemTotal, Difference = s.Difference,
LineCount = counts.TryGetValue(s.Id, out var c) ? c : 0,
}).ToList();
return new PagedResult<OfferingSessionListItemDto>
{ Items = items, TotalCount = total, Page = page, PageSize = pageSize };
}
public async Task<bool> DateExistsAsync(DateOnly date)
=> await _db.OfferingSessions.AnyAsync(s => s.SessionDate == date);
public async Task<OfferingSessionDto?> GetByIdAsync(int id)
{
var s = await _db.OfferingSessions.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);
if (s is null) return null;
var lines = await _db.Givings.AsNoTracking()
.Where(g => g.OfferingSessionId == id).ToListAsync();
var catNames = await _db.GivingCategories.AsNoTracking()
.ToDictionaryAsync(c => c.Id, c => c.Name_en);
var memberIds = lines.Where(l => l.MemberId != null).Select(l => l.MemberId!.Value).ToHashSet();
var memberNames = await _db.Members.AsNoTracking()
.Where(m => memberIds.Contains(m.Id))
.ToDictionaryAsync(m => m.Id, m => $"{m.FirstName_en} {m.LastName_en}");
return new OfferingSessionDto
{
Id = s.Id, SessionDate = s.SessionDate, Status = s.Status,
CashTotal = s.CashTotal, CheckTotal = s.CheckTotal,
SystemTotal = s.SystemTotal, Difference = s.Difference, Notes = s.Notes,
Givings = lines.Select(l => new OfferingGivingLineDto
{
Id = l.Id, MemberId = l.MemberId,
MemberName = l.MemberId != null && memberNames.TryGetValue(l.MemberId.Value, out var n) ? n : null,
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,
}).ToList(),
};
}
public async Task<int> CreateAsync(CreateOfferingSessionRequest r)
{
if (await DateExistsAsync(r.SessionDate))
throw new InvalidOperationException($"An offering session for {r.SessionDate:yyyy-MM-dd} already exists.");
var systemTotal = r.Givings.Sum(g => g.Amount);
var session = new OfferingSession
{
SessionDate = r.SessionDate, Status = "Submitted",
CashTotal = r.CashTotal, CheckTotal = r.CheckTotal,
SystemTotal = systemTotal,
Difference = (r.CashTotal + r.CheckTotal) - systemTotal,
Notes = r.Notes,
SubmittedAt = DateTimeOffset.UtcNow, SubmittedBy = CurrentUserId,
Givings = r.Givings.Select(line => MapLine(line, r.SessionDate)).ToList(),
};
_db.OfferingSessions.Add(session);
await _db.SaveChangesAsync();
return session.Id;
}
public async Task ReopenAsync(int id)
{
var s = await _db.OfferingSessions.FindAsync(id)
?? throw new KeyNotFoundException($"OfferingSession {id} not found.");
if (s.Status != "Submitted")
throw new InvalidOperationException($"Only a Submitted session can be reopened (current: {s.Status}).");
s.Status = "Draft";
s.SubmittedAt = null; s.SubmittedBy = null;
await _db.SaveChangesAsync();
}
public async Task ReplaceAsync(int id, CreateOfferingSessionRequest r)
{
var s = await _db.OfferingSessions
.Include(x => x.Givings)
.FirstOrDefaultAsync(x => x.Id == id)
?? throw new KeyNotFoundException($"OfferingSession {id} not found.");
if (s.Status != "Draft")
throw new InvalidOperationException($"Only a Draft (reopened) session can be edited (current: {s.Status}).");
_db.Givings.RemoveRange(s.Givings);
var systemTotal = r.Givings.Sum(g => g.Amount);
s.CashTotal = r.CashTotal; s.CheckTotal = r.CheckTotal;
s.SystemTotal = systemTotal;
s.Difference = (r.CashTotal + r.CheckTotal) - systemTotal;
s.Notes = r.Notes;
s.Status = "Submitted";
s.SubmittedAt = DateTimeOffset.UtcNow; s.SubmittedBy = CurrentUserId;
s.Givings = r.Givings.Select(line => MapLine(line, s.SessionDate)).ToList();
await _db.SaveChangesAsync();
}
private static Giving MapLine(OfferingGivingLineRequest line, DateOnly sessionDate) => new()
{
MemberId = line.IsAnonymous ? null : line.MemberId,
GivingCategoryId = line.GivingCategoryId,
Amount = line.Amount,
PaymentMethod = line.PaymentMethod,
CheckNumber = line.CheckNumber,
ZelleReferenceCode = line.ZelleReferenceCode,
PayPalTransactionId = line.PayPalTransactionId,
GivingDate = sessionDate,
IsAnonymous = line.IsAnonymous,
Notes = line.Notes,
};
}