Esempio n. 1
0
        public async Task GetNewEmailsByUserIdAsync(int userId)
        {
            var userEmailAccounts = await _emailAccountRepository.GetAllByUserIdWithUserAndImapAsync(userId);

            if (userEmailAccounts != null && userEmailAccounts.Count() > 0)
            {
                foreach (var emailAccount in userEmailAccounts)
                {
                    var emailAccountMessages = await _emailMessageRepository.GetAllForEmailAccountAsync(emailAccount.Id);

                    var emailAccountSendedMessages = await _sendedEmailMessageRepository.GetAllForEmailAccountAsync(emailAccount.Id);

                    if (emailAccountMessages.Count() == 0)
                    {
                        await _emailClientAggregate.AddEmailsFromEmailAccountToEmailMessagesAsync(emailAccount.Email, emailAccount.Password, emailAccount.Imap.Host, emailAccount.Imap.Port, DateTime.UtcNow.AddDays(-14));
                    }
                    if (emailAccountSendedMessages.Count() == 0)
                    {
                        await _emailClientAggregate.AddEmailsFromEmailAccountToEmailMessagesAsync(emailAccount.Email, emailAccount.Password, emailAccount.Imap.Host, emailAccount.Imap.Port, DateTime.UtcNow.AddDays(-14));
                    }
                    var lastDeliveredDate = emailAccountMessages.Select(x => x.DeliveredAt).FirstOrDefault();
                    var lastSentDate      = emailAccountSendedMessages.Select(x => x.SendedAt).FirstOrDefault();
                    await _emailClientAggregate.GetNewEmailsFromEmailAccountAsync(emailAccount.Email, emailAccount.Password, emailAccount.Imap.Host, emailAccount.Imap.Port, lastDeliveredDate, lastSentDate);
                }
            }
        }