public async Task Handle(DraftExpireAccountFundsCommand message, IMessageHandlerContext context) { _logger.Info($"DRAFT: Expiring funds for account ID '{message.AccountId}' with expiry period '{_configuration.FundsExpiryPeriod}'"); var now = _currentDateTime.Now; var fundsIn = await _levyFundsInRepository.GetLevyFundsIn(message.AccountId); var fundsOut = (await _paymentFundsOutRepository.GetPaymentFundsOut(message.AccountId)).ToList(); var existingExpiredFunds = await _expiredFundsRepository.GetDraft(message.AccountId); if (message.DateTo != null && fundsOut.Count > 0) { fundsOut = fundsOut.Where(c => new DateTime(c.CalendarPeriodYear, c.CalendarPeriodMonth, 1) < new DateTime(message.DateTo.Value.Year, message.DateTo.Value.Month, 1)).ToList(); } var expiredFunds = _expiredFunds.GetExpiredFunds( fundsIn.ToCalendarPeriodDictionary(), fundsOut.ToCalendarPeriodDictionary(), existingExpiredFunds.ToCalendarPeriodDictionary(), _configuration.FundsExpiryPeriod, now); if (!message.DateTo.HasValue) { message.DateTo = DateTime.UtcNow; } var currentCalendarPeriod = new CalendarPeriod(message.DateTo.Value.Year, message.DateTo.Value.Month); if (!expiredFunds.ContainsKey(currentCalendarPeriod)) { expiredFunds.Add(currentCalendarPeriod, 0); } await _expiredFundsRepository.CreateDraft(message.AccountId, expiredFunds.Where(c => c.Key.Equals(currentCalendarPeriod)).ToDictionary(key => key.Key, value => value.Value).ToExpiredFundsList(), now); _logger.Info($"DRAFT: Expired '{expiredFunds.Count}' month(s) of funds for account ID '{message.AccountId}' with expiry period '{_configuration.FundsExpiryPeriod}'"); }
public async Task Handle(ExpireAccountFundsCommand message, IMessageHandlerContext context) { _logger.Info($"Expiring funds for account ID '{message.AccountId}' with expiry period '{_configuration.FundsExpiryPeriod}'"); var now = _currentDateTime.Now; var fundsIn = await _levyFundsInRepository.GetLevyFundsIn(message.AccountId); var fundsOut = await _paymentFundsOutRepository.GetPaymentFundsOut(message.AccountId); var existingExpiredFunds = await _expiredFundsRepository.Get(message.AccountId); var expiredFunds = _expiredFunds.GetExpiredFunds( fundsIn.ToCalendarPeriodDictionary(), fundsOut.ToCalendarPeriodDictionary(), existingExpiredFunds.ToCalendarPeriodDictionary(), _configuration.FundsExpiryPeriod, now); var currentCalendarPeriod = new CalendarPeriod(_currentDateTime.Now.Year, _currentDateTime.Now.Month); if (!expiredFunds.ContainsKey(currentCalendarPeriod)) { expiredFunds.Add(currentCalendarPeriod, 0); } await _expiredFundsRepository.Create(message.AccountId, expiredFunds.ToExpiredFundsList(), now); //todo: do we publish the event if no fund expired? we could add a bool like the levy declared message // once an account has an expired fund, we'll publish every run, even if no additional funds have expired if (expiredFunds.Any(ef => ef.Value != 0m)) { await PublishAccountFundsExpiredEvent(context, message.AccountId); } _logger.Info($"Expired '{expiredFunds.Count}' month(s) of funds for account ID '{message.AccountId}' with expiry period '{_configuration.FundsExpiryPeriod}'"); }