diff --git a/MoneyMap/Services/ReceiptManager.cs b/MoneyMap/Services/ReceiptManager.cs index dd8605a..a6d9c3e 100644 --- a/MoneyMap/Services/ReceiptManager.cs +++ b/MoneyMap/Services/ReceiptManager.cs @@ -27,14 +27,16 @@ namespace MoneyMap.Services private readonly MoneyMapContext _db; private readonly IWebHostEnvironment _environment; private readonly IConfiguration _configuration; + private readonly IServiceProvider _serviceProvider; private const long MaxFileSize = 10 * 1024 * 1024; // 10MB private static readonly string[] AllowedExtensions = { ".jpg", ".jpeg", ".png", ".pdf", ".gif", ".heic" }; - public ReceiptManager(MoneyMapContext db, IWebHostEnvironment environment, IConfiguration configuration) + public ReceiptManager(MoneyMapContext db, IWebHostEnvironment environment, IConfiguration configuration, IServiceProvider serviceProvider) { _db = db; _environment = environment; _configuration = configuration; + _serviceProvider = serviceProvider; } private string GetReceiptsBasePath() @@ -128,6 +130,21 @@ namespace MoneyMap.Services _db.Receipts.Add(receipt); await _db.SaveChangesAsync(); + // Automatically parse the receipt after upload (in background, don't wait for result) + _ = Task.Run(async () => + { + try + { + using var scope = _serviceProvider.CreateScope(); + var parser = scope.ServiceProvider.GetRequiredService(); + await parser.ParseReceiptAsync(receipt.Id); + } + catch + { + // Silently fail - upload was successful, parsing is optional + } + }); + return ReceiptUploadResult.Success(receipt, duplicateWarnings); }