Skip to content

Commit

Permalink
[TM-921] Sync relations on form submissions.
Browse files Browse the repository at this point in the history
  • Loading branch information
roguenet committed May 22, 2024
1 parent bee264f commit 733e222
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
17 changes: 11 additions & 6 deletions app/Models/Traits/UsesLinkedFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 733e222

Please sign in to comment.