diff --git a/CHANGELOG.md b/CHANGELOG.md index 372d567d..7d39c24d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ "@hourly": "0 * * * *" ``` +- Fill the `LastVerificationSentAt` and `LastResetSentAt` fields only after a successfull email send. + ## v0.17.5 diff --git a/forms/admin_password_reset_request.go b/forms/admin_password_reset_request.go index 99edaf41..cecdfb69 100644 --- a/forms/admin_password_reset_request.go +++ b/forms/admin_password_reset_request.go @@ -75,14 +75,14 @@ func (form *AdminPasswordResetRequest) Submit(interceptors ...InterceptorFunc[*m return errors.New("You have already requested a password reset.") } - // update last sent timestamp - admin.LastResetSentAt = types.NowDateTime() - return runInterceptors(admin, func(m *models.Admin) error { if err := mails.SendAdminPasswordReset(form.app, m); err != nil { return err } + // update last sent timestamp + m.LastResetSentAt = types.NowDateTime() + return form.dao.SaveAdmin(m) }, interceptors...) } diff --git a/forms/admin_password_reset_request_test.go b/forms/admin_password_reset_request_test.go index 9bb4fd13..79f304b5 100644 --- a/forms/admin_password_reset_request_test.go +++ b/forms/admin_password_reset_request_test.go @@ -117,7 +117,7 @@ func TestAdminPasswordResetRequestInterceptors(t *testing.T) { t.Fatalf("Expected interceptor2 to be called") } - if interceptorLastResetSentAt.String() == admin.LastResetSentAt.String() { - t.Fatalf("Expected the form model to be filled before calling the interceptors") + if interceptorLastResetSentAt.String() != admin.LastResetSentAt.String() { + t.Fatalf("Expected the form model to NOT be filled before calling the interceptors") } } diff --git a/forms/record_password_reset_request.go b/forms/record_password_reset_request.go index 2dc900af..286878ed 100644 --- a/forms/record_password_reset_request.go +++ b/forms/record_password_reset_request.go @@ -78,14 +78,14 @@ func (form *RecordPasswordResetRequest) Submit(interceptors ...InterceptorFunc[* return errors.New("You've already requested a password reset.") } - // update last sent timestamp - authRecord.Set(schema.FieldNameLastResetSentAt, types.NowDateTime()) - return runInterceptors(authRecord, func(m *models.Record) error { if err := mails.SendRecordPasswordReset(form.app, m); err != nil { return err } + // update last sent timestamp + m.Set(schema.FieldNameLastResetSentAt, types.NowDateTime()) + return form.dao.SaveRecord(m) }, interceptors...) } diff --git a/forms/record_password_reset_request_test.go b/forms/record_password_reset_request_test.go index a81ec47c..1755282e 100644 --- a/forms/record_password_reset_request_test.go +++ b/forms/record_password_reset_request_test.go @@ -164,7 +164,7 @@ func TestRecordPasswordResetRequestInterceptors(t *testing.T) { t.Fatalf("Expected interceptor2 to be called") } - if interceptorLastResetSentAt.String() == authRecord.LastResetSentAt().String() { - t.Fatalf("Expected the form model to be filled before calling the interceptors") + if interceptorLastResetSentAt.String() != authRecord.LastResetSentAt().String() { + t.Fatalf("Expected the form model to NOT be filled before calling the interceptors") } } diff --git a/forms/record_verification_request.go b/forms/record_verification_request.go index 7c1686bb..09b46799 100644 --- a/forms/record_verification_request.go +++ b/forms/record_verification_request.go @@ -82,9 +82,6 @@ func (form *RecordVerificationRequest) Submit(interceptors ...InterceptorFunc[*m if (now.Sub(lastVerificationSentAt)).Seconds() < form.resendThreshold { return errors.New("A verification email was already sent.") } - - // update last sent timestamp - record.SetLastVerificationSentAt(types.NowDateTime()) } return runInterceptors(record, func(m *models.Record) error { @@ -96,6 +93,9 @@ func (form *RecordVerificationRequest) Submit(interceptors ...InterceptorFunc[*m return err } + // update last sent timestamp + m.SetLastVerificationSentAt(types.NowDateTime()) + return form.dao.SaveRecord(m) }, interceptors...) } diff --git a/forms/record_verification_request_test.go b/forms/record_verification_request_test.go index 797fd8ad..598ebbd6 100644 --- a/forms/record_verification_request_test.go +++ b/forms/record_verification_request_test.go @@ -182,7 +182,7 @@ func TestRecordVerificationRequestInterceptors(t *testing.T) { t.Fatalf("Expected interceptor2 to be called") } - if interceptorLastVerificationSentAt.String() == authRecord.LastVerificationSentAt().String() { - t.Fatalf("Expected the form model to be filled before calling the interceptors") + if interceptorLastVerificationSentAt.String() != authRecord.LastVerificationSentAt().String() { + t.Fatalf("Expected the form model to NOT be filled before calling the interceptors") } }