public async Task Should_send_correct_data()
        {
            InitializeMock();
            var service = CreateService();

            var(account, chargeAmount) = CreateAccountAndAmount(10000, 9943, 1, 1);
            AccountBalanceManagementNotificationData actualMailData = null;

            SaveMailData();

            await service.SendNotificationIfRequired(account, chargeAmount);

            Assert.Equal(1, actualMailData.AgencyAccountId);
            Assert.Equal(1, actualMailData.AgencyId);
            Assert.Equal("AgencyName1", actualMailData.AgencyName);
            Assert.Equal(EnumFormatters.FromDescription(Currencies.USD), actualMailData.Currency);
            Assert.Equal(MoneyFormatter.ToCurrencyString(57, Currencies.USD), actualMailData.NewAmount);


            void SaveMailData()
            => _notificationServiceMock
            .Setup(x => x.Send(It.IsAny <DataWithCompanyInfo>(), It.IsAny <NotificationTypes>(), It.IsAny <string>()))
            .Callback <DataWithCompanyInfo, NotificationTypes, string>((data, _, _)
                                                                       => actualMailData = (AccountBalanceManagementNotificationData)data);
        }
        public async Task Should_use_lowest_threshold(decimal initialDecimalAmount, decimal chargeDecimalAmount, int expectedThreshold)
        {
            InitializeMock();
            var service = CreateService();

            var(account, chargeAmount) = CreateAccountAndAmount(initialDecimalAmount, chargeDecimalAmount, 1, 1);
            AccountBalanceManagementNotificationData actualMailData = null;

            SaveMailData();

            await service.SendNotificationIfRequired(account, chargeAmount);

            Assert.Equal(expectedThreshold, actualMailData.Threshold);


            void SaveMailData()
            => _notificationServiceMock
            .Setup(x => x.Send(It.IsAny <DataWithCompanyInfo>(), It.IsAny <NotificationTypes>(), It.IsAny <string>()))
            .Callback <DataWithCompanyInfo, NotificationTypes, string>((data, _, _)
                                                                       => actualMailData = (AccountBalanceManagementNotificationData)data);
        }
        public async Task SendNotificationIfRequired(AgencyAccount account, MoneyAmount chargedAmount)
        {
            var resultingBalance = account.Balance - chargedAmount.Amount;

            var(_, isFailure, setting, _) = await _balanceNotificationsManagementService.Get(account.Id);

            if (isFailure || !setting.Thresholds.Any(t => account.Balance >= t && resultingBalance < t))
            {
                return;
            }

            var(_, isAgencyFailure, agency, _) = await _adminAgencyManagementService.Get(account.AgencyId);

            if (isAgencyFailure)
            {
                return;
            }

            var lowestThreshold = setting.Thresholds
                                  .Where(t => account.Balance >= t && resultingBalance < t)
                                  .OrderBy(t => t)
                                  .First();

            var messageData = new AccountBalanceManagementNotificationData
            {
                AgencyAccountId = account.Id,
                AgencyId        = agency.Id ?? 0,
                AgencyName      = agency.Name,
                Currency        = EnumFormatters.FromDescription(account.Currency),
                Threshold       = lowestThreshold,
                NewAmount       = MoneyFormatter.ToCurrencyString(resultingBalance, account.Currency)
            };

            await _notificationService.Send(messageData : messageData,
                                            notificationType : NotificationTypes.AccountBalanceManagementNotification,
                                            email : _options.AccountsEmail);
        }