feat(store): persist garmin_connected_at, set once on first successful auth
This commit is contained in:
@@ -204,3 +204,31 @@ func TestIsolation_DeleteUserLeavesOtherUsersDataIntact(t *testing.T) {
|
|||||||
t.Fatalf("userB's activities affected by deleting userA: len=%d err=%v", len(activitiesB), err)
|
t.Fatalf("userB's activities affected by deleting userA: len=%d err=%v", len(activitiesB), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestIsolation_MarkGarminConnectedNeverLeaksAcrossUsers confirms marking
|
||||||
|
// one user's Garmin connection never sets another user's flag.
|
||||||
|
func TestIsolation_MarkGarminConnectedNeverLeaksAcrossUsers(t *testing.T) {
|
||||||
|
db := openTestDB(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
userA, err := db.ProvisionUser(ctx, "sub-a", "A")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ProvisionUser(a): %v", err)
|
||||||
|
}
|
||||||
|
userB, err := db.ProvisionUser(ctx, "sub-b", "B")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ProvisionUser(b): %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.MarkGarminConnected(ctx, userA); err != nil {
|
||||||
|
t.Fatalf("MarkGarminConnected(a): %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
profileB, err := db.GetProfile(ctx, userB)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetProfile(b): %v", err)
|
||||||
|
}
|
||||||
|
if profileB.GarminConnectedAt != nil {
|
||||||
|
t.Fatal("userA's MarkGarminConnected call leaked into userB's profile")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ type Profile struct {
|
|||||||
Name string
|
Name string
|
||||||
GarminEmail string
|
GarminEmail string
|
||||||
GarminPassword string
|
GarminPassword string
|
||||||
|
// GarminConnectedAt is nil until this user's first successful Garmin
|
||||||
|
// authentication (see store.MarkGarminConnected) -- the login gate uses
|
||||||
|
// it, not UpdateProfile, so it's deliberately excluded from
|
||||||
|
// UpdateProfile's SET clause below and can only ever move from nil to
|
||||||
|
// set, never reset by a normal profile save.
|
||||||
|
GarminConnectedAt *string
|
||||||
RollingWindowDays int
|
RollingWindowDays int
|
||||||
// BackfillHorizonDays bounds how far back "Sync now" reaches when
|
// BackfillHorizonDays bounds how far back "Sync now" reaches when
|
||||||
// walking backward from today; it's read fresh on every sync (not fixed
|
// walking backward from today; it's read fresh on every sync (not fixed
|
||||||
@@ -70,7 +76,7 @@ type Profile struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const profileColumns = `
|
const profileColumns = `
|
||||||
name, garmin_email, garmin_password, rolling_window_days, backfill_horizon_days, max_heart_rate, resting_heart_rate,
|
name, garmin_email, garmin_password, garmin_connected_at, rolling_window_days, backfill_horizon_days, max_heart_rate, resting_heart_rate,
|
||||||
hr_zone1_min_pct, hr_zone1_max_pct, hr_zone2_min_pct, hr_zone2_max_pct,
|
hr_zone1_min_pct, hr_zone1_max_pct, hr_zone2_min_pct, hr_zone2_max_pct,
|
||||||
hr_zone3_min_pct, hr_zone3_max_pct, hr_zone4_min_pct, hr_zone4_max_pct,
|
hr_zone3_min_pct, hr_zone3_max_pct, hr_zone4_min_pct, hr_zone4_max_pct,
|
||||||
hr_zone5_min_pct, hr_zone5_max_pct,
|
hr_zone5_min_pct, hr_zone5_max_pct,
|
||||||
@@ -85,7 +91,7 @@ const profileColumns = `
|
|||||||
func (db *DB) GetProfile(ctx context.Context, userID int64) (Profile, error) {
|
func (db *DB) GetProfile(ctx context.Context, userID int64) (Profile, error) {
|
||||||
var p Profile
|
var p Profile
|
||||||
err := db.QueryRowContext(ctx, `SELECT `+profileColumns+` FROM profile WHERE user_id = ?`, userID).Scan(
|
err := db.QueryRowContext(ctx, `SELECT `+profileColumns+` FROM profile WHERE user_id = ?`, userID).Scan(
|
||||||
&p.Name, &p.GarminEmail, &p.GarminPassword, &p.RollingWindowDays, &p.BackfillHorizonDays, &p.MaxHeartRate, &p.RestingHeartRate,
|
&p.Name, &p.GarminEmail, &p.GarminPassword, &p.GarminConnectedAt, &p.RollingWindowDays, &p.BackfillHorizonDays, &p.MaxHeartRate, &p.RestingHeartRate,
|
||||||
&p.HRZone1MinPct, &p.HRZone1MaxPct, &p.HRZone2MinPct, &p.HRZone2MaxPct,
|
&p.HRZone1MinPct, &p.HRZone1MaxPct, &p.HRZone2MinPct, &p.HRZone2MaxPct,
|
||||||
&p.HRZone3MinPct, &p.HRZone3MaxPct, &p.HRZone4MinPct, &p.HRZone4MaxPct,
|
&p.HRZone3MinPct, &p.HRZone3MaxPct, &p.HRZone4MinPct, &p.HRZone4MaxPct,
|
||||||
&p.HRZone5MinPct, &p.HRZone5MaxPct,
|
&p.HRZone5MinPct, &p.HRZone5MaxPct,
|
||||||
@@ -132,3 +138,14 @@ func (db *DB) UpdateProfile(ctx context.Context, userID int64, p Profile) error
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarkGarminConnected records the first time userID successfully
|
||||||
|
// authenticates with Garmin. A no-op if already set, so it always reflects
|
||||||
|
// the first connection, not the most recent one.
|
||||||
|
func (db *DB) MarkGarminConnected(ctx context.Context, userID int64) error {
|
||||||
|
_, err := db.ExecContext(ctx, `UPDATE profile SET garmin_connected_at = datetime('now') WHERE user_id = ? AND garmin_connected_at IS NULL`, userID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("mark garmin connected for user %d: %w", userID, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -107,3 +107,44 @@ func TestProfile_DefaultsThenUpdate(t *testing.T) {
|
|||||||
t.Errorf("TargetBrightenPct after update = %v, want 50", got.TargetBrightenPct)
|
t.Errorf("TargetBrightenPct after update = %v, want 50", got.TargetBrightenPct)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMarkGarminConnected_SetsTimestampOnceOnFirstCall(t *testing.T) {
|
||||||
|
db := openTestDB(t)
|
||||||
|
ctx := context.Background()
|
||||||
|
userID, err := db.ProvisionUser(ctx, "test-sub", "Test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("ProvisionUser: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
before, err := db.GetProfile(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetProfile: %v", err)
|
||||||
|
}
|
||||||
|
if before.GarminConnectedAt != nil {
|
||||||
|
t.Fatalf("expected a fresh profile to have nil GarminConnectedAt, got %v", *before.GarminConnectedAt)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.MarkGarminConnected(ctx, userID); err != nil {
|
||||||
|
t.Fatalf("MarkGarminConnected: %v", err)
|
||||||
|
}
|
||||||
|
afterFirst, err := db.GetProfile(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetProfile after first mark: %v", err)
|
||||||
|
}
|
||||||
|
if afterFirst.GarminConnectedAt == nil {
|
||||||
|
t.Fatal("expected GarminConnectedAt to be set after MarkGarminConnected")
|
||||||
|
}
|
||||||
|
firstValue := *afterFirst.GarminConnectedAt
|
||||||
|
|
||||||
|
// A second call must not change the recorded first-connection time.
|
||||||
|
if err := db.MarkGarminConnected(ctx, userID); err != nil {
|
||||||
|
t.Fatalf("MarkGarminConnected (second call): %v", err)
|
||||||
|
}
|
||||||
|
afterSecond, err := db.GetProfile(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetProfile after second mark: %v", err)
|
||||||
|
}
|
||||||
|
if afterSecond.GarminConnectedAt == nil || *afterSecond.GarminConnectedAt != firstValue {
|
||||||
|
t.Fatalf("GarminConnectedAt changed on second call: first=%q second=%v", firstValue, afterSecond.GarminConnectedAt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,6 +30,12 @@ CREATE TABLE profile (
|
|||||||
name TEXT NOT NULL DEFAULT 'Default',
|
name TEXT NOT NULL DEFAULT 'Default',
|
||||||
garmin_email TEXT NOT NULL DEFAULT '',
|
garmin_email TEXT NOT NULL DEFAULT '',
|
||||||
garmin_password TEXT NOT NULL DEFAULT '',
|
garmin_password TEXT NOT NULL DEFAULT '',
|
||||||
|
-- Set once, the first time this user successfully authenticates with
|
||||||
|
-- Garmin (handleAuthLogin/handleAuthMFA). Null means never connected --
|
||||||
|
-- the login gate uses this (not the in-memory auth status, which
|
||||||
|
-- resets on restart) to decide whether a returning user must
|
||||||
|
-- reconnect before entering the app.
|
||||||
|
garmin_connected_at TEXT,
|
||||||
rolling_window_days INTEGER NOT NULL DEFAULT 90,
|
rolling_window_days INTEGER NOT NULL DEFAULT 90,
|
||||||
-- BackfillHorizonDays bounds how far back "Sync now" reaches when
|
-- BackfillHorizonDays bounds how far back "Sync now" reaches when
|
||||||
-- walking backward from today; read fresh on every sync (not cached at
|
-- walking backward from today; read fresh on every sync (not cached at
|
||||||
|
|||||||
@@ -35,6 +35,12 @@ CREATE TABLE profile (
|
|||||||
name TEXT NOT NULL DEFAULT 'Default',
|
name TEXT NOT NULL DEFAULT 'Default',
|
||||||
garmin_email TEXT NOT NULL DEFAULT '',
|
garmin_email TEXT NOT NULL DEFAULT '',
|
||||||
garmin_password TEXT NOT NULL DEFAULT '',
|
garmin_password TEXT NOT NULL DEFAULT '',
|
||||||
|
-- Set once, the first time this user successfully authenticates with
|
||||||
|
-- Garmin (handleAuthLogin/handleAuthMFA). Null means never connected --
|
||||||
|
-- the login gate uses this (not the in-memory auth status, which
|
||||||
|
-- resets on restart) to decide whether a returning user must
|
||||||
|
-- reconnect before entering the app.
|
||||||
|
garmin_connected_at TEXT,
|
||||||
rolling_window_days INTEGER NOT NULL DEFAULT 90,
|
rolling_window_days INTEGER NOT NULL DEFAULT 90,
|
||||||
-- BackfillHorizonDays bounds how far back "Sync now" reaches when
|
-- BackfillHorizonDays bounds how far back "Sync now" reaches when
|
||||||
-- walking backward from today; read fresh on every sync (not cached at
|
-- walking backward from today; read fresh on every sync (not cached at
|
||||||
|
|||||||
Reference in New Issue
Block a user