public async Task Process(BatchProcess data)
        {
            var flowId      = HttpContext.Items["flowId"].ToString();
            var messages    = data.Messages.Select(m => mapper.Map <Message>(m)).ToArray();
            var transfers   = data.Transfers.Select(t => mapper.Map <Transfer>(t)).ToArray();;
            var instalments = data.RepaidInstalmentsIds;

            var parallelTasks = new List <Task>();

            if (messages.Length > 0)
            {
                parallelTasks.Add(Task.Run(async() =>
                {
                    await usersClient.BatchAddMessagesAsync(new BatchAddMessagesRequest {
                        Messages = { messages }
                    }, HttpContext.CreateHeadersWithFlowId());
                }));
            }

            if (transfers.Length > 0)
            {
                parallelTasks.Add(Task.Run(async() =>
                {
                    await accountsWriteClient.BatchTransferAsync(new BatchTransferRequest {
                        Transfers = { transfers }
                    }, HttpContext.CreateHeadersWithFlowId());
                }));
            }

            if (data.ProcessedPaymentsIds.Length > 0)
            {
                parallelTasks.Add(Task.Run(async() =>
                {
                    var request = new UpdateLatestProcessingTimestampRequest {
                        Ids = { data.ProcessedPaymentsIds }, LatestProcessingTimestamp = data.ProcessingTimestamp.ToNullableTimestamp()
                    };
                    await paymentsWriteClient.UpdateLatestProcessingTimestampAsync(request, HttpContext.CreateHeadersWithFlowId());
                }));
            }

            if (instalments.Length > 0)
            {
                parallelTasks.Add(Task.Run(async() =>
                {
                    await loansWriteClient.BatchRepayInstalmentsAsync(new BatchRepayInstalmentsRequest {
                        Ids = { instalments }
                    }, HttpContext.CreateHeadersWithFlowId());
                }));
            }

            if (parallelTasks.Count > 0)
            {
                await Task.WhenAll(parallelTasks);
            }
        }
        public override async Task <Empty> Process(ProcessBatchRequest request, ServerCallContext context)
        {
            var tasks = new List <Task>();

            if (request.Messages.Count > 0)
            {
                tasks.Add(Task.Run(async() => await usersClient.BatchAddMessagesAsync(new BatchAddMessagesRequest
                {
                    Messages = { request.Messages }
                }, context.RequestHeaders.SelectCustom())));
            }


            if (request.Transfers.Count > 0)
            {
                tasks.Add(Task.Run(async() => await accountsClient.BatchTransferAsync(new BatchTransferRequest
                {
                    Transfers = { request.Transfers }
                }, context.RequestHeaders.SelectCustom())));
            }

            if (request.ProcessedPaymentsIds.Count > 0)
            {
                tasks.Add(Task.Run(async() => await paymentsClient.UpdateLatestProcessingTimestampAsync(new UpdateLatestProcessingTimestampRequest
                {
                    Ids = { request.ProcessedPaymentsIds },
                    LatestProcessingTimestamp = request.ProcessingTimestamp
                }, context.RequestHeaders.SelectCustom())));
            }

            if (request.RepaidInstalmentsIds.Count > 0)
            {
                tasks.Add(Task.Run(async() => await loansClient.BatchRepayInstalmentsAsync(new BatchRepayInstalmentsRequest
                {
                    Ids = { request.RepaidInstalmentsIds }
                }, context.RequestHeaders.SelectCustom())));
            }


            if (tasks.Count > 0)
            {
                await Task.WhenAll(tasks.ToArray());
            }

            return(new Empty());
        }