Пример #1
0
        public async Task <bool> CreateOrUpdate(User user, MailingSettings settings)
        {
            var existingSettings = await Get(user);

            if (existingSettings == null)
            {
                existingSettings = new MailingSettings
                {
                    UserId           = user.Id,
                    NumberOfArticles = settings.NumberOfArticles,
                    Schedule         = settings.Schedule,
                    UpdatedAt        = DateTime.UtcNow,
                };
                _db.MailingSettings.Add(existingSettings);
            }
            else
            {
                existingSettings.NumberOfArticles = settings.NumberOfArticles;
                existingSettings.Schedule         = settings.Schedule;
                existingSettings.UpdatedAt        = DateTime.UtcNow;
                _db.MailingSettings.Update(existingSettings);
            }

            return(await _db.SaveChangesAsync() > 0);
        }
Пример #2
0
        private async Task SendEmail(IServiceScope serviceScope, MailingSettings settings)
        {
            var articleService = serviceScope.ServiceProvider.GetRequiredService <ArticleService>();
            var articles       = await articleService.GetForSending(settings.User, settings.NumberOfArticles);

            if (articles.Count == 0)
            {
                _logger.LogInformation("No articles to send for {email}", settings.User.Email);
                return;
            }

            var usersService     = serviceScope.ServiceProvider.GetRequiredService <UsersService>();
            var unsubscribeToken = usersService.GetUnsubscribeTokenByUser(settings.User);

            await _mailService.SendArticleList(settings.User, unsubscribeToken, articles, _cronService.ExpressionToSchedule(settings.Schedule));

            await articleService.MarkAsSent(articles, settings.User);
        }
Пример #3
0
        private bool IsApplicable(MailingSettings settings, DateTime lastMailSentAt, TimeSpan schedulePeriod)
        {
            var dateFrom = DateTime.SpecifyKind(lastMailSentAt, DateTimeKind.Utc);

            return(_cronService.HasTimeCome(dateFrom, schedulePeriod, settings.Schedule));
        }