> ## Documentation Index
> Fetch the complete documentation index at: https://dhanurgo.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# CSV Import

> Importing Turo trip earnings and earnings report CSVs, deduplication, and audit trail

# CSV Import

HostMetrics supports importing two types of Turo CSV exports. The Data Sources page (`/data-sources`) provides the upload UI and import history.

## Import Types

| Type                | Tab                 | What It Contains                                                                        |
| ------------------- | ------------------- | --------------------------------------------------------------------------------------- |
| **Trip Earnings**   | `TripEarningsTab`   | Per-trip breakdown with 30+ fields (guest, dates, earnings line items, fees, discounts) |
| **Earnings Report** | `EarningsReportTab` | Monthly summary entries from Turo's earnings report                                     |

See [Trip Earnings CSV](/data-reference/trip-earnings-csv) and [Earnings Report CSV](/data-reference/earnings-report-csv) for full field specifications.

## Upload Flow

```
User selects CSV → FileUploader parses file → Preview shown
→ User confirms → import-service.ts processes rows
→ csv_imports record created → Rows inserted to trips/earnings table
→ Vehicles auto-created if new → Import marked 'active'
```

1. **File selection** — Drag-and-drop or file picker via `DataUploadSection`
2. **Parsing** — CSV is parsed client-side, columns are mapped to expected fields
3. **Preview** — User sees a summary of rows, detected year, and any warnings
4. **Import** — Rows are inserted with deduplication checks
5. **Confirmation** — Import history updates in `ImportHistoryTable`

## Chrome Extension Alternative

Instead of manually downloading CSVs from Turo, the Chrome extension can sync trip data directly. See [Chrome Extension Sync](/features/chrome-extension-sync) for details.

## Deduplication

Each imported row generates a checksum from its key fields (reservation ID, dates, amounts). If a row with the same checksum already exists, it is skipped. This makes re-importing the same CSV safe — no duplicate records are created.

## Vehicle Auto-Creation

During trip import, if a vehicle name in the CSV does not match any existing vehicle, a new vehicle record is automatically created and linked. This ensures all trips have a valid `vehicle_id`.

## Year Detection and Partitioning

The import system detects the year from trip dates in the CSV. The `YearDataTable` component shows imported data organized by year, allowing users to manage and delete imports per year.

## Audit Trail

Every import creates a `csv_imports` record containing:

* File name and type
* Row count
* Import timestamp
* Status (`active` or `deleted`)
* User ID

The `ImportHistoryTable` displays all past imports with the ability to delete (soft-remove) an import and its associated records.

## Error Handling

<Note>
  If a CSV has unexpected columns or missing required fields, the import shows a clear error message before any data is written. Partial imports do not occur — it is all or nothing.
</Note>

## Key Files

| File                                                 | Purpose                                      |
| ---------------------------------------------------- | -------------------------------------------- |
| `src/lib/db/import-service.ts`                       | Core import logic, parsing, dedup, insertion |
| `src/components/data-sources/DataUploadSection.tsx`  | Upload UI with drag-and-drop                 |
| `src/components/data-sources/TripEarningsTab.tsx`    | Trip earnings import tab                     |
| `src/components/data-sources/EarningsReportTab.tsx`  | Earnings report import tab                   |
| `src/components/data-sources/ImportHistoryTable.tsx` | Past imports with delete                     |
| `src/components/data-sources/YearDataTable.tsx`      | Year-partitioned data view                   |
