Fix PocketBase date query - use range operators not contains
All checks were successful
Deploy / deploy (push) Successful in 1m39s
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:
@@ -517,18 +517,18 @@ describe("GET /api/today", () => {
|
||||
});
|
||||
|
||||
describe("dailyLog query", () => {
|
||||
it("queries dailyLogs with YYYY-MM-DD date format using contains operator", async () => {
|
||||
// PocketBase filters don't accept ISO format with T separator
|
||||
// Must use simple YYYY-MM-DD with ~ contains operator
|
||||
it("queries dailyLogs with YYYY-MM-DD date format using range operators", async () => {
|
||||
// PocketBase accepts simple YYYY-MM-DD in comparison operators
|
||||
// Use >= today and < tomorrow for exact day match
|
||||
currentMockUser = createMockUser();
|
||||
currentMockDailyLog = createMockDailyLog();
|
||||
|
||||
await GET(mockRequest);
|
||||
|
||||
// Verify filter uses YYYY-MM-DD format (2025-01-10) not ISO format
|
||||
// The filter should use ~ contains operator, not >= range
|
||||
// Verify filter uses YYYY-MM-DD format with range operators
|
||||
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
|
||||
expect(lastDailyLogFilter).not.toContain("T");
|
||||
});
|
||||
|
||||
@@ -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
|
||||
let biometrics = { ...DEFAULT_BIOMETRICS, phaseLimit };
|
||||
try {
|
||||
// Use YYYY-MM-DD format with contains operator for PocketBase date field
|
||||
// PocketBase filters don't accept ISO format with T separator
|
||||
// Use YYYY-MM-DD format with >= and < operators for PocketBase date field
|
||||
// PocketBase accepts simple date strings in comparison operators
|
||||
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
|
||||
.collection("dailyLogs")
|
||||
.getFirstListItem<DailyLog>(`user="${user.id}" && date~"${today}"`, {
|
||||
sort: "-created",
|
||||
});
|
||||
.getFirstListItem<DailyLog>(
|
||||
`user="${user.id}" && date>="${today}" && date<"${tomorrow}"`,
|
||||
{ sort: "-created" },
|
||||
);
|
||||
|
||||
logger.info(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user