From 957d2210a94f5d795540386cac9507bba70a9426 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Sat, 24 Oct 2026 12:11:48 +0200 Subject: [PATCH] Refactored sync process --- .../Extensions/General.cs | 66 +++---------------- 1 file changed, 10 insertions(+), 56 deletions(-) diff --git a/PostFundManagement.Application/Extensions/General.cs b/PostFundManagement.Application/Extensions/General.cs index 91e7fdb..c5bab52 100644 --- a/PostFundManagement.Application/Extensions/General.cs +++ b/PostFundManagement.Application/Extensions/General.cs @@ -1,65 +1,19 @@ +using PostFundManagement.Application.Abstractions; +using PostFundManagement.Domain.Abstractions; + namespace PostFundManagement.Application.Extensions; public static class General { - public static string Clean(this string input) => Patterns.SpaceRegex().Replace(input, string.Empty); - - public static string ToJson(this TValue obj) where TValue : class => JsonSerializer.Serialize(obj); - - public static TValue FromJson(this string json) where TValue : class => JsonSerializer.Deserialize(json)!; - - public static Guid GetUserId(this HttpContext context) => (Guid)context.Items[Domain.Extensions.Constants.UserIdItemKey]!; - - public static string GetUserEmail(this HttpContext context) => (string)context.Items[Domain.Extensions.Constants.EmailtemKey]!; - - public static string GetUserRole(this HttpContext context) => (string)context.Items[Domain.Extensions.Constants.UserRoleItemKey]!; - - public static (bool Decoded, Guid userId) GetUserId(this ClaimsPrincipal principal) + public static IServiceCollection AddRegisterServices(this IServiceCollection services) { - var subClaim = principal.FindFirst(JwtRegisteredClaimNames.Sub)?.Value - ?? principal.FindFirst(ClaimTypes.NameIdentifier)?.Value; + var serviceType = typeof(IService); - var decoded = Guid.TryParse(subClaim, out var userId); + var sharedImplementations = typeof(IApplication).Assembly.GetTypes() + .Where(t => serviceType.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract); - return (decoded, userId); + foreach (var sharedImplementation in sharedImplementations) services.AddScoped(sharedImplementation); + + return services; } - - public static (bool Decoded, string email) GetUserEmail(this ClaimsPrincipal principal) - { - var emailClaim = principal.FindFirst("email")?.Value - ?? principal.FindFirst(ClaimTypes.Email)?.Value - ?? principal.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")?.Value - ?? principal.FindFirst("preferred_username")?.Value - ?? principal.FindFirst("name")?.Value - ?? principal.FindFirst(ClaimTypes.Name)?.Value; - - var decoded = !string.IsNullOrWhiteSpace(emailClaim); - - return (decoded, emailClaim ?? string.Empty); - } - - public static (bool Decoded, string role) GetUserRole(this ClaimsPrincipal principal) - { - var roleClaims = principal.FindAll("role") - .Concat(principal.FindAll(ClaimTypes.Role)) - .Concat(principal.FindAll("http://schemas.microsoft.com/ws/2008/06/identity/claims/role")) - .Concat(principal.FindAll("roles")) - .Select(c => c.Value) - .Where(v => !string.IsNullOrWhiteSpace(v)) - .ToList(); - - var partnerRole = roleClaims.FirstOrDefault(r => r.Equals("Partner", StringComparison.OrdinalIgnoreCase)); - var role = partnerRole ?? roleClaims.FirstOrDefault(); - - var decoded = !string.IsNullOrWhiteSpace(role); - - return (decoded, role ?? string.Empty); - } - - public static Microsoft.AspNetCore.Mvc.ProblemDetails ToProblems(this IReadOnlyList errors, int statusCode = StatusCodes.Status400BadRequest) => new() - { - Detail = string.Join(',', errors.Select(e => e.Message)), - Status = statusCode, - Title = "Error", - }; }