/// <summary>
        /// Inserts an event.
        /// </summary>
        /// <param name="eventItem">The event to insert.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual async Task <ValidationResult> CreateAsync(EventItem eventItem, IEnumerable <AccountItem> users, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            if (eventItem == null)
            {
                throw new ArgumentNullException(nameof(eventItem));
            }
            if (users == null)
            {
                throw new ArgumentNullException(nameof(users));
            }

            eventItem.ClientId    = ClientIdProvider.Generate(eventItem.ClientId);
            eventItem.ReferrerUrl = UrlProvider.Generate(eventItem.ReferrerUrl);
            return(await Store.CreateAsync(eventItem, users, cancellationToken));
        }
Example #2
0
        /// <summary>
        /// Inserts an event.
        /// </summary>
        /// <param name="eventItem">The event to insert.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual async Task <ValidationResult> LogAsync(EventItem eventItem, IEnumerable <AccountItem> users, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (eventItem == null)
            {
                throw new ArgumentNullException(nameof(eventItem));
            }
            if (users == null)
            {
                throw new ArgumentNullException(nameof(users));
            }

            var validationResult = await EventManager.CreateAsync(eventItem, users, cancellationToken);

            if (validationResult.Succeeded)
            {
                // TODO: Post process operations can be optimized out to a background job
                if (eventItem.AnonymId != null && eventItem.Contact != null)
                {
                    await EventManager.BulkSetContactsAsync((Guid)eventItem.AnonymId, eventItem.Contact.Id, cancellationToken);
                }

                await EventManager.SetCorrelationAsync(eventItem.Id, cancellationToken);

                // Add the event to the queue that can be even read by a SignalR Hub
                // Of course, it is worth supporting some eviction technics avoiding memory wasting
                var eventResults = await EventManager.FindAllByIdAsync(eventItem.Id, cancellationToken);

                foreach (var eventResult in eventResults)
                {
                    await ApplyRules(eventResult, cancellationToken);

                    Queue.Enqueue(eventResult);
                }
            }
            return(validationResult);
        }
        //
        // Private Members
        //

        /// <summary>
        /// Validates project and update. Called by other ProjectManager methods.
        /// </summary>
        private async Task <ValidationResult> UpdateEventAsync(EventItem eventItem, CancellationToken cancellationToken)
        {
            return(await Store.UpdateAsync(eventItem, cancellationToken));
        }