internal void UpdateTemplateSendType(Guid templateId, RefListNotificationType sendType)
 {
     ExecuteNonQuery("update Core_NotificationTemplates set SendTypeLkp = @SendTypeLkp where Id = @Id", command => {
         command.AddParameter("@SendTypeLkp", (int)sendType);
         command.AddParameter("@Id", templateId);
     });
 }
        /// <summary>
        /// Send template-based notifications
        /// </summary>
        public void SendNotifications(List <NotificationMessageDto> notificationMessages,
                                      RefListNotificationType notificationType)
        {
            var messageDtos = notificationMessages.Where(n =>
                                                         n.SendType?.ItemValue == (int)notificationType &&
                                                         n.Status?.ItemValue == (int)RefListNotificationStatus.Preparing &&
                                                         !string.IsNullOrWhiteSpace(n.RecipientText)).ToList();

            if (!messageDtos.Any())
            {
                return;
            }

            using (var uow = UowManager.Begin())
            {
                foreach (var messageDto in messageDtos)
                {
                    var message = NotificationMessageRepository.Get(messageDto.Id);
                    if (message.TenantNotification == null)
                    {
                        throw new Exception($"{message.TenantNotification} must not be null");
                    }

                    var tenantNotification = message.TenantNotification.ToTenantNotification();
                    if (tenantNotification.Data is ShaNotificationData shaNotificationData)
                    {
                        var template = shaNotificationData.TemplateId.HasValue
                            ? TemplateRepository.Get(shaNotificationData.TemplateId.Value)
                            : GetTemplate(message.TenantNotification.NotificationName, notificationType);

                        if (template == null || !template.IsEnabled)
                        {
                            continue;
                        }

                        if (template.SendType != notificationType)
                        {
                            throw new Exception(
                                      $"Wrong type of template. Expected `{notificationType}`, actual `{template.SendType}`");
                        }

                        var notificationData = shaNotificationData.GetData();

                        message.Notification = template.Notification;
                        message.Template     = template;
                        message.Subject      = GenerateContent(template.Subject, notificationData, true, message.SendType == RefListNotificationType.SMS);
                        message.Body         = GenerateContent(template.Body, notificationData,
                                                               template.BodyFormat == RefListNotificationTemplateType.PlainText, message.SendType == RefListNotificationType.SMS);
                        message.Status = RefListNotificationStatus.Outgoing;
                        NotificationMessageRepository.Update(message);

                        // schedule sending
                        SendNotificationInternal(message);
                    }
                }

                uow.Complete();
            }
        }
 private IQueryable <NotificationTemplate> GetTemplateQuery(string notificationName,
                                                            RefListNotificationType sendType)
 {
     return(TemplateRepository.GetAll()
            .Where(t => t.Notification != null &&
                   t.Notification.Name == notificationName &&
                   t.SendType == sendType)
            .OrderByDescending(t => t.CreationTime));
 }
 /// <summary>
 /// Get template by notification name and send type (email/sms etc)
 /// </summary>
 protected NotificationTemplate GetTemplate(string notificationName, RefListNotificationType sendType)
 {
     return(GetTemplateQuery(notificationName, sendType).FirstOrDefault());
 }
 /// <summary>
 /// Get template by notification name and send type (email/sms etc)
 /// </summary>
 protected async Task <NotificationTemplate> GetTemplateAsync(string notificationName,
                                                              RefListNotificationType sendType)
 {
     return(await GetTemplateQuery(notificationName, sendType).FirstOrDefaultAsync());
 }
        /// <summary>
        /// Send template-based notifications
        /// </summary>
        public async Task SendNotificationsAsync(List <NotificationMessageDto> notificationMessages,
                                                 RefListNotificationType notificationType)
        {
            var messageDtos = notificationMessages.Where(n =>
                                                         n.SendType?.ItemValue == (int)notificationType &&
                                                         n.Status?.ItemValue == (int)RefListNotificationStatus.Preparing &&
                                                         !string.IsNullOrWhiteSpace(n.RecipientText)).ToList();

            if (!messageDtos.Any())
            {
                return;
            }

            var messagesToSend = new List <Guid>();

            using (var uow = UowManager.Begin())
            {
                foreach (var messageDto in messageDtos)
                {
                    var message = await NotificationMessageRepository.GetAsync(messageDto.Id);

                    if (message.TenantNotification == null)
                    {
                        throw new Exception($"{message.TenantNotification} must not be null");
                    }

                    var tenantNotification = message.TenantNotification.ToTenantNotification();
                    if (tenantNotification.Data is ShaNotificationData shaNotificationData)
                    {
                        var template = shaNotificationData.TemplateId.HasValue
                            ? await TemplateRepository.GetAsync(shaNotificationData.TemplateId.Value)
                            : await GetTemplateAsync(message.TenantNotification.NotificationName, notificationType);

                        if (template == null || !template.IsEnabled)
                        {
                            continue;
                        }

                        if (template.SendType != notificationType)
                        {
                            throw new Exception(
                                      $"Wrong type of template. Expected `{notificationType}`, actual `{template.SendType}`");
                        }

                        var notificationData = shaNotificationData.GetData();

                        message.Notification = template.Notification;
                        message.Template     = template;
                        message.Subject      = GenerateContent(template.Subject, notificationData, true);
                        message.Body         = GenerateContent(template.Body, notificationData,
                                                               template.BodyFormat == RefListNotificationTemplateType.PlainText);
                        message.Status = RefListNotificationStatus.Outgoing;
                        await NotificationMessageRepository.UpdateAsync(message);

                        messagesToSend.Add(message.Id);
                    }
                }

                // send all prepared messages in background
                UowManager.Current.DoAfterTransaction(() =>
                {
                    foreach (var messageId in messagesToSend)
                    {
                        BackgroundJob.Enqueue(() => SendNotification(messageId));
                    }
                });

                await uow.CompleteAsync();
            }
        }
Exemple #7
0
 public IUpdateNotificationTemplateSyntax SetSendType(RefListNotificationType sendType)
 {
     Expression.Template.SendType.Set(sendType);
     return(this);
 }