Commit graph

161 commits

Author SHA1 Message Date
DEls
2d36e9b1cd fix(vehicles): handle empty unit_type during updates
Updated the vehicles API to allow PATCH requests with an empty unit_type. This change ensures that existing records with unit_type='' can be updated without validation errors, while maintaining the default value of 'fuel' for new inserts. This adjustment resolves issues with updating pre-existing vehicle records that lack a unit_type field.
2026-08-29 19:17:14 +02:00
DEls
9ea0a1f65e fix(pb): make vehicles.unit_type optional
53 vehicles predate the unit_type field and have unit_type=''. PB
validates the WHOLE record on any PATCH, so as long as unit_type was
required, no record with an empty value could be updated (Dismiss,
inline-edit, anything calling .update()). Making the field optional
unblocks all 53 immediately.

The default='fuel' still applies to new inserts.

Note: the live PB schema lost its 'default' value during the PATCH
(see review below) — only 'required' was actually changed in the
stored schema. Fresh records will be inserted with unit_type='' (or
null) until the application sets it explicitly. The bootstrap
definition still has default='fuel' so fresh environments get it.

Idempotent migration: scripts/migrations/2026-08-28-vehicles-unit-type-optional.py
applied successfully; re-run is a no-op.
2026-08-28 15:26:33 +02:00
DEls
0ea0257edb fix(plant-hours): surface real PB field-level error in import response
formatPbError walked err.data but PB's ClientResponseError wraps the
field-level error object at err.data.data. The outer err.data has
just three keys (data, message, status), all 'invalid' on a failed
insert — useless to operators. Read err.data.data (and
err.data.data.fieldErrors if present) so the operator sees WHICH field
rejected the row.
2026-08-28 14:43:29 +02:00
DEls
0290ced51a fix(schema): ensure is_recurring=false writes are preserved
The live PB fixed_costs collection never received the is_recurring field
added in 87bb179 — the bootstrap script is collection-create-only and
errors out on already-existing collections, and no migration added the
field. Smoke-test rows sent is_recurring=false and got it silently
dropped (PB ignores unknown fields on write), then the partition helper
treated the missing value as monthly (its !=== false fallback).

Two changes:

1. Drop 'default: true' from the bootstrap schema definition. A bool
   default only applies when the field is absent from the write payload,
   so this is belt-and-braces — but it removes any doubt that a default
   could ever silently override an explicit false. The partition helper
   still treats absent values as monthly, and the backfill migration
   patches pre-existing rows to true.

2. Make the backfill migration self-contained: if the field is missing
   from the collection, add it (bool, required=false, no default) before
   patching rows. Idempotent — no-op if already present.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-28 13:43:38 +02:00
DEls
ad12a3c01c feat(plant): weekly hours log + attention required on /dashboard/plant
Adds two new sections below the existing equipment table:
- Attention required: rows where the row's vehicle unit_type doesn't
  match vehicle_unit_type_at_import. Operator can delete the row from
  here.
- Weekly plant hours log: per-week entries with hours, budget,
  cost/hr, value, and over-budget badge.

The topbar gets two new buttons (Download template, Import plant
hours) sitting next to the existing EditModeToggle.
2026-08-28 13:40:30 +02:00
DEls
a214012d99 fix(plant-hours): restore spec-mandated warnings[] in import response
Task 8's implementer dropped warnings[] in favour of a mismatched
count, but the design spec mandates warnings[]. Task 11's
ImportPlantHoursButton consumes warnings[] and crashes at runtime
when the response is parsed. Restore warnings[] (one entry per
mismatched-unit-type row, plus any schema-level warnings from
validatePlantHoursRow) while keeping the useful mismatched count.
2026-08-28 13:36:46 +02:00
DEls
394a5c0033 feat(plant-hours): ImportPlantHoursButton component
Mirrors ImportFuelLogsButton for the plant-hours weekly log upload
flow. Supports drag/drop, click-to-browse, shows import summary
(total / imported / mismatched / skipped), and links to the
exception-report CSV when rows are skipped.
2026-08-28 13:31:30 +02:00
DEls
c9789d89f4 feat(vehicles): PATCH /api/vehicles/[id] accepts unit_type
Lets the inline-edit flow on /dashboard/plant set the vehicle's
unit_type (fuel|plant) so the plant-hours import route's type-mismatch
warnings can be resolved by reclassifying the vehicle.

Adds a route.test.ts mirroring the attention-dismiss precedent: 11
cases covering happy paths for unit_type='plant'/'fuel', invalid
unit_type dropped (with/without other valid fields), existing
description+project_code behaviour preserved, JSON parse errors,
empty-payload errors, and PB write failures.
2026-08-28 13:27:22 +02:00
DEls
b22badb4f0 feat(sites-list): include one-off overheads in per-site spent total 2026-08-28 13:24:46 +02:00
DEls
1909c5c3d3 feat(plant-hours): DELETE /api/plant-hours/[id] route
Removes one plant_hours_log row. Used by the Attention Required panel
and the weekly log table's row menu.
2026-08-28 13:23:58 +02:00
DEls
512c68f7a2 refactor(site-detail): extract PnGSection, add one-off to outflows panel, reuse PnGItemLike 2026-08-28 13:22:13 +02:00
DEls
da21518f42 refactor(fixed-cost-dialog): drop defaultType prop, use field-message lookup, local-date default 2026-08-28 13:22:07 +02:00
DEls
84a7d5bd74 feat(plant-hours): POST /api/plant-hours/import route
Pure orchestration (importPlantHours) takes parsed source rows +
vehicle/site lookups and produces prepared plant_hours_log records
+ per-row errors. The route handles multipart upload, batch creation,
PB lookups, and insert-with-error-capture. Mismatched unit_type
vehicles are imported as audit-only records; PB dup-key failures
are recorded as 'duplicate' errors instead of failing the batch.
2026-08-28 13:19:06 +02:00
DEls
28c4bc3eeb fix(site-detail): make is_recurring optional on PnGItem per spec 2026-08-28 13:17:24 +02:00
DEls
b59380455c feat(site-detail): split P&G card into monthly + one-off sections 2026-08-28 13:14:15 +02:00
DEls
014954b670 fix(plant-hours): exception-report correctness + safety
- Normalise date diff keys via parseDate()/slice() so imported rows
  aren't all dumped as exceptions (date format mismatch silently
  mis-classified every row as not_imported).
- Branch CSV vs XLSX when re-parsing the source file (type:'buffer'
  doesn't work for CSV uploads).
- Validate batchId against /^[A-Za-z0-9]{15}$/ before interpolating
  into the PB filter to close a filter-injection sink.
- Lift headers.indexOf out of the row loop, gate console.log, convert
  dynamic imports to static, fix variable shadow.
2026-08-28 13:13:27 +02:00
DEls
bd47d3fc70 feat(site-detail): partition fixed costs into monthly + one-off 2026-08-28 13:08:12 +02:00
DEls
5636730f5e feat(plant-hours): exception-report CSV builder + route
buildPlantHoursExceptionCsv() emits a 15-column CSV with the 13
template columns + reason + operator_note, RFC-4180-quoted. The
GET /api/plant-hours/exception-report/[batchId] route re-parses the
source file from data_import_batches.original_file, diffs against
the imported plant_hours_log rows for that batch, and emits the CSV
of rows that did NOT import.
2026-08-28 13:06:17 +02:00
DEls
73ad9fdb5a feat(types): extend PnGItem + SiteDetailData for one-off partition 2026-08-28 13:04:48 +02:00
DEls
11658bce2d feat(api): POST /api/sites/[id]/fixed-costs 2026-08-28 13:01:58 +02:00
DEls
9f2c10851c test(fixed-costs): add failing API route tests 2026-08-28 12:57:49 +02:00
DEls
7b8fe71db3 feat(plant-hours): GET /api/plant-hours/template route
Emits the canonical xlsx with a sample row derived from the first
vehicle in PocketBase. Falls back to a hardcoded sample if the
vehicles collection is empty.
2026-08-28 12:57:29 +02:00
DEls
c36eaeed51 fix(plant-hours-template): ESM-safe __dirname + freeze note
Two follow-up fixes from the Task 5 code-quality review:

- scripts/generate-plant-hours-template.ts uses
  fileURLToPath(import.meta.url) instead of __dirname. The
  project's tsconfig has module: esnext, so the moment the
  package flips to ESM the original would throw
  ReferenceError: __dirname is not defined.
- src/lib/plant-hours-template.ts documents that the frozen
  header row is a best-effort request — SheetJS Community
  Edition silently drops the !freeze property (only Pro emits
  frozen-pane XML). The function still produces the canonical
  13-column workbook, but the doc comment no longer claims a
  behaviour that doesn't happen.
2026-08-28 12:55:08 +02:00
DEls
70de32cd62 feat(fuel): Dismissed pill + Restore on vehicle detail page
Threaded attentionDismissedAt from the page mapper into the view.
When set, the header shows a pill with the timestamp and a Restore
button that DELETEs /api/vehicles/{id}/attention-dismiss and
refreshes the page. Errors render inline below the pill.

Slug-to-prose map for restore failures mirrors the dismiss-side
pattern from commit c633681 — RESTORE_ERROR_COPY at module scope,
friendly copy in window-less inline error slot.
2026-08-28 12:54:34 +02:00
DEls
71eb1c00cc docs(plan): fix byDateDesc to return 0 on equal dates 2026-08-28 12:54:02 +02:00
DEls
e0e46b1c46 feat(lib): add partitionFixedCosts helper 2026-08-28 12:53:38 +02:00
DEls
c633681789 fix(fuel): review fixes for dismiss button
- Disable all dismiss buttons while any is in flight so rapid
  clicks on a second vehicle don't silently no-op.
- Map API error slugs (bad_id, not_found, update_failed) to
  user-facing copy so window.alert shows prose, not machine tags.
- Add aria-busy to the dismiss buttons.
- Drop redundant e.stopPropagation(); preventDefault() is
  sufficient to suppress the wrapping <Link>.
2026-08-28 12:52:29 +02:00
DEls
1e212a7339 feat(plant-hours): template workbook generator + seed file
buildPlantHoursTemplateWorkbook() emits the canonical 13-column xlsx
with a frozen header row, typed cells (date/time/number), a sample
row using the supplied vehicle, and a Notes column widened for
readability. Used by both /api/plant-hours/template and the seed
script that writes RAW_DOCS/Plant_Hours_Template_2.xlsx.
2026-08-28 12:51:14 +02:00
DEls
f629e4b369 test(partition-fixed-costs): add failing tests 2026-08-28 12:50:36 +02:00
DEls
3c379de5c4 test(plant-hours): cover unknown-QR deviation + value rounding
Adds two tests that lock in behaviour the previous test file relied on
implicitly:
- validatePlantHoursRow rejects when the supplied vehicle's qr_code_id
  doesn't match row.vehicle_qr (the guard the schema extends beyond
  the original spec)
- computePlantHoursFields rounds value to 2 decimal places (a
  fractional-input case the integer-only assertion didn't exercise)
2026-08-28 12:48:42 +02:00
DEls
4b5c93da37 feat(types): add is_recurring to FixedCostRecord 2026-08-28 12:47:40 +02:00
DEls
dc966086bd docs(plan): mark backfill migration as required after schema deploy 2026-08-28 12:47:02 +02:00
DEls
840c18cf6e feat(plant-hours): row validator + derived field helpers
Pure validator covering all the error/warning cases from the spec:
unknown QR, bad date range, end_hours < start_hours, bad time format,
missing cost (cell + vehicle), duplicate week, and the type-mismatch
warning. computePlantHoursFields() derives hours/value/over_budget/
hours_mismatch and the vehicle_unit_type_at_import snapshot.
2026-08-28 12:46:13 +02:00
DEls
29524b362d feat(fuel): Dismiss button on attention rows + optimistic overlay
Click on an AlertCard or AlertListRow to hide the vehicle from
'Attention required' instantly. POST /api/vehicles/{id}/attention-dismiss
stamps the field; optimistic state mirrors that locally so the row
disappears before the network roundtrip. Error path reverts the
overlay and shows window.alert (same pattern as inline-edit).
2026-08-28 12:45:59 +02:00
DEls
70522ed1ae feat(migrations): backfill is_recurring=true on fixed_costs 2026-08-28 12:43:25 +02:00
DEls
077dc35cf2 refactor(imports): extract shared import-parse helpers + add parseTime
Moves normHeader/resolveColumn/parseDate/toNumber out of fuel-import-parse
into a new src/lib/import-parse.ts so plant-hours can reuse them. Adds
parseTime (HH:MM, 24h, single-digit hour normalised). Adds the
PLANT_COL_ALIASES table for the plant-hours route. fuel-import-parse
becomes a thin re-export shim — the fuel-logs route and its existing
test file are unchanged.
2026-08-28 12:41:59 +02:00
DEls
87bb1790fe feat(schema): add is_recurring bool to fixed_costs collection
Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-28 12:40:00 +02:00
DEls
4fe2870f42 feat(fuel): filter attention list through isAlertAcknowledged
Server mapper now emits attentionDismissedAt and maxFlaggedDate on
each VehicleSummary. The flaggedVehicles memo uses the new helper
to hide vehicles whose current flagged logs are all at-or-before
the user's most recent dismissal. Auto-undismiss is implicit: a
new fuel log with date_logged > attentionDismissedAt brings the
vehicle back on the next page load.
2026-08-28 12:39:45 +02:00
DEls
45e2cd7827 docs(plan): clarify Burn by category stays monthly-only 2026-08-28 12:37:53 +02:00
DEls
574df58011 docs(plan): site overhead monthly + one-off implementation plan 2026-08-28 12:37:29 +02:00
DEls
272e23b925 feat(types): VehicleRecord.unit_type + PlantHoursLogRecord
Adds UnitType union ("fuel" | "plant"), the new field on VehicleRecord,
the new PlantHoursLogRecord, and "plant_hours" to BatchType.
2026-08-28 12:37:28 +02:00
DEls
207a13244e feat(fuel): isAlertAcknowledged helper for dismiss filter
Pure predicate: returns true when a vehicle has been dismissed and
no flagged log is strictly newer than the dismissal. Six Vitest cases
covering each branch and the auto-undismiss boundary.
2026-08-28 12:35:36 +02:00
DEls
ccaeee6896 docs(spec): clarify card subtitle covers monthly + one-off 2026-08-28 12:33:53 +02:00
DEls
2fac85e79d docs(spec): site overhead monthly vs one-off design 2026-08-28 12:33:39 +02:00
DEls
535649bf54 fix(pb): default vehicles.unit_type to fuel
The plan spec called for default "fuel" on the new select so existing
vehicles rows (which would otherwise fail the required=True
constraint) stay valid. PocketBase applies the default at the field
level when adding it to an existing collection.
2026-08-28 12:32:52 +02:00
DEls
6cdcb4fa9f feat(fuel): POST/DELETE /api/vehicles/[id]/attention-dismiss
Sets or clears vehicles.attention_dismissed_at via pbAdmin().
15-char PB id pattern, 404 when PB reports the record is missing,
500 on any other write failure. Test mocks pbAdmin() so no live PB
needed.
2026-08-28 12:31:23 +02:00
DEls
ffd840eac1 feat(pb): vehicles.unit_type + plant_hours_log collection
Adds the schema needed for the weekly plant-equipment hours log:
- vehicles gets a unit_type select (fuel|plant), defaults to fuel
  for backward-compat with existing rows
- plant_hours_log collection with the 13 plant-hours fields plus
  vehicle_unit_type_at_import snapshot and composite unique index
  (vehicle_qr, start_date)
- data_import_batches.batch_type gains plant_hours
2026-08-28 12:31:17 +02:00
DEls
2204989a3c docs(plan): plant hours weekly log implementation plan
Bite-sized TDD task decomposition covering 13 tasks:
1. PocketBase schema (vehicles.unit_type + plant_hours_log)
2. Type updates (UnitType, PlantHoursLogRecord, BatchType)
3. Shared import-parse helpers + parseTime
4. Per-row validator + derived fields
5. Template generator + seed script
6. GET /api/plant-hours/template
7. Exception-report CSV builder + route
8. POST /api/plant-hours/import
9. DELETE /api/plant-hours/[id]
10. PATCH /api/vehicles/[id] accepts unit_type
11. ImportPlantHoursButton component
12. /dashboard/plant page extension
13. End-to-end smoke test

Each task has files, failing tests, implementation code, run commands,
and commit step. Follows the approved design spec.
2026-08-28 12:26:35 +02:00
DEls
fb7d3d51b1 feat(fuel): PB schema migration for attention_dismissed_at
Adds the optional date field to VEHICLES via a one-shot idempotent
migration, and keeps scripts/pb-bootstrap-schema.py in sync so freshly
bootstrapped environments also get it. No data backfill — existing
rows have the field set to null, which the UI treats as 'not yet
dismissed'.
2026-08-28 12:26:23 +02:00
DEls
c5230190d0 docs(plan): fuel attention dismissal implementation plan
7-task TDD plan: PB migration, API route + tests, isAlertAcknowledged
helper + tests, server mapper + filter, Dismiss button + optimistic
overlay, Dismissed pill + Restore on detail page, full verification
with Agent Browser. Steps use checkbox syntax for subagent-driven
or inline execution.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-28 12:24:14 +02:00