From a11fb01bef24abf954fbe0fa120116abe1d3f933 Mon Sep 17 00:00:00 2001 From: Chirag Chhatrala <60499540+chiragchhatrala@users.noreply.github.com> Date: Thu, 5 Jun 2025 20:28:24 +0530 Subject: [PATCH] =?UTF-8?q?ESC-590=20-=20Fix=20UUID=20and=20Auto=20Increme?= =?UTF-8?q?nt=20ID=20Generation=20Logic=20in=20StoreFormSubmissio=E2=80=A6?= =?UTF-8?q?=20(#774)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix UUID and Auto Increment ID Generation Logic in StoreFormSubmissionJob - Updated the conditions for generating UUID and auto-increment IDs in the `StoreFormSubmissionJob` to ensure they only trigger when the answer value is not provided. This change enhances the logic for handling form submissions, particularly for users with non-pro subscriptions, by preventing unnecessary ID generation when an answer is already present. These modifications aim to improve the accuracy of form submissions and ensure proper handling of ID generation based on user subscription status. * Enhance ID Generation Logic in StoreFormSubmissionJob - Updated conditions for UUID and auto-increment ID generation in `StoreFormSubmissionJob` to ensure they only trigger when the answer value is either not provided or invalid. This change improves the accuracy of ID generation based on user subscription status, preventing unnecessary ID creation when valid input is present. These modifications aim to enhance the reliability of form submissions and ensure proper handling of ID generation features. * Test case --- api/app/Jobs/Form/StoreFormSubmissionJob.php | 4 +- api/tests/Feature/Forms/FormUpdateTest.php | 55 +++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/api/app/Jobs/Form/StoreFormSubmissionJob.php b/api/app/Jobs/Form/StoreFormSubmissionJob.php index 5326c237..42a7f1ad 100644 --- a/api/app/Jobs/Form/StoreFormSubmissionJob.php +++ b/api/app/Jobs/Form/StoreFormSubmissionJob.php @@ -179,9 +179,9 @@ class StoreFormSubmissionJob implements ShouldQueue } } else { // Standard field processing (text, ID generation, etc.) - if ($field['type'] == 'text' && isset($field['generates_uuid']) && $field['generates_uuid']) { + if ((!$answerValue || !Str::isUuid($answerValue)) && $field['type'] == 'text' && isset($field['generates_uuid']) && $field['generates_uuid']) { $finalData[$field['id']] = ($this->form->is_pro) ? Str::uuid()->toString() : 'Please upgrade your OpenForm subscription to use our ID generation features'; - } elseif ($field['type'] == 'text' && isset($field['generates_auto_increment_id']) && $field['generates_auto_increment_id']) { + } elseif ((!$answerValue || !is_int($answerValue)) && $field['type'] == 'text' && isset($field['generates_auto_increment_id']) && $field['generates_auto_increment_id']) { $finalData[$field['id']] = ($this->form->is_pro) ? (string) ($this->form->submissions_count + 1) : 'Please upgrade your OpenForm subscription to use our ID generation features'; } else { $finalData[$field['id']] = $answerValue; diff --git a/api/tests/Feature/Forms/FormUpdateTest.php b/api/tests/Feature/Forms/FormUpdateTest.php index e4efb476..22fa80e8 100644 --- a/api/tests/Feature/Forms/FormUpdateTest.php +++ b/api/tests/Feature/Forms/FormUpdateTest.php @@ -1,6 +1,6 @@ actingAsProUser(); @@ -39,3 +39,56 @@ it('can update form with existing record', function () { expect($response->json('data.' . $nameProperty['id']))->toBe('Testing Updated'); } }); + +it('can update form with existing record but generates_uuid field is not update', function () { + $user = $this->actingAsProUser(); + $workspace = $this->createUserWorkspace($user); + $form = $this->createForm($user, $workspace, [ + 'editable_submissions' => true, + 'properties' => [ + [ + 'id' => 'uuid_field', + 'type' => 'text', + 'generates_uuid' => true, + 'name' => 'UUID Field' + ], + [ + 'id' => 'name', + 'type' => 'text', + 'name' => 'Name' + ] + ] + ]); + + $response = $this->postJson(route('forms.answer', $form->slug), ['name' => 'Testing', 'uuid_field' => null]) + ->assertSuccessful() + ->assertJson([ + 'type' => 'success', + 'message' => 'Form submission saved.', + ]); + $submissionId = $response->json('submission_id'); + expect($submissionId)->toBeString(); + $response = $this->getJson(route('forms.fetchSubmission', [$form->slug, $submissionId])) + ->assertSuccessful(); + $uuid = $response->json('data.uuid_field'); + expect(Str::isUuid($uuid))->toBeTrue(); + + if ($submissionId) { + $formData = $this->generateFormSubmissionData($form, ['submission_id' => $submissionId, 'name' => 'Testing Updated', 'uuid_field' => $uuid]); + $response = $this->postJson(route('forms.answer', $form->slug), $formData) + ->assertSuccessful() + ->assertJson([ + 'type' => 'success', + 'message' => 'Form submission saved.', + ]); + $submissionId2 = $response->json('submission_id'); + expect($submissionId2)->toBeString(); + expect($submissionId2)->toBe($submissionId); + + $response = $this->getJson(route('forms.fetchSubmission', [$form->slug, $submissionId])) + ->assertSuccessful(); + expect($response->json('data.name'))->toBe('Testing Updated'); + $uuid2 = $response->json('data.uuid_field'); + expect($uuid2)->toBe($uuid); + } +});