Fix PocketBase date query - use range operators not contains
All checks were successful
Deploy / deploy (push) Successful in 1m39s

The ~ contains operator doesn't work with PocketBase date fields.
Use >= and < operators with YYYY-MM-DD format instead, matching
the working /api/history pattern.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-15 15:24:00 +00:00
parent 55a3505b55
commit 3e2d9047fb
2 changed files with 16 additions and 12 deletions

View File

@@ -517,18 +517,18 @@ describe("GET /api/today", () => {
}); });
describe("dailyLog query", () => { describe("dailyLog query", () => {
it("queries dailyLogs with YYYY-MM-DD date format using contains operator", async () => { it("queries dailyLogs with YYYY-MM-DD date format using range operators", async () => {
// PocketBase filters don't accept ISO format with T separator // PocketBase accepts simple YYYY-MM-DD in comparison operators
// Must use simple YYYY-MM-DD with ~ contains operator // Use >= today and < tomorrow for exact day match
currentMockUser = createMockUser(); currentMockUser = createMockUser();
currentMockDailyLog = createMockDailyLog(); currentMockDailyLog = createMockDailyLog();
await GET(mockRequest); await GET(mockRequest);
// Verify filter uses YYYY-MM-DD format (2025-01-10) not ISO format // Verify filter uses YYYY-MM-DD format with range operators
// The filter should use ~ contains operator, not >= range
expect(lastDailyLogFilter).toBeDefined(); expect(lastDailyLogFilter).toBeDefined();
expect(lastDailyLogFilter).toContain('date~"2025-01-10"'); expect(lastDailyLogFilter).toContain('date>="2025-01-10"');
expect(lastDailyLogFilter).toContain('date<"2025-01-11"');
// Should NOT contain ISO format with T separator // Should NOT contain ISO format with T separator
expect(lastDailyLogFilter).not.toContain("T"); expect(lastDailyLogFilter).not.toContain("T");
}); });

View File

@@ -74,16 +74,20 @@ 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 {
// Use YYYY-MM-DD format with contains operator for PocketBase date field // Use YYYY-MM-DD format with >= and < operators for PocketBase date field
// PocketBase filters don't accept ISO format with T separator // PocketBase accepts simple date strings in comparison operators
const today = new Date().toISOString().split("T")[0]; const today = new Date().toISOString().split("T")[0];
const tomorrow = new Date(Date.now() + 86400000)
.toISOString()
.split("T")[0];
logger.info({ userId: user.id, date: today }, "Fetching dailyLog"); logger.info({ userId: user.id, today, tomorrow }, "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>="${today}" && date<"${tomorrow}"`,
}); { sort: "-created" },
);
logger.info( logger.info(
{ {