Handler.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Domain.Entities.Common.ValueObject;
  4. using Domain.Entities.Donations;
  5. using Domain.Entities.Donations.ValueObject;
  6. using Microsoft.EntityFrameworkCore;
  7. namespace Application.Features.Api.Donation.Send;
  8. internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Response>
  9. {
  10. public async Task<Response> Handle(Command request, CancellationToken ct)
  11. {
  12. // 1. 채널 조회 + 수수료율
  13. var channel = await db.Channel
  14. .AsNoTracking()
  15. .Where(c => c.ID == request.ChannelID && c.IsActive)
  16. .Select(c => new { c.ID, c.MemberID, c.PlatformFeeRate })
  17. .FirstOrDefaultAsync(ct);
  18. if (channel is null)
  19. {
  20. throw new KeyNotFoundException("채널을 찾을 수 없습니다.");
  21. }
  22. // 2. 자기 자신에게 후원 불가
  23. if (request.SponsorMemberID == channel.MemberID && !request.IsTest)
  24. {
  25. throw new InvalidOperationException("본인 채널에 후원할 수 없습니다.");
  26. }
  27. // 3. 후원 수락 상태 확인
  28. var meta = await db.DonationMeta
  29. .AsNoTracking()
  30. .FirstOrDefaultAsync(m => m.ChannelID == request.ChannelID, ct);
  31. if (meta is not null && !meta.IsAccepting)
  32. {
  33. throw new InvalidOperationException("현재 후원을 받지 않는 채널입니다.");
  34. }
  35. var minAmount = meta?.MinAmount ?? DonationConstants.MinAmount;
  36. if (request.Amount < minAmount)
  37. {
  38. throw new InvalidOperationException($"최소 후원 금액은 {minAmount}원입니다.");
  39. }
  40. if (request.Amount > DonationConstants.MaxAmount)
  41. {
  42. throw new InvalidOperationException($"최대 후원 금액은 {DonationConstants.MaxAmount:N0}원입니다.");
  43. }
  44. // 4. 크루 세션 검증 (크루 후원인 경우)
  45. if (request.CrewSessionID.HasValue)
  46. {
  47. var session = await db.CrewSession
  48. .AsNoTracking()
  49. .FirstOrDefaultAsync(s => s.ID == request.CrewSessionID.Value && s.Status == CrewSessionStatus.Active, ct);
  50. if (session is null)
  51. {
  52. throw new InvalidOperationException("활성화된 크루 후원 방송이 아닙니다.");
  53. }
  54. }
  55. // 5. 후원자 지갑 조회 + 잔액 차감
  56. var sponsorWallet = await db.Wallet
  57. .Include(w => w.Balances)
  58. .FirstOrDefaultAsync(w => w.MemberID == request.SponsorMemberID, ct);
  59. if (sponsorWallet is null)
  60. {
  61. throw new KeyNotFoundException("지갑을 찾을 수 없습니다.");
  62. }
  63. var spendAmount = Money.KRW(request.Amount);
  64. var totalAvailable = sponsorWallet.GetTotalAvailable();
  65. if (totalAvailable.Value < spendAmount.Value)
  66. {
  67. throw new InvalidOperationException("잔액이 부족합니다.");
  68. }
  69. // 6. Donation 생성 (수수료 스냅샷)
  70. var donation = Domain.Entities.Donations.Donation.Create(
  71. sponsorMemberID: request.SponsorMemberID,
  72. receiverMemberID: channel.MemberID,
  73. channelID: channel.ID,
  74. amount: request.Amount,
  75. feeRate: channel.PlatformFeeRate,
  76. message: request.Message,
  77. sendName: request.SendName,
  78. crewSessionID: request.CrewSessionID,
  79. crewMemberID: request.CrewMemberID,
  80. isTest: request.IsTest
  81. );
  82. db.Donation.Add(donation);
  83. // 7. 후원자 지갑 차감
  84. if (!request.IsTest)
  85. {
  86. sponsorWallet.Spend(spendAmount, "DONATION_OUT", donation.ID.ToString());
  87. }
  88. // 8. 수신자 지갑에 입금 (NetAmount)
  89. if (!request.IsTest)
  90. {
  91. var receiverWallet = await db.Wallet
  92. .Include(w => w.Balances)
  93. .FirstOrDefaultAsync(w => w.MemberID == channel.MemberID, ct);
  94. if (receiverWallet is not null)
  95. {
  96. receiverWallet.CreditDonationIn(Money.KRW(donation.NetAmount), "DONATION_IN", donation.ID.ToString());
  97. }
  98. }
  99. // SaveChanges로 Donation ID 확보
  100. await db.SaveChangesAsync(ct);
  101. // 9. DonationAlert 생성
  102. var alert = Domain.Entities.Donations.DonationAlert.Create(
  103. donationID: donation.ID,
  104. sponsorMemberID: request.SponsorMemberID,
  105. receiverMemberID: channel.MemberID
  106. );
  107. db.DonationAlert.Add(alert);
  108. // 10. 크루 후원인 경우 집계 갱신
  109. if (request.CrewSessionID.HasValue && request.CrewMemberID.HasValue)
  110. {
  111. var summary = await db.CrewDonationSummary
  112. .FirstOrDefaultAsync(s => s.CrewSessionID == request.CrewSessionID.Value
  113. && s.CrewMemberID == request.CrewMemberID.Value, ct);
  114. if (summary is null)
  115. {
  116. summary = CrewDonationSummary.Create(request.CrewSessionID.Value, request.CrewMemberID.Value);
  117. db.CrewDonationSummary.Add(summary);
  118. }
  119. summary.AddDonation(request.Amount);
  120. // 세션 전체 합계 갱신
  121. var session = await db.CrewSession.FindAsync([request.CrewSessionID.Value], ct);
  122. session?.AddDonation(request.Amount);
  123. // 기여도 재계산
  124. if (session is not null && session.TotalAmount > 0)
  125. {
  126. var allSummaries = await db.CrewDonationSummary
  127. .Where(s => s.CrewSessionID == session.ID)
  128. .OrderByDescending(s => s.TotalAmount)
  129. .ToListAsync(ct);
  130. var rank = 1;
  131. foreach (var s in allSummaries)
  132. {
  133. var rate = (decimal)s.TotalAmount / session.TotalAmount * 100;
  134. s.UpdateContribution(rate, rank++);
  135. }
  136. }
  137. }
  138. await db.SaveChangesAsync(ct);
  139. // SignalR Push는 Endpoint 레이어 또는 별도 서비스에서 처리
  140. return new Response(donation.ID, alert.CorrelationID, donation.Amount, donation.FeeAmount, donation.NetAmount);
  141. }
  142. }