# DB State – Fleet / Cost / Service Search
**Snapshot:** 2026-01-28  
**Source:** “Adatbázis állapot napló” (user log)  
**Scope:** Fleet management + cost tracking + service search  
**Out of scope:** CRM (explicit), Email/Auth/Bot/Subscriptions (kezelve, de nem most)

---

## 1. Canonical status (most reliable)
### 1.1 Database base
- **Schema separation:** `data` schema is the business schema (public cleanup in progress earlier, later marked stable).
- **Scale:** ~40 tables reported as “stabil” on 2026-01-28 (2105–2230 blocks).
- **Integrity:** FK constraints + enums “élesek” (2026-01-28 2219).

### 1.2 Backup reference
- **Backup created:** `/mnt/nas/git_vault/backup_20260128_alap_kesz.sql` (20260128_2130)
- **Action:** Use this as baseline snapshot identifier for future diffs.

---

## 2. In-scope modules

## 2.1 Fleet: vehicle registry & hierarchy
### Reported as implemented
- Vehicle hierarchy guaranteed: **Category -> Brand -> Model -> Variant** (20260128_2145)
- Vehicle categories seeded: `CAR`, `MOTORCYCLE`, `TRUCK` (20260128_2200)
- Vehicle variants extended with:
  - `power`
  - `fuel`
  - `cylinder`
  (2026-01-28 22:45)

### VIN verification
- `vin_deadline` logic (14 days) for temporary vehicles (2026-01-28 22:15)
- User Vehicles extended with:
  - `vin_verified`
  - `vin_deadline`
  (20260128_2230)

### OPEN / NEED CONFIRMATION
- Exact table names for fleet core (mentioned: `vehicles`, `vehicle_ownership`, `user_vehicles`).
- Confirm whether `user_vehicles` exists or it’s a logical label for ownership.

---

## 2.2 Cost Tracking (expenses / fueling / TCO)
### Reported as partially implemented
- Mentioned: “companies, equipment and subscription tables live” and “specifications (tire, service interval) added” (2026-01-28 21:50)
- Mentioned custom variable:
  - `custom_service_interval_km` introduced (2026-01-28 21:50)

### OPEN / NEED CONFIRMATION (critical)
- Expense tables (expected examples):
  - `expenses` / `vehicle_expenses`
  - `fuel_logs` / `refuels`
  - `service_events` / `maintenance_records`
- Expense category enum exists: `expense_category` (reported in 20260128_2105)
- We must confirm:
  - columns (amount, currency, vendor, odometer, date, invoice link)
  - whether costs are per vehicle and per org (tenant).

---

## 2.3 Service Search / Marketplace (matching)
### Reported as implemented
- `MatchingService` exists + `/api/v1/search/match` endpoint created (v2.1 / 2026-01-27)
- Ranking formula recorded:
  - `S = (Pdist * Wdist) + (Prate * Wrate) + Btier`
  (v1.9)
- Dynamic weights served via ConfigService reading from `data.system_settings` (v1.9–v1.7)
- Geo base:
  - `data.organization_locations` created to support multi-site providers (v2.2–v2.6)
  - lat/lng stored there; join to organizations

### Provider entities
- `data.organizations` is canonical org table
- `orgtype.SERVICE` enum introduced for service providers (v2.7)
- `service_specialties` exists (v2.0)

### OPEN / NEED CONFIRMATION
- Whether distance calculation is in SQL or Python (Haversine mention, but no final implementation proof).
- Whether PostGIS is used (not mentioned) vs plain numeric calculation.

---

## 3. Config / rules engine (used by in-scope modules)
### system_settings canonical structure
- Canonical columns:
  - `key_name` (varchar)
  - `value_json` (jsonb)
  - overrides: `region_code`, `tier_id`, `org_id`
- Unique index:
  - `idx_settings_lookup` over `(key_name, COALESCE(region_code,''), COALESCE(tier_id,0), COALESCE(org_id,0))`
- Confirmed “sync with ConfigService” (v1.7)

### Known keys (from variable map)
- `max_vehicles` default 3
- `search_radius` default 20 (mentioned)
- ranking policy weights exist via system_settings or `ranking_policies`

---

## 4. Out of scope (present but not in current focus)
> Keep in DB, ignore in implementation planning for now.
- Auth v1/v2, verification_tokens, audit_logs
- email_providers/email_logs/email_templates
- subscription tiers / org_subscriptions / notification rules
- bot_discovery_logs & bot module

---

## 5. Known fixes already applied (from logs)
- `system_settings` and `email_templates` got a `key` column earlier, then standardized to `key_name/value_json` for system_settings.
- verification_tokens expiration validation: “now() based check” added.
- Sequences resynced with `setval(...)` to resolve ID collisions.
- Enum issue fixed (`tokentype email_verify` added).

---

## 6. Risks & ambiguity markers
### High risk (must verify)
- Cost tracking schema: tables/columns not explicitly listed.
- Fleet ownership: `vehicle_ownership` vs `user_vehicles` naming.
- Ranking storage split: `ranking_policies` table exists but weights also in `system_settings`.

### Medium risk
- Duplicate log entries may hide a later revert.
- “40 tables stable” statement needs objective verification.

---

## 7. Verification checklist (run on DB, capture output)
Run these and paste results into a new section “Verification Output”.

1) List schemas and table count
- `SELECT table_schema, count(*) FROM information_schema.tables WHERE table_type='BASE TABLE' GROUP BY 1 ORDER BY 1;`

2) Confirm canonical tables exist
- `SELECT to_regclass('data.system_settings'), to_regclass('data.organizations'), to_regclass('data.organization_locations');`

3) system_settings columns
- `SELECT column_name, data_type FROM information_schema.columns WHERE table_schema='data' AND table_name='system_settings' ORDER BY ordinal_position;`

4) idx_settings_lookup exists
- `SELECT indexname, indexdef FROM pg_indexes WHERE schemaname='data' AND tablename='system_settings';`

5) Find cost tables (discovery)
- `SELECT table_name FROM information_schema.tables WHERE table_schema='data' AND table_name ILIKE '%expense%' OR table_name ILIKE '%fuel%' OR table_name ILIKE '%service%' ORDER BY 1;`

6) Enumerations list
- `SELECT t.typname, e.enumlabel FROM pg_type t JOIN pg_enum e ON t.oid=e.enumtypid ORDER BY t.typname, e.enumsortorder;`

---

## 8. Next actions (implementation-oriented)
1) Freeze baseline: label backup as DB_BASELINE_20260128
2) Validate in-scope tables and fill missing schema details (cost module)
3) Create “Module Status” doc based on verified tables:
   - Fleet
   - Cost
   - Service Search
