AOSSIE-Org / AOSSIE-Org/NeuroTrack

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

オープン
#184 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Dart
スター
29
フォーク
50
PR マージ指標
30日以内にマージされた PR はありません

説明

### 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

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。