Skip to content

Commit

Permalink
Fix: user can get more than one events for daily question in one day …
Browse files Browse the repository at this point in the history
…if service will restarted
  • Loading branch information
Zaptoss committed Aug 26, 2024
1 parent 1c21c62 commit e2ae59b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type EventsQ interface {
Page(*pgdb.OffsetPageParams) EventsQ
Select() ([]Event, error)
Get() (*Event, error)
// GetLast order events by created_at field in descending order
GetLast() (*Event, error)
// Count returns the total number of events that match filters. Page is not
// applied to this method.
Count() (int, error)
Expand Down
16 changes: 16 additions & 0 deletions internal/data/pg/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type events struct {
deleter squirrel.DeleteBuilder
counter squirrel.SelectBuilder
reopenable squirrel.SelectBuilder
last squirrel.SelectBuilder
}

func NewEvents(db *pgdb.DB) data.EventsQ {
Expand All @@ -32,6 +33,7 @@ func NewEvents(db *pgdb.DB) data.EventsQ {
deleter: squirrel.Delete(eventsTable),
counter: squirrel.Select("COUNT(*) AS count").From(eventsTable),
reopenable: squirrel.Select("nullifier", "type").Distinct().From(eventsTable + " e1"),
last: squirrel.Select("*").From(eventTypesTable).OrderBy("created_at DESC"),
}
}

Expand Down Expand Up @@ -129,6 +131,19 @@ func (q *events) Get() (*data.Event, error) {
return &res, nil
}

func (q *events) GetLast() (*data.Event, error) {
var res data.Event

if err := q.db.Get(&res, q.last); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, fmt.Errorf("get last event: %w", err)
}

return &res, nil
}

func (q *events) Count() (int, error) {
var res struct {
Count int `db:"count"`
Expand Down Expand Up @@ -265,5 +280,6 @@ func (q *events) applyCondition(cond squirrel.Sqlizer) data.EventsQ {
q.deleter = q.deleter.Where(cond)
q.counter = q.counter.Where(cond)
q.reopenable = q.reopenable.Where(cond)
q.last = q.last.Where(cond)
return q
}
16 changes: 16 additions & 0 deletions internal/service/handlers/daily_question_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-chi/chi"
"github.com/rarimo/geo-auth-svc/pkg/auth"
"github.com/rarimo/geo-points-svc/internal/data"
"github.com/rarimo/geo-points-svc/internal/data/evtypes/models"
"github.com/rarimo/geo-points-svc/resources"
"gitlab.com/distributed_lab/ape"
"gitlab.com/distributed_lab/ape/problems"
Expand Down Expand Up @@ -45,6 +46,21 @@ func GetDailyQuestion(w http.ResponseWriter, r *http.Request) {
}

localDayStart := atDayStart(dq.LocalTime(time.Now().UTC()))

ev, err := EventsQ(r).FilterByNullifier(nullifier).FilterByType(models.TypeDailyQuestion).GetLast()
if err != nil {
log.WithError(err).Error("Failed to get last daily_question event")
ape.RenderErr(w, problems.InternalError())
return
}
if ev != nil &&
ev.CreatedAt > int32(localDayStart.Unix()) &&
ev.CreatedAt < int32(localDayStart.Add(24*time.Hour).Unix()) {
log.Debug("Points already accruing for daily question")
ape.RenderErr(w, problems.Conflict())
return
}

question, err := DailyQuestionsQ(r).FilterByStartsAtAfter(localDayStart).Get()
if err != nil {
log.WithError(err).Error("Failed to get question")
Expand Down
2 changes: 0 additions & 2 deletions resources/model_daily_questions_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package resources

type DailyQuestionsAttributes struct {
// Time limit after which it is impossible to answer the question. Calculated as current time + time for answer
Deadline int64 `json:"deadline"`
// Answer options. Minimum 2, maximum 6
Options []DailyQuestionOptions `json:"options"`
// Question title
Expand Down

0 comments on commit e2ae59b

Please sign in to comment.