From 733e222fec2d725d0e7040c736002c4ed3644e87 Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Wed, 22 May 2024 12:13:04 -0700 Subject: [PATCH] [TM-921] Sync relations on form submissions. --- .../V2/Forms/UpdateFormSubmissionController.php | 3 +-- app/Models/Traits/UsesLinkedFields.php | 17 +++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/V2/Forms/UpdateFormSubmissionController.php b/app/Http/Controllers/V2/Forms/UpdateFormSubmissionController.php index 05869883e..9822b2bec 100644 --- a/app/Http/Controllers/V2/Forms/UpdateFormSubmissionController.php +++ b/app/Http/Controllers/V2/Forms/UpdateFormSubmissionController.php @@ -4,7 +4,6 @@ use App\Http\Controllers\Controller; use App\Http\Requests\V2\Forms\UpdateFormSubmissionRequest; -use App\Http\Resources\V2\Forms\AnswersCollection; use App\Http\Resources\V2\Forms\FormSubmissionResource; use App\Models\V2\Forms\FormSubmission; use Illuminate\Support\Facades\App; @@ -18,7 +17,7 @@ public function __invoke(FormSubmission $formSubmission, UpdateFormSubmissionReq $data = $updateFormSubmissionRequest->validated(); - $data['answers'] = $formSubmission->updateAllAnswers($updateFormSubmissionRequest->get('answers')); //AnswersCollection::fromArray($updateFormSubmissionRequest->get('answers')); + $data['answers'] = $formSubmission->updateAllAnswers($updateFormSubmissionRequest->get('answers')); $data['user_id'] = Auth::user()->uuid; $formSubmission->update($data); diff --git a/app/Models/Traits/UsesLinkedFields.php b/app/Models/Traits/UsesLinkedFields.php index 7a4d13b80..9f1e3216e 100644 --- a/app/Models/Traits/UsesLinkedFields.php +++ b/app/Models/Traits/UsesLinkedFields.php @@ -244,29 +244,34 @@ private function updateLinkedFieldValue(array $linkedFieldInfo, $answer): void if ($linkedFieldInfo['link-type'] == 'fields') { $model->$property = $answer; $model->save(); + } elseif ($linkedFieldInfo['link-type'] == 'relations') { + $inputType = data_get($linkedFieldInfo, 'input_type'); + $this->syncRelation($property, $inputType, $answer, $model); } } - private function syncRelation(string $property, string $inputType, $data): void + private function syncRelation(string $property, string $inputType, $data, $model = null): void { + $model ??= $this; + // This will expand as we complete more tickets in TM-747, until eventually we support all form relations. if (! in_array($inputType, ['treeSpecies', 'workdays'])) { return; } - $class = get_class($this->$property()->make()); + $class = get_class($model->$property()->make()); if (is_a($class, HandlesLinkedFieldSync::class, true)) { - $class::syncRelation($this, $property, $data); + $class::syncRelation($model, $property, $data); return; } - $this->$property()->whereNotIn('uuid', $data->pluck('uuid')->filter())->delete(); + $model->$property()->whereNotIn('uuid', $data->pluck('uuid')->filter())->delete(); // This would be better as a bulk operation, but too much processing is required to make that feasible // in Eloquent (upsert isn't supported on MorphMany, for instance), and these sets will always be small // so doing them one at a time is OK. - $entries = $this->$property()->get(); + $entries = $model->$property()->get(); foreach ($data as $entry) { $model = null; if (! empty($entry['uuid'])) { @@ -277,7 +282,7 @@ private function syncRelation(string $property, string $inputType, $data): void } else { // protection against updating a deleted entry unset($entry['uuid']); - $this->$property()->create($entry); + $model->$property()->create($entry); } } }