public async Task <ActionResult <NotificationDto> > SendEmail(
            EmailNotificationDto dto,
            CancellationToken cancellationToken)
        {
            EmailNotification emailNotification;
            var eventFilter = await GetEventParticipantFilterAsync(dto, cancellationToken);

            if (eventFilter != null)
            {
                emailNotification = await _notificationManagementService
                                    .CreateEmailNotificationForEventAsync(
                    dto.Subject,
                    dto.BodyMarkdown,
                    eventFilter.EventId.Value,
                    eventFilter.ProductId,
                    eventFilter.RegistrationStatuses,
                    eventFilter.RegistrationTypes);
            }
            else
            {
                emailNotification = await _notificationManagementService
                                    .CreateEmailNotificationAsync(
                    dto.Subject,
                    dto.BodyMarkdown,
                    dto.Recipients);
            }

            // send notification right away, not queueing it or something.
            // TODO: use queue for messages

            await _notificationDeliveryService
            .SendNotificationAsync(emailNotification, cancellationToken);

            return(Ok(new NotificationDto(emailNotification)));
        }
Exemple #2
0
        public async Task <ActionResult <NotificationResponseDto> > SendEmail(EmailNotificationDto dto, CancellationToken cancellationToken)
        {
            var email = new EmailNotification
            {
                Subject      = dto.Subject,
                BodyMarkdown = dto.BodyMarkdown
            };

            if (dto.Recipients?.Any() != true && dto.Filter?.IsDefined != true)
            {
                return(BadRequest("Either recipient list of registrant filter must be specified"));
            }

            var recipients = dto.Recipients ?? Array.Empty <string>();

            if (dto.Filter?.IsDefined == true)
            {
                var reader = new PageReader <Registration>(async(offset, limit, token) =>
                                                           await _registrationRetrievalService.ListRegistrationsAsync(
                                                               new RegistrationListRequest
                {
                    Limit  = limit,
                    Offset = offset,
                    Filter = new RegistrationFilter
                    {
                        EventInfoId              = dto.Filter.EventId,
                        AccessibleOnly           = true,
                        ActiveUsersOnly          = true,
                        HavingEmailConfirmedOnly = true,
                        HavingStatuses           = dto.Filter.RegistrationStatuses,
                        HavingTypes              = dto.Filter.RegistrationTypes
                    }
                },
                                                               new RegistrationRetrievalOptions
                {
                    IncludeUser = true
                }, token));

                var recipientList = new List <string>();

                while (await reader.HasMoreAsync(cancellationToken))
                {
                    recipientList.AddRange(from registration in await reader
                                           .ReadNextAsync(cancellationToken)
                                           let userName = registration.User?.Name
                                                          let userEmail = registration.User?.Email
                                                                          where !string.IsNullOrEmpty(userEmail)
                                                                          select $"{userName} <{userEmail}>");
                }

                recipients = recipientList
                             .Distinct()
                             .ToArray();
            }

            if (recipients.Any())
            {
                await _emailNotificationService.SendEmailToRecipientsAsync(email, recipients, cancellationToken);
            }
            else
            {
                _logger.LogWarning("No recipients were selected by the notification filter criteria");
            }

            return(Ok(new NotificationResponseDto
            {
                TotalRecipients = recipients.Length
            }));
        }