public async Task SendAsync(SendNotificationAdto sendNotificationAdto)
        {
            using (ITransaction transaction = _transactionManager.Create())
            {
                try
                {
                    NotificationType notificationType = await
                                                        _queryRepository.GetSingleAsync(n => n.Type == sendNotificationAdto.NotificationType);

                    if (notificationType != null)
                    {
                        IEnumerable <NotificationTypeChannel> notificationTypeNotificationTypeChannels = notificationType.NotificationTypeChannels;

                        foreach (NotificationTypeChannel notificationTypeChannel in notificationTypeNotificationTypeChannels)
                        {
                            IChannelAudienceResolver channelAudienceResolver =
                                _channelAudienceResolverProvider.GetByType(notificationTypeChannel.ChannelType,
                                                                           sendNotificationAdto.NotificationType);

                            IEnumerable <string> emailAddresses = await channelAudienceResolver.GetAudienceAsync(sendNotificationAdto.IdentityId);

                            switch (notificationTypeChannel.ChannelType)
                            {
                            case ChannelType.Email:
                                foreach (string emailAddress in emailAddresses)
                                {
                                    if (!(notificationTypeChannel.NotificationChannelTemplate is EmailChannelTemplate emailChannelTemplate))
                                    {
                                        throw new NullReferenceException("No template defined for channel");
                                    }

                                    EmailAdto emailAdto = _emailBuilder.Build(new BuildEmailAdto
                                    {
                                        EmailTemplate = emailChannelTemplate.Template,
                                        Subject       = emailChannelTemplate.Subject,
                                        PropertyBag   = sendNotificationAdto.PropertyBag
                                    });

                                    await Message.SendAsync(SendEmailNotificationMessage.Create("*****@*****.**", emailAddress, emailAdto.Subject, emailAdto.HtmlBody));
                                }

                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                    }

                    transaction.Commit();
                }
                catch (BusinessApplicationException e)
                {
                    _logger.LogDebug(e.Message);
                }
            }
        }
Ejemplo n.º 2
0
        public IChannelAudienceResolver GetByType(ChannelType channelType, string notificationType)
        {
            try
            {
                IChannelAudienceResolver channelAudienceResolver = _channelAudienceResolvers.SingleOrDefault(r =>
                                                                                                             r.ChannelType == channelType && r.NotificationTypes.Any(n => n == notificationType));

                if (channelAudienceResolver == null)
                {
                    throw new BusinessApplicationException(ExceptionType.Unknown, $"No audience resolver defined for Channel: {channelType} and NotificationType: {notificationType}");
                }

                return(channelAudienceResolver);
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e);
                throw;
            }
        }