< Summary

Information
Class: Elsa.Extensions.BookmarkExecutionContextExtensions
Assembly: Elsa.Http
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Extensions/BookmarkExecutionContextExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 84
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Http/Extensions/BookmarkExecutionContextExtensions.cs

#LineLine coverage
 1using Elsa.Expressions.Models;
 2using Elsa.Http;
 3using Elsa.SasTokens.Contracts;
 4using Elsa.Workflows;
 5using Elsa.Workflows.Api;
 6using Elsa.Workflows.Runtime;
 7using Microsoft.Extensions.Options;
 8
 9// ReSharper disable once CheckNamespace
 10namespace Elsa.Extensions;
 11
 12/// <summary>
 13/// Provides extension methods for working with <see cref="ExpressionExecutionContext"/> and generating bookmark trigger
 14/// </summary>
 15public static class BookmarkExecutionContextExtensions
 16{
 17    extension(ActivityExecutionContext context)
 18    {
 019        public string GenerateBookmarkTriggerUrl(string bookmarkId, TimeSpan lifetime) => context.ExpressionExecutionCon
 020        public string GenerateBookmarkTriggerUrl(string bookmarkId, DateTimeOffset expiresAt) => context.ExpressionExecu
 021        public string GenerateBookmarkTriggerUrl(string bookmarkId) => context.ExpressionExecutionContext.GenerateBookma
 022        public string GenerateBookmarkTriggerToken(string bookmarkId, TimeSpan? lifetime = null, DateTimeOffset? expires
 23    }
 24
 25    /// <param name="context">The expression execution context.</param>
 26    extension(ExpressionExecutionContext context)
 27    {
 28        /// <summary>
 29        /// Generates a URL that can be used to resume a bookmarked workflow.
 30        /// </summary>
 31        /// <param name="bookmarkId">The ID of the bookmark to resume.</param>
 32        /// <param name="lifetime">The lifetime of the bookmark trigger token.</param>
 33        /// <returns>A URL that can be used to resume a bookmarked workflow.</returns>
 34        public string GenerateBookmarkTriggerUrl(string bookmarkId, TimeSpan lifetime)
 35        {
 036            var token = context.GenerateBookmarkTriggerToken(bookmarkId, lifetime);
 037            return context.GenerateBookmarkTriggerUrlInternal(token);
 38        }
 39
 40        /// <summary>
 41        /// Generates a URL that can be used to resume a bookmarked workflow.
 42        /// </summary>
 43        /// <param name="bookmarkId">The ID of the bookmark to resume.</param>
 44        /// <param name="expiresAt">The expiration date of the bookmark trigger token.</param>
 45        /// <returns>A URL that can be used to resume a bookmarked workflow.</returns>
 46        public string GenerateBookmarkTriggerUrl(string bookmarkId, DateTimeOffset expiresAt)
 47        {
 048            var token = context.GenerateBookmarkTriggerToken(bookmarkId, expiresAt: expiresAt);
 049            return context.GenerateBookmarkTriggerUrlInternal(token);
 50        }
 51
 52        /// <summary>
 53        /// Generates a URL that can be used to resume a bookmarked workflow.
 54        /// </summary>
 55        /// <param name="bookmarkId">The ID of the bookmark to resume.</param>
 56        /// <returns>A URL that can be used to trigger an event.</returns>
 57        public string GenerateBookmarkTriggerUrl(string bookmarkId)
 58        {
 059            var token = context.GenerateBookmarkTriggerToken(bookmarkId);
 060            return context.GenerateBookmarkTriggerUrlInternal(token);
 61        }
 62
 63        public string GenerateBookmarkTriggerToken(string bookmarkId, TimeSpan? lifetime = null, DateTimeOffset? expires
 64        {
 065            var workflowInstanceId = context.GetWorkflowExecutionContext().Id;
 066            var payload = new BookmarkTokenPayload(bookmarkId, workflowInstanceId);
 067            var tokenService = context.GetRequiredService<ITokenService>();
 68
 069            return lifetime != null
 070                ? tokenService.CreateToken(payload, lifetime.Value)
 071                : expiresAt != null
 072                    ? tokenService.CreateToken(payload, expiresAt.Value)
 073                    : tokenService.CreateToken(payload);
 74        }
 75
 76        private string GenerateBookmarkTriggerUrlInternal(string token)
 77        {
 078            var options = context.GetRequiredService<IOptions<ApiEndpointOptions>>().Value;
 079            var url = $"{options.RoutePrefix}/bookmarks/resume?t={token}";
 080            var absoluteUrlProvider = context.GetRequiredService<IAbsoluteUrlProvider>();
 081            return absoluteUrlProvider.ToAbsoluteUrl(url).ToString();
 82        }
 83    }
 84}