Ejemplo n.º 1
0
        public async Task <IEnumerable <Email> > GetEmails()
        {
            var transferRequests = await _commitmentRepository.GetPendingTransferRequests();

            _logger.Info($"Found {transferRequests.Count} pending transfer requests");

            var distinctEmployerAccountIds = transferRequests.Select(x => x.SendingEmployerAccountId)
                                             .Distinct()
                                             .ToList();

            var distinctAccountsTasks =
                distinctEmployerAccountIds
                .Select(x => _retryPolicy.ExecuteAndCaptureAsync(() => _accountApi.GetAccount(x)))
                .ToList();

            var userPerAccountTasks =
                distinctEmployerAccountIds
                .Select(ToUserModel)
                .ToList();

            await Task.WhenAll(distinctAccountsTasks);

            await Task.WhenAll(userPerAccountTasks);

            var accounts = distinctAccountsTasks
                           .Select(x => x.Result)
                           .Where(x => x.Outcome == OutcomeType.Successful)
                           .Select(x => x.Result).ToList();

            // Only accountIds where user in DB
            var accountIds = accounts.Select(m => m.AccountId);

            var accountsWithUsers = userPerAccountTasks
                                    .Select(x => x.Result)
                                    .Where(u => u.Users != null && accountIds.Contains(u.AccountId))
                                    .ToList();

            return(transferRequests.SelectMany(transferRequest =>
            {
                var account = accounts.Single(a => a.AccountId == transferRequest.SendingEmployerAccountId);

                var users = accountsWithUsers.Where(a => a.AccountId == account.AccountId)
                            .SelectMany(a => a.Users)
                            .Where(u => u.CanReceiveNotifications &&
                                   (u.Role == "Owner" || u.Role == "Transactor"));

                return (users.Select(userModel =>
                                     MapToEmail(userModel, transferRequest, account.HashedAccountId, account.DasAccountName)));
            }));
        }
Ejemplo n.º 2
0
 public Task <IClientConnection> Connect(Guid connectionId)
 {
     return(_connections.GetOrAdd(connectionId, async id =>
     {
         var hubConnection = new HubConnection(_options.ConnectionUri)
         {
             TraceLevel = TraceLevels.All,
             TraceWriter = new SerilogTextWriter(),
             ConnectionId = connectionId.ToString()
         };
         var gameHub = hubConnection.CreateHubProxy("GameHub");
         try
         {
             if (_options.IsDebug)
             {
                 await hubConnection.Start();
             }
             else
             {
                 var result = await _connectionPolicy.ExecuteAndCaptureAsync(hubConnection.Start);
                 if (result.Outcome == OutcomeType.Failure)
                 {
                     throw result.FinalException;
                 }
             }
         }
         catch (Exception ex)
         {
             throw new ConnectionException($"Failed to connect to the API: {ex.Message}", ex);
         }
         var connection = new SignalRClientConnection(hubConnection, gameHub);
         return connection;
     }));
 }
Ejemplo n.º 3
0
        private async Task <IEnumerable <ProviderUser> > GetProvider(long ukprn)
        {
            var users = await _emailService.GetUsersAsync(ukprn);

            if (users == null)
            {
                _logger.Warn($"Can't find and user for provider {ukprn}.");
                return(new List <ProviderUser>());
            }

            var accountUserResult = (await _retryPolicy.ExecuteAndCaptureAsync(() => _providerAccountClient.GetAccountUsers(ukprn)));
            var accountUser       = accountUserResult.Result?.ToArray();

            foreach (var user in users)
            {
                var u = accountUser
                        ?.FirstOrDefault(m => user.Email.Trim().ToLower() == m.EmailAddress.Trim()
                                         ?.ToLower());

                user.ReceiveNotifications = u == null || u.ReceiveNotifications;
                user.Ukprn = ukprn;
            }

            return(users);
        }