Example #1
0
        protected async Task <int> SendNotifications(User user, IEnumerable <Event> eventsToNotifyUserFor, CancellationToken cancellationToken)
        {
            // Populate email
            if (eventsToNotifyUserFor.Any())
            {
                // Update the database first so that a user is not notified multiple times
                foreach (var mobEvent in eventsToNotifyUserFor)
                {
                    var userNotification = new UserNotification
                    {
                        Id       = Guid.NewGuid(),
                        EventId  = mobEvent.Id,
                        UserId   = user.Id,
                        SentDate = DateTimeOffset.UtcNow,
                        UserNotificationTypeId = (int)NotificationType,
                    };

                    await UserNotificationRepository.AddUserNotification(userNotification).ConfigureAwait(false);
                }

                var emailTemplate = GetEmailTemplate();
                var content       = await PopulateTemplate(emailTemplate, user, eventsToNotifyUserFor).ConfigureAwait(false);

                var htmlEmailTemplate = GetHtmlEmailTemplate();
                var htmlContent       = await PopulateTemplate(htmlEmailTemplate, user, eventsToNotifyUserFor).ConfigureAwait(false);

                var email = new Email();
                email.Addresses.Add(new EmailAddress()
                {
                    Email = user.Email, Name = $"{user.GivenName} {user.SurName}"
                });
                email.Subject     = EmailSubject;
                email.Message     = content;
                email.HtmlMessage = htmlContent;

                Logger.LogInformation("Sending email to {0}, Subject {0}", email.Addresses[0].Email, email.Subject);

                // send email
                await EmailSender.SendEmailAsync(email, cancellationToken).ConfigureAwait(false);

                return(1);
            }

            return(0);
        }