AOSSIE-Org / AOSSIE-Org/NeuroTrack
BUG: daily_activity_logs insert loop has no deduplication re-saving an activity creates duplicate logs
- Ngôn ngữ chính
- Dart
- Star
- 29
- Fork
- 50
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
### Is there an existing issue for this?
- [x] 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:**
```dart
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`:
```dart
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:
```sql
ALTER TABLE daily_activity_logs
ADD CONSTRAINT uq_activity_date_patient
UNIQUE (activity_id, date, patient_id);
```
### Record
- [x] I agree to follow this project's Code of Conduct
- [x] I want to work on this issue
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Đánh giá
Issue này chưa được đánh giá.