/
user_workouts_calendar_service.go
215 lines (198 loc) · 7.25 KB
/
user_workouts_calendar_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package services
import (
"fmt"
"github.com/paulrozhkin/sport-tracker/internal/models"
"github.com/paulrozhkin/sport-tracker/internal/repositories"
"github.com/paulrozhkin/sport-tracker/internal/utils"
"go.uber.org/zap"
"time"
)
type UserWorkoutsCalendarService struct {
workoutsStatisticRepository *repositories.WorkoutsStatisticRepository
logger *zap.SugaredLogger
workoutsService *WorkoutsService
userWorkoutsRepository *repositories.UserWorkoutsRepository
}
func NewUserWorkoutsCalendarService(logger *zap.SugaredLogger,
workoutsStatisticRepository *repositories.WorkoutsStatisticRepository,
workoutsService *WorkoutsService,
userWorkoutsRepository *repositories.UserWorkoutsRepository) (*UserWorkoutsCalendarService, error) {
return &UserWorkoutsCalendarService{logger: logger,
workoutsStatisticRepository: workoutsStatisticRepository,
workoutsService: workoutsService,
userWorkoutsRepository: userWorkoutsRepository}, nil
}
func (us *UserWorkoutsCalendarService) GetWorkoutStatisticById(statisticId string) (*models.WorkoutStatistic, error) {
workoutStatisticInfo, err := us.workoutsStatisticRepository.GetWorkoutStatisticById(statisticId)
if err != nil {
return nil, err
}
workoutStatisticInfo.Workout, err = us.workoutsService.GetWorkoutById(workoutStatisticInfo.Workout.Id)
if err != nil {
return nil, err
}
return workoutStatisticInfo, nil
}
func (us *UserWorkoutsCalendarService) GetCalendarForUser(userId string) (*models.WorkoutsCalendar, error) {
if userId == "" {
return nil, fmt.Errorf("userId %s in GetCalendarForUser", models.ArgumentNullOrEmptyError)
}
statistic, err := us.workoutsStatisticRepository.GetShortWorkoutsStatisticByUser(userId)
if err != nil {
return nil, err
}
us.logger.Infof("Get %d items in statistic", len(statistic))
calendar := new(models.WorkoutsCalendar)
calendar.History = make([]*models.WorkoutStatistic, 0)
calendar.Upcoming = make([]*models.WorkoutStatistic, 0)
currentDayUnix := utils.GetTodayUtc().Unix()
var nextWorkout *models.WorkoutStatistic
for _, statisticItem := range statistic {
dateToCompare := statisticItem.ScheduledDate
if statisticItem.WorkoutDate != nil {
dateToCompare = *statisticItem.WorkoutDate
}
if currentDayUnix > dateToCompare.Unix() {
calendar.History = append(calendar.History, statisticItem)
} else {
if nextWorkout == nil {
nextWorkout = statisticItem
} else {
calendar.Upcoming = append(calendar.Upcoming, statisticItem)
}
}
}
if nextWorkout != nil {
workoutStatisticInfo, err := us.workoutsStatisticRepository.GetWorkoutStatisticById(nextWorkout.Id)
if err != nil {
return nil, err
}
workoutStatisticInfo.Workout, err = us.workoutsService.GetWorkoutById(workoutStatisticInfo.Workout.Id)
if err != nil {
return nil, err
}
calendar.Current = workoutStatisticInfo
}
return calendar, nil
}
func (us *UserWorkoutsCalendarService) ConfirmVisit(confirmModel models.ConfirmVisit) (*models.WorkoutStatistic, error) {
if confirmModel.WorkoutVisitId == "" {
return nil, fmt.Errorf("userId %s in GetCalendarForUser", models.ArgumentNullOrEmptyError)
}
statistic, err := us.workoutsStatisticRepository.GetWorkoutStatisticById(confirmModel.WorkoutVisitId)
if err != nil {
return nil, err
}
statistic.Comment = confirmModel.Comment
statistic.WorkoutDate = confirmModel.WorkoutDate
workout, err := us.workoutsStatisticRepository.UpdateWorkoutsStatistic(*statistic)
if err != nil {
return nil, err
}
workout.Workout, err = us.workoutsService.GetWorkoutById(workout.Workout.Id)
if err != nil {
return nil, err
}
return workout, nil
}
func (us *UserWorkoutsCalendarService) DeleteScheduledWorkoutsForUserWorkout(userWorkoutId string) error {
return us.workoutsStatisticRepository.DeleteScheduledWorkoutsForUserWorkout(userWorkoutId)
}
func (us *UserWorkoutsCalendarService) CreateCalendarForRepeatableActiveUserWorkouts() error {
us.logger.Info("Started generation of new calendar")
// Get all active repeatable workouts
activeWorkouts, err := us.userWorkoutsRepository.GetActiveRepeatableUserWorkouts()
if err != nil {
return err
}
return us.createCalendarForUserWorkouts(activeWorkouts)
}
func (us *UserWorkoutsCalendarService) CreateCalendarForUserWorkout(userWorkout *models.UserWorkout) error {
return us.createCalendarForUserWorkouts([]*models.UserWorkout{userWorkout})
}
func (us *UserWorkoutsCalendarService) createCalendarForUserWorkouts(workouts []*models.UserWorkout) error {
var userWorkoutIds []string
for _, item := range workouts {
userWorkoutIds = append(userWorkoutIds, item.Id)
}
// Get last scheduled time for every active user workout
lastSchedules, err := us.workoutsStatisticRepository.GetLastWorkoutStatisticForUserWorkouts(userWorkoutIds)
if err != nil {
return err
}
_ = lastSchedules
var newStatistic []*models.WorkoutStatistic
// Add two week for generation
twoWeek := utils.GetTodayUtc().AddDate(0, 0, 7*2)
for _, item := range workouts {
var lastSchedule time.Time
var lastWorkout *models.Workout
if value, ok := lastSchedules[item.Id]; ok && value != nil {
lastSchedule = value.ScheduledDate
lastSchedule = lastSchedule.AddDate(0, 0, 1)
lastWorkout = getNextWorkout(item.WorkoutPlan.Workouts, value.Workout, item.WorkoutPlan.Repeatable)
} else {
lastSchedule = utils.TruncateToDay(item.Created)
lastWorkout = item.WorkoutPlan.Workouts[0]
}
if item.WorkoutPlan.Repeatable {
// Generate a scheduled calendar for each next day
for ; lastSchedule.Before(twoWeek); lastSchedule = lastSchedule.AddDate(0, 0, 1) {
if weekdayContainsInSchedule(item.Schedule, lastSchedule.Weekday()) {
statistic := &models.WorkoutStatistic{UserWorkout: &models.UserWorkout{}, Workout: &models.Workout{}}
statistic.ScheduledDate = lastSchedule
statistic.UserWorkout = item
statistic.Workout = lastWorkout
newStatistic = append(newStatistic, statistic)
lastWorkout = getNextWorkout(item.WorkoutPlan.Workouts, lastWorkout, true)
}
}
} else {
for ; ; lastSchedule = lastSchedule.AddDate(0, 0, 1) {
if weekdayContainsInSchedule(item.Schedule, lastSchedule.Weekday()) {
if lastWorkout == nil {
break
}
statistic := &models.WorkoutStatistic{UserWorkout: &models.UserWorkout{}, Workout: &models.Workout{}}
statistic.ScheduledDate = lastSchedule
statistic.UserWorkout = item
statistic.Workout = lastWorkout
newStatistic = append(newStatistic, statistic)
lastWorkout = getNextWorkout(item.WorkoutPlan.Workouts, lastWorkout, false)
}
}
}
}
if len(newStatistic) > 0 {
err = us.workoutsStatisticRepository.CreateWorkoutStatistics(newStatistic)
if err != nil {
return err
}
}
us.logger.Infof("Finished generation of new calendar. New items created %d", len(newStatistic))
return nil
}
func weekdayContainsInSchedule(schedule []time.Weekday, compareValue time.Weekday) bool {
for _, item := range schedule {
if item == compareValue {
return true
}
}
return false
}
func getNextWorkout(items []*models.Workout, current *models.Workout, isRing bool) *models.Workout {
nextIndex := 0
for i, item := range items {
if item.Id == current.Id {
nextIndex = i + 1
break
}
}
if nextIndex >= len(items) {
if !isRing {
return nil
}
nextIndex = 0
}
return items[nextIndex]
}