Skip to content

Commit

Permalink
feat(api): Add DTSTART equivalent support to spending/funding. (#1329)
Browse files Browse the repository at this point in the history
Right now we do not store the DTSTART component of the RRule, this has
led to some bugs already where we have to assume the DTSTART based on
some educated guess data. This can lead to wrong calculations in the
future. So going forward I want to support this parameter. This is the
start of the work on that.
  • Loading branch information
elliotcourant committed Feb 20, 2023
1 parent d27edfa commit 4db243e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/migrations/schema/2023022000_DateStarted.tx.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE "spending" ADD COLUMN "date_started" TIMESTAMPTZ NULL;
UPDATE "spending" SET "date_started" = "next_recurrence" WHERE "date_started" IS NULL;
ALTER TABLE "spending" ALTER COLUMN "date_started" SET NOT NULL;

ALTER TABLE "funding_schedules" ADD COLUMN "date_started" TIMESTAMPTZ NULL;
UPDATE "funding_schedules" SET "date_started" = "next_occurrence" WHERE "date_started" IS NULL;
ALTER TABLE "funding_schedules" ALTER COLUMN "date_started" SET NOT NULL;
14 changes: 14 additions & 0 deletions pkg/models/funding_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"time"

"github.com/getsentry/sentry-go"
"github.com/go-pg/pg/v10"
"github.com/monetr/monetr/pkg/crumbs"
"github.com/monetr/monetr/pkg/util"
)

var _ pg.BeforeInsertHook = (*FundingSchedule)(nil)

type FundingSchedule struct {
tableName string `pg:"funding_schedules"`

Expand All @@ -25,6 +28,7 @@ type FundingSchedule struct {
EstimatedDeposit *int64 `json:"estimatedDeposit" pg:"estimated_deposit"`
LastOccurrence *time.Time `json:"lastOccurrence" pg:"last_occurrence"`
NextOccurrence time.Time `json:"nextOccurrence" pg:"next_occurrence,notnull"`
DateStarted time.Time `json:"dateStarted" pg:"date_started,notnull"`
}

func (f *FundingSchedule) GetNumberOfContributionsBetween(start, end time.Time, timezone *time.Location) int64 {
Expand Down Expand Up @@ -128,3 +132,13 @@ func (f *FundingSchedule) CalculateNextOccurrence(ctx context.Context, timezone

return true
}

func (f *FundingSchedule) BeforeInsert(ctx context.Context) (context.Context, error) {
// Make sure when we are creating a funding schedule that we set the date started field for the first instance. This
// way subsequent rule evaluations can use this date started as a reference point.
if f.DateStarted.IsZero() {
f.DateStarted = f.NextOccurrence
}

return ctx, nil
}
14 changes: 14 additions & 0 deletions pkg/models/spending.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"time"

"github.com/go-pg/pg/v10"
"github.com/monetr/monetr/pkg/crumbs"
"github.com/monetr/monetr/pkg/internal/myownsanity"
"github.com/monetr/monetr/pkg/util"
Expand All @@ -19,6 +20,8 @@ const (
SpendingTypeOverflow
)

var _ pg.BeforeInsertHook = (*Spending)(nil)

type Spending struct {
tableName string `pg:"spending"`

Expand All @@ -42,6 +45,7 @@ type Spending struct {
IsBehind bool `json:"isBehind" pg:"is_behind,notnull,use_zero"`
IsPaused bool `json:"isPaused" pg:"is_paused,notnull,use_zero"`
DateCreated time.Time `json:"dateCreated" pg:"date_created,notnull"`
DateStarted time.Time `json:"dateStarted" pg:"date_started,notnull"`
}

func (e Spending) GetIsStale(now time.Time) bool {
Expand Down Expand Up @@ -179,3 +183,13 @@ func (e *Spending) CalculateNextContribution(

return nil
}

func (s *Spending) BeforeInsert(ctx context.Context) (context.Context, error) {
// Make sure when we are creating a funding schedule that we set the date started field for the first instance. This
// way subsequent rule evaluations can use this date started as a reference point.
if s.DateStarted.IsZero() {
s.DateStarted = s.NextRecurrence
}

return ctx, nil
}
1 change: 1 addition & 0 deletions pkg/repository/expenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (r *repositoryBase) CreateSpending(ctx context.Context, spending *models.Sp

spending.AccountId = r.AccountId()
spending.DateCreated = time.Now().UTC()
spending.DateStarted = spending.NextRecurrence

if _, err := r.txn.ModelContext(span.Context(), spending).Insert(spending); err != nil {
span.Status = sentry.SpanStatusInternalError
Expand Down
2 changes: 2 additions & 0 deletions pkg/repository/funding_schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func (r *repositoryBase) CreateFundingSchedule(ctx context.Context, fundingSched
}

fundingSchedule.AccountId = r.AccountId()
fundingSchedule.DateStarted = fundingSchedule.NextOccurrence

if _, err := r.txn.ModelContext(span.Context(), fundingSchedule).Insert(fundingSchedule); err != nil {
span.Status = sentry.SpanStatusInternalError
return errors.Wrap(err, "failed to create funding schedule")
Expand Down

0 comments on commit 4db243e

Please sign in to comment.