Compare commits

...

2 Commits

Author SHA1 Message Date
a1495ff23f Fix garmin-sync to upsert dailyLogs instead of always creating
All checks were successful
Deploy / deploy (push) Successful in 1m39s
The sync was creating a new record every time it ran, causing duplicate
records for the same day. Combined with PocketBase's inability to sort
by the 'created' field, this caused the dashboard to display stale data.

Now checks for an existing record for the user+date before creating,
and updates the existing record if found.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-15 22:06:03 +00:00
4dad370e66 PB credentials. 2026-01-15 22:02:09 +00:00
2 changed files with 16 additions and 3 deletions

2
.gitignore vendored
View File

@@ -59,3 +59,5 @@ result
__pycache__/
*.pyc
.venv/
.env.phaseflow

View File

@@ -206,12 +206,12 @@ export async function POST(request: Request) {
user.activeOverrides,
);
// Create DailyLog entry
// Upsert DailyLog entry - update existing record for today or create new one
// Store default value 100 for body battery when Garmin returns null.
// This prevents PocketBase's number field null-to-0 coercion from
// causing the dashboard to display 0 instead of a meaningful value.
// Use YYYY-MM-DD format for PocketBase date field compatibility
await pb.collection("dailyLogs").create({
const dailyLogData = {
user: user.id,
date: today,
cycleDay,
@@ -225,7 +225,18 @@ export async function POST(request: Request) {
trainingDecision: decision.status,
decisionReason: decision.reason,
notificationSentAt: null,
});
};
// Check if record already exists for this user today
try {
const existing = await pb
.collection("dailyLogs")
.getFirstListItem(`user="${user.id}" && date="${today}"`);
await pb.collection("dailyLogs").update(existing.id, dailyLogData);
} catch {
// No existing record - create new one
await pb.collection("dailyLogs").create(dailyLogData);
}
// Log sync complete with metrics
const userSyncDuration = Date.now() - userSyncStartTime;