Skip to content

Commit

Permalink
Use correct end date for test run depending on org set timezone.
Browse files Browse the repository at this point in the history
  • Loading branch information
apognu committed Feb 24, 2025
1 parent 101186a commit 78d0e77
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 8 additions & 1 deletion api/handle_scenario_test_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ func handleCreateScenarioTestRun(uc usecases.Usecases) func(c *gin.Context) {
c.Status(http.StatusBadRequest)
return
}

orgUsecase := usecasesWithCreds(ctx, uc).NewOrganizationUseCase()
org, err := orgUsecase.GetOrganization(c.Request.Context(), organizationId)
if presentError(ctx, c, err) {
return
}

usecase := usecasesWithCreds(ctx, uc).NewScenarioTestRunUseCase()
input, err := dto.AdaptCreateScenarioTestRunBody(data)
input, err := dto.AdaptCreateScenarioTestRunBody(data, org.DefaultScenarioTimezone)
if presentError(ctx, c, err) {
return
}
Expand Down
17 changes: 16 additions & 1 deletion dto/scenario_testrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ type CreateScenarioTestRunBody struct {
EndDate time.Time `json:"end_date"`
}

func AdaptCreateScenarioTestRunBody(dto CreateScenarioTestRunBody) (models.ScenarioTestRunInput, error) {
func AdaptCreateScenarioTestRunBody(dto CreateScenarioTestRunBody, tzName *string) (models.ScenarioTestRunInput, error) {
tz := time.UTC

if tzName != nil {
orgTz, err := time.LoadLocation(*tzName)
if err == nil {
tz = orgTz
}
}

// Frontend always sends UTC timestamps granular to the day, which means it will be "the day before at 11pm".
// We add enough hours to be in the right day (passing any potential DST changes), then build a new end date
// in the organization timezone to represent the end of the selected day.
dto.EndDate = dto.EndDate.Add(5 * time.Hour)
dto.EndDate = time.Date(dto.EndDate.Year(), dto.EndDate.Month(), dto.EndDate.Day(), 23, 59, 59, 999999999, tz)

return models.ScenarioTestRunInput{
ScenarioId: dto.ScenarioId,
EndDate: dto.EndDate,
Expand Down

0 comments on commit 78d0e77

Please sign in to comment.