using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Tiku.Application.Content; using Tiku.Application.Security; using Tiku.Domain.Tenancy; using Tiku.Infrastructure.Persistence; namespace Tiku.Infrastructure.ContentV2; internal sealed class PlatformContentActorResolver(ITenantExecutionScope tenantExecutionScope) : IPlatformContentActorResolver { public Task ResolveAsync( Guid userId, CancellationToken cancellationToken = default) { return tenantExecutionScope.ExecuteAsync( new SystemScopeRequest( null, SystemScopeCallerType.Platform, nameof(PlatformContentActorResolver), "Resolve the single platform-owned V2 content owner", Guid.NewGuid().ToString("N"), true), async (provider, token) => { var tenancy = provider.GetRequiredService(); var ownerIds = await tenancy.Tenants.AsNoTracking() .Where(item => item.Mode == TenantMode.PlatformOwned && item.Status == TenantStatus.Active) .Select(item => item.Id) .Take(2) .ToArrayAsync(token); if (ownerIds.Length != 1) throw new ContentV2Exception( "platform_content_owner_invalid", "Exactly one active platform content owner is required."); return new ContentAuthorActor(ownerIds[0], userId); }, cancellationToken); } }