Smoking habit tracker

Export feature rewrite #1

Open io-industries.dev
Open opened by io-industries.dev

fix(export): CSV should export date | time | brand name | cost #

Area: Dashboard · Export · server/utils/csv.ts / server/utils/export.ts Type: bug / enhancement Priority: p1

Summary #

The dashboard Export button currently exports the raw DB table. It should return a human-readable CSV with columns date | time | brand name | cost.

Current behavior #

  • POST /api/v2/cigs/export (and GET /api/v2/cigs/export) uses buildCigCsv in server/utils/csv.ts:19:
    id,timestamp,date,cig_type_id
    
  • server/utils/export.ts:mapCigRowsToExportData maps rows to { id, timestamp: ms, date: ISO, cigTypeId } — no brand resolution, no cost, date+time collapsed to a single ISO string.
  • pages/dashboard.vue:exportData() just blobs the response. No column mapping on the frontend.
  • fetchExportCurrency() is available but never used for the CSV.

Expected behavior #

CSV header must be exactly:

date,time,brand name,cost

Row example:

2025-09-13,14:32,Marlboro,€0.30

Rules:

  • date — local YYYY-MM-DD derived from cig.timestamp
  • time — local HH:mm (24h) derived from cig.timestamp
  • brand name — resolved via cigTypes.name (cigs.cigTypeId -> cigTypes.id). Empty string if null / deleted type. RFC4180-quoted if it contains , or " (reuse csvEscape).
  • cost — cigTypes.pricePerUnit formatted to 2 decimals with user currency symbol (fetchExportCurrency() / getSymbol() — EUR default). If no type/price, empty or 0.00 (decide and document). Must not double-count source-attributed cost — per-cig type pricing takes precedence (see server/utils/stats/cost.ts:attributedCost).
  • Keep existing ?format=json path untouched.
  • Keep RFC4180 quoting for all fields (already handles "/\n/,/=).
  • Range / filtering should respect existing resolveExportRange() logic (default 30-day window, range=custom with from/to).

Files to touch #

  • server/utils/csv.ts — change header + row mapping
  • server/utils/export.ts — add brand+cost resolution (join cigTypes), add formatDate/formatTime helpers, update CigExportRow / CigExportDataRow
  • server/api/v2/cigs/export.post.ts — pass currency/types into CSV builder if needed
  • pages/dashboard.vue — no change needed unless filename/headers change
  • tests/integration/export.test.ts — update expectations

Acceptance criteria #

How to test #

  1. Create 2 cig types (e.g. Marlboro €0.30, Camel €0.25) in Settings.
  2. Log cigs with each type + one without type.
  3. Click Export on dashboard → open CSV → verify 4 columns, correct local date/time, brand, cost.
  4. curl -H "Authorization: Bearer …" -X POST /api/v2/cigs/export?format=csv → inspect header.
Sign up or Sign in to add to the discussion.