Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions backend/tests/frontend/home-logbooks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// tests/frontend/home-logbooks.test.ts
// The home panel asked for the important logbook entries one day at a time:
// up to 31 requests to /api/logbooks/day/ for the month. It now asks /all for
// the whole range, page by page. Reads the frontend sources; no browser.

import { describe, it, expect } from 'vitest'
import { readFileSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const frontend = join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..', 'frontend')
const read = (...p: string[]) => readFileSync(join(frontend, ...p), 'utf8')
const page = read('app', 'dashboard', 'page.tsx')
const queries = read('app', 'lib', 'logbooks', 'queries.ts')

describe('home panel logbook entries', () => {
it('asks for the whole period at once, not one request per day', () => {
expect(page).not.toContain('getLogbooksByDay')
expect(page).not.toMatch(/dates\.map\(/)
expect(page).toMatch(
/logbooksApi\s*\.getLogbooksInRange\(range\[0\], range\[range\.length - 1\]\)/
)
})

it('sorts the dates first: the week comes newest first', () => {
expect(page).toContain('const range = [...dates].sort()')
})

it('reads every page of /all for that range', () => {
const start = queries.indexOf('getLogbooksInRange: async (')
expect(start).toBeGreaterThan(-1)
const body = queries.slice(start, queries.indexOf('\n },', start))
expect(body).toContain('/api/logbooks/all?date_from=${dateFrom}&date_to=${dateTo}')
expect(body).toContain('&limit=${RANGE_PAGE}&offset=${offset}')
expect(body).toMatch(/if \(page\.length < RANGE_PAGE\) return entries/)
// The backend accepts up to 500 per page
expect(queries).toContain('const RANGE_PAGE = 500')
})
})
10 changes: 5 additions & 5 deletions frontend/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ export default function DashboardHome() {
}
}

const responses = await Promise.all(
dates.map((date) => logbooksApi.getLogbooksByDay(date).catch(() => []))
)

// One request for the whole period (one per page), not one per day
const range = [...dates].sort()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const allLogbooks: any[] = responses.flat().filter(Boolean)
const allLogbooks: any[] = await logbooksApi
.getLogbooksInRange(range[0], range[range.length - 1])
.catch(() => [])

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const logbooksInPeriod = allLogbooks.filter((entry: any) => {
Expand Down
16 changes: 16 additions & 0 deletions frontend/app/lib/logbooks/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

import apiClient from '@/app/lib/apiClient'
import { API_BASE_URL } from '@/app/lib/env'
import type { LogbookEntry } from './types'
// Types available in ./types if needed: LogbookEntry, LogbookComment

const API_URL = API_BASE_URL

// Largest page /api/logbooks/all accepts
const RANGE_PAGE = 500

// =============== LOGBOOKS API ===============

export const logbooksApi = {
Expand Down Expand Up @@ -63,6 +67,18 @@ export const logbooksApi = {
}
},

// Entries whose day falls between two dates (YYYY-MM-DD), every page
getLogbooksInRange: async (dateFrom: string, dateTo: string): Promise<LogbookEntry[]> => {
const entries: LogbookEntry[] = []
for (let offset = 0; ; offset += RANGE_PAGE) {
const page = await apiClient.get<LogbookEntry[]>(
`${API_URL}/api/logbooks/all?date_from=${dateFrom}&date_to=${dateTo}&limit=${RANGE_PAGE}&offset=${offset}`
)
entries.push(...page)
if (page.length < RANGE_PAGE) return entries
}
},

// Eliminar un logbook (soft-delete)
deleteLogbook: async (id: number) => {
return apiClient.delete(`${API_URL}/api/logbooks/${id}`)
Expand Down
Loading