Skip to content

Commit 07596ea

Browse files
committed
repos: segments and validate_test updates
1 parent 071502a commit 07596ea

2 files changed

Lines changed: 5 additions & 8 deletions

File tree

internal/repositories/segments.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ func validateSegmentArgs(tokenID int, from, to time.Time) error {
3434
return fmt.Errorf("from and to times cannot be equal")
3535
}
3636

37-
if to.After(time.Now()) {
38-
return fmt.Errorf("to time cannot be in the future")
39-
}
37+
// to in future is handled by caller (GetSegments caps to now before calling)
4038

4139
maxDuration := maxDateRangeDays * 24 * time.Hour
4240
if to.Sub(from) > maxDuration {
@@ -190,7 +188,11 @@ func sortSegmentSignals(signals []*model.SignalAggregationValue) {
190188
// GetSegments returns segments detected using the specified mechanism in the time range.
191189
// Pagination: pass after (exclusive cursor = startTime of last segment from previous page) and limit (default 100, max 200).
192190
// Segments are ordered by startTime ascending. When after is set, only segments with startTime > after are requested from CH.
191+
// If to is in the future (e.g. client sent end-of-day in user TZ), it is capped to now so the query succeeds.
193192
func (r *Repository) GetSegments(ctx context.Context, tokenID int, from, to time.Time, mechanism model.DetectionMechanism, config *model.SegmentConfig, signalRequests []*model.SegmentSignalRequest, eventRequests []*model.SegmentEventRequest, limit *int, after *time.Time) ([]*model.Segment, error) {
193+
if now := time.Now(); to.After(now) {
194+
to = now
195+
}
194196
if err := validateSegmentArgs(tokenID, from, to); err != nil {
195197
return nil, errorhandler.NewBadRequestError(ctx, err)
196198
}

internal/repositories/validate_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ func TestValidateSegmentArgs(t *testing.T) {
7979
require.Error(t, err)
8080
})
8181

82-
t.Run("to in future", func(t *testing.T) {
83-
err := validateSegmentArgs(1, validFrom, time.Now().Add(time.Hour))
84-
require.ErrorContains(t, err, "to time cannot be in the future")
85-
})
86-
8782
t.Run("date range exceeded", func(t *testing.T) {
8883
from := validTo.Add(-32 * 24 * time.Hour) // max is 31 days
8984
err := validateSegmentArgs(1, from, validTo)

0 commit comments

Comments
 (0)