Enhance smoking tracking API with new features and improvements

- Added a new API endpoint `GET /api/v1/smoke/home` to consolidate core modules for the home dashboard, reducing the need for multiple requests.
- Updated the `smoke` routes to include the new home endpoint and improved user profile management with the addition of a `quit_date` field.
- Enhanced the algorithm for calculating daily targets and next smoke suggestions, ensuring accurate future time handling and user-specific recommendations.
- Improved API documentation to reflect new endpoints, response formats, and detailed field descriptions for better clarity and usability.
- Refactored user authentication handling in various handlers to streamline the process and ensure consistent error responses.
This commit is contained in:
nepiedg
2026-01-29 17:16:35 +00:00
parent 3154365ab2
commit 9200600b1c
21 changed files with 703 additions and 178 deletions
@@ -25,7 +25,7 @@ func NewSmokeProfileService(db *gorm.DB) *SmokeProfileService {
}
type SmokeProfileView struct {
Exists bool `json:"exists"`
Exists bool `json:"exists"`
Profile *smokemodel.SmokeUserProfile `json:"profile,omitempty"`
IsCompleted bool `json:"is_completed"`
@@ -89,6 +89,9 @@ type UpsertSmokeProfileRequest struct {
WakeUpTime *string
SleepTime *string
QuitDateProvided bool
QuitDate *time.Time
}
func (s *SmokeProfileService) Upsert(ctx context.Context, uid int, req UpsertSmokeProfileRequest) (SmokeProfileView, error) {
@@ -146,6 +149,9 @@ func (s *SmokeProfileService) Upsert(ctx context.Context, uid int, req UpsertSmo
if err := applyTimeStr(&profile.SleepTime, req.SleepTime); err != nil {
return SmokeProfileView{}, err
}
if req.QuitDateProvided {
profile.QuitDate = req.QuitDate
}
now := time.Now()
if profile.OnboardingCompletedAt == nil && isSmokeProfileCompleted(profile) {
@@ -195,7 +201,7 @@ func awakeMinutesWithFallback(wakeUp, sleep string) (int, error) {
if sleepMin > wakeMin {
return sleepMin - wakeMin, nil
}
return (24 * 60 - wakeMin) + sleepMin, nil
return (24*60 - wakeMin) + sleepMin, nil
}
func baselineIntervalMinutes(awakeMinutes int, baselineCigsPerDay int) int {