123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- using InABox.Core;
- using InABox.Database;
- using PRS.Shared.Events;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace PRSStores.Events;
- [Caption("Send Email")]
- public partial class SendEmailEventAction<TEvent> : IStandardEventAction<TEvent>
- where TEvent : IEvent
- {
- #region Public Properties
- // Each of these properties are expressions.
- public string To { get; set; } = "";
- public string CC { get; set; } = "";
- public string BCC { get; set; } = "";
- public string Subject { get; set; } = "";
- public string Message { get; set; } = "";
- #endregion
- #region Expressions
- private CoreExpression? _toExpression;
- public CoreExpression ToExpression
- {
- get
- {
- if(_toExpression is null || _toExpression.ExpressionString != To)
- {
- _toExpression = new CoreExpression(To);
- }
- return _toExpression;
- }
- }
- private CoreExpression? _ccExpression;
- public CoreExpression CCExpression
- {
- get
- {
- if(_ccExpression is null || _ccExpression.ExpressionString != CC)
- {
- _ccExpression = new CoreExpression(CC);
- }
- return _ccExpression;
- }
- }
- private CoreExpression? _bccExpression;
- public CoreExpression BCCExpression
- {
- get
- {
- if(_bccExpression is null || _bccExpression.ExpressionString != BCC)
- {
- _bccExpression = new CoreExpression(BCC);
- }
- return _bccExpression;
- }
- }
- private CoreExpression? _subjectExpression;
- public CoreExpression SubjectExpression
- {
- get
- {
- if(_subjectExpression is null || _subjectExpression.ExpressionString != Subject)
- {
- _subjectExpression = new CoreExpression(Subject, returnType: typeof(string));
- }
- return _subjectExpression;
- }
- }
- private CoreExpression? _messageExpression;
- public CoreExpression MessageExpression
- {
- get
- {
- if(_messageExpression is null || _messageExpression.ExpressionString != Message)
- {
- _messageExpression = new CoreExpression(Message, returnType: typeof(string));
- }
- return _messageExpression;
- }
- }
- #endregion
- public IEnumerable<string> ReferencedVariables => ToExpression.ReferencedVariables
- .Concat(CCExpression.ReferencedVariables)
- .Concat(BCCExpression.ReferencedVariables)
- .Concat(SubjectExpression.ReferencedVariables)
- .Concat(MessageExpression.ReferencedVariables);
- public string Description => "Create Email";
- public object? Execute(IEventDataModel dataModel)
- {
- var mailer = DbFactory.Mailer;
- if(mailer is not null)
- {
- if (mailer.Connect())
- {
- var msg = mailer.CreateMessage();
- // msg.From = DbFactory.EmailAddress;
- msg.To = ConvertAddressList(ToExpression.Evaluate(dataModel));
- msg.CC = ConvertAddressList(CCExpression.Evaluate(dataModel));
- msg.BCC = ConvertAddressList(BCCExpression.Evaluate(dataModel));
- msg.Subject = SubjectExpression.Evaluate<string>(dataModel) ?? "";
- msg.Body = MessageExpression.Evaluate<string>(dataModel) ?? "";
- mailer.SendMessage(msg);
- }
- }
- return null;
- }
- private IEnumerable<string> ConvertAddressList(object? value)
- {
- if(value is IEnumerable<string> list)
- {
- return list;
- }
- else if(value is IEnumerable enumerable && value is not string)
- {
- var lst = new List<string>();
- foreach(var obj in enumerable)
- {
- var objStr = obj.ToString();
- if(objStr is not null)
- {
- lst.Add(objStr);
- }
- }
- return lst;
- }
- var str = value?.ToString() ?? "";
- var regex = AddressListRegex();
- return regex.Matches(str).Select(x => x.Value);
- }
- [GeneratedRegex(@"[^\s,;]+")]
- private static partial Regex AddressListRegex();
- }
|