Exemple #1
0
        public async Task Consume(ConsumeContext <IActionNotificationEvent> context)
        {
            DateTime actionStartDate = DateTime.UtcNow;

            try
            {
                if (context.Message != null && context.Message as IActionNotificationEvent != null)
                {
                    IActionNotificationEvent action = context.Message;
                    _logger.LogDebug($"ResponseActionNotificationEventConsumer: ActionId: '{action.ActionId}'.");
                    _logger.LogDebug($"ResponseActionNotificationEventConsumer: User: '******'.");
                    _logger.LogDebug($"ResponseActionNotificationEventConsumer: Message: '{action.Message}'.");
                    _logger.LogDebug($"ResponseActionNotificationEventConsumer: IsSilent: '{action.IsSilent}'.");

                    NotificationModel result = await _notificationRestService.SendNotification(new NotificationCreationModel()
                    {
                        ResponseId       = action.ResponseId,
                        NotificationText = action.Message,
                        Status           = 1,
                        Title            = "Alert Notification",
                        Tags             = null
                    });

                    //Success
                    if (result != null)
                    {
                        await GenerateActionCallback(context, ActionStatus.Success, actionStartDate);

                        _logger.LogDebug($"ResponseActionNotificationEventConsumer: NotificationId: '{result.NotificationId}' published.");
                        return;
                    }

                    //Error
                    await GenerateActionCallback(context, ActionStatus.Error, actionStartDate, "The notification could not be sent.");

                    _logger.LogError("ResponseActionNotificationEventConsumer: The notification could not be sent.");
                }
                _logger.LogError("ResponseActionNotificationEventConsumer: Invalid Null or Empty Action Notification");
                throw new Exception("Invalid or Null Action Notification");
            }
            catch (Exception e)
            {
                await GenerateActionCallback(context, ActionStatus.Error, actionStartDate, $"The notification could not be sent: {e.Message}.");

                _logger.LogError($"ResponseActionNotificationEventConsumer: {e.Message}");
                throw e;
            }
        }
        public async Task Consume(ConsumeContext <INotificationSendEvent> context)
        {
            try
            {
                if (context.Message != null && context.Message as INotificationSendEvent != null && context.Message.Notification != null)
                {
                    NotificationCreationModel notification = context.Message.Notification;
                    _logger.LogDebug($"NotificationSendEventConsumer: Text: '{notification.NotificationText}'.");
                    _logger.LogDebug($"NotificationSendEventConsumer: Status: '{notification.Status}'.");
                    _logger.LogDebug($"NotificationSendEventConsumer: Title: '{notification.Title}'.");
                    _logger.LogDebug($"NotificationSendEventConsumer: User: '******'.");
                    DateTime date = DateTime.UtcNow;
                    notification.ResponseId = context.Message.ResponseId;
                    NotificationModel result = await _notificationRestService.SendNotification(notification);

                    if (result != null)
                    {
                        await context.Publish(new EventSagaReceiveResponseActionCallback()
                        {
                            IsCloseAction = context.Message.IsCloseAction,
                            ResponseId    = context.Message.ResponseId,
                            ActionId      = context.Message.ActionId,
                            Status        = ActionStatus.Success
                        });

                        _logger.LogDebug($"NotificationSendEventConsumer: NotificationId: '{result.NotificationId}' published.");
                        return;
                    }
                }
                _logger.LogError("NotificationSendEventConsumer: Invalid Null or Empty Action Notification");
                throw new Exception("Invalid or Null Action Notification");
            }
            catch (Exception e)
            {
                await context.Publish(new EventSagaReceiveResponseActionCallback()
                {
                    IsCloseAction = context.Message.IsCloseAction,
                    ResponseId    = context.Message.ResponseId,
                    ActionId      = context.Message.ActionId,
                    Status        = ActionStatus.Error
                });

                _logger.LogError($"NotificationSendEventConsumer: {e.Message}");
                throw e;
            }
        }