Harden workstation UX and seed placeholder scans after Playwright QA.
CI / backend (push) Failing after 38s
CI / frontend (push) Successful in 9m35s

Fix mobile queue navigation, draft/loading states, and Intake overflow; seed MinIO demo PDFs so document viewing does not 500; add e2e smoke coverage.
This commit is contained in:
2026-10-06 15:07:15 +08:00
parent 7d7332641f
commit 3c7bf5bccb
47 changed files with 1160 additions and 140 deletions
@@ -50,6 +50,9 @@ public sealed class AppBootstrapHostedService : BackgroundService
_logger.LogInformation("Created MinIO bucket {Bucket}", minioOpts.BucketName);
}
var storage = scope.ServiceProvider.GetRequiredService<IDocumentStorageService>();
await SeedDocumentStorage.EnsureAsync(db, storage, _logger, stoppingToken);
_startupHealthCheck.StartupCompleted = true;
_logger.LogInformation("Application bootstrap completed.");
}
@@ -111,6 +111,7 @@ public class CoverSheetController : ControllerBase
AssignToUserName: sheet.AssignToUser?.FullName,
IsUsed: sheet.IsUsed,
BatchId: sheet.BatchId,
BatchStatus: sheet.Batch?.Status.ToDbString(),
CreatedAt: sheet.CreatedAt,
UsedAt: sheet.UsedAt
);
@@ -160,9 +160,25 @@ public class DigitizationBatchesController : ControllerBase
await RecordDocumentAccessAsync(id);
var stream = await _storage.DownloadAsync(batch.DocumentRef);
Response.Headers.CacheControl = "private, max-age=300";
return File(stream, contentType);
try
{
if (!await _storage.ObjectExistsAsync(batch.DocumentRef))
return NotFound(ApiResponse<object>.Fail(
404,
"Scanned document is missing from storage. Re-upload the batch or restore the object.",
"DOCUMENT_NOT_FOUND"));
var stream = await _storage.DownloadAsync(batch.DocumentRef);
Response.Headers.CacheControl = "private, max-age=300";
return File(stream, contentType);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
return StatusCode(503, ApiResponse<object>.Fail(
503,
"Document storage is temporarily unavailable.",
"DOCUMENT_STORAGE_UNAVAILABLE"));
}
}
/// <summary>
@@ -0,0 +1,85 @@
using System.Text;
using Microsoft.EntityFrameworkCore;
/// <summary>
/// Ensures demo/seeded batches have ScannedDocument rows and placeholder PDF
/// objects in MinIO so workstation document viewing does not 500.
/// Idempotent — safe to run on every bootstrap.
/// </summary>
public static class SeedDocumentStorage
{
/// <summary>Minimal valid single-page PDF used as a seed placeholder.</summary>
public static readonly byte[] MinimalPdf = Encoding.ASCII.GetBytes(
"%PDF-1.4\n" +
"1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n" +
"2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj\n" +
"3 0 obj<</Type/Page/MediaBox[0 0 612 792]/Parent 2 0 R>>endobj\n" +
"xref\n0 4\n0000000000 65535 f \n0000000009 00000 n \n0000000052 00000 n \n0000000101 00000 n \n" +
"trailer<</Size 4/Root 1 0 R>>\nstartxref\n178\n%%EOF\n");
public static async Task EnsureAsync(
AppDbContext db,
IDocumentStorageService storage,
ILogger logger,
CancellationToken ct = default)
{
var batches = await db.DigitizationBatches
.AsNoTracking()
.Where(b => b.DocumentRef != "live-capture" && b.DocumentRef != "")
.Select(b => new { b.Id, b.DocumentRef, b.DocumentSha256 })
.ToListAsync(ct);
if (batches.Count == 0)
return;
var batchIds = batches.Select(b => b.Id).ToList();
var existingDocBatchIds = await db.ScannedDocuments
.AsNoTracking()
.Where(d => batchIds.Contains(d.BatchId))
.Select(d => d.BatchId)
.ToListAsync(ct);
var existingSet = existingDocBatchIds.ToHashSet();
var missingDocs = batches
.Where(b => !existingSet.Contains(b.Id))
.Select(b => new ScannedDocument
{
Id = Guid.NewGuid(),
BatchId = b.Id,
ObjectKey = b.DocumentRef,
Sha256 = NormalizeSha(b.DocumentSha256),
ContentType = "application/pdf",
FileSizeBytes = MinimalPdf.Length,
UploadedAt = DateTimeOffset.UtcNow,
})
.ToList();
if (missingDocs.Count > 0)
{
db.ScannedDocuments.AddRange(missingDocs);
await db.SaveChangesAsync(ct);
logger.LogInformation("Seeded {Count} missing ScannedDocument rows", missingDocs.Count);
}
var uploaded = 0;
foreach (var batch in batches)
{
ct.ThrowIfCancellationRequested();
if (!await storage.ObjectExistsAsync(batch.DocumentRef))
{
await storage.EnsureObjectAsync(batch.DocumentRef, MinimalPdf, "application/pdf");
uploaded++;
}
}
if (uploaded > 0)
logger.LogInformation("Uploaded {Count} placeholder scan PDFs to object storage", uploaded);
}
private static string NormalizeSha(string? sha)
{
if (string.IsNullOrWhiteSpace(sha))
return new string('0', 64);
return sha.Length >= 64 ? sha[..64] : sha.PadRight(64, '0');
}
}
@@ -10,6 +10,7 @@ public record CoverSheetResponse(
string? AssignToUserName,
bool IsUsed,
Guid? BatchId,
string? BatchStatus,
DateTimeOffset CreatedAt,
DateTimeOffset? UsedAt
);
@@ -67,6 +67,7 @@ public class CoverSheetService : ICoverSheetService
return await _db.CoverSheets
.Include(c => c.Patient)
.Include(c => c.AssignToUser)
.Include(c => c.Batch)
.FirstOrDefaultAsync(c => c.Code == code.ToUpperInvariant().Trim());
}
@@ -75,6 +76,7 @@ public class CoverSheetService : ICoverSheetService
return await _db.CoverSheets
.Include(c => c.Patient)
.Include(c => c.AssignToUser)
.Include(c => c.Batch)
.FirstOrDefaultAsync(c => c.Id == id);
}
@@ -122,6 +124,7 @@ public class CoverSheetService : ICoverSheetService
.AsNoTracking()
.Include(c => c.Patient)
.Include(c => c.AssignToUser)
.Include(c => c.Batch)
.AsQueryable();
if (isUsed.HasValue)
@@ -70,6 +70,36 @@ public class DocumentStorageService : IDocumentStorageService
return memStream;
}
public async Task<bool> ObjectExistsAsync(string objectKey)
{
try
{
await _minio.StatObjectAsync(new StatObjectArgs()
.WithBucket(_options.BucketName)
.WithObject(objectKey));
return true;
}
catch (Minio.Exceptions.ObjectNotFoundException)
{
return false;
}
}
public async Task EnsureObjectAsync(string objectKey, byte[] content, string contentType)
{
await EnsureBucketAsync();
if (await ObjectExistsAsync(objectKey))
return;
using var stream = new MemoryStream(content);
await _minio.PutObjectAsync(new PutObjectArgs()
.WithBucket(_options.BucketName)
.WithObject(objectKey)
.WithStreamData(stream)
.WithObjectSize(stream.Length)
.WithContentType(contentType));
}
private async Task EnsureBucketAsync()
{
var exists = await _minio.BucketExistsAsync(new BucketExistsArgs().WithBucket(_options.BucketName));
@@ -3,4 +3,8 @@ public interface IDocumentStorageService
Task<(string objectKey, string sha256, long fileSize)> UploadAsync(Stream fileStream, string contentType, Guid batchId);
Task<string> GetPresignedUrlAsync(string objectKey);
Task<Stream> DownloadAsync(string objectKey);
/// <summary>Returns false when the object is missing from the bucket.</summary>
Task<bool> ObjectExistsAsync(string objectKey);
/// <summary>Uploads bytes only when the object key is not already present.</summary>
Task EnsureObjectAsync(string objectKey, byte[] content, string contentType);
}