using DigitalData.MessagingService.Application.Common.Interfaces; using DigitalData.MessagingService.Domain.Entities; using DigitalData.MessagingService.Domain.Enums; using Microsoft.Extensions.DependencyInjection; namespace DigitalData.MessagingService.Infrastructure.Services; /// /// Routes OAuth2 token requests to the correct provider-specific implementation /// based on . /// Registered as the single in DI — all other /// services depend on this dispatcher rather than on a concrete provider directly. /// public class OAuth2TokenServiceDispatcher(IServiceProvider ServiceProvider) : IOAuth2TokenService { public Task GetAccessTokenAsync(EmailAccount account, CancellationToken cancellationToken = default) { var service = account.OAuth2Provider switch { OAuth2Provider.Microsoft => ServiceProvider.GetRequiredService(), OAuth2Provider.Google => (IOAuth2TokenService)ServiceProvider.GetRequiredService(), OAuth2Provider.None => throw new InvalidOperationException( $"Account '{account.Username}' (Id: {account.Id}) has OAuth2Provider = None. " + "Set UseOAuth2 = false or configure a valid OAuth2Provider."), _ => throw new NotSupportedException( $"OAuth2Provider '{account.OAuth2Provider}' is not supported. " + $"Supported providers: {string.Join(", ", Enum.GetNames())}") }; return service.GetAccessTokenAsync(account, cancellationToken); } }