/// <summary>
        /// Takes a business event and returns it for addition
        /// </summary>
        /// <param name="createBusinessEventView"></param>
        /// <returns>The business event to be added</returns>
        public IntegrationEvent AddBusinessEvent(CreateBusinessEventViewModel createBusinessEventView)
        {
            var existingEvent = _integrationEventRepository.Find(null, d => d.Name.ToLower() == createBusinessEventView.Name.ToLower())?.Items?.FirstOrDefault();

            if (existingEvent != null)
            {
                throw new EntityAlreadyExistsException("Business event already exists");
            }
            IntegrationEvent newEvent = createBusinessEventView.Map(createBusinessEventView); //assign request to schedule entity

            return(newEvent);
        }
        public async Task <IActionResult> AllIntegrationEvents(
            [FromQuery(Name = "$filter")] string filter   = "",
            [FromQuery(Name = "$orderby")] string orderBy = "",
            [FromQuery(Name = "$top")] int top            = 100,
            [FromQuery(Name = "$skip")] int skip          = 0)
        {
            try
            {
                ODataHelper <IntegrationEvent> oDataHelper = new ODataHelper <IntegrationEvent>();
                var oData = oDataHelper.GetOData(HttpContext, oDataHelper);

                var response = _repository.Find(null, oData.Filter, oData.Sort, oData.SortDirection, oData.Skip, oData.Top);
                IntegrationEventEntitiesLookupViewModel eventLogList = new IntegrationEventEntitiesLookupViewModel();

                if (response != null)
                {
                    eventLogList.EntityNameList = new List <string>();

                    foreach (var item in response.Items)
                    {
                        eventLogList.EntityNameList.Add(item.EntityType);
                    }
                    eventLogList.EntityNameList = eventLogList.EntityNameList.Distinct().ToList();
                }
                return(Ok(eventLogList));
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
        public IntegrationEventEntitiesLookupViewModel AllIntegrationEvents()
        {
            var response = repository.Find(null, x => x.IsDeleted == false);
            IntegrationEventEntitiesLookupViewModel eventLogList = new IntegrationEventEntitiesLookupViewModel();

            if (response != null)
            {
                eventLogList.EntityNameList = new List <string>();

                foreach (var item in response.Items)
                {
                    eventLogList.EntityNameList.Add(item.EntityType);
                }
                eventLogList.EntityNameList = eventLogList.EntityNameList.Distinct().ToList();
            }
            return(eventLogList);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AllIntegrationEvents()
        {
            try
            {
                var response = _repository.Find(null, x => x.IsDeleted == false);
                IntegrationEventEntitiesLookupViewModel eventLogList = new IntegrationEventEntitiesLookupViewModel();

                if (response != null)
                {
                    eventLogList.EntityNameList = new List <string>();

                    foreach (var item in response.Items)
                    {
                        eventLogList.EntityNameList.Add(item.EntityType);
                    }
                    eventLogList.EntityNameList = eventLogList.EntityNameList.Distinct().ToList();
                }
                return(Ok(eventLogList));
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Publishes IntegrationEvents to all subscriptions
        /// </summary>
        /// <param name="integrationEventName"> Unique name for IntegrationEvent</param>
        /// <param name="entityId">Optional parameter that specifies the entity which was affected</param>
        /// <param name="entityName">Optional parameter that specifies the name of the affected entity</param>
        /// <returns></returns>
        public async Task PublishAsync(string integrationEventName, string entityId = "", string entityName = "")
        {
            //get all subscriptions for the event
            var eventSubscriptions = _eventSubscriptionRepository.Find(0, 1).Items?.
                                     Where(s => s.IntegrationEventName == integrationEventName || s.EntityID == Guid.Parse(entityId));

            if (eventSubscriptions == null)
            {
                return;
            }

            //get current integration event
            var integrationEvent = _eventRepository.Find(0, 1).Items?.Where(e => e.Name == integrationEventName).FirstOrDefault();

            if (integrationEvent == null)
            {
                return;
            }
            WebhookPayload payload = CreatePayload(integrationEvent, entityId, entityName);

            //log integration event
            IntegrationEventLog eventLog = new IntegrationEventLog()
            {
                IntegrationEventName = integrationEventName,
                OccuredOnUTC         = DateTime.UtcNow,
                EntityType           = integrationEvent.EntityType,
                EntityID             = Guid.Parse(entityId),
                PayloadJSON          = JsonConvert.SerializeObject(payload),
                CreatedOn            = DateTime.UtcNow,
                Message    = "",
                Status     = "",
                SHA256Hash = ""
            };

            eventLog = _eventLogRepository.Add(eventLog);


            //get subscriptions that must receive webhook
            foreach (var eventSubscription in eventSubscriptions)
            {
                //handle subscriptions that should not get notified
                if (!((eventSubscription.IntegrationEventName == integrationEventName || eventSubscription.IntegrationEventName == null) &&
                      (eventSubscription.EntityID == new Guid(entityId) || eventSubscription.EntityID == null)))
                {
                    continue; //do not create an attempt in this case
                }

                //create new IntegrationEventSubscriptionAttempt
                IntegrationEventSubscriptionAttempt subscriptionAttempt = new IntegrationEventSubscriptionAttempt()
                {
                    EventLogID                     = eventLog.Id,
                    IntegrationEventName           = eventSubscription.IntegrationEventName,
                    IntegrationEventSubscriptionID = eventSubscription.Id,
                    Status         = "InProgress",
                    AttemptCounter = 0,
                    CreatedOn      = DateTime.UtcNow,
                };

                switch (eventSubscription.TransportType)
                {
                case TransportType.HTTPS:
                    //create a background job to send the webhook
                    _backgroundJobClient.Enqueue <WebhookSender>(x => x.SendWebhook(eventSubscription, payload, subscriptionAttempt));
                    break;

                case TransportType.Queue:
                    QueueItem queueItem = new QueueItem
                    {
                        Name       = eventSubscription.Name,
                        IsLocked   = false,
                        QueueId    = eventSubscription.QUEUE_QueueID ?? Guid.Empty,
                        Type       = "Json",
                        JsonType   = "IntegrationEvent",
                        DataJson   = JsonConvert.SerializeObject(payload),
                        State      = "New",
                        RetryCount = eventSubscription.Max_RetryCount ?? 1,
                        Source     = eventSubscription.IntegrationEventName,
                        Event      = integrationEvent.Description,
                        CreatedOn  = DateTime.UtcNow,
                    };
                    _queueItemRepository.Add(queueItem);
                    subscriptionAttempt.AttemptCounter = 1;
                    _attemptRepository.Add(subscriptionAttempt);
                    break;

                case TransportType.SignalR:
                    await _hub.Clients.All.SendAsync(integrationEventName, JsonConvert.SerializeObject(payload)).ConfigureAwait(false);

                    subscriptionAttempt.AttemptCounter = 1;
                    _attemptRepository.Add(subscriptionAttempt);
                    break;
                }
            }

            return;
        }