public async Task RegisterNewSessionAsync(AuthorizationToken token, RefreshToken refreshToken, string userAgent, string ip, string description, PlatformTypes platformType, LoginApplication application)
        {
            if (!token.RootSessionId.HasValue)
            {
                throw new Exception("Cannot RegisterNewSession without RootSessionId");
            }

            var pkBase64 = token.PublicKey != null?Convert.ToBase64String(token.PublicKey) : "";

            var session = RootSessionNoSqlEntity.Generate(token.RootSessionId.Value, token.TraderId(), token.BrandId, userAgent, ip, pkBase64,
                                                          token.Passed2FA, token.EmailVerified, description, platformType, application);

            session.Expires = refreshToken.Expires;

            var task1 = _sessionWriter.InsertOrReplaceAsync(session).AsTask();

            var shortSession = ShortRootSessionNoSqlEntity.Create(session);

            shortSession.Expires = refreshToken.Expires;
            var task2 = _rootSessionWriter.InsertOrReplaceAsync(shortSession).AsTask();

            var task3 = _rootSessionWriter.GetCountAsync(
                ShortRootSessionNoSqlEntity.GeneratePartitionKey(token.TraderId())).AsTask();
            await Task.WhenAll(task1, task2, task3);

            if (task3.Result > _maxSessionsCount)
            {
                var sessions = await _sessionWriter.GetAsync(
                    ShortRootSessionNoSqlEntity.GeneratePartitionKey(token.TraderId()));

                var oldestSession = sessions.OrderBy(s => s.CreateTime).First();
                await Logout(token.TraderId(), oldestSession.RootSessionId);
            }
        }
        private async Task DoTime()
        {
            var maxCount = Program.ReloadedSettings(e => e.PreGeneratedAddressesCount).Invoke();
            var wallets  = await _bitGoAssetMapSettingsService.GetAllAssetMapsAsync();

            foreach (var wallet in wallets)
            {
                var assetIdentity = new AssetIdentity
                {
                    BrokerId = wallet.BrokerId,
                    Symbol   = wallet.AssetSymbol
                };

                var paymentSettings = _assetPaymentSettingsClient.GetAssetById(assetIdentity);
                if (paymentSettings?.BitGoCrypto?.IsEnabledDeposit != true)
                {
                    continue;
                }

                var asset = _assetsDictionaryClient.GetAssetById(assetIdentity);

                if (asset == null || !asset.IsEnabled)
                {
                    continue;
                }

                var blockchain = asset.DepositBlockchains.FirstOrDefault();
                if (string.IsNullOrEmpty(blockchain))
                {
                    continue;
                }

                var entitiesCount = await _dataWriter.GetCountAsync(
                    GeneratedDepositAddressEntity.GeneratePartitionKey(wallet.BrokerId, wallet.BitgoCoin,
                                                                       wallet.BitgoWalletId, blockchain));

                if (entitiesCount < maxCount)
                {
                    for (var i = 1; i <= maxCount - entitiesCount; i++)
                    {
                        try
                        {
                            var id    = Guid.NewGuid().ToString();
                            var label = $"PreGenerated-{id}";
                            var(addressId, address, error) =
                                await _depositAddressGeneratorService.GenerateOrGetAddressIdAsync(wallet.BitgoCoin,
                                                                                                  wallet.BitgoWalletId, label);

                            if (string.IsNullOrEmpty(addressId) || error != null)
                            {
                                _logger.LogError(
                                    "Unable to pre-generate address for broker {broker}, asset {asset}, wallet id {walletId}: {error}",
                                    wallet.BrokerId, wallet.BitgoCoin, wallet.BitgoWalletId, error);
                                continue;
                            }

                            await _dataWriter.InsertAsync(GeneratedDepositAddressEntity.Create(
                                                              new GeneratedDepositAddress
                            {
                                BrokerId             = wallet.BrokerId,
                                Asset                = wallet.BitgoCoin,
                                WalletId             = wallet.BitgoWalletId,
                                PreGeneratedWalletId = id,
                                AddressLabel         = label,
                                Address              = address,
                                BitGoAddressId       = addressId,
                                Blockchain           = blockchain
                            }));
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex,
                                             "Unable to pre-generate address for broker {broker}, asset {asset}, wallet id {walletId}, blockchain {blockchain}",
                                             wallet.BrokerId, wallet.BitgoCoin, wallet.BitgoWalletId, blockchain);
                        }
                    }

                    _logger.LogInformation(
                        "Pre-generated {count} addresses for broker {broker}, asset {asset}, wallet id {walletId}, blockchain {blockchain}",
                        maxCount - entitiesCount, wallet.BrokerId, wallet.BitgoCoin, wallet.BitgoWalletId, blockchain);
                }
            }
        }