Exemplo n.º 1
0
        /// <summary>
        /// Asynchronously produces notification messages for all intended recipients based on the specified event type.
        /// </summary>
        /// <param name="args">An object that holds the event data.</param>
        /// <param name="cancellationToken">The token used to cancel an ongoing async operation.</param>
        /// <returns></returns>
        public virtual Task ProcessAsync(SysEventArgs args, CancellationToken cancellationToken = default(CancellationToken))
        {
            CurrentEvent = args;
            if (args.EventType == SysEventType.Unspecified)
            {
                return(Task.CompletedTask);
            }

            try
            {
                var userName = GetUserName(args.User);
                var targetId = GetUserId(args.User);
                var cat      = args.GetEventCategory();

                if (cat == SysEventCategory.User && args.Data is IdentityUser iduser)
                {
                    userName = iduser.UserName;
                    targetId = iduser.Id;
                }

                int?dataId   = null;
                var dataName = Empty;

                if (args.Data is ILogItem data)
                {
                    dataId   = data.Id;
                    dataName = data.Name;
                }

                string      message     = null;
                MessageType messageType = MessageType.Info;

                switch (CurrentEvent.EventType)
                {
                case SysEventType.LoginFailure:
                    messageType = MessageType.Warn;
                    message     = Format(SysEventLoginFailure, userName);
                    break;

                case SysEventType.LoginSuccess:
                    message = Format(SysEventLoginSuccess, userName);
                    break;

                case SysEventType.Logout:
                    message = Format(SysEventLogout, userName);
                    break;

                case SysEventType.IssueCreated:
                    message = Format(SysEventIssueCreated, userName);
                    break;

                case SysEventType.IssueAssigned:
                    message = Format(SysEventIssueAssigned, dataId, userName);
                    break;

                case SysEventType.IssueUpdated:
                    message = Format(SysEventIssueUpdated, dataId, userName);
                    break;

                case SysEventType.IssueClosed:
                    message = Format(SysEventIssueClosed, dataId, userName);
                    break;

                case SysEventType.IssueReopened:
                    message = Format(SysEventIssueReopened, userName);
                    break;

                case SysEventType.IssueDeleted:
                    messageType = MessageType.Warn;
                    message     = Format(SysEventIssueDeleted, userName);
                    break;

                case SysEventType.UserRegistered:
                    message = Format(SysEventUserRegistered, userName);
                    break;

                case SysEventType.UserCreated:
                    messageType = MessageType.Success;
                    message     = Format(SysEventUserCreated, userName);
                    break;

                case SysEventType.UserUpdated:
                    messageType = MessageType.Success;
                    message     = Format(SysEventUserUpdated, userName);
                    break;

                case SysEventType.UserDeleted:
                    messageType = MessageType.Warn;
                    message     = Format(SysEventUserDeleted, userName);
                    break;

                case SysEventType.UserImported:
                    message = Format(SysEventUserImported, userName);
                    break;

                case SysEventType.UserPasswordChanged:
                    message = Format(SysEventUserPasswordChanged, userName);
                    break;

                case SysEventType.CategoryCreated:
                    messageType = MessageType.Success;
                    message     = Format(SysEventCategoryCreated, dataName);
                    break;

                case SysEventType.CategoryUpdated:
                    messageType = MessageType.Success;
                    message     = Format(SysEventCategoryUpdated, dataName);
                    break;

                case SysEventType.CategoryDeleted:
                    messageType = MessageType.Warn;
                    message     = Format(SysEventCategoryDeleted, dataName);
                    break;

                case SysEventType.EmailConfigUpdated:
                    messageType = MessageType.Success;
                    message     = SysEventEmailConfigUpdated;
                    break;

                case SysEventType.EmailSendFailed:
                    messageType = MessageType.Warn;
                    message     = SysEventEmailSendFailed;
                    break;

                case SysEventType.EmailSendSuccess:
                    messageType = MessageType.Success;
                    message     = SysEventEmailSendSuccess;
                    break;

                default:
                    break;
                }
                if (message != null)
                {
                    Consumer.Enqueue(CreateNotification(message, targetId, messageType, userName));
                    Consumer.Notify();
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, Empty);
            }

            return(Task.CompletedTask);
        }