eloquent-driver icon indicating copy to clipboard operation
eloquent-driver copied to clipboard

prevent crated_at being null while submitting a from submission

Open enespolat24 opened this issue 3 years ago • 15 comments

Hi , i was tinkering with form submission then i noticed that created_at being null. Can you review my changes to see if it fixes the problem. thank you

image

enespolat24 avatar Sep 15 '22 16:09 enespolat24

thank you for your help @ryanmitchell

enespolat24 avatar Sep 15 '22 18:09 enespolat24

this pr should fix #61

enespolat24 avatar Sep 21 '22 16:09 enespolat24

Hi, beside the modifications you've made I'd suggest to make the firstOrNew attribute based on the id, not on form_id and timestamp, to prevent parallel submissions with the same timestamp to overwrite each other. `public function toModel() { $class = app('statamic.eloquent.forms.submission_model'); $timestamp = (new $class)->fromDateTime($this->date());

    $model = $class::findOrNew($this->id());
    return (!empty($model->id)) ? $model->fill([
        'data' => $this->data,
    ]) : $model->fill([
        'data' => $this->data,
        'form_id' => $this->form->model()->id,
        'created_at' => $timestamp,
    ]);
}`

andrasvati avatar Sep 26 '22 06:09 andrasvati

This breaks all made submissions when they are shown in statamic and is therefore a critical bug.

@jasonvarga can you take a look and merge if possible?

okaufmann avatar Sep 26 '22 14:09 okaufmann

The overwrites are real, I just checked a project where I use the latest version of eloquent-driver and some submissions got overwritten… Fortunately, Emails were sent for the certain submissions, so the data isn't lost.

@enespolat24 would you mind adding some tests, which cover these two bugs, so they never happen again?

okaufmann avatar Sep 26 '22 15:09 okaufmann

I'm working on it

enespolat24 avatar Sep 26 '22 15:09 enespolat24

@okaufmann i made some changes. I don't fully understand the overwrite situation. Do you have a chance to send an example case or video?

Thank you for your help !

enespolat24 avatar Sep 26 '22 20:09 enespolat24

i also need help about tests

enespolat24 avatar Sep 26 '22 20:09 enespolat24

@enespolat24 I have the tests working... can you add me to your fork so I can push to it?

ryanmitchell avatar Sep 28 '22 08:09 ryanmitchell

@enespolat24 Sorry for the delay.

I don't fully understand the overwrite situation. Do you have a chance to send an example case or video? In the current state, the date field will not be saved. Therefore, a form can only have one submission (as the filter is form_id and created_at, where created_at is always null).

@ryanmitchell Is there anything I could help? I have time now

okaufmann avatar Sep 28 '22 11:09 okaufmann

@okaufmann i added a test here - seems to work ok for me: https://github.com/statamic/eloquent-driver/blob/cc3780c3a9654346206cb1de4ba96a9c181ca234/tests/Forms/FormSubmissionTest.php#L56

ryanmitchell avatar Sep 28 '22 11:09 ryanmitchell

@ryanmitchell I would also freeze the time, so it creates two submissions width the same timestamp. So we can be sure it does not overwrite any submission.

okaufmann avatar Sep 28 '22 11:09 okaufmann

@okaufmann that will fail due to the unique constraint in the database schema. Two submissions should never have the same microsecond time...

ryanmitchell avatar Sep 28 '22 11:09 ryanmitchell

@ryanmitchell You're right. Then the tests cover all cases 💪

okaufmann avatar Sep 29 '22 11:09 okaufmann

@jasonvarga can you check if this is mergeable. This fixes a serious problem we face

enespolat24 avatar Oct 20 '22 09:10 enespolat24