Fix patient show 500 when assessment catalog reseed hits live answers.
Deploy Ladill Care / deploy (push) Successful in 1m8s

ensureAssessmentCatalog was re-running AssessmentTemplateSeeder whenever
nursing packs were missing; the seeder hard-deleted questions and blew up
on FK care_assessment_answers. Upsert questions by code, only drop unused
orphans, and never let catalog ensure throw on web requests.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-07-20 16:09:45 +00:00
co-authored by Cursor
parent 218023746d
commit 3e194e9d9d
3 changed files with 112 additions and 7 deletions
+26 -7
View File
@@ -94,18 +94,16 @@ class AssessmentTemplateSeeder extends Seeder
->update(['is_current' => false]);
}
// Replace questions for this template version (stable codes within version).
AssessmentQuestion::query()->where('template_id', $template->id)->delete();
// Upsert questions by code so live answers keep their FK targets.
$keptCodes = [];
$sort = 0;
foreach ($data['questions'] as $q) {
if (empty($q['code']) || empty($q['label']) || empty($q['answer_type'])) {
throw new RuntimeException("Invalid question in pack {$path}");
}
$sort++;
AssessmentQuestion::create([
'template_id' => $template->id,
'code' => $q['code'],
$keptCodes[] = $q['code'];
$attributes = [
'section' => $q['section'] ?? null,
'label' => $q['label'],
'help_text' => $q['help_text'] ?? null,
@@ -115,9 +113,30 @@ class AssessmentTemplateSeeder extends Seeder
'sort_order' => (int) ($q['sort_order'] ?? $sort),
'score_key' => $q['score_key'] ?? null,
'validation' => $q['validation'] ?? null,
]);
];
$question = AssessmentQuestion::query()
->where('template_id', $template->id)
->where('code', $q['code'])
->first();
if ($question) {
$question->fill($attributes)->save();
} else {
AssessmentQuestion::create(array_merge($attributes, [
'template_id' => $template->id,
'code' => $q['code'],
]));
}
}
// Drop obsolete pack questions only when nothing references them.
AssessmentQuestion::query()
->where('template_id', $template->id)
->whereNotIn('code', $keptCodes)
->whereDoesntHave('answers')
->delete();
$this->command?->info(sprintf(
'Seeded assessment pack %s v%d (%d questions)',
$template->code,