|
@@ -178,10 +178,41 @@ public static class EventUtils
|
|
|
NotifySubscribers(store, ev, evData, dataModel);
|
|
|
}
|
|
|
|
|
|
+ public static void SendNotifications(IStore store, IEnumerable<(EventSubscriberType type, Notification notification)> notifications)
|
|
|
+ {
|
|
|
+ var toSave = new List<Notification>();
|
|
|
+ foreach(var (type, notification) in notifications)
|
|
|
+ {
|
|
|
+ if(type == EventSubscriberType.Notification)
|
|
|
+ {
|
|
|
+ toSave.Add(notification);
|
|
|
+ }
|
|
|
+ else if(type == EventSubscriberType.Email)
|
|
|
+ {
|
|
|
+ var mailer = DbFactory.Mailer;
|
|
|
+ if(mailer is not null)
|
|
|
+ {
|
|
|
+ if (mailer.Connect())
|
|
|
+ {
|
|
|
+ var msg = mailer.CreateMessage();
|
|
|
+ // msg.From = DbFactory.EmailAddress;
|
|
|
+ msg.To = [notification.Employee.Email];
|
|
|
+ msg.Subject = notification.Title;
|
|
|
+ msg.Body = notification.Description;
|
|
|
+ var bOK = mailer.SendMessage(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ store.Provider.Save(toSave);
|
|
|
+ }
|
|
|
+
|
|
|
private static void NotifySubscribers<T, TDataModel>(IStore store, Event ev, EventData<T, TDataModel> evData, TDataModel dataModel)
|
|
|
where T : IEvent<TDataModel>
|
|
|
where TDataModel : IEventDataModel
|
|
|
{
|
|
|
+ if (!ev.NotificationsEnabled) return;
|
|
|
+
|
|
|
string? description;
|
|
|
if (ev.NotificationExpression.IsNullOrWhiteSpace())
|
|
|
{
|
|
@@ -202,37 +233,16 @@ public static class EventUtils
|
|
|
Columns.None<EventSubscriber>().Add(x => x.Employee.ID).Add(x => x.Employee.Email).Add(x => x.SubscriberType))
|
|
|
.ToArray<EventSubscriber>();
|
|
|
|
|
|
- var notifications = new List<Notification>();
|
|
|
- foreach(var subscriber in subscribers)
|
|
|
+ SendNotifications(store, subscribers.Select(x =>
|
|
|
{
|
|
|
var notification = evData.GenerateNotification(dataModel);
|
|
|
- notification.Employee.CopyFrom(subscriber.Employee);
|
|
|
- if(description is not null)
|
|
|
+ notification.Employee.CopyFrom(x.Employee);
|
|
|
+ if (description is not null)
|
|
|
{
|
|
|
notification.Description = description;
|
|
|
}
|
|
|
- if(subscriber.SubscriberType == EventSubscriberType.Notification)
|
|
|
- {
|
|
|
- notifications.Add(notification);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- var mailer = DbFactory.Mailer;
|
|
|
- if(mailer is not null)
|
|
|
- {
|
|
|
- if (mailer.Connect())
|
|
|
- {
|
|
|
- var msg = mailer.CreateMessage();
|
|
|
- // msg.From = DbFactory.EmailAddress;
|
|
|
- msg.To = [subscriber.Employee.Email];
|
|
|
- msg.Subject = notification.Title;
|
|
|
- msg.Body = notification.Description;
|
|
|
- var bOK = mailer.SendMessage(msg);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- store.Provider.Save(notifications);
|
|
|
+ return (x.SubscriberType, notification);
|
|
|
+ }));
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
@@ -268,7 +278,7 @@ public static class EventUtils
|
|
|
_genericEventMap.Clear();
|
|
|
var events = provider.Query(
|
|
|
new Filter<Event>(x => x.Enabled).IsEqualTo(true),
|
|
|
- Columns.None<Event>().Add(x => x.ID).Add(x => x.Code).Add(x => x.EventType).Add(x => x.Data).Add(x => x.NotificationExpression))
|
|
|
+ Columns.None<Event>().Add(x => x.ID).Add(x => x.Code).Add(x => x.EventType).Add(x => x.Data).Add(x => x.NotificationExpression).Add(x => x.NotificationsEnabled))
|
|
|
.ToObjects<Event>();
|
|
|
foreach(var ev in events)
|
|
|
{
|