Add automatic receipt parsing after upload

Receipts are now automatically parsed using OpenAI Vision API immediately after upload. The parsing runs in the background and triggers auto-mapping if successful. This streamlines the workflow - users just upload a receipt and it's automatically parsed and mapped to the matching transaction without any additional clicks.

The parsing happens asynchronously via Task.Run to not block the upload response. If parsing fails, the upload is still considered successful.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
AJ
2025-10-12 17:51:19 -04:00
parent c29c94ab62
commit d5852f2bb3

View File

@@ -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<IReceiptParser>();
await parser.ParseReceiptAsync(receipt.Id);
}
catch
{
// Silently fail - upload was successful, parsing is optional
}
});
return ReceiptUploadResult.Success(receipt, duplicateWarnings);
}