/// <summary> /// Creates a notification. /// </summary> /// <param name="users">The users.</param> /// <param name="userTriggerEvent">The user trigger event.</param> /// <param name="type">The type.</param> /// <param name="targetUrl">The target URL.</param> /// <param name="parameters">The parameters.</param> /// <param name="defaultFromName">The default from name.</param> /// <param name="defaultSubject">The default subject.</param> /// <param name="defaultMessage">The default message.</param> /// <returns> /// the task /// </returns> public async Task NewNotification( IList <User> users, User userTriggerEvent, NotificationType type, string targetUrl, IList <NotificationParameter> parameters, string defaultFromName, string defaultSubject, string defaultMessage) { var notificationId = Convert.ToInt32(type); var notification = this.GetCachedNotifications() .FirstOrDefault(n => n.Id == notificationId); var settings = new Beto.Core.Data.Notifications.NotificationSettings() { BaseHtml = this.generalSettings.BodyBaseHtml, DefaultFromName = defaultFromName, DefaultMessage = defaultMessage, DefaultSubject = defaultSubject, IsManual = false, SiteUrl = this.generalSettings.SiteUrl }; await this.coreNotificationService.NewNotification <SystemNotification, EmailNotification>( users.Select(c => (IUserEntity)c).ToList(), userTriggerEvent, notification, targetUrl, parameters, settings); }
public async Task NewEmailNotification <TEmailNotification>( IList <IUserEntity> users, IUserEntity userTriggerEvent, INotificationEntity notification, string targetUrl, IList <NotificationParameter> parameters, NotificationSettings settings) where TEmailNotification : class, IEmailNotificationEntity, new() { await this.NewNotification <DefaultSystemNotification, TEmailNotification, DefaultUnsubscriber>(users, userTriggerEvent, notification, targetUrl, parameters, settings); }
public async Task NewNotification( IList <User> users, User userTriggerEvent, NotificationType type, string targetUrl, IList <NotificationParameter> parameters, string defaultFromName, string defaultSubject, string defaultMessage) { var notificationId = Convert.ToInt32(type); var notification = this.GetCachedNotifications() .FirstOrDefault(n => n.Id == notificationId); var settings = new Beto.Core.Data.Notifications.NotificationSettings() { BaseHtml = this.configuration["BaseHtmlBody"], DefaultFromName = defaultFromName, DefaultMessage = defaultMessage, DefaultSubject = defaultSubject, IsManual = false, SiteUrl = this.configuration["SiteUrl"] }; try { await this.coreNotificationService.NewNotification <MockSystemNotification, EmailNotification, DefaultMobileNotification, DefaultUnsubscriber>( users.Select(c => (IUserEntity)c).ToList(), userTriggerEvent, notification, targetUrl, parameters, settings); } catch { Console.WriteLine($"-----------------------------------------------> Error saving notification {type}"); } }
public async Task NewNotification <TSystemNotification, TEmailNotification, TMobileNotification, TUnsubscriber>(IList <IUserEntity> users, IUserEntity userTriggerEvent, INotificationEntity notification, string targetUrl, IList <NotificationParameter> parameters, NotificationSettings settings) where TSystemNotification : class, ISystemNotificationEntity, new() where TEmailNotification : class, IEmailNotificationEntity, new() where TMobileNotification : class, IMobileNotificationEntity, new() where TUnsubscriber : class, IUnsubscriberEntity, new() { await this.SaveNewNotification <TSystemNotification, TEmailNotification, TMobileNotification, TUnsubscriber>(users, userTriggerEvent, notification, targetUrl, parameters, settings); }
private async Task SaveNewNotification <TSystemNotification, TEmailNotification, TMobileNotification, TUnsubscriber>( IList <IUserEntity> users, IUserEntity userTriggerEvent, INotificationEntity notification, string targetUrl, IList <NotificationParameter> parameters, NotificationSettings settings) where TSystemNotification : class, ISystemNotificationEntity, new() where TEmailNotification : class, IEmailNotificationEntity, new() where TMobileNotification : class, IMobileNotificationEntity, new() where TUnsubscriber : class, IUnsubscriberEntity, new() { ////En los casos manuales no las busca, sino que quedan quemadas ////var notification = type != NotificationType.Manual ? this.GetCachedNotification(type) : new Notification() { Active = true, IsEmail = true }; if (parameters == null) { parameters = new List <NotificationParameter>(); } if (notification.Active) { ////Listado de usuarios a los que no se les envía la notificación var usersNotSend = new List <int>(); ////Se agrega la raiz del sitio parameters.Add("RootUrl", settings.SiteUrl); ////Asigna por defecto el parametro url el target url if (!string.IsNullOrEmpty(targetUrl) && !parameters.Any(c => c.Key.Equals("Url"))) { if (targetUrl.StartsWith("/")) { string.Concat(settings.SiteUrl, targetUrl); } parameters.Add("Url", targetUrl); } /////TODO: Agregar antes <<<<<<<<----------------------------------------------------------- /////parameters.Add("FacebookUrl", this.generalSettings.FacebookUrl); /////parameters.Add("InstagramUrl", this.generalSettings.InstagramUrl); if (userTriggerEvent != null) { parameters.AddOrReplace("TriggerUser.Name", userTriggerEvent.Name); parameters.AddOrReplace("TriggerUser.Email", userTriggerEvent.Email); } var systemNotificationsToInsert = new List <TSystemNotification>(); var emailNotificationsToInsert = new List <TEmailNotification>(); var mobileNotificationsToInsert = new List <TMobileNotification>(); int[] emailUnsubscribers = null; int[] mobileUnsubscribers = null; if (!typeof(TUnsubscriber).Equals(typeof(DefaultUnsubscriber))) { emailUnsubscribers = this.GetEmailUnsubscriberByNotificationId <TUnsubscriber>(notification.Id); mobileUnsubscribers = this.GetMobileUnsubscriberByNotificationId <TUnsubscriber>(notification.Id); } ////Recorre los usuarios a los que debe realizar la notificación foreach (var user in users) { parameters.AddOrReplace("NotifiedUser.Name", user.Name); parameters.AddOrReplace("NotifiedUser.Email", user.Email); ////Si la notificación es del sistema la envia if (notification.IsSystem) { var systemNotification = new TSystemNotification(); systemNotification.UserId = user.Id; systemNotification.Value = this.GetStringFormatted(notification.SystemText, parameters); systemNotification.TargetUrl = targetUrl; systemNotification.CreationDate = DateTime.UtcNow; systemNotification.Seen = false; ////Inserta la notificación de este tipo systemNotificationsToInsert.Add(systemNotification); } if (notification.IsMobile && (user.DeviceId.HasValue || user.IOsDeviceId.HasValue) && (mobileUnsubscribers == null || !mobileUnsubscribers.Contains(user.Id))) { Func <TMobileNotification> getMobileNotification = () => { var mobileNotification = new TMobileNotification(); mobileNotification.UserId = user.Id; mobileNotification.Subject = this.GetStringFormatted(notification.MobileText, parameters); mobileNotification.TargetUrl = targetUrl; mobileNotification.CreatedDate = DateTime.UtcNow; mobileNotification.MessageHash = StringHelpers.ToMd5(mobileNotification.Subject); return(mobileNotification); }; // Notification for Android if (user.DeviceId.HasValue) { var mobileNotification = getMobileNotification(); mobileNotification.DeviceId = user.DeviceId.Value; mobileNotification.IsAndroid = true; mobileNotificationsToInsert.Add(mobileNotification); } // Notification for iOS if (user.IOsDeviceId.HasValue) { var mobileNotification = getMobileNotification(); mobileNotification.DeviceId = user.IOsDeviceId.Value; mobileNotificationsToInsert.Add(mobileNotification); } } if (notification.IsEmail && !string.IsNullOrWhiteSpace(user.Email) && (emailUnsubscribers == null || !emailUnsubscribers.Contains(user.Id))) { var emailNotification = this.GetEmailNotificationToAdd <TEmailNotification>( notification, user, parameters, settings.DefaultFromName, settings.DefaultSubject, settings.DefaultMessage, settings.SiteUrl, settings.BaseHtml, settings.IsManual); emailNotificationsToInsert.Add(emailNotification); } } if (emailNotificationsToInsert.Count > 0) { var emailNotificationRepository = this.context.Set <TEmailNotification>(); foreach (var entity in emailNotificationsToInsert) { emailNotificationRepository.Add(entity); } await this.context.SaveChangesAsync(); } if (systemNotificationsToInsert.Count > 0) { var systemNotificationRepository = this.context.Set <TSystemNotification>(); foreach (var entity in systemNotificationsToInsert) { systemNotificationRepository.Add(entity); } await this.context.SaveChangesAsync(); await this.publisher.EntitiesInserted(systemNotificationsToInsert); } if (mobileNotificationsToInsert.Count > 0) { var systemNotificationRepository = this.context.Set <TMobileNotification>(); foreach (var entity in mobileNotificationsToInsert) { systemNotificationRepository.Add(entity); } await this.context.SaveChangesAsync(); await this.publisher.EntitiesInserted(mobileNotificationsToInsert); } } }