< Summary

Information
Class: Orchestrator.Commands.Utility.UploadTransfers.UploadTransfersCommand
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Commands/Utility/UploadTransfers/UploadTransfersCommand.cs
Line coverage
100%
Covered lines: 59
Uncovered lines: 0
Coverable lines: 59
Total lines: 122
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ExecuteAsync()100%1818100%
.ctor()100%11100%

File(s)

/home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Commands/Utility/UploadTransfers/UploadTransfersCommand.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.FileProviders;
 3using Microsoft.Extensions.Logging;
 4using Spectre.Console.Cli;
 5using Spectre.Console;
 6using System.Text.Json;
 7using EHonda.KicktippAi.Core;
 8using Orchestrator.Infrastructure;
 9using Orchestrator.Infrastructure.Factories;
 10
 11namespace Orchestrator.Commands.Utility.UploadTransfers;
 12
 13public class UploadTransfersCommand : AsyncCommand<UploadTransfersSettings>
 14{
 15    private readonly IAnsiConsole _console;
 16    private readonly IFirebaseServiceFactory _firebaseServiceFactory;
 17    private readonly IFileProvider _fileProvider;
 18    private readonly ILogger<UploadTransfersCommand> _logger;
 19
 120    public UploadTransfersCommand(
 121        IAnsiConsole console,
 122        IFirebaseServiceFactory firebaseServiceFactory,
 123        [FromKeyedServices(ServiceRegistrationExtensions.TransfersDocumentsFileProviderKey)] IFileProvider fileProvider,
 124        ILogger<UploadTransfersCommand> logger)
 25    {
 126        _console = console;
 127        _firebaseServiceFactory = firebaseServiceFactory;
 128        _fileProvider = fileProvider;
 129        _logger = logger;
 130    }
 31
 32    protected override async Task<int> ExecuteAsync(CommandContext context, UploadTransfersSettings settings, Cancellati
 33    {
 34
 35        try
 36        {
 137            var docName = $"{settings.TeamAbbreviation.ToLowerInvariant()}-transfers.csv";
 138            _console.MarkupLine($"[green]Upload Transfers command initialized for document:[/] [yellow]{docName}[/]");
 139            _console.MarkupLine($"[blue]Using community context:[/] [yellow]{settings.CommunityContext}[/]");
 140            var competition = CompetitionResolver.ResolveCompetition(settings.Competition, settings.CommunityContext, se
 141            var repositoryCompetition = CompetitionResolver.ToRepositoryCompetitionArgument(competition);
 142            _console.MarkupLine($"[blue]Using competition:[/] [yellow]{competition}[/]");
 143            if (settings.Verbose) _console.MarkupLine("[dim]Verbose mode enabled[/]");
 44
 45            // JSON file path produced by Create-TransfersDocument.ps1 firebase mode
 146            var jsonPath = $"output/{settings.CommunityContext}/{docName}.json";
 147            var fileInfo = _fileProvider.GetFileInfo(jsonPath);
 148            if (!fileInfo.Exists)
 49            {
 150                _console.MarkupLine($"[red]Transfers document JSON not found:[/] {jsonPath}");
 151                _console.MarkupLine("[dim]Run Create-TransfersDocument.ps1 in firebase mode first.[/]");
 152                return 1;
 53            }
 54
 155            _console.MarkupLine($"[blue]Reading transfers document from:[/] {jsonPath}");
 156            using var stream = fileInfo.CreateReadStream();
 157            var transfersDoc = await JsonSerializer.DeserializeAsync<TransfersDocumentJson>(stream, new JsonSerializerOp
 158            if (transfersDoc == null)
 59            {
 160                _console.MarkupLine("[red]Failed to parse transfers document JSON[/]");
 161                return 1;
 62            }
 63
 164            if (settings.Verbose)
 65            {
 166                _console.MarkupLine($"[dim]Document Name: {transfersDoc.DocumentName}[/]");
 167                _console.MarkupLine($"[dim]Community Context: {transfersDoc.CommunityContext}[/]");
 168                _console.MarkupLine($"[dim]Content length: {transfersDoc.Content.Length} characters[/]");
 69            }
 70
 71            // Create Firebase services using factory (factory handles env var loading)
 172            var contextRepo = _firebaseServiceFactory.CreateContextRepository(repositoryCompetition);
 173            var existing = await contextRepo.GetLatestContextDocumentAsync(transfersDoc.DocumentName, transfersDoc.Commu
 174            if (existing != null)
 75            {
 176                _console.MarkupLine($"[blue]Found existing transfers document '{transfersDoc.DocumentName}' (version {ex
 177                if (settings.Verbose)
 78                {
 179                    _console.MarkupLine("[dim]Checking for changes...[/]");
 80                }
 81            }
 82            else
 83            {
 184                _console.MarkupLine($"[blue]No existing transfers document found - will create version 0[/]");
 85            }
 86
 187            var savedVersion = await contextRepo.SaveContextDocumentAsync(
 188                transfersDoc.DocumentName,
 189                transfersDoc.Content,
 190                transfersDoc.CommunityContext);
 91
 192            if (existing != null && savedVersion == null)
 93            {
 194                _console.MarkupLine($"[green]✓ Content unchanged - transfers document remains at version {existing.Versi
 95            }
 196            else if (existing != null)
 97            {
 198                _console.MarkupLine($"[green]✓ Content changed - created new version {savedVersion}[/]");
 99            }
 100            else
 101            {
 1102                _console.MarkupLine($"[green]✓ Created transfers document version {savedVersion}[/]");
 103            }
 104
 1105            return 0;
 106        }
 1107        catch (Exception ex)
 108        {
 1109            _logger.LogError(ex, "Error in upload-transfers command");
 1110            _console.MarkupLine($"[red]Error: {ex.Message}[/]");
 1111            return 1;
 112        }
 1113    }
 114
 115    private class TransfersDocumentJson
 116    {
 1117        public string DocumentName { get; set; } = string.Empty;
 1118        public string Content { get; set; } = string.Empty;
 1119        public string Description { get; set; } = string.Empty;
 1120        public string CommunityContext { get; set; } = string.Empty;
 121    }
 122}