Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions supabase/functions/evaluate-assessments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ Deno.serve(async (req) => {

const { error: insertError } = await supabase
.from("assessment_results")
.insert({
.upsert({
assessment_id: assessment_id,
submission: answered_questions,
patient_id: patient_id,
result: responseData,
});
}, { onConflict: 'patient_id,assessment_id', ignoreDuplicates: true });

if (insertError) {
const code = (insertError as { code?: string }).code;
Expand Down
31 changes: 20 additions & 11 deletions supabase/schemas/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ CREATE TABLE session (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ DEFAULT NOW(),
timestamp TIMESTAMPTZ NOT NULL,
therapist_id UUID REFERENCES therapist(id),
patient_id UUID REFERENCES patient(id),
therapist_id UUID REFERENCES therapist(id) ON DELETE SET NULL,
patient_id UUID REFERENCES patient(id) ON DELETE SET NULL,

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says patient-owned data should cascade on patient deletion, but session.patient_id is set to ON DELETE SET NULL. This leaves sessions behind with no patient and doesn’t match the stated integrity rule; consider switching this FK to ON DELETE CASCADE (or update the PR description/behavior consistently across patient-owned tables).

Copilot uses AI. Check for mistakes.
is_consultation BOOLEAN DEFAULT FALSE,
mode INT2,
duration INT4,
Expand All @@ -59,21 +59,29 @@ CREATE TABLE session (
declined_reason TEXT,

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The session table definition still has a trailing comma after the last column (declined_reason), which makes the SQL invalid and prevents schema.sql from executing from scratch. Remove the trailing comma before the closing ).

Suggested change
declined_reason TEXT,
declined_reason TEXT

Copilot uses AI. Check for mistakes.
);

-- Create the therapy_type table
CREATE TABLE therapy_type (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ DEFAULT NOW(),
name TEXT NOT NULL UNIQUE,
description TEXT
);

-- Create the therapy_goal table
CREATE TABLE therapy_goal (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ DEFAULT NOW(),
performed_on TIMESTAMPTZ,
therapist_id UUID REFERENCES therapist(id),
therapist_id UUID REFERENCES therapist(id) ON DELETE SET NULL,
therapy_mode INT2,
duration INT4,
therapy_type INT2,
therapy_type_id UUID REFERENCES therapy_type(id),
therapy_type_id UUID REFERENCES therapy_type(id) ON DELETE SET NULL,
goals JSONB,
observations JSONB,
regressions JSONB,
activities JSONB,
patient_id UUID REFERENCES patient(id)
patient_id UUID REFERENCES patient(id) ON DELETE CASCADE
);

-- Create the assessments table
Expand All @@ -93,10 +101,11 @@ CREATE TABLE assessments (
CREATE TABLE assessment_results (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ DEFAULT NOW(),
assessment_id UUID REFERENCES assessments(id),
patient_id UUID REFERENCES auth.users(id),
assessment_id UUID REFERENCES assessments(id) ON DELETE CASCADE,
patient_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
submission JSONB,
result JSONB
result JSONB,
CONSTRAINT uq_patient_assessment UNIQUE (patient_id, assessment_id)
);

-- Therapy Table
Expand Down Expand Up @@ -146,8 +155,8 @@ CREATE TABLE daily_activities (
activity_name TEXT NOT NULL,
activity_list JSONB,
is_active BOOLEAN DEFAULT TRUE,
therapist_id UUID REFERENCES therapist(id),
patient_id UUID REFERENCES patient(id),
therapist_id UUID REFERENCES therapist(id) ON DELETE SET NULL,
patient_id UUID REFERENCES patient(id) ON DELETE CASCADE,
start_time TIMESTAMPTZ,
end_time TIMESTAMPTZ,
days_of_week INT2[],
Expand All @@ -156,7 +165,7 @@ CREATE TABLE daily_activities (
CREATE TABLE daily_activity_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
activity_id UUID REFERENCES daily_activities(id) ON DELETE CASCADE,
date TIMESTAMPTZ NOT NULL,
date DATE NOT NULL,

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

daily_activity_logs.date was changed from TIMESTAMPTZ to DATE, but this change isn’t mentioned in the PR description and may break existing app queries/inserts that use ISO timestamps (e.g., .gte('date', startDate.toIso8601String())) and rely on time-of-day semantics. If the column should remain a timestamp, revert the type change; if it should be date-only, update the client code to write/query date-only values consistently.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟑 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find all usages of daily_activity_logs date column comparisons
rg -n -A2 -B2 "daily_activity_logs" --type dart | rg -A2 -B2 "date"

Repository: AOSSIE-Org/NeuroTrack

Length of output: 1819


🏁 Script executed:

# Check the full context around the date queries and DateTime creation
head -n 230 patient/lib/repository/supabase_patient_repository.dart | tail -n 100

Repository: AOSSIE-Org/NeuroTrack

Length of output: 3830


🏁 Script executed:

# Check what type of DateTime is being used (local vs UTC)
rg -B5 "startDate\.toIso8601String" patient/lib/repository/supabase_patient_repository.dart

Repository: AOSSIE-Org/NeuroTrack

Length of output: 952


🏁 Script executed:

# Check if there's any timezone handling in the DateTime creation
rg -B10 "DateTime\(dateTime\.year" patient/lib/repository/supabase_patient_repository.dart

Repository: AOSSIE-Org/NeuroTrack

Length of output: 369


🏁 Script executed:

# Create a quick test to verify what format Dart produces
cat > /tmp/test_datetime.dart << 'EOF'
void main() {
  // Local DateTime
  var local = DateTime(2024, 3, 9, 0, 0, 0);
  print("Local DateTime.toIso8601String(): ${local.toIso8601String()}");
  
  // UTC DateTime
  var utc = DateTime.utc(2024, 3, 9, 0, 0, 0);
  print("UTC DateTime.toIso8601String(): ${utc.toIso8601String()}");
  
  // DateTime.now()
  var now = DateTime.now();
  print("DateTime.now().toIso8601String(): ${now.toIso8601String()}");
}
EOF

Repository: AOSSIE-Org/NeuroTrack

Length of output: 47


🏁 Script executed:

# Check the schema change to see what the original type was
git log --oneline -p supabase/schemas/schema.sql | grep -A5 -B5 "daily_activity_logs" | head -50

Repository: AOSSIE-Org/NeuroTrack

Length of output: 436


🏁 Script executed:

# Check if there are any test failures or bugs reported related to date queries
rg -i "timezone|utc|date.*issue|date.*bug" --type dart patient/lib/repository/ therapist/lib/repository/

Repository: AOSSIE-Org/NeuroTrack

Length of output: 508


Fix the date query format inconsistency and timezone handling.

Client code in supabase_patient_repository.dart queries the date column using local DateTime.toIso8601String() (format: "2024-03-09T00:00:00"), which can cause off-by-one day errors depending on the app's timezone. The codebase uses UTC conversions elsewhere (e.g., supabase_therapist_repository.dart), suggesting this should too.

Update the queries to use .toUtc().toIso8601String() for consistency, or extract date-only strings using startDate.toIso8601String().split('T')[0] (e.g., "2024-03-09").

πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@supabase/schemas/schema.sql` at line 168, The patient repository queries
against the SQL DATE column using local DateTime string which can cause timezone
off-by-one errors; in supabase_patient_repository.dart locate any call that
converts a DateTime to a string for the 'date' column (e.g., calls that use
toIso8601String() in query parameters) and change it to use either
startDate.toUtc().toIso8601String() for full-UTC timestamps or extract a
date-only value via startDate.toIso8601String().split('T')[0] (preferred for
DATE columns); update the query parameter construction so the 'date' filter
consistently uses the UTC or date-only string format to match other repositories
like supabase_therapist_repository.dart.

activity_items JSONB NOT NULL
patient_id UUID REFERENCES patient(id) ON DELETE CASCADE,
Comment on lines 169 to 170

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The daily_activity_logs table definition is syntactically invalid: there is no comma between activity_items JSONB NOT NULL and patient_id ..., and there is also a trailing comma before the closing ). This will prevent schema.sql from executing. Add the missing comma separator and remove the trailing comma.

Suggested change
activity_items JSONB NOT NULL
patient_id UUID REFERENCES patient(id) ON DELETE CASCADE,
activity_items JSONB NOT NULL,
patient_id UUID REFERENCES patient(id) ON DELETE CASCADE

Copilot uses AI. Check for mistakes.
);
Comment on lines 165 to 171

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | πŸ”΄ Critical

Missing comma causes SQL syntax error.

Line 169 is missing a comma after activity_items JSONB NOT NULL, which will cause a PostgreSQL syntax error when executing this schema. This blocks the PR's objective of making the schema executable from scratch.

Proposed fix
 CREATE TABLE daily_activity_logs (
     id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
     activity_id UUID REFERENCES daily_activities(id) ON DELETE CASCADE,
     date DATE NOT NULL,
-    activity_items JSONB NOT NULL
-    patient_id UUID REFERENCES patient(id) ON DELETE CASCADE,
+    activity_items JSONB NOT NULL,
+    patient_id UUID REFERENCES patient(id) ON DELETE CASCADE
 );
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
CREATE TABLE daily_activity_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
activity_id UUID REFERENCES daily_activities(id) ON DELETE CASCADE,
date TIMESTAMPTZ NOT NULL,
date DATE NOT NULL,
activity_items JSONB NOT NULL
patient_id UUID REFERENCES patient(id) ON DELETE CASCADE,
);
CREATE TABLE daily_activity_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
activity_id UUID REFERENCES daily_activities(id) ON DELETE CASCADE,
date DATE NOT NULL,
activity_items JSONB NOT NULL,
patient_id UUID REFERENCES patient(id) ON DELETE CASCADE
);
πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@supabase/schemas/schema.sql` around lines 165 - 171, The CREATE TABLE
daily_activity_logs has a missing comma after the column declaration
activity_items JSONB NOT NULL and also an extra trailing comma before the
closing ); β€” fix the schema by adding a comma after activity_items JSONB NOT
NULL and removing the trailing comma after patient_id UUID REFERENCES
patient(id) ON DELETE CASCADE so the column list is correctly comma-separated;
locate the CREATE TABLE daily_activity_logs block and update those two
punctuation issues.

Expand Down
Loading