BUG: daily_activity_logs insert loop has no deduplication re-saving an activity creates duplicate logs

Open
#184 0 comments 0 reactions 0 assignees View on GitHub

@Varadraj75 is already working on this.

Since Mar 8, 2026.

  • #185 by @Varadraj75 — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
dart, supabase
Domain
database

Research direction

Start in therapist/lib/repository/supabase_therapy_repository.dart around line 316 and inspect the database definition for daily_activity_logs. Confirm the save path handles an existing activity/date/patient combination without creating duplicates, and that the database enforces the stated uniqueness constraint.

Written by the indexing model from the issue text.

Description

Is there an existing issue for this?
  • I have searched the existing issues
What happened?

📌 Issue Overview

The insert loop in supabase_therapy_repository.dart unconditionally
inserts a new row into daily_activity_logs for every date in the
activity's date range with no check for existing records. Re-saving or
editing a daily activity creates duplicate logs for the same
activity_id + date + patient_id combination.

This is the root cause of the PGRST116 crash fixed in #167 — duplicate
logs for the same day cause .maybeSingle() to throw "Results contain
more than one row".

🔍 Steps to Reproduce

  1. Therapist creates a daily activity for a patient for dates 1–7
  2. Therapist edits and re-saves the same activity
  3. daily_activity_logs now has 2 rows per date for the same activity
  4. Patient opens the Tasks screen → PGRST116 crash or wrong data shown

🎯 Expected Behavior

Re-saving a daily activity should update existing logs rather than
inserting duplicates. Each activity_id + date + patient_id
combination should have at most one log.

🚨 Actual Behavior

Every save unconditionally inserts new rows, creating duplicates.

therapist/lib/repository/supabase_therapy_repository.dart, line 316:

await _supabaseClient.from('daily_activity_logs').insert({
  'activity_id': dailyActivity.id,
  'date': date.toString(),
  'activity_items': dailyActivity.activityList,
  'patient_id': dailyActivity.patientId,
}); //   no deduplication — inserts duplicate rows on re-save

📷 Screenshot

N/A

💡 Suggested Improvements

Replace .insert() with .upsert() using onConflict on
activity_id + date + patient_id:

await _supabaseClient.from('daily_activity_logs').upsert({
  'activity_id': dailyActivity.id,
  'date': date.toString(),
  'activity_items': dailyActivity.activityList,
  'patient_id': dailyActivity.patientId,
}, onConflict: 'activity_id, date, patient_id'); //  upsert prevents duplicates

Additionally, add a unique constraint at the DB level:

ALTER TABLE daily_activity_logs 
ADD CONSTRAINT uq_activity_date_patient 
UNIQUE (activity_id, date, patient_id);
Record
  • I agree to follow this project's Code of Conduct
  • I want to work on this issue
Dominant language
Dart
Stars
30
Forks
49
PR merge metrics
No merged PRs in 30d

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AOSSIE-Org/NeuroTrack

All issues in AOSSIE-Org/NeuroTrack

Similar issues

More Dart issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.