fix Score Import
All checks were successful
Build & Deploy on Dev / build (push) Successful in 49s

This commit is contained in:
Suphonchai Phoonsawat 2026-05-19 16:47:12 +07:00
parent 73b0db6b36
commit e0adbb0b71

View file

@ -570,12 +570,23 @@ public class ImportBackgroundService : BackgroundService
if (rec_import == null) throw new Exception("RecruitImport not found"); if (rec_import == null) throw new Exception("RecruitImport not found");
if (rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null) var rec_import_id = rec_import.Id;
var rec_import_year = rec_import.Year;
var hasExistingScores = rec_import.ScoreImport != null && rec_import.ScoreImport.Scores != null;
if (hasExistingScores)
{ {
await _context.BulkDeleteAsync(rec_import.ScoreImport.Scores.ToList()); var existingScores = rec_import.ScoreImport.Scores.ToList();
await _context.BulkDeleteAsync(existingScores);
} }
rec_import.ImportHostories.Add(new RecruitImportHistory // Clear tracker to avoid stale references after BulkDelete (which bypasses EF tracking)
_context.ChangeTracker.Clear();
// Add history record using Attach stub for navigation (no explicit FK property on model)
var importStub = new RecruitImport { Id = rec_import_id };
_context.Attach(importStub);
_context.RecruitImportHistories.Add(new RecruitImportHistory
{ {
Description = "นำเข้าข้อมูลผลคะแนนสอบ", Description = "นำเข้าข้อมูลผลคะแนนสอบ",
CreatedAt = DateTime.Now, CreatedAt = DateTime.Now,
@ -584,13 +595,15 @@ public class ImportBackgroundService : BackgroundService
LastUpdatedAt = DateTime.Now, LastUpdatedAt = DateTime.Now,
LastUpdateUserId = job.UserId ?? "", LastUpdateUserId = job.UserId ?? "",
LastUpdateFullName = job.FullName ?? "System Administrator", LastUpdateFullName = job.FullName ?? "System Administrator",
RecruitImport = importStub,
}); });
// get doc from minio // get doc from minio
var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile)); var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile));
var imported = new ScoreImport var imported = new ScoreImport
{ {
Year = rec_import.Year, Year = rec_import_year,
RecruitImportId = rec_import_id,
ImportFile = doc, ImportFile = doc,
CreatedAt = DateTime.Now, CreatedAt = DateTime.Now,
CreatedUserId = job.UserId ?? "", CreatedUserId = job.UserId ?? "",
@ -602,7 +615,7 @@ public class ImportBackgroundService : BackgroundService
}; };
// Save ScoreImport parent first to get its Id // Save ScoreImport parent first to get its Id
rec_import.ScoreImport = imported; _context.ScoreImports.Add(imported);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
_context.ChangeTracker.Clear(); _context.ChangeTracker.Clear();