public async Task<IEnumerable<AccountDTO>> Get(string userId)
 {
     var request = new GetUserAccountsRequest { UserId = userId };
     var response = await accountsReadClient.GetUserAccountsAsync(request, HttpContext.CreateHeadersWithFlowId());
     var accounts = response.Accounts.Select(a => mapper.Map<AccountDTO>(a));
     return accounts;
 }
        public async Task <ReportPortions> GetUserActivityPortions(Metadata headers, string userId, Timestamp from, Timestamp to, Granularity granularity)
        {
            var portions = new ReportPortions();

            var accountsResponse = await accountsClient.GetUserAccountsAsync(new GetUserAccountsRequest { UserId = userId }, headers);

            var accountsIds = accountsResponse.Accounts.Select(a => a.Id).ToArray();

            var parallelTasks = new List <Task>();

            parallelTasks.Add(Task.Run(async() =>
            {
                var request = new AccountsReadMicroservice.AggregateUserActivityRequest {
                    UserId = userId, TimestampFrom = from, TimestampTo = to, Granularity = granularity
                };
                var response      = await accountsClient.AggregateUserActivityAsync(request, headers);
                portions.Accounts = response.Portions.ToArray();
            }));
            parallelTasks.Add(Task.Run(async() =>
            {
                var request = new PaymentsReadMicroservice.AggregateUserActivityRequest {
                    AccountsIds = { accountsIds }, TimestampFrom = from, TimestampTo = to, Granularity = granularity
                };
                var response      = await paymentsClient.AggregateUserActivityAsync(request, headers);
                portions.Payments = response.Portions.ToArray();
            }));
            parallelTasks.Add(Task.Run(async() =>
            {
                var request = new LoansReadMicroservice.AggregateUserActivityRequest {
                    AccountsIds = { accountsIds }, TimestampFrom = from, TimestampTo = to, Granularity = granularity
                };
                var response   = await loansClient.AggregateUserActivityAsync(request, headers);
                portions.Loans = response.Portions.ToArray();
            }));
            parallelTasks.Add(Task.Run(async() =>
            {
                var request = new CardsReadMicroservice.AggregateUserActivityRequest {
                    AccountsIds = { accountsIds }, TimestampFrom = from, TimestampTo = to, Granularity = granularity
                };
                var response   = await cardsClient.AggregateUserActivityAsync(request, headers);
                portions.Cards = response.Portions.ToArray();
            }));

            await Task.WhenAll(parallelTasks);

            return(portions);
        }
        public async Task <Panel> Panel(string userId)
        {
            var headers = HttpContext.CreateHeadersWithFlowId();

            RepeatedField <Loan>        loans        = new RepeatedField <Loan>();
            RepeatedField <Payment>     payments     = new RepeatedField <Payment>();
            RepeatedField <Account>     accounts     = new RepeatedField <Account>();
            RepeatedField <Card>        cards        = new RepeatedField <Card>();
            RepeatedField <Transaction> transactions = new RepeatedField <Transaction>();

            var accountsResponse = await accountsReadClient.GetUserAccountsAsync(new GetUserAccountsRequest { UserId = userId }, headers);

            var accountsIds = accountsResponse.Accounts.Select(a => a.Id).ToArray();

            accounts = accountsResponse.Accounts;

            if (accounts.Any())
            {
                var parallelTasks = new List <Task>();
                parallelTasks.Add(Task.Run(async() =>
                {
                    var transactionsResponse = await transactionsReadClient.FilterAsync(new FilterTransactionsRequest {
                        Senders = { accountsIds }, Top = PanelTransactionsCount
                    }, headers);
                    transactions = transactionsResponse.Transactions;
                }));

                parallelTasks.Add(Task.Run(async() =>
                {
                    var paymentsResponse = await paymentsReadClient.GetByAccountsAsync(new GetPaymentsRequest {
                        Ids = { accountsIds }
                    }, headers);
                    payments = paymentsResponse.Payments;
                }));

                parallelTasks.Add(Task.Run(async() =>
                {
                    var loansResponse = await loansReadClient.GetByAccountsAsync(new GetLoansRequest {
                        Ids = { accountsIds }
                    }, headers);
                    loans = loansResponse.Loans;
                }));

                parallelTasks.Add(Task.Run(async() =>
                {
                    var cardsResponse = await cardsReadClient.GetByAccountsAsync(new GetCardsRequest {
                        Ids = { accountsIds }
                    }, headers);
                    cards = cardsResponse.Cards;
                }));


                await Task.WhenAll(parallelTasks);
            }

            return(new Panel
            {
                Accounts = mapper.Map <AccountDTO[]>(accounts),
                Loans = mapper.Map <LoanDTO[]>(loans),
                Payments = mapper.Map <PaymentDTO[]>(payments),
                Transactions = mapper.Map <TransactionDTO[]>(transactions),
                Cards = mapper.Map <CardDTO[]>(cards),
            });
        }