From 15dbce86b619a406219e80ff8132aff5d6806cc8 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 5 Sep 2024 17:34:14 +0300 Subject: [PATCH 1/4] fix timezone bug --- .../service/handlers/daily_question_create.go | 13 +++++---- .../service/handlers/daily_question_delete.go | 28 +++++++++++++------ .../service/handlers/daily_question_edit.go | 17 +++++------ 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/internal/service/handlers/daily_question_create.go b/internal/service/handlers/daily_question_create.go index c6646b2..0c59022 100644 --- a/internal/service/handlers/daily_question_create.go +++ b/internal/service/handlers/daily_question_create.go @@ -47,16 +47,19 @@ func CreateDailyQuestion(w http.ResponseWriter, r *http.Request) { })...) return } - nowTime := time.Now().UTC() - if !timeReq.After(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, DailyQuestions(r).Location)) { - Log(r).Errorf("Arg start_at must be more or equal tomorow midnoght noe: %s", timeReq.String()) + + nowTime := time.Now().In(location).UTC() + TimeDeadline := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, location).UTC() + + if TimeDeadline.AddDate(0, 0, -1).After(timeReq.UTC()) { + Log(r).Errorf("Error %s", timeReq.UTC().String()) ape.RenderErr(w, problems.BadRequest(validation.Errors{ - "starts_at": fmt.Errorf("argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.String()), + "starts_at": fmt.Errorf("argument start_at must be more or equal tomorow mid night now: %s", timeReq.UTC().String()), })...) return } - question, err := DailyQuestionsQ(r).FilterDayQuestions(timeReq).Get() + question, err := DailyQuestionsQ(r).FilterDayQuestions(timeReq.UTC()).Get() if err != nil { Log(r).WithError(err).Error("Error on this day") ape.RenderErr(w, problems.InternalError()) diff --git a/internal/service/handlers/daily_question_delete.go b/internal/service/handlers/daily_question_delete.go index fbc308c..c79b2db 100644 --- a/internal/service/handlers/daily_question_delete.go +++ b/internal/service/handlers/daily_question_delete.go @@ -9,6 +9,7 @@ import ( "time" "github.com/go-chi/chi" + validation "github.com/go-ozzo/ozzo-validation/v4" "github.com/rarimo/geo-auth-svc/pkg/auth" "github.com/rarimo/geo-points-svc/internal/data" "github.com/rarimo/geo-points-svc/resources" @@ -42,13 +43,24 @@ func DeleteDailyQuestion(w http.ResponseWriter, r *http.Request) { ape.RenderErr(w, problems.NotFound()) return } - deletedQuestion := *question - timeReq := question.StartsAt - nowTime := time.Now().UTC() - if !timeReq.After(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, DailyQuestions(r).Location)) { - Log(r).Errorf("Only questions that start tomorrow or later can be delete: %s", timeReq.String()) - ape.RenderErr(w, problems.BadRequest(err)...) + location := DailyQuestions(r).Location + timeReq, err := time.ParseInLocation("2006-01-02 15:04:05 -0700 MST", question.StartsAt.String(), location) + if err != nil { + Log(r).WithError(err).Error("Failed to parse start time") + ape.RenderErr(w, problems.BadRequest(validation.Errors{ + "starts_at": fmt.Errorf("failed to parse start time %s err: %s", question.StartsAt.String(), err), + })...) + return + } + + nowTime := time.Now().In(location).UTC() + + if timeReq.UTC().Before(nowTime.AddDate(0, 0, 1)) { + Log(r).Errorf("Error %s", timeReq.UTC().String()) + ape.RenderErr(w, problems.BadRequest(validation.Errors{ + "starts_at": fmt.Errorf("argument start_at must be more or equal tomorow mid night now: %s", timeReq.UTC().String()), + })...) return } @@ -58,8 +70,8 @@ func DeleteDailyQuestion(w http.ResponseWriter, r *http.Request) { ape.RenderErr(w, problems.InternalError()) return } - loc := DailyQuestions(r).Location - response, err := NewDailyQuestionDelete(ID, deletedQuestion, loc) + + response, err := NewDailyQuestionDelete(ID, *question, location) if err != nil { Log(r).WithError(err).Error("Error deleting daily question") ape.RenderErr(w, problems.InternalError()) diff --git a/internal/service/handlers/daily_question_edit.go b/internal/service/handlers/daily_question_edit.go index 819cf11..a46b0ed 100644 --- a/internal/service/handlers/daily_question_edit.go +++ b/internal/service/handlers/daily_question_edit.go @@ -53,8 +53,10 @@ func EditDailyQuestion(w http.ResponseWriter, r *http.Request) { return } - nowTime := time.Now().UTC() - if !question.StartsAt.After(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, DailyQuestions(r).Location)) { + location := DailyQuestions(r).Location + nowTime := time.Now().In(location).UTC() + + if question.StartsAt.UTC().Before(nowTime) { Log(r).Errorf("Cannot change a question id: %v that is available today or in the past", ID) ape.RenderErr(w, problems.BadRequest(validation.Errors{ "starts_at": fmt.Errorf("cannot change a question id: %v that is available today or in the past", ID), @@ -69,7 +71,6 @@ func EditDailyQuestion(w http.ResponseWriter, r *http.Request) { } if attributes.StartsAt != nil { - location := DailyQuestions(r).Location timeReq, err := time.ParseInLocation("2006-01-02", *attributes.StartsAt, location) if err != nil { Log(r).WithError(err).Error("Failed to parse start time") @@ -78,16 +79,16 @@ func EditDailyQuestion(w http.ResponseWriter, r *http.Request) { })...) return } - nowTime := time.Now().UTC() - if !timeReq.After(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, DailyQuestions(r).Location)) { - Log(r).Errorf("Argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.String()) + + if !timeReq.After(nowTime.UTC().AddDate(0, 0, -1)) { + Log(r).Errorf("Argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.UTC().String()) ape.RenderErr(w, problems.BadRequest(validation.Errors{ - "starts_at": fmt.Errorf("argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.String()), + "starts_at": fmt.Errorf("argument start_at must be more or equal tomorow midnoght now its: %s", timeReq.UTC().String()), })...) return } - question, err := DailyQuestionsQ(r).FilterDayQuestions(timeReq).Get() + question, err := DailyQuestionsQ(r).FilterDayQuestions(timeReq.UTC()).Get() if err != nil { Log(r).WithError(err).Error("Error on this day") ape.RenderErr(w, problems.InternalError()) From c3d24276b51f711271e04abf80555a9fec9b7919 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 5 Sep 2024 17:54:50 +0300 Subject: [PATCH 2/4] fix start_at in req --- internal/service/handlers/daily_question_create.go | 2 +- internal/service/handlers/daily_question_delete.go | 4 ++-- internal/service/handlers/daily_question_edit.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/service/handlers/daily_question_create.go b/internal/service/handlers/daily_question_create.go index 0c59022..053d09c 100644 --- a/internal/service/handlers/daily_question_create.go +++ b/internal/service/handlers/daily_question_create.go @@ -48,7 +48,7 @@ func CreateDailyQuestion(w http.ResponseWriter, r *http.Request) { return } - nowTime := time.Now().In(location).UTC() + nowTime := time.Now().UTC() TimeDeadline := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, location).UTC() if TimeDeadline.AddDate(0, 0, -1).After(timeReq.UTC()) { diff --git a/internal/service/handlers/daily_question_delete.go b/internal/service/handlers/daily_question_delete.go index c79b2db..f2e4714 100644 --- a/internal/service/handlers/daily_question_delete.go +++ b/internal/service/handlers/daily_question_delete.go @@ -45,7 +45,7 @@ func DeleteDailyQuestion(w http.ResponseWriter, r *http.Request) { } location := DailyQuestions(r).Location - timeReq, err := time.ParseInLocation("2006-01-02 15:04:05 -0700 MST", question.StartsAt.String(), location) + timeReq, err := time.ParseInLocation("2006-01-02", question.StartsAt.Format("2006-01-02"), location) if err != nil { Log(r).WithError(err).Error("Failed to parse start time") ape.RenderErr(w, problems.BadRequest(validation.Errors{ @@ -54,7 +54,7 @@ func DeleteDailyQuestion(w http.ResponseWriter, r *http.Request) { return } - nowTime := time.Now().In(location).UTC() + nowTime := time.Now().UTC() if timeReq.UTC().Before(nowTime.AddDate(0, 0, 1)) { Log(r).Errorf("Error %s", timeReq.UTC().String()) diff --git a/internal/service/handlers/daily_question_edit.go b/internal/service/handlers/daily_question_edit.go index a46b0ed..d7dcf9f 100644 --- a/internal/service/handlers/daily_question_edit.go +++ b/internal/service/handlers/daily_question_edit.go @@ -54,7 +54,7 @@ func EditDailyQuestion(w http.ResponseWriter, r *http.Request) { } location := DailyQuestions(r).Location - nowTime := time.Now().In(location).UTC() + nowTime := time.Now().UTC() if question.StartsAt.UTC().Before(nowTime) { Log(r).Errorf("Cannot change a question id: %v that is available today or in the past", ID) From 58eed250596a22ff5331e11bca3b92b2c6e00c59 Mon Sep 17 00:00:00 2001 From: trpdjke Date: Thu, 5 Sep 2024 18:19:54 +0300 Subject: [PATCH 3/4] add option to delete and change today's questions --- internal/service/handlers/daily_question_delete.go | 2 +- internal/service/handlers/daily_question_edit.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/handlers/daily_question_delete.go b/internal/service/handlers/daily_question_delete.go index f2e4714..8fefdc1 100644 --- a/internal/service/handlers/daily_question_delete.go +++ b/internal/service/handlers/daily_question_delete.go @@ -56,7 +56,7 @@ func DeleteDailyQuestion(w http.ResponseWriter, r *http.Request) { nowTime := time.Now().UTC() - if timeReq.UTC().Before(nowTime.AddDate(0, 0, 1)) { + if timeReq.UTC().Before(nowTime.AddDate(0, 0, -2)) { Log(r).Errorf("Error %s", timeReq.UTC().String()) ape.RenderErr(w, problems.BadRequest(validation.Errors{ "starts_at": fmt.Errorf("argument start_at must be more or equal tomorow mid night now: %s", timeReq.UTC().String()), diff --git a/internal/service/handlers/daily_question_edit.go b/internal/service/handlers/daily_question_edit.go index d7dcf9f..88a247d 100644 --- a/internal/service/handlers/daily_question_edit.go +++ b/internal/service/handlers/daily_question_edit.go @@ -56,7 +56,7 @@ func EditDailyQuestion(w http.ResponseWriter, r *http.Request) { location := DailyQuestions(r).Location nowTime := time.Now().UTC() - if question.StartsAt.UTC().Before(nowTime) { + if question.StartsAt.UTC().Before(nowTime.AddDate(0, 0, -1)) { Log(r).Errorf("Cannot change a question id: %v that is available today or in the past", ID) ape.RenderErr(w, problems.BadRequest(validation.Errors{ "starts_at": fmt.Errorf("cannot change a question id: %v that is available today or in the past", ID), From 5a3cfec17736a0890b6909361b1cf6422cfd873f Mon Sep 17 00:00:00 2001 From: trpdjke Date: Fri, 6 Sep 2024 14:27:53 +0300 Subject: [PATCH 4/4] fix logic delete question --- internal/service/handlers/daily_question_create.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/handlers/daily_question_create.go b/internal/service/handlers/daily_question_create.go index 053d09c..e728c9f 100644 --- a/internal/service/handlers/daily_question_create.go +++ b/internal/service/handlers/daily_question_create.go @@ -49,9 +49,9 @@ func CreateDailyQuestion(w http.ResponseWriter, r *http.Request) { } nowTime := time.Now().UTC() - TimeDeadline := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day()+1, 0, 0, 0, 0, location).UTC() + TimeDeadline := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, location).UTC() - if TimeDeadline.AddDate(0, 0, -1).After(timeReq.UTC()) { + if TimeDeadline.After(timeReq.UTC()) { Log(r).Errorf("Error %s", timeReq.UTC().String()) ape.RenderErr(w, problems.BadRequest(validation.Errors{ "starts_at": fmt.Errorf("argument start_at must be more or equal tomorow mid night now: %s", timeReq.UTC().String()),