Fix patient show 500 when assessment catalog reseed hits live answers.
Deploy Ladill Care / deploy (push) Successful in 1m8s
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:
@@ -91,4 +91,75 @@ class CareContentPacksTest extends TestCase
|
||||
$this->assertTrue($copd->questions()->where('code', 'mmrc')->exists());
|
||||
$this->assertTrue($hf->questions()->where('code', 'nyha')->exists());
|
||||
}
|
||||
|
||||
public function test_reseed_preserves_questions_that_have_answers(): void
|
||||
{
|
||||
$this->seed(AssessmentTemplateSeeder::class);
|
||||
|
||||
$user = \App\Models\User::create([
|
||||
'public_id' => 'pack-reseed-user',
|
||||
'name' => 'Pack',
|
||||
'email' => 'pack-reseed@example.com',
|
||||
]);
|
||||
$organization = \App\Models\Organization::create([
|
||||
'owner_ref' => $user->public_id,
|
||||
'name' => 'Pack Clinic',
|
||||
'slug' => 'pack-clinic',
|
||||
'settings' => ['onboarded' => true],
|
||||
]);
|
||||
$branch = \App\Models\Branch::create([
|
||||
'owner_ref' => $user->public_id,
|
||||
'organization_id' => $organization->id,
|
||||
'name' => 'Main',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$template = AssessmentTemplate::currentSystemByCode('universal_intake');
|
||||
$this->assertNotNull($template);
|
||||
$question = $template->questions()->orderBy('id')->first();
|
||||
$this->assertNotNull($question);
|
||||
$questionId = $question->id;
|
||||
|
||||
$patient = \App\Models\Patient::create([
|
||||
'owner_ref' => $user->public_id,
|
||||
'organization_id' => $organization->id,
|
||||
'branch_id' => $branch->id,
|
||||
'patient_number' => 'P-PACK-1',
|
||||
'first_name' => 'Pack',
|
||||
'last_name' => 'Patient',
|
||||
]);
|
||||
|
||||
$assessment = \App\Models\Assessment::create([
|
||||
'owner_ref' => $user->public_id,
|
||||
'organization_id' => $organization->id,
|
||||
'branch_id' => $branch->id,
|
||||
'patient_id' => $patient->id,
|
||||
'template_id' => $template->id,
|
||||
'status' => \App\Models\Assessment::STATUS_DRAFT,
|
||||
'assessed_at' => now(),
|
||||
]);
|
||||
|
||||
\App\Models\AssessmentAnswer::create([
|
||||
'owner_ref' => $user->public_id,
|
||||
'assessment_id' => $assessment->id,
|
||||
'question_id' => $questionId,
|
||||
'value_text' => 'kept',
|
||||
]);
|
||||
|
||||
// Delete nursing packs so ensureAssessmentCatalog re-runs the seeder.
|
||||
AssessmentTemplate::query()
|
||||
->whereNull('organization_id')
|
||||
->whereIn('code', ['news2', 'braden', 'morse', 'pain_nrs'])
|
||||
->delete();
|
||||
|
||||
app(\App\Services\Care\CareFeatures::class)->ensureAssessmentCatalog();
|
||||
|
||||
$this->assertDatabaseHas('care_assessment_questions', ['id' => $questionId]);
|
||||
$this->assertDatabaseHas('care_assessment_answers', [
|
||||
'assessment_id' => $assessment->id,
|
||||
'question_id' => $questionId,
|
||||
'value_text' => 'kept',
|
||||
]);
|
||||
$this->assertNotNull(AssessmentTemplate::currentSystemByCode('news2'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user