Skip to content

[Backend] Implement Weight Tracking Service and Controller #497

@llinsss

Description

@llinsss

⚠️ This is a backend issue — work is done inside the backend/ folder

Description

The weight-tracking module has an entity (backend/src/weight-tracking/entities/weight-entry.entity.ts), a controller (weight-tracking.controller.ts), and a module file, but weight-tracking.service.ts does not exist. The controller has no service to delegate to, making all weight tracking endpoints non-functional.

Current State

  • backend/src/weight-tracking/entities/weight-entry.entity.ts — entity exists
  • backend/src/weight-tracking/weight-tracking.controller.ts — controller exists
  • backend/src/weight-tracking/weight-tracking.module.ts — module exists
  • backend/src/weight-tracking/weight-tracking.service.tsmissing

What Needs to Be Built

1. WeightTrackingService (weight-tracking.service.ts)

@Injectable()
export class WeightTrackingService {
  async addEntry(petId: string, dto: CreateWeightEntryDto): Promise<WeightEntry>
  async getHistory(petId: string, limit?: number): Promise<WeightEntry[]>
  async getLatest(petId: string): Promise<WeightEntry | null>
  async getTrend(petId: string): Promise<{ trend: 'gaining' | 'losing' | 'stable'; changeKg: number; periodDays: number }>
  async deleteEntry(id: string): Promise<void>
}

2. Trend Calculation Logic

  • Compare the last 2 entries to determine direction
  • Calculate changeKg and periodDays between entries
  • Return stable if change is < 0.1 kg

3. Alert Integration

  • If weight change exceeds a configurable threshold (e.g. >10% in 30 days), emit a notification via NotificationsService

4. Unit Tests (weight-tracking.spec.ts)

  • Test trend calculation for gaining, losing, and stable cases
  • Test alert threshold logic

Acceptance Criteria

  • All CRUD operations work via the existing controller
  • GET /weight-tracking/:petId/trend returns trend direction, change, and period
  • Significant weight changes trigger a notification to the pet owner
  • Unit tests cover trend and alert logic
  • Service is registered in WeightTrackingModule

Files to Create / Modify

  • backend/src/weight-tracking/weight-tracking.service.ts (new)
  • backend/src/weight-tracking/weight-tracking.module.ts (modify — register service)
  • backend/src/weight-tracking/weight-tracking.spec.ts (modify — add service tests)

Priority

Medium — module is wired but completely non-functional without the service

Estimated Effort

1–2 days

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions