Skip to content

Commit

Permalink
Merge pull request #69 from freekode/fix-tp-copy-plan
Browse files Browse the repository at this point in the history
Fix TP copy plan
  • Loading branch information
freekode authored Jun 3, 2024
2 parents 16159d2 + 42d9d2c commit bf22be5
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -127,20 +125,17 @@ class TrainingPeaksWorkoutRepository(

private fun getWorkoutsFromTPPlan(libraryContainer: LibraryContainer): List<Workout> {
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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit bf22be5

Please sign in to comment.