From d160afb782c4e8e0d3e6133625c98c3857af241b Mon Sep 17 00:00:00 2001 From: Antoine Popineau Date: Mon, 24 Feb 2025 09:01:46 +0100 Subject: [PATCH] Use correct end date for test run depending on org set timezone. --- api/handle_scenario_test_run.go | 9 ++++++++- dto/scenario_testrun.go | 14 +++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/api/handle_scenario_test_run.go b/api/handle_scenario_test_run.go index 0f23f7659..e710162b4 100644 --- a/api/handle_scenario_test_run.go +++ b/api/handle_scenario_test_run.go @@ -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 } diff --git a/dto/scenario_testrun.go b/dto/scenario_testrun.go index 73d599473..5f470e378 100644 --- a/dto/scenario_testrun.go +++ b/dto/scenario_testrun.go @@ -37,7 +37,19 @@ 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 + } + } + + // Adapt the browser-locale-dependent timestamp sent by the frontend to the end of day relative to the organization's timezone. + 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,