NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

public void Submit(AutoActionTransactionWrapper e)
{
IEntity rootEntity = null;
if (e.AutoActionTemplate.CreateComment || e.AutoActionTemplate.CreateNotification)
{
rootEntity = Repository.Find(e.EntityName, e.EntityId);
}

IComment comment = null;
if (e.AutoActionTemplate.CreateComment)
{
comment = CreateComment(e, rootEntity);
}

if (e.AutoActionTemplate.CreateNotification)
{
RegisterPostPersistenceAction(() => SendNotificationForAutoAction(e, rootEntity));
}

if (e.AutoActionTemplate.CreateWorkItem)
{
CreateWorkItem(e);
}

RegisterPostPersistenceAction(() => UpdateLogDetail(e, comment));
}

private void SendNotificationForAutoAction(AutoActionTransactionWrapper e, IEntity rootEntity)
{
var context = new NotificationContext
{
Repository = Repository,
SourceModule = NotificationSourceModuleTypeValues.AutoActionLogDetail,
SourceId = e.AutoActionLogDetailId,
Entity = rootEntity,
Event = NotificationEventValues.AutoAction,
UseRecipientInputs = true,
RecipientInputs = GetAutoActionRecipientInputs(e, Repository, rootEntity)
};
ComponentAs<INotificationService>().Notify(context);
}

private IEnumerable<NotificationRecipientInput> GetAutoActionRecipientInputs(AutoActionTransactionWrapper e, IRepositoryAccessor repository, IEntity rootEntity)
{
var allowedUserIdsToNotify = e.AllowedUserIdsToNotify.Split(',').Select(long.Parse).ToList();
var wrapperDetails = Enumerable.Empty<IAutoActionTransactionWrapperDetail>().ToArray();

try
{
wrapperDetails = e.AutoActionTransactionWrapperDetails.Where(x => (x.NotificationRecipientConfig.Condition.IsBlank() || rootEntity.Eval<bool>(x.NotificationRecipientConfig.Condition))).ToArray();
}
catch (ExpressionEvaluatorException ex)
{
ActionFault.Raise(ex.Message);
}

if (!wrapperDetails.Any())
{
return Enumerable.Empty<NotificationRecipientInput>();
}

var recipientInputs = Enumerable.Empty<NotificationRecipientInput>().ToList();

foreach (var wrapperDetail in wrapperDetails)
{
var userRecipientDetailInputs = Enumerable.Empty<UserRecipientDetailInput>().ToList();
var userGroupRecipientDetailInputs = Enumerable.Empty<UserGroupRecipientDetailInput>().ToList();
var externalUserRecipientDetailInputs = Enumerable.Empty<ExternalUserRecipientDetailInput>().ToList();

var emailTemplate = wrapperDetail.NotificationRecipientConfig.OverrideEmailNotification ? wrapperDetail.EmailTemplate : e.EmailTemplate;

if (!wrapperDetail.NotificationRecipientConfig.IsMultipleUser)
EvaluateUser(wrapperDetail, rootEntity, userRecipientDetailInputs);
else
EvaluateUsers(wrapperDetail, rootEntity, allowedUserIdsToNotify, userRecipientDetailInputs);

EvaluateUserGroup(repository, rootEntity, wrapperDetail, userRecipientDetailInputs, userGroupRecipientDetailInputs);

if (userRecipientDetailInputs.Any())
{
var accessScopeInfo = ComponentAs<IEntityConfigProvider>().GetAccessScopeInfo(rootEntity);
var validUserIds = UserParamProvider.GetAccessibleUserIds((ILeasewaveRepository)repository, userRecipientDetailInputs.Select(x => x.UserId), accessScopeInfo.AccessScope, accessScopeInfo.AccessScopeId).ToArray();

userRecipientDetailInputs = userRecipientDetailInputs.Where(x => validUserIds.Contains(x.UserId)).ToList();
}

if (wrapperDetail.NotificationRecipientConfig.ExternalEmailSelectionSQL.IsNotBlank())
{
var externalEmailIds = ComponentAs<INotificationService>().EvaluateExternalEmailAddresses(wrapperDetail.NotificationRecipientConfig, rootEntity);
externalUserRecipientDetailInputs.AddRange(externalEmailIds.Select(x => new ExternalUserRecipientDetailInput(x, true)));
}

recipientInputs.Add(new NotificationRecipientInput
{
NotificationRecipientConfig = wrapperDetail.NotificationRecipientConfig,
NotificationContent = emailTemplate,
UserRecipientDetailInputs = userRecipientDetailInputs,
UserGroupRecipientDetailInputs = userGroupRecipientDetailInputs,
ExternalUserRecipientDetailInputs = externalUserRecipientDetailInputs
});
}

return recipientInputs;
}

private void EvaluateUser(IAutoActionTransactionWrapperDetail wrapperDetail, IEntity rootEntity, List<UserRecipientDetailInput> userRecipientDetailInputs)
{
IUser user = null;
if (wrapperDetail.User != null)
{
user = wrapperDetail.User;
}
else if (wrapperDetail.NotificationRecipientConfig.UserExpression.IsNotBlank())
{
try
{
user = rootEntity.Eval<IUser>(wrapperDetail.NotificationRecipientConfig.UserExpression);
}
catch (ExpressionEvaluatorException ex)
{
ActionFault.Raise(ex.Message);
}
}

if (user != null && user.ApprovalStatus.IsApproved && user.IsEmailNotificationAllowed && user.UserNotificationPreferences.Any(y => y.IsActive && y.IsNotify && y.Event.Event == NotificationEventValues.AutoAction))
{
userRecipientDetailInputs.Add(new UserRecipientDetailInput(user.Id, user.Email, true));
};
}

private void EvaluateUsers(IAutoActionTransactionWrapperDetail wrapperDetail, IEntity rootEntity, List<long> allowedUserIdsToNotify, List<UserRecipientDetailInput> userRecipientDetailInputs)
{
var users = Enumerable.Empty<IUser>();
if (wrapperDetail.NotificationRecipientConfig.UserExpression.IsNotBlank())
{
try
{
users = rootEntity.Eval<IEnumerable<IUser>>(wrapperDetail.NotificationRecipientConfig.UserExpression) ?? Enumerable.Empty<IUser>();
}
catch (ExpressionEvaluatorException ex)
{
ActionFault.Raise(ex.Message);
}
}

users = users.Where(x => x != null && x.ApprovalStatus.IsApproved && x.IsEmailNotificationAllowed && allowedUserIdsToNotify.Contains(x.Id)).ToArray();

userRecipientDetailInputs.AddRange(users.Select(user => new UserRecipientDetailInput(user.Id, user.Email, true)));
}

private void EvaluateUserGroup(IRepositoryAccessor repository, IEntity rootEntity, IAutoActionTransactionWrapperDetail wrapperDetail, List<UserRecipientDetailInput> userRecipientInputs, List<UserGroupRecipientDetailInput> userGroupRecipientDetailInputs)
{
if (wrapperDetail.UserGroup == null || !wrapperDetail.UserGroup.IsActive)
return;

if (wrapperDetail.UserGroup.GroupEmailId.IsBlank())
{
var userNotificationInfos = ComponentAs<IUserGroupParamProvider>().GetUserIdsInUserGroupForNotification((ILeasewaveRepository)repository, wrapperDetail.UserGroup.Id, NotificationEventValues.AutoAction);

userRecipientInputs.AddRange(userNotificationInfos.Select(x => new UserRecipientDetailInput(x.UserId, x.UserEmail, true)));
}
else
{
userGroupRecipientDetailInputs.Add(new UserGroupRecipientDetailInput(wrapperDetail.UserGroup.Id, wrapperDetail.UserGroup.GroupEmailId, true));
}
}

private NotificationRecipientInput GetRecipientInput(IAutoActionTransactionWrapperDetail wrapperDetail, IEnumerable<UserRecipientDetailInput> receipientDetailInputs, IUserGroup userGroup, dynamic notificationContent)
{
var receipentGroupDetailInput = new List<UserGroupRecipientDetailInput>();

if (userGroup != null)
{
receipentGroupDetailInput.Add(new UserGroupRecipientDetailInput(userGroup.Id, string.Empty, true));
}

return new NotificationRecipientInput
{
NotificationRecipientConfig = wrapperDetail.NotificationRecipientConfig,
NotificationContent = notificationContent,
UserRecipientDetailInputs = receipientDetailInputs,
UserGroupRecipientDetailInputs = receipentGroupDetailInput
};
}


private NotificationRecipientInput GetRecipientInput(IAutoActionTransactionWrapperDetail wrapperDetail, IEnumerable<string> externalEmailIds, dynamic notificationContent)
{
var receipientExternalDetailInput = externalEmailIds.Select(x => new ExternalUserRecipientDetailInput(x, true)).ToList();

return new NotificationRecipientInput
{
NotificationRecipientConfig = wrapperDetail.NotificationRecipientConfig,
NotificationContent = notificationContent,
ExternalUserRecipientDetailInputs = receipientExternalDetailInput
};
}

private IComment CreateComment(AutoActionTransactionWrapper e, IEntity rootEntity)
{
Comment comment;
var autoactionTemplateComment = e.AutoActionTemplateComment;

var author = autoactionTemplateComment.AuthorExpression.IsNotBlank()
? rootEntity.Eval<IUser>(autoactionTemplateComment.AuthorExpression)
: e.Author;
if (author == null)
return null;

if (!UserParamProvider.IsValidUser(Repository, author.Id, AccessScopeValues.Portfolio.Value, autoactionTemplateComment.CommentType.PortfolioId))
return null;

var accessScopeInfoForRootEntity = ComponentAs<IEntityConfigProvider>().GetAccessScopeInfo(rootEntity);
if (!UserParamProvider.IsValidUser(Repository, author.Id, accessScopeInfoForRootEntity.AccessScope, accessScopeInfoForRootEntity.AccessScopeId))
return null;

using (var transaction = TransactionService.Begin<CommentTransactions.Create>(transactionStartupOptions: new TransactionStartupOptions(disableInputRules: true)))
{
comment = transaction.EntityAs<Comment>();
comment.CommentTypeId = autoactionTemplateComment.CommentTypeId;
comment.Title = autoactionTemplateComment.Title;
comment.ConversationMode = autoactionTemplateComment.ConversationMode;
comment.IsInternal = autoactionTemplateComment.IsInternal;
comment.Body = autoactionTemplateComment.Body;
comment.Author = author;

var commentEntityTag = comment.CreateCommentEntityTag();
commentEntityTag.EntityTypeId = e.EntityConfigId;
commentEntityTag.EntityId = e.EntityId;
commentEntityTag.IsActive = true;

var commentTypeSubTypeIds = autoactionTemplateComment.AutoActionTemplateCommentSubTypes.Where(x => x.IsActive).Select(x => x.CommentTypeSubTypeId).ToArray();
foreach (var commentTypeSubTypeId in commentTypeSubTypeIds)
{
var commentSubType = comment.CreateCommentSubType();
commentSubType.CommentTypeSubTypeId = commentTypeSubTypeId;
}

transaction.ExecuteAction(Comment.Actions.Submit);
}
return comment;
}

private void CreateWorkItem(AutoActionTransactionWrapper e)
{
var transactionConfig = e.TransactionConfig;
using (var txn = TransactionService.Begin(null, transactionConfig.EntityName, transactionConfig.Name, null, e.EntityId,
new TransactionStartupOptions(readonlyMode: false, isFromAutoAction: true, disableInputRules: true, disableEventTriggers: true, skipLocking: true)))
{
txn.Save(disableValidations: true);
}
}

private void UpdateLogDetail(AutoActionTransactionWrapper e, IComment comment)
{
var parameters = new[]
{ new SqlParameter("@AutoActionLogDetailId", SqlDbType.BigInt)
{ Value = e.AutoActionLogDetailId }
,new SqlParameter("@EntityId", SqlDbType.BigInt)
{ Value = e.EntityId }
,new SqlParameter("@EntityName", SqlDbType.NVarChar,100)
{ Value =e.AutoActionTemplate.CreateWorkItem ? e.TransactionConfig.EntityName : null }
,new SqlParameter("@TransactionName", SqlDbType.NVarChar,100)
{ Value = e.AutoActionTemplate.CreateWorkItem ? e.TransactionConfig.Name : null }
,new SqlParameter("@WorkflowSource", SqlDbType.NVarChar,100)
{ Value = e.AutoActionTemplate.CreateWorkItem ? e.TransactionConfig.WorkflowSource : null }
, new SqlParameter("@CommentId", SqlDbType.BigInt)
{ Value = comment?.Id ?? 0}
, new SqlParameter("@IsNotificationOnly", SqlDbType.Bit)
{ Value = !e.AutoActionTemplate.CreateWorkItem && !e.AutoActionTemplate.IsCreateComment && e.AutoActionTemplate.CreateNotification}
};
Repository.ExecuteStoredProc("UpdateAutoActionLogDetail", parameters);
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.