From 94bd67e2395c3b15cd993d19845c0a552bfd8020 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 27 May 2024 17:47:08 +0200 Subject: [PATCH 1/2] fix tp plan start date during export --- .../trainingpeaks/plan/TPPlanRepository.kt | 4 +--- .../workout/TPToWorkoutConverter.kt | 4 ++-- .../workout/TrainingPeaksWorkoutRepository.kt | 17 ++++------------- .../workout/structure/TPTargetMapper.kt | 2 -- 4 files changed, 7 insertions(+), 20 deletions(-) diff --git a/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/plan/TPPlanRepository.kt b/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/plan/TPPlanRepository.kt index da0075b..6bf013e 100644 --- a/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/plan/TPPlanRepository.kt +++ b/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/plan/TPPlanRepository.kt @@ -59,10 +59,8 @@ class TPPlanRepository( } private fun toLibraryContainer(planDto: TPPlanDto): LibraryContainer { - return LibraryContainer( + return LibraryContainer.planFromMonday( planDto.title, - planDto.startDate ?: Date.thisMonday(), - true, planDto.workoutCount, ExternalData.empty().withTrainingPeaks(planDto.planId) ) diff --git a/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/TPToWorkoutConverter.kt b/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/TPToWorkoutConverter.kt index d6f2631..37b864b 100644 --- a/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/TPToWorkoutConverter.kt +++ b/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/TPToWorkoutConverter.kt @@ -38,8 +38,8 @@ class TPToWorkoutConverter { val workoutsStructure = tpWorkout.structure?.let { try { toWorkoutStructure(it) - } catch (e: Exception) { - log.warn("Can't convert workout - ${tpWorkout.title}, error - ${e.message}'", e) + } catch (e: IllegalArgumentException) { + log.warn("Can't convert workout - ${tpWorkout.title}, error - ${e.message}'") null } } diff --git a/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/TrainingPeaksWorkoutRepository.kt b/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/TrainingPeaksWorkoutRepository.kt index 831b626..adf9f1e 100644 --- a/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/TrainingPeaksWorkoutRepository.kt +++ b/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/TrainingPeaksWorkoutRepository.kt @@ -1,9 +1,7 @@ package org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.workout import com.fasterxml.jackson.databind.ObjectMapper -import java.time.DayOfWeek import java.time.LocalDate -import java.time.temporal.TemporalAdjusters import org.freekode.tp2intervals.domain.ExternalData import org.freekode.tp2intervals.domain.Platform import org.freekode.tp2intervals.domain.librarycontainer.LibraryContainer @@ -127,20 +125,17 @@ class TrainingPeaksWorkoutRepository( private fun getWorkoutsFromTPPlan(libraryContainer: LibraryContainer): List { val planId = libraryContainer.externalData.trainingPeaksId!! - val planApplyDate = getPlanApplyDate() + val planStartDateShift = trainingPeaksConfigurationRepository.getConfiguration().planDaysShift + val planStartDate = Date.thisMonday() + val planApplyDate = planStartDate.plusDays(planStartDateShift) val response = tpPlanRepository.applyPlan(planId, planApplyDate) try { val planEndDate = response.endDate.toLocalDate() - val workoutDateShiftDays = Date.daysDiff(libraryContainer.startDate, planApplyDate) val workouts = getWorkoutsFromCalendar(planApplyDate, planEndDate).map { - it.withDate( - it.date!!.minusDays(workoutDateShiftDays.toLong()) - ) + it.withDate(it.date!!.minusDays(planStartDateShift)) } - val tpPlan = tpPlanRepository.getPlan(planId) - assert(tpPlan.workoutCount == workouts.size) return workouts } catch (e: Exception) { throw e @@ -153,10 +148,6 @@ class TrainingPeaksWorkoutRepository( return tpWorkoutLibraryRepository.getLibraryWorkouts(library.externalData.trainingPeaksId!!) } - private fun getPlanApplyDate(): LocalDate = LocalDate.now() - .plusDays(trainingPeaksConfigurationRepository.getConfiguration().planDaysShift) - .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)) - private fun getNoteEndDateForFilter(startDate: LocalDate, endDate: LocalDate): LocalDate = if (startDate == endDate) endDate.plusDays(1) else endDate diff --git a/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/structure/TPTargetMapper.kt b/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/structure/TPTargetMapper.kt index cebe936..b945912 100644 --- a/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/structure/TPTargetMapper.kt +++ b/boot/src/main/kotlin/org/freekode/tp2intervals/infrastructure/platform/trainingpeaks/workout/structure/TPTargetMapper.kt @@ -1,8 +1,6 @@ package org.freekode.tp2intervals.infrastructure.platform.trainingpeaks.workout.structure -import org.freekode.tp2intervals.domain.Platform import org.freekode.tp2intervals.domain.workout.structure.WorkoutStructure -import org.freekode.tp2intervals.infrastructure.PlatformException class TPTargetMapper { companion object { From 42d9d2c47326355b1aa9c032c70de7a048400fd9 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Tue, 28 May 2024 11:44:01 +0200 Subject: [PATCH 2/2] fix tp plan start date during export --- .../org/freekode/tp2intervals/app/plan/LibraryService.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/boot/src/main/kotlin/org/freekode/tp2intervals/app/plan/LibraryService.kt b/boot/src/main/kotlin/org/freekode/tp2intervals/app/plan/LibraryService.kt index 67bf947..20c54bf 100644 --- a/boot/src/main/kotlin/org/freekode/tp2intervals/app/plan/LibraryService.kt +++ b/boot/src/main/kotlin/org/freekode/tp2intervals/app/plan/LibraryService.kt @@ -4,7 +4,6 @@ import org.freekode.tp2intervals.domain.Platform import org.freekode.tp2intervals.domain.librarycontainer.LibraryContainer import org.freekode.tp2intervals.domain.librarycontainer.LibraryContainerRepository import org.freekode.tp2intervals.domain.workout.WorkoutRepository -import org.freekode.tp2intervals.infrastructure.utils.Date import org.springframework.stereotype.Service @Service @@ -26,7 +25,11 @@ class LibraryService( val targetWorkoutRepository = workoutRepositoryMap[request.targetPlatform]!! val workouts = sourceWorkoutRepository.getWorkoutsFromLibrary(request.libraryContainer) - val newPlan = targetPlanRepository.createLibraryContainer(request.newName, Date.thisMonday(), request.libraryContainer.isPlan) + val newPlan = targetPlanRepository.createLibraryContainer( + request.newName, + workouts.first().date, + request.libraryContainer.isPlan + ) targetWorkoutRepository.saveWorkoutsToLibrary(newPlan, workouts) return CopyPlanResponse(newPlan.name, workouts.size) }