Skip to content

Commit

Permalink
CORE-2016: added request body validation to the `POST /v1/plans/:plan…
Browse files Browse the repository at this point in the history
…-id/rates` endpoint
  • Loading branch information
slr71 committed Oct 30, 2024
1 parent 5d84e2c commit 99476db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/controllers/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ func (s Server) AddPlanRates(ctx echo.Context) error {
if err = ctx.Bind(&planRateList); err != nil {
return model.Error(ctx, err.Error(), http.StatusBadRequest)
}
if err = planRateList.Validate(); err != nil {
return model.Error(ctx, err.Error(), http.StatusBadRequest)
}

// Begin a transaction.
return s.GORMDB.Transaction(func(tx *gorm.DB) error {
Expand Down
9 changes: 9 additions & 0 deletions internal/httpmodel/new_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,15 @@ func (prl *NewPlanRateList) Validate() error {
}
}

// Check for multiple plan rates with the same effective date.
uniquePlanRates := make(map[int64]bool)
for _, pr := range prl.PlanRates {
if uniquePlanRates[pr.EffectiveDate.UnixMilli()] {
return fmt.Errorf("multiple plan rates found with the same effective date")
}
uniquePlanRates[pr.EffectiveDate.UnixMilli()] = true
}

return nil
}

Expand Down

0 comments on commit 99476db

Please sign in to comment.