Fix dailyLog date query to use proper date comparison
All checks were successful
Deploy / deploy (push) Successful in 1m40s
All checks were successful
Deploy / deploy (push) Successful in 1m40s
- Change /api/today query from string contains (~) to date range (>=, <) - Store dates in full ISO format in garmin-sync for consistent comparison - PocketBase date fields need proper date operators, not string contains Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -414,15 +414,18 @@ describe("POST /api/cron/garmin-sync", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sets date to today's date string", async () => {
|
it("sets date to today's ISO date string", async () => {
|
||||||
mockUsers = [createMockUser()];
|
mockUsers = [createMockUser()];
|
||||||
const today = new Date().toISOString().split("T")[0];
|
// Full ISO format for consistent date comparison in queries
|
||||||
|
const todayDate = new Date();
|
||||||
|
todayDate.setUTCHours(0, 0, 0, 0);
|
||||||
|
const todayISO = todayDate.toISOString();
|
||||||
|
|
||||||
await POST(createMockRequest(`Bearer ${validSecret}`));
|
await POST(createMockRequest(`Bearer ${validSecret}`));
|
||||||
|
|
||||||
expect(mockPbCreate).toHaveBeenCalledWith(
|
expect(mockPbCreate).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
date: today,
|
date: todayISO,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -91,7 +91,13 @@ export async function POST(request: Request) {
|
|||||||
(u) => u.garminConnected && u.garminTokenExpiresAt && u.lastPeriodDate,
|
(u) => u.garminConnected && u.garminTokenExpiresAt && u.lastPeriodDate,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// YYYY-MM-DD format for Garmin API calls
|
||||||
const today = new Date().toISOString().split("T")[0];
|
const today = new Date().toISOString().split("T")[0];
|
||||||
|
// Full ISO format for PocketBase date field storage
|
||||||
|
// This ensures consistent date comparison in queries
|
||||||
|
const todayDate = new Date();
|
||||||
|
todayDate.setUTCHours(0, 0, 0, 0);
|
||||||
|
const todayISO = todayDate.toISOString();
|
||||||
|
|
||||||
for (const user of users) {
|
for (const user of users) {
|
||||||
const userSyncStartTime = Date.now();
|
const userSyncStartTime = Date.now();
|
||||||
@@ -208,9 +214,10 @@ export async function POST(request: Request) {
|
|||||||
// Store default value 100 for body battery when Garmin returns null.
|
// Store default value 100 for body battery when Garmin returns null.
|
||||||
// This prevents PocketBase's number field null-to-0 coercion from
|
// This prevents PocketBase's number field null-to-0 coercion from
|
||||||
// causing the dashboard to display 0 instead of a meaningful value.
|
// causing the dashboard to display 0 instead of a meaningful value.
|
||||||
|
// Use full ISO date format for consistent date comparison in queries
|
||||||
await pb.collection("dailyLogs").create({
|
await pb.collection("dailyLogs").create({
|
||||||
user: user.id,
|
user: user.id,
|
||||||
date: today,
|
date: todayISO,
|
||||||
cycleDay,
|
cycleDay,
|
||||||
phase,
|
phase,
|
||||||
bodyBatteryCurrent: bodyBattery.current ?? 100,
|
bodyBatteryCurrent: bodyBattery.current ?? 100,
|
||||||
|
|||||||
@@ -74,13 +74,24 @@ export const GET = withAuth(async (_request, user, pb) => {
|
|||||||
// Sort by created DESC to get the most recent record if multiple exist
|
// Sort by created DESC to get the most recent record if multiple exist
|
||||||
let biometrics = { ...DEFAULT_BIOMETRICS, phaseLimit };
|
let biometrics = { ...DEFAULT_BIOMETRICS, phaseLimit };
|
||||||
try {
|
try {
|
||||||
const today = new Date().toISOString().split("T")[0];
|
// Use date range query for proper date field comparison
|
||||||
logger.info({ userId: user.id, today }, "Fetching dailyLog");
|
// PocketBase date fields need >= and < operators, not string contains
|
||||||
|
const todayStart = new Date();
|
||||||
|
todayStart.setUTCHours(0, 0, 0, 0);
|
||||||
|
const tomorrowStart = new Date(todayStart.getTime() + 86400000);
|
||||||
|
const todayISO = todayStart.toISOString();
|
||||||
|
const tomorrowISO = tomorrowStart.toISOString();
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
{ userId: user.id, todayISO, tomorrowISO },
|
||||||
|
"Fetching dailyLog",
|
||||||
|
);
|
||||||
const dailyLog = await pb
|
const dailyLog = await pb
|
||||||
.collection("dailyLogs")
|
.collection("dailyLogs")
|
||||||
.getFirstListItem<DailyLog>(`user="${user.id}" && date~"${today}"`, {
|
.getFirstListItem<DailyLog>(
|
||||||
sort: "-created",
|
`user="${user.id}" && date>="${todayISO}" && date<"${tomorrowISO}"`,
|
||||||
});
|
{ sort: "-created" },
|
||||||
|
);
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -193,9 +193,7 @@ describe("fetchGarminData", () => {
|
|||||||
json: () => Promise.resolve(mockResponse),
|
json: () => Promise.resolve(mockResponse),
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await fetchGarminData("/wellness/daily/123", {
|
const result = await fetchGarminData("/wellness/daily/123", "test-token");
|
||||||
oauth2Token: "test-token",
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(result).toEqual(mockResponse);
|
expect(result).toEqual(mockResponse);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user