284 Commits

Author SHA1 Message Date
c20fc5462a fix(build): per-commit cache-bust so web-only changes actually deploy
Auto-deploys (docker build with cache) were reusing a stale build layer, so
web-only commits shipped an old bundle — the switcher removal (#216) and the
canonical fix (#217) only appeared after a manual --no-cache/force rebuild.
Reference SOURCE_COMMIT (substituted by Coolify) right before `COPY . .`, so the
build layer's cache key changes every commit and COPY + `pnpm build` re-run. The
deps stage stays cached on the lockfile, so installs aren't repeated.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 07:43:05 +03:00
2bf7525fe2 Merge pull request 'fix(catalog): Fiat Birleşik = canonical view (not raw pcat)' (#217) from feat/fiat-birlesik-canonical into dev
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
2026-07-14 07:11:53 +03:00
bf49900b57 fix(catalog): Fiat "Birleşik" stays the canonical view, not the raw pcat tree
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Correction to #216: "Birleşik" must fold the catalog into OUR canonical 20-bucket
structure (pcat + eper), not show parts-catalogs' raw tree. Only "Orijinal
Katalog" swaps to the native ePER tree for Fiat.

- treeSource → eper-pekidi for Fiat (Orijinal); canonical tab renders CanonicalView
- reverts #216's "canonical tab → pcat source tree"

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 07:11:50 +03:00
ff96dcac1c Merge pull request 'feat(catalog): Fiat tabs → eper/pcat, drop extra switcher' (#216) from feat/fiat-eper-simplify into dev 2026-07-14 06:56:14 +03:00
5a3e1a2299 feat(catalog): map the two Fiat catalog tabs onto eper/pcat, drop the extra switcher
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Simplify the previous change: instead of a third source-switcher pill, reuse the
two tabs that are already there. For an eper (Fiat) vehicle:
  "Orijinal Katalog" → the native ePER tree (default)
  "Birleşik"         → parts-catalogs
Non-eper vehicles keep the original source/canonical tabs untouched.

- web: derive treeSource from browseMode when the vehicle has eper; canonical
  tab renders the pcat source tree (not CanonicalView) for eper vehicles
- removes the ePER/Diğer switcher, SOURCE_LABELS, and the activeSource state
Net −27 lines.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 06:56:12 +03:00
24495d8970 Merge pull request 'feat(catalog): eper as default Fiat catalog source (pcat switchable)' (#215) from feat/fiat-eper-default into dev 2026-07-14 06:42:06 +03:00
de960a0dd4 feat(catalog): eper as the default Fiat catalog source, pcat one tap away
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Fiat dealers know the native ePER tree; parts-catalogs' broader-but-lazy folders
are where they hit gaps (a motor OEM missing under "Motor" — the reason we
crawled pekidi ePER). So for a multi-source decoded vehicle, default the
"Orijinal Katalog" view to eper-pekidi — fully populated with parts + diagrams —
instead of showing every source's roots mixed together.

- categories.service.getCategoryTree(vehicleId, source?): prefer eper-pekidi when
  present; ?source= overrides; cache keyed per source
- categories: getCatalogSources(vehicleId) → sources present, default (eper) first
- controller: tree/:id/sources endpoint + ?source query
- web: source switcher pill (ePER ⇄ Diğer) in source mode when >1 source;
  defaults to the first (eper). pcat never hidden, just one tap away.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 06:42:04 +03:00
cee11b5769 Merge pull request 'fix(vinpin): reject stale-breadcrumb decodes + harden matcher' (#214) from fix/vinpin-false-positive into dev 2026-07-13 12:09:49 +03:00
70d901f9f5 fix(vinpin): reject stale-breadcrumb decodes (require SINCOM) + harden matcher
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
A failed ePER VIN lookup ("vehicle not found") whose Cyrillic "не найден" alert
OCRs to garbage ("He HaaeHs") slipped the not-found check; the parser then
grabbed the operator's last-browsed catalog from the breadcrumb
("FIAT » TIPO-EGEA") and false-mapped it to a catalog — NM4131 landed on the
ancient "Tipo 1100-1370-1580 (1987-1993)", plus 2× PANDA. Every genuine decode
carries a SINCOM; the breadcrumb false positives never do.

- parser: isUsableParse now requires a SINCOM (a model alone is not a decode)
- matcher: modelTokens keeps 4-digit engine displacements (1100/1370/1580) —
  only 1950-2039 count as years — so ancient catalogs no longer collapse to
  ["TIPO"] and win the fewest-extra-tokens tiebreak
- matcher: recency tiebreak on equal score — never fall back to an ancient
  generation for a bare / year-less model

15 unit tests (parser + matcher) pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-13 12:08:40 +03:00
731ac93a55 Merge pull request 'public-api: katalog gezinme (VIN'siz araç ağacı) endpoint'leri /v1/catalog/* — marka→model→motor→kategori, API-key auth' (#212) from dev into main
Reviewed-on: #212
2026-07-10 12:44:09 +03:00
oyp
0e8b2ab294 public-api: katalog gezinme (VIN'siz araç ağacı) endpoint'leri /v1/catalog/* — marka→model→motor→kategori, API-key auth
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-10 12:27:36 +03:00
814b798b3f Merge pull request 'feat(internal-admin): extend limiti 90 → 3650 gün' (#211) from fix/extend-days-cap into main
Reviewed-on: #211
2026-07-09 11:27:23 +03:00
c51b7b4e78 feat(internal-admin): extend limiti 90 → 3650 gün
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Panelin 'Bonus süre ekle (goodwill)' aksiyonu 360 gün gibi süreler
gerektiriyor; 90 gün sınırı iş ihtiyacını karşılamıyordu. Üst sınır
3650 (10 yıl) parmak-kayması koruması olarak kalıyor. Panel tarafı
(sp) aynı limite güncellendi.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 10:35:28 +03:00
ff81b244b7 Merge pull request 'fix: şerit-bazlı rate bütçesi' (#209) from dev into main 2026-07-09 10:01:55 +03:00
8dd2d223e9 fix(prefetch): şerit-bazlı kaynak bütçesi — fast lane açlığını bitir
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Fast worker canlıydı ama pcat'in 90/dk penceresini ana worker yiyordu → fast
işler sürekli 18s source-rate defer (BMW init'i slot bulamadı). checkSourceRate
artık şerit-bazlı: fast = total/3 (pcat 30), main = kalan (60); toplam
değişmedi → upstream yükü aynı. Cooldown pause'u da doğru worker'ı duraklatıyor.
Spec fast-queue davranışına güncellendi (17/17).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 10:01:47 +03:00
36ed37d5c0 Merge pull request 'feat: prefetch fast kuyruk' (#208) from dev into main 2026-07-09 09:13:34 +03:00
c8803ada35 feat(prefetch): ayrı FAST kuyruk — taze araçlar backlog'la yarışmasın
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Kök neden: ana kuyruk saatlerce üretim>tüketim rejiminde (children patlaması);
lifo'da sonradan eklenen HER iş bir öncekini gömer → taze araç init'leri bile
açlığa düşer (BMW 320i 58dk 0 parça). Fix: catalog-prefetch-fast kuyruğu +
ikinci worker (conc=4, 120/dk) — decode-tetiği ve fast:true işler (children
zinciri dahil) buraya akar; per-source redis rate gate iki worker'ı da ortak
sınırlar (upstream korunur). vehicles.service fast kuyruğa bağlandı.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 09:13:26 +03:00
ccc2cd75a5 Merge pull request 'feat: birleşikte görünüm değiştiriciler' (#207) from dev into main 2026-07-09 08:11:24 +03:00
f2a37b78cb feat(web): Birleşik görünümde grid/ağaç/kolon değiştiricileri çalışsın
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
CanonicalView viewMode prop'u: tree/columns için kanonik veriden sentetik
hiyerarşi (kova → alt-yaprak → gerçek kategoriler; sentetik düğümler children'lı
geldiğinden lazy-fetch tetiklenmez, gerçek kategoriler children'sız geçip normal
drill yapar; tpl-* placeholder'lar bu görünümlere alınmaz — grid'de kalır).
vehicles sayfasında CategoryViewToggle artık canonical'da da görünür; katalog
sayfası viewMode'u geçirir.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 08:11:16 +03:00
0e2871c964 Merge pull request 'feat: katalog sayfası Birleşik toggle' (#206) from dev into main 2026-07-09 08:07:21 +03:00
116def2c79 feat(web): pl24 katalog sayfasına Birleşik/Orijinal toggle
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
/dashboard/catalog/:brand/:modelId sayfasında birleşik görünüm yoktu — backend
endpoint'i (/catalog/vehicles/:id/canonical) hazırdı ama web'e hiç bağlanmamıştı.
CanonicalView'a endpoint/catalogMode/brandName/variantSearch prop'ları eklendi
(varyant qs dahil); sayfaya vehicles/$id ile aynı toggle. Varsayılan Orijinal.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 08:07:13 +03:00
bcf8bb38cd Merge pull request 'fix: kanonik sayaç + entry cap' (#205) from dev into main 2026-07-09 07:45:09 +03:00
d38bcaa36f fix(canonical): kova başlık sayacı subgroup'ları da saysın + entry cap 40/kova
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
(1) Placeholder'lar subgroup içinde olduğundan başlık "0 kategori" diyordu —
sayaç artık flat+subgroup birleşik unique. (2) Kalabalık markalar (pl24 BMW
elektrik 537 entry) görünümü boğuyordu — kova başına en-paylaşılan 40 entry
(vehicle_count desc).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 07:45:02 +03:00
25965717c3 Merge pull request 'fix: entries ambiguity' (#204) from dev into main 2026-07-09 07:28:15 +03:00
671e9af15b fix(canonical-backfill): entries GROUP BY'da name'i c.name ile nitele (parent join ambiguity)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 07:28:07 +03:00
da95b0484d Merge pull request 'canonical: şablon entry-seviyesi' (#203) from dev into main 2026-07-09 07:23:51 +03:00
5ed3959a12 feat(canonical): şablon ENTRY-seviyesi — decode anında tam gezinilebilir yapı
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Kullanıcının orijinal tasarımı: aynı (kaynak, marka)da kategori yapısı birebir
aynı — değişen yalnız parça kodları/şemalar. Önceki şablon sadece kova/alt-yaprak
BAŞLIKLARINI overlay ediyordu → taze araçta "Aydınlatma — 0 kategori". Yeni:
canonical_template_entries (migration 0031) — markanın paylaşılan kaynak-kategori
ADLARI (≥2 araç); backfill'de rebuild. overlayTemplate eksik adları disabled
placeholder olarak basar (unavailable=true, CategoryGrid zaten soluk+tıklanamaz
render ediyor) → decode anında her kova tam adlarıyla dolu; drill tamamlanınca
placeholder'lar gerçek satıra dönüşür.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 07:23:43 +03:00
eb92c5c5f8 Merge pull request 'fix: orijinal default' (#202) from dev into main 2026-07-09 07:12:00 +03:00
8a079d84f8 fix(web): Orijinal Katalog tekrar varsayılan — taze araçta boş kanonik kovalar çözülene dek
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 07:11:52 +03:00
1c4c896d64 Merge pull request 'feature/vin-ocr' (#201) from feature/vin-ocr into main
Reviewed-on: #201
2026-07-09 07:05:10 +03:00
3c6c27cb00 fix(vin-ocr): sunucu hata mesajını toast'ta göster
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
"Fotoğraf okunamadı" genel metni gerçek nedeni (örn. 503 "görselden şase
okuma şu anda kullanılamıyor", 400 "desteklenmeyen tür") yutuyordu — dev'de
OPENROUTER_API_KEY boş kaldığında teşhisi zorlaştırdı. ApiError mesajı artık
toast açıklamasında; 429 için Türkçe "çok fazla deneme" metni; PostHog
reason artık http_<status> taşıyor.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 06:52:14 +03:00
8d11ae7400 feat: fotoğraftan şase okuma (VIN foto-OCR)
Dashboard şase kutusunun sağına kamera butonu: kullanıcı fotoğraf yükler
(mobilde kamera), görsel istemcide ~1800px'e küçültülüp JPEG'e çevrilir,
POST /vehicles/decode/ocr OpenRouter üzerinden vision modeline gönderir
(varsayılan google/gemini-2.5-flash; bulamazsa openai/gpt-5.4-nano yedeği),
dönen VIN kutuya yazılır. Decode HER ZAMAN kullanıcının "Şase Çöz"
tıklamasıyla başlar — OCR yalnızca yazma zahmetini alır.

Model seçimi 2026-07-09 canlı bake-off'una dayanır (Commons'tan gerçek VIN
fotoğrafları): flash plaka/etiketleri doğru okudu; flash-lite rakam bozdu,
Haiku 4.5 olmayan VIN uydurdu, OCR.space damgalı şasede 8→S hatası verdi.
Sunucu tarafı katı doğrulama (17 karakter, I→1 O→0 Q→9, aksan ayrıştırma)
"regex'ten geçen ama yanlış" VIN'leri keser; okunaksızsa vin:null döner.

- throttle: kullanıcı başına 10 istek/dk; görsel sınırı 5MB (mevcut multipart
  middleware'iyle uyumlu), sadece JPEG/PNG/WebP
- compose: OPENROUTER_API_KEY api VE worker bloklarına eklendi (worker çeviri
  işçisi bu anahtarı bekliyordu ama compose'a hiç enjekte edilmemişti),
  VIN_OCR_MODEL / VIN_OCR_FALLBACK_MODEL api bloğuna eklendi
- analitik: vin_ocr_used eventi (success/reason/confidence/duration_ms),
  query_source="ocr"
- i18n: search.ocr.* (tr+en)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 06:41:33 +03:00
cafe206001 Merge pull request 'Merge branch 'feature/widget-canonical' into dev' (#199) from dev into main 2026-07-08 06:48:39 +03:00
a803d9da10 Merge pull request 'fix(vinpin): query_logs muhasebesi — pending poll'lar failure satırı yazmasın' (#200) from fix/vinpin-query-log-accounting into main 2026-07-08 06:47:50 +03:00
Semih
8054e2faf3 fix(vinpin): query_logs muhasebesi — poll'lar failure satırı yazmasın
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Vinpin fallback'i failure logQuery'sinden ÖNCE denenir oldu:

- Pending decode'un her 3sn'lik frontend poll'u 'No catalog — identified
  as Fiat' failure satırı yazıyordu: vinpin'in 10sn'de ÇÖZDÜĞÜ tek Egea
  VIN'i 17 failure satırı üretti, ham 24h success -20.7pp okundu
  (2026-07-07, 4 kullanıcı/4 VIN, gerçek infra hatası 0).
- Resolved VIN'lerde her decode kalıcı olarak fail+success çifti
  yazıyordu (failure log vinpin lookup'tan önceydi ve negatif cache
  vinpin çözümünden haberdar değil).

Yeni muhasebe: VIN başına TEK coverage-gap failure satırı (ilk görüş,
enqueue anı — coverage backlog sinyali korunur); pending poll'lar hiç
loglanmaz; resolved decode tek success satırı loglar; not_found/failed
eskisi gibi caller'ın failure log'una düşer.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 06:40:23 +03:00
7956fc9e9b feat: kategori sağlık paketi — dedup+index, prefetch throughput, v1 taxonomy, Birleşik default
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
(1) migration 0030: 57.780 duplike kategori merge (prod'da uygulandı, idempotent)
+ categories unique index NULLS NOT DISTINCT → decode retry duplike üretimi
KALICI olarak durdu (onConflictDoNothing artık NULL sınıflarında da çalışıyor).
(2) prefetch throughput: backlog %89 pcat + net-büyüyordu (82k→94k). pcat
20→90/dk (DataImpulse rotating gateway), pace 5s→1.5s, conc 3→8, global 180.
pl24/emex dokunulmadı (ban riski).
(3) GET /v1/taxonomy: araçtan bağımsız kanonik yapı kontratı (B2C navigasyon).
(4) Birleşik tekrar varsayılan görünüm (edge-case'ler çözüldü).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-08 06:26:54 +03:00
006cf1fd63 Merge pull request 'feat(widget): kanonik (birleşik) kategori görünümü' (#198) from feature/widget-canonical into main
Reviewed-on: #198
2026-07-06 11:47:33 +03:00
6fb27d9509 Merge branch 'feature/widget-canonical' into dev 2026-07-06 11:32:55 +03:00
394e71e269 feat(widget): kanonik (birleşik) kategori görünümü
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Widget artık /categories varsayılan kanonik cevabını render eder (view=raw
kaldırıldı): kaynak-bağımsız kova → alt-grup → yaprak kategori → parça gezinme.
Frame-tabanlı breadcrumb (her seviye kendi render'ını taşır), ortak gridEl/grid
çizici, leafItem drill'i /categories/:id ile parça+şema. Dashboard'daki
'Birleşik' varsayılanıyla aynı deneyim.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 11:32:55 +03:00
454eab6b0e Merge pull request 'dev' (#197) from dev into main
Reviewed-on: #197
2026-07-06 11:10:03 +03:00
6585412932 Merge remote-tracking branch 'origin/dev' into dev
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
2026-07-06 11:02:55 +03:00
df04784ddb fix(widget): kategori ağacını ?view=raw ile çağır (API default kanonik oldu)
/v1/vehicles/:id/categories artık varsayılan kanonik kova görünümü döndüğü
için, ham ağaç şeklini render eden widget shell'i açıkça view=raw ister —
yoksa {canonical:{name},categories[]} şeklini {name,partCount,id} sanıp
kırılırdı. Kanonik-kova görünümünü widget'a getirmek ayrı iş.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 11:02:08 +03:00
239a46451d Merge pull request 'main' (#196) from main into dev
Reviewed-on: #196
2026-07-06 11:01:30 +03:00
d167c15c5e Merge pull request 'feature/public-canonical-categories' (#195) from feature/public-canonical-categories into main
Reviewed-on: #195
2026-07-06 11:01:04 +03:00
ae31a7b61a feat(jobs): kanonik backfill gecelik cron + artımlı (keep-fresh) mod
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Faz 2 — decode edilen yeni araçların kategorilerini materyalize tutar:
- Processor'a incremental mod: step-2 yalnız HENÜZ map satırı olmayan yeni
  isimleri sınıflandırır (tüm ~113k değil). Materialize (step-5) differential +
  inheritance NULL-only olduğundan, adı zaten map'li yeni kategori de id'sini
  alır — hiçbir şey atlanmaz. is_manual korunur. reset varsa yok sayılır
  (reset tam yeniden-eşleme demek).
- Gecelik cron: 05:00 İstanbul, { incremental: true }. Dev+prod'da güvenli
  (harici yan-etki yok, kendi DB'sine yazar). Lexicon iyileştirmesi sonrası
  tam yeniden-eşleme hâlâ ELLE { reset:true } global run.
- Dokümante edilen 'backfill trigger yok' boşluğunu kapatır; serving zaten
  anlık lexicon fallback yapıyordu, bu cron materyalizasyonu güncel tutar.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 10:59:19 +03:00
101ee755f1 Merge branch 'feature/public-canonical-categories' into dev (Faz 1) 2026-07-06 10:52:12 +03:00
dc65c61440 feat(api): /v1 kategori ağacı varsayılan birleşik (kanonik) + source-strip
Faz 1 — public API kategori ucunu kanonik taksonomiye bağlar:
- GET /v1/vehicles/:id/categories artık VARSAYILAN olarak birleşik (kanonik)
  görünüm döner (kaynak-bağımsız 20 kova + alt-kategoriler,
  getCanonicalTree; materyalize %96,9 L1 + anlık lexicon fallback).
- ?view=raw ile ham kaynak ağacı (kaçış kapısı — plaka-kodu yapraklı yeni
  PSA/Renault kataloglarında birleşik zayıfsa).
- GÜVENLİK: iki görünüm de public-api.sanitize ile temizlenir — 'source'
  (upstream sağlayıcı: parts-catalogs/emex/pl24) ve 'linkWid' (upstream widget
  id'si) ARTIK SIZMAZ. Önceki ham-ağaç ucu bunları sızdırıyordu (Faz 1 widget
  işinde açılmış mevcut sızıntı, burada kapatıldı).
- Swagger: view param + iki görünümün şeması dokümante.
- 6 birim test (sanitizer beyaz-liste + source-drop).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 10:52:03 +03:00
380ab60572 Merge pull request 'fix: kova doğal alt-kategori seviyesi' (#194) from dev into main 2026-07-06 10:15:59 +03:00
28720c7d7d fix(canonical): kova → doğal alt-kategori seviyesi (entry-children, subtree count)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Önceki hibrit sadece derin parça-yapraklarını + kök/dal girişlerini gösteriyordu;
kısmen drill edilmiş kovada ("Fren sistemi" subtree'de 39 parça) sistem-kökü
skip ediliyor + 0-parçalı kardeşleri düşüyordu → Fren'de 14 yerine 2. Yeni model
(buildCanonicalInput): her kovanın "entry container" köklerinin AYNI-KOVA
çocuklarını SUBTREE parça sayısıyla göster (drilled olsun olmasın). Audi Fren →
11 alt-kategori (Disk frenler/Fren Kaliyeri/El fren…), orijinal katalog gibi.
On-the-fly bucketing (materialize edilmemiş taze kategoriler için).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 10:15:52 +03:00
8b7bd56c7b Merge pull request 'fix: kova→alt doğrudan + subgroup render' (#193) from dev into main 2026-07-06 09:59:26 +03:00
7fb3608dea fix(canonical): kova→alt-kategori doğrudan (redundant "sistem" hop'unu kaldır)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
(1) Serving: "Fren sistemi" gibi sistem-container dalını çocuklarıyla değiştir
(Fren Kaliyeri/Disk frenler/Balata…) → kova doğrudan alt-parçaları içerir.
(2) Frontend: CanonicalView artık subgroups'u (2. seviye: Balata/Disk/Kaliper)
render ediyor — önce sadece flat categories gösteriyordu, alt-seviye işi hiç
görünmüyordu. Böylece Fren'e tıkla → alt-kategoriler doğrudan gelir.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 09:59:19 +03:00
8ce387c0c5 Merge pull request 'fix: dal-giriş yaprak dahil' (#192) from dev into main 2026-07-06 09:42:33 +03:00
5a933fec9d fix(canonical-tree): dal-girişi filtresini yaprakları da kapsayacak şekilde düzelt
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Debriyaj sistemi / İç Aydınlatma Sistemi drill-edilmemiş YAPRAK (0 çocuk) —
önceki "çocuğu olmalı" şartı bunları dışlıyordu → hâlâ 0. Şart: mapped + kök-
değil + 0-parça + entry-point (parent farklı kova). Yaprak/dal fark etmez.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 09:42:25 +03:00
682f189e6e Merge pull request 'fix: dal-girişleri kanonik' (#191) from dev into main 2026-07-06 09:40:40 +03:00
f5d7a27ea3 fix(canonical-tree): drill-edilmemiş dal-girişlerini de göster (Debriyaj/Aydınlatma)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Sorun: aracın "Debriyaj sistemi"/"Aydınlatma Sistemi" kategorisi var + doğru
kovaya map olmuş ama parçası drill edilmemiş → hibrit sadece parça-yaprak +
drill-edilmemiş KÖK gösterdiğinden bu DALLAR düşüyordu → "0 kategori". Fix:
bucket'a map olan, çocuğu olan ama subtree'de parçası olmayan dal-girişlerini de
input'a ekle (parent farklı kova = entry point). Böylece kova boş görünmez,
tıklanınca on-demand drill olur (prefetch beklemez).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 09:40:33 +03:00
d997abc3d3 Merge pull request 'fix: prefetch decode fast-lane' (#190) from dev into main 2026-07-06 09:19:38 +03:00
e5fd44820a fix(prefetch): decode-tetiğini fast-lane (lifo) yap — backlog'u atla
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Sorun: yeni decode edilen araç prefetch'i normal öncelikle (5dk delay, fast yok)
enqueue ediliyordu → 82k'lık deep-drill backlog'unun ARKASINA düşüp hiç
işlenmiyordu → taze araçlarda kanonik view boş kalıyordu. Fix: fast:true + lifo
→ taze araç saniyeler içinde drill olur, eski children backlog'unu atlar.
Per-source rate gate upstream'i hâlâ korur.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 09:19:30 +03:00
b5b846f3f2 Merge pull request 'feat: kanonik yapı şablonu' (#189) from dev into main 2026-07-06 08:47:06 +03:00
f25ef2d54f feat(canonical): marka/platform yapı şablonu — decode anında tam kanonik yapı
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
canonical_structure_template(source, brand, bucket, sub, vehicle_count): drill
edilmiş araçlardan her (kaynak, marka) için görülen kova/alt-yaprak kümesi.
Backfill sonunda yeniden üretilir (global run). Serving overlayTemplate ile
aracın ağacına markanın bilinen ama bu araçta henüz drill edilmemiş kova/alt-
yapraklarını BOŞ ekler → VIN decode olur olmaz FULL kanonik yapı görünür;
parçalar backfill ile dolar. migration 0029. vehicle + catalog akışı.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 08:46:59 +03:00
6eb7dc11a4 Merge pull request 'feat: hibrit canonical tree — tüm kovalar anında' (#188) from dev into main 2026-07-06 08:32:35 +03:00
7b1daffe97 feat(canonical-tree): hibrit — tüm üst kovalar decode anında görünür
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Sorun: bir araçta sadece TEK bir kategori (drill edilmiş olan) görünüyordu —
"herhangi parça varsa leaf-mode" mantığı drill-edilmemiş kökleri gizliyordu.
Fix: parça-tutan yapraklar + drill-edilmemiş kökler (subtree'sinde parça yok)
birlikte gösterilir. Kök yapı decode'da oluştuğundan tüm 20 kova ANINDA görünür;
parçalar backfill deep-drill ile dolar. vehicle + catalog akışı.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 08:32:28 +03:00
c5c569b941 Merge pull request 'fix: orijinal default + dedupe' (#187) from dev into main 2026-07-06 08:06:15 +03:00
013cd43e7e fix: Orijinal Katalog varsayılan + canonical duplike-kök dedupe
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
(1) Araç sayfası varsayılanı "Orijinal Katalog" (source) — canonical view'ın
edge-case'leri (duplike/drill-edilmemiş kökler) çözülene kadar.
(2) canonical grouping ad-bazlı dedupe: bazı decode'lar kökü iki kez üretiyor
("Motor, Motor"; Seat Ateca'da her kök 2×) → aynı adı en zengin kopyayla
(parça, sonra çocuk) birleştir. Önceki boş-kök filtresi GERİ ALINDI (drill-
edilmemiş kökleri de siliyordu = "2 kategori" regresyonu).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 08:06:08 +03:00
c6e5d06172 Merge pull request 'fix: boş kök gösterme' (#186) from dev into main 2026-07-06 07:54:47 +03:00
34d97b47ac fix(canonical-tree): boş kökleri (çocuksuz+parçasız) root-fallback'ta gösterme
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
emex bazen aynı kökü iki kez decode ediyor (biri boş) → "Motor → Motor, Motor"
gibi redundant başlık. Root-fallback artık sadece çocuğu VEYA parçası olan
kökleri gösteriyor. vehicle + catalog akışlarının ikisinde de.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 07:54:39 +03:00
dc5cf4039c Merge pull request 'canonical: 3 yanlış-kova override daha' (#185) from dev into main 2026-07-06 07:39:40 +03:00
6f8e9ff618 feat(canonical-lexicon): 3 yanlış-kova override daha (IC Motor prefix + türbo şarj)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
"motor soğutma"→Soğutma (2516, engine cooling — "motor" subsystem qualifier),
"türbo şarj"→Motor (684, "şarj"=charge Elektrik'e çekmiş), "tahrik kayışı"→
Triger (767, drive belt). pcat "IC Motor <sistem>" prefix örüntüsü. 32 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 07:39:32 +03:00
2581908cc4 Merge pull request 'canonical: phrase override yanlış-kova' (#184) from dev into main 2026-07-06 06:48:47 +03:00
6e556e198a feat(canonical-lexicon): phrase-override katmanı — adjective+head yanlış-kovalar
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Skordan önce yüksek-güven phrase kuralları: "<sistem> yağı$" → Yağlar (end-
anchor, "motor yağı filtresi/pompası" hariç), "(cam|silecek|kalorifer) motoru"
→ Elektrik, "zamanlama (dişli|zincir|kayış)" → Triger. ~1400 motor/şanzıman
yanlış-kovası + ~230 silecek-motoru + ~790 zamanlama düzelir. 29/29 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 06:48:40 +03:00
cec68a8cd9 Merge pull request 'canonical: SP kuyruk küçültme' (#183) from dev into main 2026-07-06 06:28:13 +03:00
75dbadcb47 feat(canonical-sublexicon): SP kuyruğunu küçültmek için yüksek-frekans alt-terimler
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Kuyruğun tepesindeki net-eşlenebilir adları otomatikleştir: gösterge/anten→
Modül, korna→Korna(yeni), silecek→Silecek(yeni), güneşlik→Tavan, havalandırma/
ısıtma/eşanjör→Kalorifer, soğutucu→Radyatör, absorber→Amortisör, dingil→Aks,
pim→Burç, kontrol kol→Salıncak, yağ tava/keçe/basınç→Yağlama, motor takımı→
Komple, iç kontrol→Vites. +2 yaprak (silecek/korna), migration 0028. 6/6 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 06:28:06 +03:00
046b8a8f4c Merge pull request 'canonical: emex düşük-marka deep-dive' (#182) from dev into main 2026-07-05 17:15:58 +03:00
1aaf2d8d8b feat(canonical): emex düşük-marka deep-dive terimleri
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
emex Dacia/Toyota/BMW/Kia eşleşmeyenlerinden tek-token distinctive terimler
(üst-lexicon token-bazlı): buharlastirici/kurutucu→sogutma, aktuator/selenoid/
anten/ses→elektrik, lambda/rolanti→yakit, kanat/kiris→panjur, burkma→susp. +
ilgili sub keyword'ler. 2-kelime adlar (su pompası) token-matcher'da eşleşmez,
atlandı → SP.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 17:15:51 +03:00
33f58e14c6 Merge pull request 'canonical: zayıf kova sub tuning' (#181) from dev into main 2026-07-05 16:48:49 +03:00
db646d10f4 feat(canonical-sublexicon): zayıf kova tuning (yaglar/panjur/direksiyon/debriyaj)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
+7 yaprak (migration 0027): yag-sistemi (yağlama/pompa/çubuk), panjur kaput/
tavan/şasi/cam/kanat, direksiyon simit/dişli/hidrolik/elektronik + debriyaj
serbest-bırakma. En düşük kovaları (Yağlar %10, Panjur %17, Direksiyon %38)
yükseltir. 150 yaprak toplam.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 16:48:42 +03:00
a9a40e4587 Merge pull request 'canonical: 2-level serving' (#180) from dev into main 2026-07-05 16:40:46 +03:00
2d5e2d04a9 feat(canonical): 2-level serving — taksonomi endpoint + araç sub-gruplama
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
GET /categories/canonical/taxonomy: kova→alt-yaprak + populasyon sayısı (B2C API
kontratı). getCanonicalTree (vehicle + catalog) artık subLeaves geçip her kovayı
alt-gruplara böler (materialize + on-the-fly sub fallback). jant-tire slug fix
(jant-lastik üst-kova ile çakışıyordu). Kategori sub-kapsama: Filtre %88, Fren/
Aydınlatma %74, Motor/Süsp/Elektrik %60-66. Eşleşmeyen → Genel/SP manuel.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 16:40:39 +03:00
cf11379b49 Merge pull request 'canonical: 2. seviye alt-kategori' (#179) from dev into main 2026-07-05 16:32:32 +03:00
9dd3b2c858 feat(canonical): 2. seviye alt-kategori mapping (otoparcasan-türevi)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Kendi kanonik yapımıza tam geçiş — B2C API için alt-kategori. migration 0026:
canonical_categories'e 143 alt-yaprak (parent_id=üst kova) + categories/
category_canonical_map'e canonical_subcategory_id. canonical-sublexicon.ts:
bucket başına satılabilir parça yaprakları + Türkçe varyant (kaliyer/hidroliği)
+ Bağlantı&Donanım catch-all. Backfill artık isim→kova→alt-yaprak deterministik
materialize ediyor (inheritance dahil). Eşleşmeyen = null → SP manuel. Testler +.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 16:32:25 +03:00
5ac92556e3 Merge pull request 'canonical: Opel inheritance yanlış-pozitif fix' (#178) from dev into main 2026-07-05 15:18:26 +03:00
458603fb98 fix(canonical-backfill): model/yıl seçici düğümlere parent-inheritance verme
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Opel model-öncelikli: "X 2004-2014" model düğümü parent'ından (MOTOR VE DEBRIYAJ)
Debriyaj miras alıyor, sonra jenerik çocukları (BOYALAR/MÜTEFERRİK/SIZDIRMAZ) de
ondan Debriyaj miras alıyordu → yanlış eşleme zinciri. Inheritance artık yıl-
aralıklı + ^[a-z]{1,3}\d{2} model düğümlerine miras VERMEZ (null kalır, cascade
kırılır). Kod/plaka yaprakları (X1318_/20-201A) bu desenlere uymaz → Renault/
Hyundai etkilenmez. Sadece Opel 58 + Nissan 1 düğüm.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 15:18:18 +03:00
f63ccf0aef Merge pull request 'canonical: MAN kalan platform prefix' (#177) from dev into main 2026-07-05 14:46:16 +03:00
5900b8d399 feat(canonical-lexicon): MAN kalan otobüs platform prefixleri (SG/SL/SR/SUE/UEL/RH/NM…) + model adları
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 14:46:09 +03:00
57d9ff2e30 Merge pull request 'canonical: MAN EL/GS/LE platform kod' (#176) from dev into main 2026-07-05 14:31:02 +03:00
a10654f210 feat(canonical-lexicon): MAN EL/GS/LE/MAZ/MFTPL platform kodlarını da yapısal yap
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 14:30:54 +03:00
d04d27d221 Merge pull request 'canonical: MAN deep-tune' (#175) from dev into main 2026-07-05 14:16:46 +03:00
86a08dbe12 feat(canonical-lexicon): MAN deep-tune — otobüs/koçbuilder model adları yapısal
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
MAN neredeyse tamamen model-öncelikli (LION'S CITY, SKYLINER/CITYLINER/TOURLINER,
NL/ND/NG/MAN platform kodları, AUFBAU koçbuilder). classifyNode MAN_MODEL_CODE:
^(nd|ng|nl|ngt|man) \d + distinctive model kelimeleri → structural. Truck-kod
^\d{4,5} KULLANILMADI (2800 emex mapped bozardı). Ayrıca LPG-model yanlış-pozitif
(NL 205 M11 LPG→Yakıt) düzelir. elektr→elektrik, aggregate→motor. 27/27.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 14:16:38 +03:00
c4608a8421 Merge pull request 'canonical: Kollar/Ön Takım + conta strong' (#174) from dev into main 2026-07-05 13:49:06 +03:00
c88b6b45c5 feat(canonical-lexicon): Kollar/Ön Takım→süspansiyon; conta→motor strong
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
emex "Kollar"(salıncak kolları)/"Ön Takım"(aks) → süspansiyon (kollar/takim
weak). "conta" motor strong'a alındı ki "conta takımı"→motor kalsın (takim ile
tie'ı kazansın). 26/26 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 13:49:01 +03:00
0517a57021 Merge pull request 'canonical: emex servis sayfa junk' (#173) from dev into main 2026-07-05 13:45:33 +03:00
e42345f35b feat(canonical-lexicon): emex servis sayfalarını (Servis Aralıkları/Ek Onarımlar) junk yap
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 13:45:28 +03:00
dce3a4f332 Merge pull request 'canonical Faz 3: emex terimleri' (#172) from dev into main 2026-07-05 13:31:56 +03:00
ee7925c960 feat(canonical-lexicon): Faz 3 emex yüksek-frekans terimleri
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
emex'in en sık eşleşmeyen adları (100-250x, tüm araçlarda paylaşılan):
stabilizer/absorber→suspansiyon; esanjor/kondansator/ventilator/kompresor/
sogutucu/uflec→sogutma; hali/halilar→ic-doseme; kilit/yagmurluk→dis-kaporta;
stepne→jant-lastik. Jenerik (BOYALAR/İZOLATÖRLER/Montaj) bırakıldı. 25/25.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 13:22:06 +03:00
527f69dc1b Merge pull request 'canonical: son cila edge-case' (#171) from dev into main 2026-07-05 12:37:00 +03:00
50a92c78cc feat(canonical-lexicon): son cila — koltuğu/takoz/stabilizatör edge-case'leri
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
koltugu (koltuk k→ğ eki)→ic-doseme, stabilizator→suspansiyon strong, takoz/
takozu→suspansiyon weak. Küçük markaların (Smart/Mini) kalan gerçek boşluklarını
kapatır. Kalan eşleşmeyenler artık junk/generic/veri-kalitesi (pratik maksimum).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 12:36:57 +03:00
b6a6893f6f Merge pull request 'canonical: küçük markalar maksimum kapsama' (#170) from dev into main 2026-07-05 12:29:59 +03:00
db4b85da8e feat(canonical-lexicon): küçük/düşük markalar için maksimum kapsama terimleri
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Tüm sub-90 markaların global eşleşmeyen adlarından net olanlar (çoğu İngilizce,
markalar arası): diagnostic/entertainment/bilgisayar/elektrigi→elektrik,
heater/ventilation→sogutma, braking→fren, sunroof/exterior/locking→dis-kaporta,
restraint/occupant/konfor→ic-doseme, mekanik→motor, yapistirici→yaglar. 24/24.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 12:29:57 +03:00
e428a82bd5 Merge pull request 'canonical: Ford + BMW fix' (#169) from dev into main 2026-07-05 12:13:30 +03:00
44987e7e7e feat(canonical-lexicon): Ford + BMW fix'leri
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
BMW (~%81): elektronik terimleri → elektrik (audio/navigasyon/elektronik/
iletisim/multimedya/cruise), "ışık"→aydınlatma; servis/doküman sayfaları →
junk (teknik doküman/servis ve onarım/value parts). Ford (~%74): trans
varyantları speed/spd→sanziman, motor varyantları diesel/dohc→motor, gövde→
panjur. 23/23 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 12:07:09 +03:00
675fab0c79 Merge pull request 'canonical: MAN kamyon model kodları' (#168) from dev into main 2026-07-05 12:00:05 +03:00
1fcbfdf5d1 feat(canonical-lexicon): MAN kamyon model kodları yapısal + parça terimleri
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
MAN %32 = Opel gibi metrik yanılsaması (kamyon model tanımlayıcıları
denominatörde). classifyNode: ^\d{1,2}[./]\d ("12.155","10/11.150") + ^\d{2}t
("18T HOCL-SD") → structural (^\d{4,} KULLANILMADI — 362 mapped satırı bozardı).
Parça terimleri: salter/verici→elektrik, tahrik→sanziman (ticari araç + genel).
21/21 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 12:00:03 +03:00
afd92e90b6 Merge pull request 'canonical: Mercedes uzun-kuyruk parça adları' (#167) from dev into main 2026-07-05 11:31:50 +03:00
347d9f2ddb feat(canonical-lexicon): Mercedes uzun-kuyruk parça adları
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Mercedes fragmented long-tail'inin eşlenebilir kısmı (çoğu İngilizce, diğer
markalara da yarar): propeller→sanziman, radiator→sogutma, roof/wall/underpan→
panjur, door/window/kapak→dis-kaporta, timing→triger, carburetor→yakit,
pedal→fren, ortu→ic-doseme. Doküman: preliminary remarks/genel bakış tablosu→
junk. Genuinely jenerik (AYAR/GENEL PARÇALAR/DONANIM/CONTROL) Diğer'de kalır. 20/20.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 11:31:48 +03:00
51eec60c6c Merge pull request 'fix: MB katalog-kodu regex çıplak-koda sabit' (#166) from dev into main 2026-07-05 11:08:50 +03:00
f1f36d8cf1 fix(canonical-lexicon): MB katalog-kodu regex'ini çıplak-koda sabitle
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
^[cd]\d{3,} fazla genişti — "D765300(22H), LS 7 F, MB SERVO DİREKSİYON" gibi
parça bilgisi taşıyan düğümleri yapısal yapıp Direksiyon eşleşmesini bozuyordu
(Mercedes ham kapsama %79→%49). $ ile sabitle → sadece çıplak kod (C442,
C105011). Açıklamalı düğümler keyword'le eşleşmeye devam eder. 19/19 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 11:08:47 +03:00
bf259f3d79 Merge pull request 'canonical: Mercedes C/D katalog kodları yapısal' (#165) from dev into main 2026-07-05 10:52:44 +03:00
25cf0fa670 feat(canonical-lexicon): Mercedes C/D katalog kodları yapısal + doküman sayfaları çöp
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Mercedes'in düşük kapsaması büyük ölçüde metrik yanılsaması: 175 dağınık kök,
C/D katalog-kodu düğümleri (C371211, C442, D308802(811),308,COWL) yapısal ama
classifyNode kaçırıyordu → denominatörde. Ekle: ^[cd]\d{3,} → structural;
"SA LİSTESİ"/"…resim açıklama sayfası" → junk. 19/19 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 10:52:42 +03:00
c9f96f8666 Merge pull request 'canonical: Opel yazım varyantları + serving yapısal-atla' (#164) from dev into main 2026-07-05 10:30:31 +03:00
82793a8257 feat(canonical): Opel model-öncelikli — yazım varyantları + serving yapısal-atla
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Opel gerçek parça-kat kapsaması zaten %85; %38 model-kodu köklerin (yapısal)
denominatörde olmasından. Lexicon: sanzuman/disli→sanziman, suspensiyon/dingil→
suspansiyon (Opel yazımları). Serving: groupCategoriesByCanonical yapısal/çöp
düğümleri "Diğer"e sokmadan atlar (Opel model kökleri Birleşik'te görünmez).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 10:25:46 +03:00
51032342b8 Merge pull request 'canonical: parent-inheritance (kod-yaprak → parent kovası)' (#163) from dev into main 2026-07-05 10:16:21 +03:00
aba1bfc126 feat(canonical-backfill): parent-inheritance (kod-yaprak → parent kovası)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Kendi adı eşleşmeyen kategori (Renault P5 X1318_ plaka kodları, figür
illustration'ları) parent'ının kanonik kovasını miras alır ("Alternatör"→
Elektrik). Materyalizasyondan sonra birkaç seviye kaskad. Re-scrape gerektirmez.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 10:11:22 +03:00
3fb7fa1aa3 Merge pull request 'fix(pl24): Hyundai/Kia illustration açıklaması' (#162) from dev into main 2026-07-05 10:05:10 +03:00
c70b9b61b9 fix(pl24): Hyundai/Kia illustration açıklamasını kod-yaprağa ekle
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Upstream doğrulandı: illustration satırında td[0]=figür kodu ("20-201A .1"),
td[1]=açıklama ("SUB ENGINE ASSY") — parser ilk td'yi (kod) alıp açıklamayı
düşürüyordu. Artık ilk td figür-koduysa (\d\d-\d\d\d[A-Z]?) açıklamalı sonraki
td eklenir → "20-201A .1 SUB ENGINE ASSY", canonical eşleşir (engine→motor).
Diğer markalar etkilenmez (yalnız kod-only ilk td'de tetiklenir). 10/10 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 10:02:01 +03:00
6bbc32d9c6 debug(pl24): TEMP Hyundai illustration rows + td dump 2026-07-05 09:55:30 +03:00
05951f9853 debug(pl24): TEMP Hyundai/Kia raw HTML row log (katalogJob Faz 2 upstream doğrulama) 2026-07-05 09:51:46 +03:00
50c7d91707 Merge pull request 'canonical Faz 2: İngilizce kategori adları' (#161) from dev into main 2026-07-05 09:30:36 +03:00
4b6642a78a feat(canonical-lexicon): Faz 2 — İngilizce kategori adları
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
pl24 Asyalı markaları (Hyundai/Kia/Nissan/Toyota) ve çevrilmemiş emex/pcat
yaprakları İngilizce kategori adı taşıyor (ENGINE/BODY/BRAKE/TRANSMISSION…) →
%0 kapsama. Sistem kelimeleri strong (engine→motor, brake→fren, transmission→
sanziman, electrical→elektrik…), konum kelimeleri weak (body/chassis/floor/axle)
ki "BODY ELECTRICAL"→elektrik olsun. 17/17 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 09:30:34 +03:00
ef8240a012 Merge pull request 'canonical: Jant/Lastik + Aksesuar kovaları + pcat kararları' (#160) from dev into main 2026-07-05 09:24:49 +03:00
b547140eb1 feat(canonical): Jant ve Lastik + Aksesuar kovaları + pcat kararları
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
migration 0025: 2 yeni kanonik kova (jant-lastik #19, aksesuar #20).
lexicon: jant/lastik/tekerlek/göbek→jant-lastik, aksesuar→aksesuar (catch-all,
en düşük öncelik). Kullanıcı kararları: Hava Sistemi→Soğutma (hava weak),
Bakım parçaları→Filtreler (bakim weak). "hava yastığı"→direksiyon için yastigi
strong'a alındı. 16/16 test. Yorumlar 18→20 kova güncellendi.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 09:24:29 +03:00
5abdc6f628 feat(canonical-lexicon): Faz 1 pcat kök eşlemeleri
pcat paylaşımlı taksonomisinin eşleşmeyen köklerinden net olanları ekledim:
jenerator→elektrik, yaglama→yaglar, karo/cerceve→panjur-panel, aktarim→
sanziman, aks→suspansiyon(weak). sasi weak (süspansiyon+şasi net kalsın).
15/15 test. Kova-kararı gerektirenler (jant-lastik, aksesuar) katalogJob.md'de.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 09:18:58 +03:00
efa395b7b5 Merge pull request 'web: birleşik görünüm default + Orijinal Katalog seçeneği' (#159) from dev into main 2026-07-05 09:09:39 +03:00
b96ca78942 feat(web): birleşik görünüm varsayılan + "Orijinal Katalog" seçeneği
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Araç sayfasında browse varsayılanı "canonical" (Birleşik) oldu; toggle
"Birleşik | Orijinal Katalog" — Orijinal Katalog seçilince ham OEM ağacı
gösterilir. Kullanıcı isteği: birleşik katalog default, orijinal opsiyonel.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 09:09:37 +03:00
5a74a0e552 Merge pull request 'canonical: kova-adı lexicon fix (süspansiyon/elektrik/aydınlatma)' (#158) from dev into main 2026-07-05 09:05:50 +03:00
643e1d923c fix(canonical-lexicon): kova adının kendisini anahtar kelime yap
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Kök tam da kanonik başlıkla anılınca ("Süspansiyon, şasi", "Elektrik",
"Aydınlatma") lexicon'da o kelime yoktu → "Diğer"e düşüyordu. suspansiyon/
elektrik/aydinlatma kendi kovalarına strong olarak eklendi. Serving on-the-fly
fallback aynı lexicon'u kullandığı için taze araçlarda backfill'siz düzelir.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 09:00:33 +03:00
dedcf71419 Merge pull request 'dev' (#157) from dev into main
Reviewed-on: #157
2026-07-05 08:43:44 +03:00
07d4bf7e69 feat(canonical-lexicon): 3-harfli kök stem çekimlerini ekle
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
"yag/mil/far/aku" gibi 3-harfli kökler ≥4 prefix kuralı yüzünden çekimli
hallerine (yagi/yaglar, mili, farlar/fari, akusu) eşleşmiyordu — "HPS yağı",
"Kardan Mili", "araba aküsü" "Diğer"e düşüyordu. Çekimleri açıkça ekledim +
sanziman'a "kardan". Global düşürmek riskli (sis→sistemi). 13/13 test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 18:42:06 +03:00
cc1d22a7d4 feat(catalog): kök-seviye canonical fallback + araç sayfası "Birleşik" toggle
Backend: groupCategoriesByCanonical'a requireParts opsiyonu; her iki serving
metodu artık parça-içeren yaprak yoksa (taze/drill-edilmemiş araç) kök
kategorileri kovalayarak boş dönmüyor.

Frontend: araç sayfasına "Katalog | Birleşik" toggle'ı; Birleşik seçilince
CanonicalView aracın kataloğunu 18 kanonik başlık altında gösterir, her kova
CategoryGrid ile normal drill'e bağlanır. canonical_view_opened event'i.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 18:28:07 +03:00
af2b35e6e2 feat(catalog): pl24 catalog-vehicle canonical gezinme variantı
GET /api/catalog/vehicles/:id/canonical → catalog_vehicle_id akışının canonical
karşılığı; pl24 markaları (VW/Opel/Renault) emex/pcat ile aynı 18 başlıktan
gezilir. Parça sayısı category_id ile join'lenir (pl24 parçaları çoğu zaman
vehicle_id taşır, catalog_vehicle_id değil). Gruplama mantığı iki servisin
paylaştığı canonical-grouping.ts helper'ına çıkarıldı (categories.service da
bunu kullanacak şekilde refactor edildi).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 18:08:59 +03:00
3c26f00a19 feat(categories): canonical taksonomiyle birleşik gezinme endpoint'i
GET /categories/canonical → 18 kanonik üst kova (browse landing).
GET /categories/canonical/:vehicleId → aracın parça-içeren leaf kategorilerini
18 kova altında gruplar; VW ve Opel gibi kökten farklı OEM ağaçları tek/aynı
başlık setinden gezilir. Kovalama materyalize canonical_category_id'yi
kullanır, son backfill'den sonra decode edilmiş kategoriler için ortak
canonical-lexicon ile on-the-fly fallback yapar; kovasız olanlar "Diğer"e düşer.
foldName() NORMALIZE_SQL'in JS karşılığı olarak eklendi.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 17:58:17 +03:00
cd66947303 feat(canonical-backfill): reset/prune modu (global re-run için)
reset:true → non-manual keyword katmanını sıfırdan kurar: kapsamdaki
materyalize id'leri NULL'lar + stale non-manual map satırlarını siler,
sonra yeniden eşler. Manuel satırlar korunur. Sadece GLOBAL run'da (marka
scope'suz) geçerli — map isim-düzeyi/marka-bağımsız olduğu için marka-scope'lu
silme başka markaların paylaşılan eşlemelerini bozardı. Böylece lexicon/
sınıflandırıcı değişince eski eşlemeler dangling kalmaz.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 17:36:35 +03:00
c3d0dbaa0b fix(canonical-backfill): ">>" marker'ını yapısal sayma
Gerçek VW parça adları geçerlilik-tarihi marker'ı taşıyor ("pencere camlari
d >> - 14.07.2019"); bare ">>" onları yanlışlıkla yapısal işaretliyordu (VW
kapsamı düşüyordu). Gerçek VIN düğümlerini zaten VIN_LIKE yakalıyor.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 17:29:29 +03:00
8b88ccd238 feat(canonical-backfill): ağırlıklı skorlama + genişletilmiş lexicon
- Belirsizlik skorlaması: strong head-noun ×3 / weak ×1; kazanan skor
  marjıyla belirlenir, ≤1 marj = ambiguous (eski eşit-sayı tie-break'i
  fazla ambiguous işaretliyordu). confidence marja göre (0.5–0.95).
- Lexicon genişletildi: egzoz/egz/konvertor/katalitik/susturucu (yakıt),
  karoser (panjur-panel), bcm/ecu (elektrik) + strong/weak ayrımı.
- classifyNode sıkılaştırıldı: VIN markerları (wvwzzz, >>), bare motor/
  sipariş kodları (z16dt[lfu]) artık yapısal.
- map.confidence artık per-eşleşme marjdan yazılıyor.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 17:24:42 +03:00
754f1af61c fix(canonical-backfill): brand/source filtresini inArray ile kur
`= ANY($1)` postgres-js'te parametreyi Postgres array olarak bağlamıyor
("op ANY/ALL (array) requires array on right side") → job'lar fail'liyordu.
inArray() IN-listesine açar, sürücü-güvenli.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 17:12:49 +03:00
0ae7c3ceb6 fix(worker): canonical-backfill Worker'ını worker.ts'e bağla
Önceki commit'te (7b5e678) src/worker.ts git add kapsamı dışında kaldı
(apps/api/src/jobs/ eklendi ama worker.ts src/ altında). Queue/processor
deploy oldu ama tüketici Worker yoktu → enqueue edilen job'lar 'waiting'de
takıldı. Bu commit Worker instance'ını ekler.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 17:02:37 +03:00
7b5e678c55 feat(catalog): kanonik taksonomi şeması + backfill job'ı
Kaynak/marka-bağımsız tek parça taksonomisi. Ham OEM ağaçları (categories.parent_id
+ pl24/emex/pcat aynaları) dokunulmadan kalır; kanonik omurga onlara dik.

- migration 0024: canonical_categories (18 üst kova seed) + category_canonical_map
  (isim-düzeyi eşleme) + categories.canonical_category_id kolonu
- canonical-lexicon.ts: iki-aşamalı eşleme (classifyNode: parça/yapısal/çöp;
  mapToCanonical: 18-kova keyword). VW+Opel pilotunda kanıtlandı — Opel ağacının
  %87'si model-seçici düğüm, bunlar kovaya girmez.
- canonical-backfill BullMQ job'ı (on-demand): distinct foldlanmış adları
  sınıflandırır, category_canonical_map'e upsert eder (is_manual korumalı),
  categories.canonical_category_id'yi join ile materyalize eder. brands/sources/
  dryRun parametreli.
- 8 unit test (classifyNode/mapToCanonical)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 16:54:24 +03:00
d2f42af935 Merge pull request 'dev' (#156) from dev into main
Reviewed-on: #156
2026-07-04 06:42:02 +03:00
ec95ab025d Merge branch 'feature/api-key-admin' into dev
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
2026-07-04 06:26:44 +03:00
8f6894eecb feat(api): api-key admin uçları — PATCH güncelleme, Redis kota usage, global liste
Süper Panel'den tam anahtar yönetimi için:
- GET /internal/admin/api-keys artık userId/email'siz TÜM anahtarları
  kullanıcı e-postasıyla birlikte döner
- GET /internal/admin/api-keys/usage — günlük decode kotasının anlık
  Redis durumu (limit/used/remaining), ids ile filtrelenebilir
- PATCH /internal/admin/api-keys/:id — name, dailyDecodeLimit(null=varsayılan),
  domains (yalnız publishable), rateLimitMax(null=plugin varsayılanı),
  expiresInDays(null=süresiz)
- PublicApiQuotaService internal-admin'e provider olarak eklendi

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-04 06:20:05 +03:00
123b701ed7 Merge branch 'feature/pcat-jwt-resilience' into dev 2026-07-03 18:33:48 +03:00
cd30278ad9 feat(pcat): JWT capture proxy-outage resilience + pool cap
JWT sitelerinin (parts-catalogs v3 widget'ını gömen partner mağazalar) sağlık
incelemesinden çıkan iki iyileştirme:

1) Direct-capture fallback (PCAT_CAPTURE_ALLOW_DIRECT, default açık): proxied
   capture tünel/connect hatasıyla ölürse (Floxy bakiye 402 / tünel down,
   DataImpulse portları ölü — 2026-06-11 ve 2026-07-03'te yaşandı) aynı siteyi
   sunucu IP'sinden DIRECT yeniden dener. Güvenli: TWS- token IP-bound DEĞİL +
   capture düşük frekans (≤MAX_POOL_SIZE refresh/~8.5dk). Doğrulandı: Floxy down
   iken direct 1.4s'de gerçek TWS- token yakaladı → warm pool artık tam proxy
   kesintisinde bile açlığa girmez.

2) Sert havuz üst-sınırı (enforcePoolCap): refresh yolundaki slot-muhasebe
   yarışı havuzu MAX_POOL_SIZE=5'in üstüne şişiriyordu (prod'da 32-39 gözlendi
   → gereksiz capture yükü + partner-site ban riski). Her push'tan sonra en eski
   slot(lar) çıkarılıp cap uygulanıyor, timer'ları temizleniyor.

6 JWT sitesinin hepsi canlı (direct 1.3-2s) — liste budanmadı. Birim testler +
canlı Floxy-down doğal deneyiyle doğrulandı.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 18:33:43 +03:00
f586b2f9c2 fix(api): widget şema görseli schemaPics[0].url okusun
schemaPics öğeleri {id,url,label} nesneleri; src'ye nesnenin kendisi
verilince kırık görsel ikonu çıkıyordu. + yüklenemeyen görsel gizlenir.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 18:09:38 +03:00
da2eaecd47 fix(api): public API parça cevabını beyaz-listeye indir + widget pozisyon alanı
- Kategori-detay parçalarında iç alanlar (source=upstream sağlayıcı adı,
  presel, ham price/currency, catalogVehicleId...) public API'den sızıyordu —
  decode kontratı disipliniyle beyaz-liste: id/name/description/oemCode/
  position/quantity/remark/unavailable/categoryId
- Widget parça tablosu positionNumber yerine gerçek alanı (position ??
  hotspotIndex) okuyor

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 18:05:21 +03:00
d46a463076 fix(api): widget Sorgula butonu MouseEvent'i candidate sanıyordu
onclick handler'ına doğrudan decode verilince click event'i candidate
parametresine düşüyor, sunucu da onu aday anahtarı sanıp 'araç seçimi
zaman aşımı' dönüyordu. Sarmalayıcı + typeof string guard.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 18:00:56 +03:00
e952687710 fix(api): widget loader.js global response-interceptor'a takılmasın
String dönüş {success,data} JSON'una sarılıyordu; Content-Type JS ama gövde
JSON olunca tarayıcı ERR_BLOCKED_BY_ORB ile script'i blokladı. @Res ile ham
send'e geçildi (shell zaten öyleydi).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 17:55:52 +03:00
ae61ac055e feat(api): gömülebilir katalog widget'ı (Faz 2 POC)
- GET /widget/loader.js: müşterinin yapıştırdığı tek script — iframe kurar,
  postMessage köprüsü (yükseklik + sase:partClick/sase:decoded olayları),
  parça tıklamasını data-part-click-url şablonuna ({code},{brand}) yönlendirir,
  window.saseWidget.onPartClick/onDecode callback'leri; CORP cross-origin
  (helmet same-origin varsayılanı müşteri sitesinde script'i bloklardı)
- GET /widget?key=sase_pk_...: iframe shell — vanilla JS katalog uygulaması
  (VIN formu, çok-aday seçimi, kategori grid + breadcrumb, parça tablosu,
  şema görseli, açık/koyu tema). Domain kilidi = anahtarın metadata.domains
  listesinden üretilen frame-ancestors CSP'si (tarayıcı zorlar); publishable
  olmayan anahtar 403; X-Frame-Options bu cevapta kaldırılır
- internal-admin: publishable:true + domains[] ile sase_pk_ anahtar üretimi
- api.sase.tr kısa yolu: /widget* → /api/widget*

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 17:50:29 +03:00
acadd84e4a fix(api): createApiKey expiresIn saniye cinsinden (ms değil)
30 günlük pilot anahtarı üretimi 'expiresIn is larger than the predefined
maximum value' hatasıyla düşüyordu — plugin expiresIn'i saniye bekliyor,
gün→ms çevriliyordu.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 17:27:59 +03:00
773e9c35fa feat(api): public API v1 katalog endpoint'leri
- GET /v1/vehicles/:vehicleId/categories — kategori ağacı (rollup parça sayılı)
- GET /v1/vehicles/:vehicleId/categories/:categoryId — kategori detayı + parça
  listesi + şema görselleri (lazy drill/heal aynen çalışır)
- GET /v1/vehicles/:vehicleId/search?q= — araç kataloğunda serbest arama
Hepsi gezinme sayılır, decode kotasından düşmez; Swagger anotasyonlu.
Dashboard'daki kuralla aynı: decode edilmiş araçlar paylaşımlı, varlık
kontrolü 404 için.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 17:25:38 +03:00
06f247beb7 feat(api): public API docs (Swagger) + api.sase.tr kısa yolları
- Swagger/OpenAPI yalnız PublicApiModule'ü kapsar (iç dashboard yüzeyi spec'e
  girmez); UI /api/v1/docs, JSON /api/v1/docs-json; bearer auth + kota
  açıklamaları TR
- api.sase.tr host'unda /v1/* → /api/v1/* ve /docs* → /api/v1/docs* rewrite —
  müşteriye dokümante edilen taban https://api.sase.tr/v1
- Controller'lara ApiOperation/ApiBody/ApiOkResponse anotasyonları (decode
  tek-eşleşme + çok-aday şemaları, OEM xref)
- Coolify: api.sase.tr dev app'in api servisine ikinci domain olarak eklendi
  (docker_compose_domains)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 16:40:17 +03:00
c9c33d58e1 Merge branch 'feature/public-api' into dev 2026-07-03 16:15:30 +03:00
7a4fe51d09 feat(api): public API POC — API-key auth + /api/v1 decode & OEM xref
Kapalı-beta public API temeli (rapor: api-widget-analizi-2026-07-03):
- better-auth apiKey plugin (defaultPrefix sase_, enableMetadata, anahtar
  başına 120 istek/dk); enableSessionForAPIKeys KAPALI — anahtar dashboard
  oturumu yerine geçmez, cookie AuthGuard davranışı değişmez
- apikeys tablosu (migration 0023, plugin şemasının birebir karşılığı)
- ApiKeyGuard: Authorization Bearer / x-api-key → verifyApiKey → kullanıcı
  yüklenir (status kontrolü), request.user + request.apiKey doldurulur
- /api/v1/vin/decode: mevcut slim kontrat + aday akışı; günlük başarılı-decode
  kotası (Redis, TR günü, aynı VIN idempotent; limit: key metadata
  dailyDecodeLimit → PUBLIC_API_DAILY_DECODE_LIMIT → 100) ve
  X-Decode-Quota-* header'ları
- /api/v1/oem/:code: P çapraz-referans (kotasız)
- internal/admin/api-keys: x-internal-token ile anahtar üret/listele/kapat
  (düz metin yalnız create cevabında)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 16:15:17 +03:00
4614d79d77 Merge pull request 'dev' (#155) from dev into main
Reviewed-on: #155
2026-07-01 18:15:57 +03:00
371aed3645 chore(build): wire VITE_GOOGLE_ADS_* build args for conversion tracking
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 18:11:45 +03:00
98c21fa30e feat(web): Google Ads conversion tracking (gtag + Enhanced Conversions)
Add lib/google-ads.ts (lazy gtag init, gclid capture, Enhanced Conversions,
trackGoogleAdsConversion). Fire sign-up conversion on email registration and
purchase conversion on Stripe success. Gated on VITE_GOOGLE_ADS_* env; no-op
when unset. Mirrors the Meta Pixel/CAPI pattern.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 18:11:44 +03:00
5b640988c4 Merge pull request 'dev' (#154) from dev into main
Reviewed-on: #154
2026-07-01 16:28:55 +03:00
8735eece13 Merge branch 'feat/trial-7-day' into dev
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Shorten the free trial from 30 to 7 days: backend source of truth
(subscriptions.service TRIAL_DAYS), in-app banner rescale to the 7-day
window, tr/en copy (17 keys each), hardcoded CTAs, and the trial-progress
test. Existing trials keep their stored endDate — new sign-ups only.

Clean 3-way merge with the discount-code billing feature: trial and promo
changes touched disjoint regions of subscription/index.tsx and the message
catalogs. Post-merge verified — web+api typecheck clean, web tests 92/93
(the 1 red is the pre-existing landing index.test.tsx, unrelated to trial).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 16:26:02 +03:00
373af248b4 Merge pull request 'fix(vinpin): launch VinPower from the Horizon app-launcher on the permanent seat' (#153) from dev into main
Reviewed-on: #153
2026-07-01 10:02:38 +03:00
5763e7fbff fix(vinpin): launch VinPower from the Horizon app-launcher on the permanent seat
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
The permanent (paid) Vinpin seat does NOT auto-launch VinPower after login —
it lands on the Horizon HTML-Access app launcher ("Available: VINPIN"), so the
old brand-grid→Fiat flow failed with "Fiat ePER did not open". Add an
OCR-gated ensureBrandGrid step before the ePER-open loop that:

  - detects the launcher (Available / Running / No Running Items) and clicks the
    VINPIN app tile to launch VinPower;
  - satisfies VinPower's own secondary login dialog (username pre-filled,
    password blank) via the webclient-frame DOM password field, with a
    coordinate fallback;
  - dismisses a stray Horizon "Disconnected" dialog / waits through the blank
    connecting screen;
  - no-ops when the brand grid is already up (trial-style auto-launch), keeping
    it backward-compatible.

New coords/waits tuned for the permanent seat live in vinpin.constants.ts.
Never throws (failures fall through to the caller's retry → null). Verified
live on the permanent account: NM426300006A69963 → DOBLO, NM435600006H43436 →
TIPO-EGEA.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 09:52:33 +03:00
9df85c335f Merge pull request 'dev' (#152) from dev into main
Reviewed-on: #152
2026-07-01 09:07:08 +03:00
346ebaa0e6 feat(billing): indirim kodu sistemi (admin panel + Stripe promo + ödeme)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Admin paneli (admin@sase.tr) kod üretir/takip eder, ödeme sayfası kodu
uygular. Kodlar Stripe Coupon + Promotion Code olarak yaşar (limit/süre/
kullanım Stripe redemption'da enforce edilir); yerel discount_codes aynası
panel listeleme + checkout-anı preview içindir.

- migration 0022_discount_codes + schema (discount_codes)
- DiscountCodesModule: admin CRUD (@Roles admin) + /discount-codes/validate (login)
- StripeService: createPromotion / deactivatePromotion / getPromotionRedemptions
  / previewDiscount + resolvePlanAmount; createCheckoutSession opsiyonel
  discountCode -> discounts[{promotion_code}] + metadata.discount_code; webhook
  gerçek tahsilatı (session.amount_total) yazar + ayna times_redeemed++
- web: /dashboard/admin/discount-codes paneli + ödeme adımı indirim alanı
  (validate preview -> indirimli özet -> kod checkout'a); tr/en çeviriler
- 7 birim test (previewDiscount: % / sabit / süre / geçersiz/pasif/dolu/limit)

Not: 2026-04-22 Stripe API promotionCodes.create `promotion:{type:'coupon'}`
kullanıyor (üst-seviye `coupon` değil). dev'de Stripe key yok -> CREATE dev'de
503; E2E için dev'e Stripe TEST key gerekir.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-29 03:40:18 +03:00
a4e53f3cfd feat(trial): shorten free trial from 30 to 7 days
Backend source of truth (subscriptions.service.startFullTrial) now grants a
7-day trial via a named TRIAL_DAYS constant. Existing trials keep their stored
endDate — only new sign-ups are affected, so no migration is needed.

In-app conversion UX rescaled to the 7-day window, preserving the two-banner
no-overlap design:
  - urgency banner: visible the last 3 days (soft d3 / urgent d2 / critical d1)
  - value-upsell: owns days > 3 (the opening days), still proven-value gated
  - progress bars now derive the total span from real start->end dates, so both
    7-day and any remaining legacy 30-day trials render an accurate bar
Subscription-page thresholds (<=3 / >3) and the analytics gate updated to match.

Copy: 17 trial strings each in tr.json/en.json plus hardcoded CTAs in
site-header, _auth, register and demo-footer-cta -> "7 gun / 7 Gun / 7-Day".

Lifecycle emails need no change: trial-ending fires 3-4 days before endDate
and win-back after expiry (both already relative to endDate), and templates
use the dynamic {{daysLeft}} variable (no hardcoded 30).

Test: trial-progress-banner spec updated to 7-day scenarios.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-27 16:37:21 +03:00
3de7fa4989 ux(vinpin): brand the decoding state as Sase, drop external-catalog wording
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 14:13:22 +03:00
fff873a64d fix(vinpin): BullMQ jobId cannot contain ':' — use 'vinpin-<vin>'
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 14:02:30 +03:00
38c2afa587 chore(deploy): wire VINPIN_* env into api+worker compose (flag off by default)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 13:43:35 +03:00
8e45d00173 feat(vinpin): vision-loop decode driver (OCR) — reliable VDI automation
Replace the brittle clipboard/fixed-coord extraction with an OCR vision loop:
screenshot -> detect state (login/grid/portal/eper/modal via tesseract keywords)
-> act -> verify -> retry/recover. Adds portal->catalogue detection (the seat
non-deterministically opens a multi-brand portal with no VIN field), not-found
handling, and ffmpeg crop+upscale -> tesseract.js for the decode modal. Only the
model token + year are needed (matcher-tolerant), so OCR garbling is harmless.

Validated live against a trial seat: 6/6 of the ePER-present Fiat VINs decoded to
the correct model (EGEA/DOBLO); 2 genuine "vehicle not found" coverage gaps
reported as null. 8/8 correct outcomes. Still flag-gated off by default; tesseract
is dynamically imported only inside the worker. Warm-session persistence under
swiftshader is the remaining hardening item (currently re-logins per VIN).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 11:00:07 +03:00
dfe90fb968 feat(vehicles): automatic Vinpin VIN-decode fallback (flag-gated, off by default)
When PL24/pcat/emex can't decode a Fiat VIN, decode it via the Vinpin ePER
web catalog (warm-session Playwright worker, single seat, BullMQ concurrency 1),
cache the exact vehicle in vinpin_decodes, match it to PL24's existing
catalog_vehicle for that model, and serve the parts from there. Vinpin = decode
oracle only; PL24 already holds the parts (e.g. Egea/Linea/Doblo).

Strictly gated behind VINPIN_ENABLED (default false) + a Fiat-only brand
allowlist: with the flag off, decodeVin behaviour is byte-identical and the
queue is never touched (covered by tests). Coordinates/selectors in
vinpin.constants.ts are marked TUNE-AGAINST-LIVE-PAID-SEAT.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-26 07:36:39 +03:00
72889796e5 Merge pull request 'perf(prefetch): pcat rate 8->20/dk + pace 15s->5s (ramp adim 1)' (#151) from dev into main 2026-06-25 09:56:24 +03:00
3096d35113 perf(prefetch): pcat rate 8→20/dk + pace 15s→5s (ramp adım 1)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
pcat kuyruğun %67'si (~26.5k) ve en yavaş drenajı. Proxy = DataImpulse rotating
gateway (74.81.81.81:10000-10999, ~1000 port-rotated çıkış IP — 2026-06-25
doğrulandı: 5 port→5 farklı Alman residential IP), per-IP ban yükü dağılıyor →
8/dk shared cap gereksiz muhafazakâr. Adım 1: 20/dk + pace 5s. (Coolify env yolu
duplike-girdi yüzünden container'a inmedi; kod-default güvenilir yol.) Env override
hâlâ mümkün (PREFETCH_RATE_PCAT/_PCAT_DELAY_MS). 429/403/tunnel izlenecek.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-25 09:56:04 +03:00
a4103a8e08 Merge pull request 'fix(subscription): default billing period to monthly, not yearly' (#150) from dev into main
Reviewed-on: #150
2026-06-23 10:09:08 +03:00
31fbcf2821 fix(subscription): default billing period to monthly, not yearly
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Yearly upfront (₺9.990 Full) is the top checkout-abandonment point, so an
annual commitment shouldn't be the default selection. New users now land on
monthly; the yearly tab keeps its +2-month gift badge, and an existing/
pending subscriber's or marketing-preselected period still wins.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 09:57:39 +03:00
0760989b52 Merge pull request 'feat(vehicles): model-browse fallback for no-catalog VINs' (#149) from dev into main
Reviewed-on: #149
2026-06-23 09:13:10 +03:00
9232e40942 feat(vehicles): model-browse fallback for no-catalog VINs
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
When a VIN can't be decoded by any source but its WMI brand is known and
has a browse-able catalog (SERVICE_TO_BRAND), return a structured
{ noCatalog: { brandName, display }, vin } 200 response instead of
dead-ending. The web surfaces a "kataloğunu modelden incele" CTA that
deep-links into the existing /dashboard/catalog browse, where the parts
usually exist (Fiat Egea NM4356 -> PL24 TIPO-EGEA; old Renault VF1 -> 147k
emex parts) but aren't reachable by the specific VIN's index entry.

Brands with no browse catalog (Honda, Maserati, Alfa, ...) keep the
existing informative dead-end. New analytics: vin_decode_no_catalog +
vin_no_catalog_browse_clicked (this case no longer emits vin_decode_error).

Tests: 2 api (browseable -> fallback, non-browseable -> throws) + 1 web
(CTA renders, no error banner). RCA writeup:
/home/s/ss/katalogsiz-vin-rca-2026-06-23.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 08:58:58 +03:00
00d1e8c961 Merge pull request 'fix(faz6): generic-model skip kaldir (PSA false-poison) — cap-only' (#148) from dev into main 2026-06-23 06:36:23 +03:00
b3dd119c99 fix(prefetch/faz6): generic-model skip'i KALDIR — PSA'yı yanlış zehirliyordu
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Tespit: generic-model (model==brand) sadece Opel'i değil PSA'yı (Peugeot/Citroën)
da kapsıyor; ama PSA generic-model PATLAMIYOR (~200 kat) ve PARÇA ALIYOR (512/290).
Skip onları yanlışlıkla poison'lar + prefetch'i keserdi. Tek doğru guard =
CATEGORY_CAP (marka-bağımsız; yalnız gerçek patlamada >3000 tetiklenir → Opel'i
durdurur, küçük PSA'yı korur). isGenericModel + processInit gate kaldırıldı,
spec sadeleşti (cap + scan-skip kaldı).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 06:35:59 +03:00
335a3b0cae Merge pull request 'Faz 6: anti-poison (generic-model skip + per-arac kategori tavani)' (#147) from dev into main 2026-06-23 06:33:20 +03:00
95061dfa53 feat(prefetch/faz6): anti-poison — generic-model skip + per-araç kategori tavanı
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
5 Opel aracı (model="Opel" decode çözememiş) generic ROOT kataloğa düşüp
419k/102k/21k kategori + 0 PARÇA üretti → 97k kuyruğu zehirledi. İki guard:
- isGenericModel (model==brand veya boş) → processInit skip + markPoison
- CATEGORY_CAP (3000, env PREFETCH_CATEGORY_CAP): processChildren progress.total
  bu tavanı aşınca drill'i durdurur + markPoison
markPoison → prefetch:poison:<id> (30g TTL) + scheduled/progress temizler;
processInit & tryPick poison'ı atlar. +3 spec (generic skip / cap / scan skip).
Full api suite 321 pass. Çöp kategori temizliği ayrıca yapılacak.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 06:33:04 +03:00
fd884271b7 Merge pull request 'Tam katalog kapsama: 4-faz prefetch stratejisi (derinlik+skip-completed+per-source+env)' (#146) from dev into main 2026-06-22 11:30:48 +03:00
d119b1eb2c feat(prefetch/faz4): PREFETCH_* env'i api compose bloğuna wire et (Coolify-tunable)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Memory gotcha: Coolify env yalnız compose service `environment:` bloğu ${VAR}
referans ederse container'a ulaşır. Prefetch worker api container'ında in-process
→ 13 anahtar api bloğuna eklendi (CATALOG_BACKFILL_ENABLED + PREFETCH_CONCURRENCY/
RATE_MAX/MAX_DEPTH/RATE_PL24/RATE_EMEX/RATE_PCAT/PCAT_DELAY_MS/PL24_START/PL24_END/
NORESULT_MAX/NORESULT_TTL_DAYS/COMPLETE_TTL_DAYS). Hepsi `:-` boş→kod-default
(davranış değişmez); artık rate/concurrency Coolify UI'dan deploy'suz ramp edilebilir.
PL24 rate yükseltme rotating proxy + izleme ile (Faz 5).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 11:27:19 +03:00
5200d5a61d feat(prefetch/faz3): per-source paralel rate limit (PL24/EMEX/pcat bağımsız)
Tek global 20/dk limiter tüm kaynakları paylaştırıyordu → yavaş pcat PL24/emex'i
açlığa düşürüyordu. Artık her kaynak Redis fixed-window ile BAĞIMSIZ kısılıyor
(SOURCE_RATE_MAX: pl24=20, emex=20, pcat=8; env PREFETCH_RATE_PL24/_EMEX/_PCAT).
Global WORKER_RATE_MAX güvenlik tavanı (20→60). RateLimitError yeni cause
"source-rate" → process() per-job defer eder (emex/pcat PL24'ü beklemez); cooldown
hâlâ tüm worker'ı duraklatır. PL24 default'u 20'de KALDI (ban riski; yüksek-rate
Faz 4 rotating proxy ile). +spec: under/over ceiling + unknown-source unlimited.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 11:25:41 +03:00
7c715af0c5 feat(prefetch/faz2): tamamlanan araçları işaretle + Phase-2 rescan'de atla
Chain parça ile tamamlanınca prefetch:complete:<id> set edilir (TTL 21g,
env PREFETCH_COMPLETE_TTL_DAYS). tryPick bu işaretliyi atlar → Phase-2 bitmiş
ağaçları her dalga yeniden gezmez (eski "sonsuz churn" durur). Başarısız job
isFinished'e ulaştırmaz → kısmi ağaç asla complete işaretlenmez, gap-fill korunur.
TTL periyodik re-validasyon sağlar. +spec: complete-marker'lı araç Phase-2'de atlanır.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 11:22:02 +03:00
61436db48f feat(prefetch/faz1): tam-derinlik (MAX_DEPTH 2→12) + no-op children job üretimini durdur
Hedef = decode edilmiş tüm araçların TAM kategori+parça ağacı DB'de. Cap artık
hedef değil güvenlik tavanı (gerçek ağaçlar ~5-7 derin; doğal yaprak tespiti
durduruyor). queueCategoryJob non-leaf dalı `depth < MAX_DEPTH` ile kapılandı:
processChildren'ın early-return'ünü aynalar → kuyruğun ~%85'ini oluşturan
boş depth-job'ları (rate-limit slotu harcayan no-op) hiç üretilmez.
+spec: depth>=cap'te children enqueue YOK, cap altında VAR.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 11:17:36 +03:00
a36008017c Merge pull request 'Iletisim: Merkez adresini tek satira sadelestir' (#145) from dev into main 2026-06-22 09:42:29 +03:00
70b6597811 feat(contact): Merkez adresini tek satıra sadeleştir
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Firma Adı/Adres/Şehir/Ülke/Vergi No döküm yerine tek satır:
"1209 Mountain Road PL NE #11131, Albuquerque, NM 87110, USA" (Türkiye Ofisi ile tutarlı).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 09:39:45 +03:00
64b2c9fb06 Merge pull request 'Iletisim: Merkez/Turkiye Ofisi adresleri + click-to-call telefon' (#144) from dev into main 2026-06-22 09:31:38 +03:00
690bdeb647 feat(contact): USA adresini "Merkez" yap + Türkiye Ofisi ekle + click-to-call telefon
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
İletişim sayfası: mevcut THINXTRA LLC (USA) adresi "Merkez" başlığı altına alındı,
"Türkiye Ofisi" (Bahçekapı mah, 2473. cad. No: 98 Etimesgut / Ankara) eklendi.
Destek kartında mailin yanına 0 850 840 8939 tel: linkiyle (click-to-call) eklendi.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 09:26:48 +03:00
3cadf80286 Merge pull request 'Backfill BFS derinligini 5->2'ye indir (sig pre-warm)' (#143) from dev into main 2026-06-21 16:59:54 +03:00
50cef50289 feat(prefetch): backfill BFS derinliğini 5→2'ye indir (sığ pre-warm + tembel derin)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Exhaustive depth-5 pre-warm ~460k'lık sınırsız BFS fan-out üretiyordu, hiç
drenaj olmuyor ve faydalı sığ + reactive işi açlığa düşürüyordu. Üst ~2
seviyeyi her decode edilmiş araç için pre-warm etmek sınırlı iş (günlerde
biter), boş katalog sayfasını öldürür, ilk tıklamaları anında yapar. Derin
kategoriler on-view (getCategoryTree) tembel çekilip kalıcı cache'lenir.
Env-tunable: PREFETCH_MAX_DEPTH. MAX_DEPTH guard mevcut derin job'ları no-op'a
çevirir (PL24'e yük yok).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-21 10:05:31 +03:00
34d68510e3 Merge pull request 'PL24 scrape penceresini devre disi birak (7/24 akis)' (#142) from dev into main 2026-06-21 09:24:46 +03:00
bee68b6a34 feat(prefetch): PL24 scrape penceresini varsayılan olarak devre dışı bırak (7/24 akış)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
PL24 prefetch artık 09:00-18:00 İstanbul penceresine bağlı değil; backfill
backlog'unun ~%96.7'si PL24 children olduğundan 9 saatlik pencere drenajı
boğuyordu. Pencere env ile geri daraltılabilir (PREFETCH_PL24_START/_END).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-21 09:14:06 +03:00
5aad3e8b22 Merge pull request 'feat(proxy): otomatik Floxy→DataImpulse failover (decode regresyonu)' (#140) from dev into main
Reviewed-on: #140
2026-06-13 18:30:45 +00:00
b992c0075b feat(proxy): otomatik Floxy→DataImpulse failover (decode regresyonu)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Floxy residential proxy bitince/çökünce (402 bakiye veya tünel reddi —
ERR_TUNNEL_CONNECTION_FAILED), EMEX + pcat decode istekleri ölü proxy'ye
çarpıp decode başarı oranını çökertiyordu (2026-06-13: ~17k tünel hatası/24s,
decode %78→%45). Önceki "DataImpulse last-ditch fallback" tarayıcı yolunda hiç
yoktu ve HTTP yolunda her istekte 2 ölü Floxy denemesi ziyan ediyordu.

Paylaşılan ProxyHealthService kapısı (in-memory cooldown): herhangi bir tüketici
bir Floxy bağlantı hatası görünce kapıyı tetikler; cooldown boyunca TÜM tüketiciler
DataImpulse'a düşer. Floxy'den ilk başarılı yanıt veya cooldown bitişi kapıyı
temizler (kendi kendini iyileştirir, periyodik yeniden-deneme). FLOXY_FAILOVER_COOLDOWN_MS
ile ayarlanır (varsayılan 180s).

Bağlanan tüketiciler:
- EMEX HTTP (fetchEmexHtml): kapı açıkken DataImpulse-öncelikli zamanlama
- EMEX tarayıcı (Playwright): launch'ta dinamik sağlayıcı seçimi + ensureSession
  health-gate'i (Floxy ölünce DataImpulse'a relaunch, kapı temizlenince Floxy'i
  yeniden dene); scrape-içi tünel ölümünde tripFloxyFailover
- pcat (buildProxy): hem call hem capture (Playwright JWT) bacakları DataImpulse'a düşer

isProxyConnectFailure(): yalnız gerçek bağlantı hatalarında tetikler — yavaş-ama-
canlı exit'in nav timeout'u failover'ı tetiklemez.

Test: ProxyHealthService + isProxyConnectFailure birim testleri; tüm api suite (303) yeşil.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-13 21:03:23 +03:00
aa4503f1c9 Merge pull request 'fix(web): Turnstile yüklenemeyince kullanıcıya uyarı göster' (#139) from dev into main
Reviewed-on: #139
2026-06-12 13:51:18 +00:00
aba8e5eb9b fix(web): Turnstile yüklenemeyince kullanıcıya uyarı göster
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Reklam engelleyici / gizlilik eklentisi challenges.cloudflare.com'u
kestiğinde widget sessizce kayboluyordu; form token'sız gidince
kullanıcı yanıltıcı 'E-posta veya şifre hatalı' görüyordu. Artık
script yüklenemediğinde veya widget error-callback verdiğinde
widget'ın altında açıklayıcı bir uyarı çıkıyor (login/register/iletişim).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 16:45:26 +03:00
97c1dbcece Merge pull request 'fix(pcat): robust year extraction across key spelling + date format' (#138) from dev into main
Reviewed-on: #138
2026-06-12 13:11:22 +00:00
d5362a5390 fix(pcat): robust year extraction across key spelling + date format
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Broadened extractYearFromPcatCar after auditing 176 null-year prod pcat
vehicles: the build date sits under "production date" (space) far more often
than "production_date" (underscore), and values come both year-first
(2006/02/27) and day-first (08/03/2016). Match the date key by spelling-agnostic
substring and pull the 4-digit year via regex so parseInt no longer reads the
day. Concrete Year param still wins over build date.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 16:09:14 +03:00
7571438a6b Merge pull request 'dev' (#137) from dev into main 2026-06-12 13:08:00 +00:00
e57a9968b2 fix(pcat): extract real Year param, not the year=All filter axis
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
pcat decode emits a faceting axis param key="year" value="All" BEFORE the
concrete per-car key="Year" value="2021". extractYearFromPcatCar used
find(includes("year")) which grabbed the axis → parseInt("All")=NaN → null,
so pcat-sourced vehicles (e.g. Opel ASTRA-J W0VPD5EC1MG063839) saved with a
blank year despite the year being present in raw_data. Scan all year-ish
params for a plausible value, then fall back to production_date.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 16:07:11 +03:00
6c6b74a8e5 feat(payments): recurring billing — Stripe Checkout switches to subscription mode
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Monthly/yearly purchases were one-time charges: our "subscription" was just
an end_date stamp, access silently died at period end and no renewal
machinery existed (no auto-charge, no reminder) — every paying customer had
to notice the lockout and re-buy by hand.

- Checkout now mode:"subscription" with inline recurring price_data; the
  Stripe customer is stored on first purchase and reused (saved card +
  invoice history on one record, with a stale-customer retry guard)
- invoice.paid webhook: extends end_date to the billing-line period end,
  records a completed payment (deduped on stripe_invoice_id against webhook
  retries), captures subscription_renewed with $revenue, mails the receipt;
  late dunning recovery re-activates the row and re-grants Full-plan brands
- invoice.payment_failed webhook: dunning mail with Stripe's next retry
  date; access is NOT cut — end_date governs and the nightly cron closes it
  if every retry fails. Product rule: mail on success, mail on failure,
  never a pre-charge reminder
- customer.subscription.deleted: stamps cancelledAt; renewals stop and
  access runs out at end_date naturally
- cancel()/resume() sync cancel_at_period_end to Stripe (forwardRef pair) —
  an in-app cancel that leaves the card being charged was unacceptable
- subscription_create invoices only enrich the checkout's payment row
  (payment intent + invoice id for receipts/panel refunds); activation,
  revenue and the receipt stay on checkout.session.completed
- migration 0020: users.stripe_customer_id,
  user_subscriptions.stripe_subscription_id (+idx),
  payments.stripe_invoice_id (+idx)

Legacy one-time subs (3 live payers) are untouched: they expire at their
end_date as before and board recurring on their next manual checkout.

Promote checklist: add invoice.paid / invoice.payment_failed /
customer.subscription.deleted to the prod webhook endpoint; verify Stripe
"Customer emails" upcoming-renewal reminders stay OFF.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 15:55:44 +03:00
4879f7cdef fix(subscriptions): keep trial alive until payment actually lands
Clicking "Öde" ran create(), which expired the user's live trial BEFORE
any money moved. Anyone who then abandoned the Stripe page lost their
remaining trial days and was fully locked out (brand-access requires
active|trial) — every abandoned checkout became silent churn (3 real
victims on prod: 06-02 and 2x 06-10, incl. a 9.990 TRY yearly intent).

- create(): no longer touches the trial; only stale pending rows are
  expired at checkout start
- activateSubscription(): supersedes the trial here instead, once the
  payment has actually landed (single legitimate early-end point;
  idempotency guard unaffected)
- getMySubscription(): status-priority pick (active > pending > trial >
  cancelled > rest) so the expired remains of an abandoned checkout —
  always the newest row — no longer eclipse a live trial on the
  subscription page

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 15:15:47 +03:00
f394897a08 fix(part-prices): fiyat kimliği marka+kod oldu — kısa kodlarda marka karışması giderildi
Kısa sayısal kodlar markalar arası çakışıyor ve FARKLI fiziksel parçaların
fiyatları tek havuzda karışıyordu (vaka: FEBI 27155 ₺160 iken çip GROS+İBRAŞ+
NIFEA karışımı ₺566 medyan gösterdi).

- sku_map'e brand_norm eklendi (takip p10 kolonunun marka kısmı; yoksa sku
  öneki) — vmi'de yeniden kuruldu (6,8M satır, %99,4 markalı)
- filterOffersForBrand: önek/sıralı-altdizi marka eşleme (BCH→BOSCH,
  BLP→BLUEPRINT, BRA→İBRAŞ); markasız sorgu yalnızca uzun/benzersiz kodlarda
  (≥8 ya da ≥5+harf+rakam) tüm teklifleri kullanır; kısa salt-sayısal kod
  uyumsuzsa boş döner (yanlış veri göstermek yok)
- pg kimliği (code_norm, brand_norm) — 0019 drop+recreate (yalnız 3 test kodu
  vardı, lazy-backfill yeniden doldurur)
- API: series?code&brand; current-batch artık { parts: [{code, brand}] } alır,
  cevap "CODE::BRAND" anahtarlı; Redis anahtarları v2
- Web: satır çipleri ve dialog markayı geçirir; ana OEM kodu markasız kalır
  (uzun-kod kuralıyla eşleşir); OE çiplerinde dağıtıcı etiketi fallback'i
  (MAIS taşıyan tekliflere RENAULT isteği uzun kodda izinli)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 14:55:44 +03:00
51d8104cdc polish(part-prices): dialog'da çift başlık kaldırıldı, 'son güncelleme' → 'son fiyat değişimi'
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 13:45:58 +03:00
500738fcd4 chore(deploy): SUPPLIER_PRICE_DB env'leri sonrası dev redeploy tetikleyici
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 13:41:05 +03:00
532e1ad9ef feat(part-prices): parça kodu bazlı tedarikçi fiyat geçmişi + OEM sayfasında p50/p95/p99 grafiği
- pg: part_price_tracks + part_price_daily (0018) — (kod, kaynak, gün) başına
  stoktaki tekliflerin p50/p95/p99 + teklif sayısı; source='supplier' şimdilik,
  perakende ileride aynı tabloya 'retail' olarak girer. Tedarikçi kimliği yok.
- API: GET /part-prices/series (ilk istekte takip history'sinden lazy-backfill,
  sonrası salt-pg + Redis) ve POST /part-prices/current-batch (sayfadaki kodlar
  için canlı güncel istatistik). P-servisi sözleşmesi: asla throw yok, fail-open.
- Worker: part-price-refresh cron'u 19:30 Europe/Istanbul (takip sync'i 19:05'te
  bitiyor) — izlenen kodlara bugünün satırını upsert eder, sku_map'i artımlı
  bakar, Redis cache düşürür. SUPPLIER_PRICE_DB_* yoksa sessiz no-op.
- Kaynak köprüsü: takip.sku_map (code_norm → product_id; tam sku / ilk-boşluk /
  ilk-tire sonrası normalize adayları) vmi MySQL'inde kurulu; 6,8M satır.
- Web: OEM detayında "Tedarikçi fiyat analizi" kartı (güncel medyan + P95/P99 +
  teklif sayısı + 30g delta, 30G/90G/Tümü aralıklı step grafik, recharts) ve
  article/muadil/OE satırlarında fiyat çipi → dialog'da tam geçmiş.
- Fix(p): td snapshot'ında gerçek üretici kodu articles.name'de (article_number
  %96 upstream sayısal ID) — sayfa artık kopyalanabilir gerçek kodu gösteriyor.
- compose: SUPPLIER_PRICE_DB_ENABLED/URL api+worker bloklarına eklendi.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 13:33:37 +03:00
6a54e63c0f Merge pull request 'fix(backfill): in-process scan timer (cron marker buried behind 444k backlog)' (#136) from fix/backfill-inprocess-scan into main 2026-06-12 09:14:25 +00:00
7d10107f4b fix(backfill): run hourly scan on an in-process timer (cron marker gets buried)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
The BullMQ cron scan never fired on prod (ic frozen at 28 for days, 0
scans completed) even after the lifo change. Root cause, confirmed live:
a cron scheduler enqueues a DELAYED marker, and BullMQ promotes delayed
jobs to the wait-list HEAD via LPUSH — `promoteDelayedJobs` ignores lifo —
while the worker pops from the tail. Behind the ~444k deep-drill backlog
the scan marker sat at wait position 1 (head) with ~444k jobs ahead of it
→ unreachable for weeks. So scheduler-level lifo cannot work; the marker
must not go through the queue at all.

Run the scan on an in-process hourly timer in PrefetchWorkerService
instead (kickoff 60s after boot, then hourly), prod-host gated and
single-fired via a Redis NX lock. The scan's Phase-1 still enqueues
NON-delayed `lifo` init jobs, which DO jump the wait list (RPUSH tail →
popped next), so newly-decoded / zero-parts vehicles get onboarded ahead
of the backlog while it drains. jobs.module now removes the old cron
scheduler so stale markers stop being produced; the backfill-scan job
handler stays so any already-queued legacy marker runs harmlessly.

Tested: spec covers the NX lock (run when acquired / skip when held) plus
the existing lifo + Phase-1/Phase-2 gating; full api suite 272 passed,
`pnpm build` + biome green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 12:14:09 +03:00
a304420456 Merge pull request 'promote: sezon kazananları admin'e maillenir (otomatik üyelik uzatma kaldırıldı)' (#135) from dev into main 2026-06-12 08:57:03 +00:00
2c848df97d change(gamification): sezon ödülleri otomatik tanımlanmaz — kazananlar admin'e maillenir
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
expert-rewards cron'u artık üyelik uzatmıyor: ay kapanışında ilk 3'ü
hesaplayıp admin@sase.tr'ye Postal üzerinden kazanan listesini yollar
(sıra, ad, e-posta, puan, ödül 30/15/7 gün, kullanıcı ID) ve
oem_expert_rewards'a denetim satırı yazar; günleri admin elle tanımlar.
Dev DB'den atılan mailler "[dev]" konu önekiyle ayrışır. Mail-önce-satır-
sonra sırası retry'da mail kaybını önler (nadir çift mail kabul edilir).
Abonelik/kredi dokunuşu tamamen kaldırıldı.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 11:53:02 +03:00
33e59c9554 Merge pull request 'fix(backfill): nest-build spec typing (unblocks #133 deploy)' (#134) from fix/backfill-lifo-fast-lane into main 2026-06-12 08:47:25 +00:00
a7678acbe1 fix(backfill): type spec queue mock so production nest build compiles it
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
The prod docker build runs `nest build`, which compiles spec files too —
the untyped `vi.fn(async () => undefined)` mock made `queue.add.mock.calls[i]`
a zero-length tuple, so the destructures tripped TS2493/TS2352 and failed
`pnpm build` (tsc --noEmit had skipped the spec). Type the mock args as
unknown[]. No behaviour change; tests still 7/7, `pnpm build` green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 11:45:28 +03:00
cbdff3311f Merge pull request 'perf(backfill): fast-lane Phase-1 via lifo + run scan despite deep backlog' (#133) from fix/backfill-lifo-fast-lane into main
Reviewed-on: #133
2026-06-12 08:39:41 +00:00
137d0c07a6 Merge pull request 'promote: Parça Uzmanları programı (flag-gated) + revert düzeltmesi' (#132) from promote-expert-program into main 2026-06-12 08:25:35 +00:00
8c1d247c45 Merge remote-tracking branch 'origin/dev'
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
2026-06-12 11:16:18 +03:00
579ee8d275 tweak(gamification): uzman programı kriteri 2/2 → 5/5
En az 5 FARKLI başarılı VIN decode VE en az 5 FARKLI OEM kodu kopyası.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 11:16:01 +03:00
4df06ab3a1 Merge remote-tracking branch 'origin/dev' 2026-06-12 11:05:41 +03:00
58a0faeb99 Reapply "Merge pull request 'dev' (#130) from dev into main"
This reverts commit 5c7803e12a.
2026-06-12 11:05:41 +03:00
6c414f9a59 feat(gamification): Parça Uzmanları programı feature-flag + kullanım kriteri kapısı
Program (oylama + muadil önerisi + liderlik) artık iki katmanlı kapının
arkasında: PostHog "oem-expert-program" ana şalteri (local-eval, fail-open)
VE kullanım kriteri — en az 2 FARKLI başarılı VIN decode VE en az 2 FARKLI
OEM kodu kopyası. Karar sunucuda tek noktada (ExpertAccessService.check);
web GET /oem-votes/access ile bir kez sorar, kriterler istemciye sızmaz.

Kapalıyken: sidebar nav linki görünmez, OEM detayındaki oy kartı + öneri
bölümü render edilmez, /dashboard/uzmanlar kademeli-açılış mesajı gösterir
ve liderlik sorgusu atılmaz. Yazma uçları (oy, öneri) sunucu tarafında da
ForbiddenException ile korunur — UI gizlemek tek başına güven sınırı değil.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 11:04:20 +03:00
fe35bbd826 feat(gamification): aylık uzman sezonları — ilk 3'e otomatik üyelik uzatması
Parça Uzmanları sıralaması artık aylık sezon: liderlik tablosu içinde
bulunulan TR-ayının (Europe/Istanbul) puanlarını gösterir ve her ayın
1'i 00:00 TR'de kendiliğinden sıfırlanır. expert-rewards cron'u
(BullMQ scheduler, "0 0 1 * *" tz=Europe/Istanbul) aynı anda biten
sezonu kapatır ve ilk 3 oylayıcıya üyelik uzatması verir:
1. → 30 gün, 2. → 15 gün, 3. → 7 gün (EXPERT_REWARD_LADDER).

Ödül mekaniği referral'la birebir: canlı active/trial abonelik endDate
+gün uzar, yoksa günler users.referral_credit_days'e bankalanır (sonraki
trial/aktivasyonda tüketilir). oem_expert_rewards (migration 0017,
period+rank UNIQUE) hem denetim kaydı hem run-once garantisi — retry ya
da elle tetik çift ödül veremez. Sıralama ölçütü job ve leaderboard'da
birebir aynı (puan desc, eşitlikte puana erken ulaşan önde).

Web: uzmanlar sayfasına sezon şeridi ("Haziran 2026 sezonu" + 🥇1 ay ·
🥈15 gün · 🥉7 gün rozetleri); alt küçük-punto kural satırına aylık
sıfırlama + ödül notu eklendi. Leaderboard cevabına periodStart eklendi.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 10:48:36 +03:00
55894bec0e polish(web): oy butonlarından sayaçları kaldır
Uyumlu/Uyumsuz butonlarında oy sayıları artık görünmüyor (çoğunluğu ele
vermek oyu yönlendiriyordu); özet yine çekilir — kullanıcının kendi oyu
butonda işaretli kalır.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 09:34:44 +03:00
4068091a21 Merge pull request 'Revert PR #130 (erken prod merge geri alındı — dev'de kalacak, sonra promote)' (#131) from revert-pr130 into main 2026-06-11 21:50:14 +00:00
5c7803e12a Revert "Merge pull request 'dev' (#130) from dev into main"
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
This reverts commit eccecf73e6, reversing
changes made to 4bcac424e0.
2026-06-12 00:48:58 +03:00
24e668ea01 polish(web): oy kartı header'a — OEM kodunun sağ simetriğine, araç+parça bağlamıyla
Topluluk oyu kartının koyu dolgusu kalktı (sayfa arkaplanıyla aynı, yalnız
çerçeve); desktop'ta başlıkta OEM kodunun sağına yerleşti, mobilde kodun
altına iner. Katalogdan gelişte araç etiketi + parça adı search-param'la
taşınır (v/p/vid) ve kartta butonların üstünde "araç · parça" satırı olarak
gösterilir — neyle neyin uyumlu olduğu tek alanda. vid oy kaydına analitik
bağlam olarak geri eklendi; doğrudan ziyarette genel soru metnine düşülür.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 00:47:06 +03:00
eccecf73e6 Merge pull request 'dev' (#130) from dev into main
Reviewed-on: #130
2026-06-11 21:45:35 +00:00
6575c611e8 polish(web): puan kuralları yalnız Uzmanlar sayfası altında, oyunlaştırılmış dille
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Üstteki üç-kartlı kural bloğu ve oy kartındaki kural cümlesi kaldırıldı;
kurallar tek satır küçük puntoyla sayfanın en altında ("Oy ver +1 ·
çoğunluğu tuttur +2 bonus · kodu ilk değerlendiren 3 puanı kapar").
Başlık ve boş-durum metinleri de oyunlaştırıldı; oy kartında yalnız
"Her oy puan kazandırır · Parça Uzmanları sıralaması" linki kaldı.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 00:33:37 +03:00
4072c6e736 feat(catalog): OEM oyları detay sayfasına taşındı + topluluk muadil önerileri
Parça tablosu sadeleşti: Uyum ve Adet kolonları kalktı, liste açılışındaki
toplu istekler (/oem-votes/lookup ve /p/matched) tamamen kaldırıldı. Her OEM
kodu artık koşulsuz /dashboard/oem/$code'a linklenir — P eşleşmesi olmayan
kodda da sayfa dolu: topluluk oyu kartı, muadil önerileri ve ters katalog.

OEM detay sayfası: OemVoteCard (uyumlu/uyumsuz, sayaçlar, puan toast'ı,
puanlama özeti + Parça Uzmanları linki) ve OemSuggestionsSection — eşleşme
bulunamayan kodlar için kullanıcıdan marka + parça kodu önerisi toplar.
Öneriler oem_suggestions tablosunda (kullanıcı+kod+normalize öneri başına
tek satır, ON CONFLICT yutulur), markaya+normalize koda göre gruplanıp
"× N kullanıcı" rozetiyle listelenir; önerilen kod kendi detayına linklenir.
Şimdilik öneri puan kazandırmaz; status kolonu moderasyon kancası.

API: oem-suggestions modülü (POST 10/dk throttle, GET ?code=), migration
0016_oem_suggestions. Eski tablo-içi OemVoteButtons bileşeni silindi.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 00:26:20 +03:00
0de6bd3faa perf(backfill): fast-lane Phase-1 via lifo + run scan despite deep backlog
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
The hourly catalog-backfill scan had gone effectively dead on prod: its
job sat behind a ~430k-deep wait list and, even when it ran, self-skipped
because the queue backlog (431k) was far over the maxBacklog ceiling
(1000). Net result: newly-decoded / zero-parts vehicles were never
onboarded — they starved behind the deep-drill backlog (ic stuck at 28
for days, 0 backfill-scan jobs ever completed).

Root constraint (verified against bullmq 5.68 lua): moveToActive drains
the `wait` list (RPOPLPUSH from the tail) BEFORE the `prioritized` ZSET,
so a `priority` job is starved behind an already-deep wait queue — the
opposite of what's wanted. The lever that works is `lifo`: it RPUSHes to
the tail, where the very next RPOPLPUSH picks it, ahead of the FIFO
backlog. addJobFromScheduler honours lifo too, so the scan job itself can
jump the queue.

Changes:
- Thread a `fast` flag through the init→children→parts chain; fast jobs
  are enqueued with `lifo:true` so the whole chain jumps the backlog.
- Backfill scan: Phase-1 (zero-parts vehicles) now runs every wave in the
  fast lane even when the backlog is over the ceiling; only Phase-2 (the
  rolling rescan that piles on) is suspended while the queue is deep.
- Register the hourly scan job with `lifo:true` so it fires on the next
  tick instead of being buried for days.

Tested: new prefetch-worker.service.spec (lifo wiring + Phase-1/Phase-2
gating), full api suite green (248 passed), typecheck + biome clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 00:18:37 +03:00
d922fb7a02 test(catalog): cover P4 VIN group-node gate (children + loadError)
Mirrors the PSA VIN-scope tests for the new …group.action parent gate: a Ford
json-vin-sub-group.action node with a positional link_wid routes to children
(not a silent empty leaf), and an empty drill surfaces loadError.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 00:02:50 +03:00
8fddd09087 feat(catalog): OEM uyumluluk oylaması + Parça Uzmanları liderlik tablosu
Parça satırlarına topluluk oyu eklendi: her OEM kodu için uyumlu/uyumsuz
ikilisi (👍/👎, sayaçlı). Oylar oem_votes'a (kullanıcı+kod başına tek oy,
fikir değişikliği günceller, puan üretmez), ödüller oem_vote_points
ledger'ına yazılır: oy +1, kesin çoğunlukla aynı yönde +2 (kodu ilk
oylayan her zaman 3 alır); ödüller oy anında kesinleşir, çoğunluk sonra
dönse de geri alınmaz. Aynı koda eşzamanlı oylar advisory lock ile
sıralanır.

/dashboard/uzmanlar: Trophy Gamification UI Kit'ten (ui.trophy.so, MIT)
uyarlanan kürsü + sıralama + puan rozetiyle "Parça Uzmanları" liderlik
sayfası; adlar KVKK-maskeli (S*** Y***), cevap kullanıcı id sızdırmaz.

Not: sidebar nav linki, tr/en i18n anahtarları ve routeTree 09a9487'de
gitmişti; bu commit eksik kalan rota/bileşen/API dosyalarını tamamlayarak
dev build'ini düzeltir. Migration: 0015_oem_votes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 00:00:51 +03:00
09a9487564 remove changelog page and sidebar link
Drop /dashboard/changelog route, ChangelogTab component, useChangelog
hook, the sidebar nav item, and the related tr/en translation keys.
API-side changelog schemas are untouched.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 23:51:54 +03:00
e6c8c9c32e fix(catalog): drill P4 VIN group nodes on live click (Ford/Nissan/Opel parent-gate gap)
getCategoryWithPartsInner had parent gates for PSA (psa::/json-vin-main-groups),
pcat (hasSubgroups), pl24 link_wid *Group* tables, Volvo (vin-group.action) and
emex — but P4 VIN/legacy group nodes whose link_wid is a positional code
("1","2","CAP1"…) and whose path is a *…group.action* drill (Ford
json-vin-sub-group, Nissan/Opel json-sub-group, etc.) matched none of them. They
fell through to the leaf parts path, fetched no parts and rendered a silent empty
panel — the dominant current '0 parça' cluster (mekanik/kaporta/Elektrikli aksam…
across many vehicles; PostHog-confirmed). Add a gate that drills any pl24
…group.action node (excluding Volvo's own vin-group.action branch) → subgroups or
retryable loadError. Lazy-heals existing flag-less rows, no reseed.

Also set has_subgroups at seed time: PSA scopes → true (always parents);
P4-legacy cats → true when linkPath is a …group.action drill. Fixes folder
signposting + lets prefetch pre-drill them instead of treating them as leaves.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 23:48:04 +03:00
4bcac424e0 Merge pull request 'dev' (#126) from dev into main
Reviewed-on: #126
2026-06-11 14:34:37 +00:00
e91159003e chore(web): remove 'click to reopen' hint under recent searches
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 17:33:49 +03:00
45f7e3f5a4 feat(proxy): proxy_logs telemetry — per-attempt provider/ban/latency tracking
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
One row per proxied upstream attempt (pcat call/capture/validate, emex http)
written fire-and-forget by the new ProxyTelemetryService (buffered, capped,
errors swallowed — telemetry can never hurt the request path).

- banned = upstream 403/429 (IP-block signal), distinct from auth/data errors
- sticky legs (pcat capture, emex floxy) carry a session_key; pcat capture
  also resolves the actual residential exit IP via a parallel ipify probe
  through the same sticky session → concrete banned-IP tracking
- rotating legs log provider + outcome (ban *rate* instead of per-IP)
- 30-day retention piggybacked on the query-cleanup job

Feeds the Süper Panel /analytics/proxy page (provider grading + banned IPs).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 17:27:48 +03:00
9f47d15312 feat(web): hide Chatwoot launcher bubble on mobile
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Floating launcher covered content on small screens. Bubble is hidden
below 768px via injected CSS; programmatic openChatwoot() callers
(subscription page, onboarding modal) still open the fullscreen widget.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 17:26:04 +03:00
3c9245bd8b feat(web): keep sidebar on blog & contact for logged-in users
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Sidebar linked /blog and /contact pointed at public marketing pages, so
clicking them dropped the user out of the dashboard shell. Extract page
content into shared components (blog-content, contact-content) and add
dashboard-wrapped routes /dashboard/blog, /dashboard/blog/$slug and
/dashboard/contact; sidebar now links to those. Public SEO pages stay
unchanged and remain the canonical URLs.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 17:25:14 +03:00
ffcd417939 Merge pull request 'hotfix(pcat): widen JWT capture for slow residential IPs' (#129) from hotfix-pcat-capture into main 2026-06-11 13:59:37 +00:00
75d5b11475 fix(pcat): widen JWT capture for slow residential IPs (domcontentloaded + 12s poll)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
After the Floxy account rotation, the new residential exit IPs are slower:
measured DOM-ready ~7s and the widget's /v3/api/proxy token call firing ~11.8s —
past the 10s PAGE_TIMEOUT + networkidle, so every capture timed out ("No token
after 12s") and the warm pool stayed empty (pcat dead despite valid creds).

- waitUntil networkidle → domcontentloaded (reliable ~7s; networkidle often never
  settles on JS catalog sites through a slow proxy → goto times out pre-token).
- PAGE_TIMEOUT 10s → 25s (env PCAT_PAGE_TIMEOUT_MS); token fired at ~11.8s.
- CAPTURE_POLL_AFTER_OK 5s → 12s (env PCAT_CAPTURE_POLL_OK) to cover the ~5s
  gap between DOM-ready and the token call. Capture is background (warm pool,
  cold-pool fast-fail never blocks users), so the longer cap is free insurance.

Verified via standalone Playwright probe through the new Floxy: token captured at
11842ms (DOM 7023ms). typecheck clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 16:59:34 +03:00
4a6e673af1 fix(pcat): widen JWT capture for slow residential IPs (domcontentloaded + 12s poll)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
After the Floxy account rotation, the new residential exit IPs are slower:
measured DOM-ready ~7s and the widget's /v3/api/proxy token call firing ~11.8s —
past the 10s PAGE_TIMEOUT + networkidle, so every capture timed out ("No token
after 12s") and the warm pool stayed empty (pcat dead despite valid creds).

- waitUntil networkidle → domcontentloaded (reliable ~7s; networkidle often never
  settles on JS catalog sites through a slow proxy → goto times out pre-token).
- PAGE_TIMEOUT 10s → 25s (env PCAT_PAGE_TIMEOUT_MS); token fired at ~11.8s.
- CAPTURE_POLL_AFTER_OK 5s → 12s (env PCAT_CAPTURE_POLL_OK) to cover the ~5s
  gap between DOM-ready and the token call. Capture is background (warm pool,
  cold-pool fast-fail never blocks users), so the longer cap is free insurance.

Verified via standalone Playwright probe through the new Floxy: token captured at
11842ms (DOM 7023ms). typecheck clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 16:59:17 +03:00
d82bcb560b Merge pull request 'hotfix(proxy): rotate Floxy creds (402 balance) — restore pcat' (#128) from hotfix-floxy-creds into main 2026-06-11 13:49:58 +00:00
46e4b7cfcb fix(proxy): rotate Floxy creds (prior account hit HTTP 402 / balance exhausted)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
The hardcoded Floxy account (d739255e819b) ran out of balance → 402 Payment
Required on every CONNECT → pcat decode dead on prod since ~07:16 UTC 2026-06-11
(pcat has no proxy fallback; EMEX limped on its DataImpulse fallback). New funded
account cac4b0d96a80 verified working (call leg 401-reachable, capture leg 200).
Updates the hardcoded defaults in all 3 Floxy consumers (pcat auth, emex http,
emex browser); env overrides PCAT_FLOXY_USER/PASS + EMEX_FLOXY_USER/PASS unchanged.

Follow-up: wire FLOXY_USER/PASS into docker-compose env so future rotations are
Coolify-env-only (no code change / rebuild).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 16:49:56 +03:00
2e6c8ac751 fix(proxy): rotate Floxy creds (prior account hit HTTP 402 / balance exhausted)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
The hardcoded Floxy account (d739255e819b) ran out of balance → 402 Payment
Required on every CONNECT → pcat decode dead on prod since ~07:16 UTC 2026-06-11
(pcat has no proxy fallback; EMEX limped on its DataImpulse fallback). New funded
account cac4b0d96a80 verified working (call leg 401-reachable, capture leg 200).
Updates the hardcoded defaults in all 3 Floxy consumers (pcat auth, emex http,
emex browser); env overrides PCAT_FLOXY_USER/PASS + EMEX_FLOXY_USER/PASS unchanged.

Follow-up: wire FLOXY_USER/PASS into docker-compose env so future rotations are
Coolify-env-only (no code change / rebuild).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 16:49:37 +03:00
2f6a0e3158 Merge pull request 'Merge dev → main: surveys, P/OEM page, trial upsell, catalog-issue report, pcat timings' (#127) from merge-dev-to-main into main 2026-06-11 13:28:15 +00:00
b6e4deb7bd Merge dev into main: catalog-issue report + pcat skip/transient timings
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Resolves the recurring promote-by-cherry-pick divergence (decode/pcat/emex live
on both branches with different SHAs — all auto-merged). Net-new to main:
- fix(decode) fd78957: mark pcat skip/transient in timings (stop false catalog-gap reads)
- feat(catalog) c972a17: one-click report on empty/error catalog states
- compose: P_DB_ENABLED/P_DB_URL env (inert until set in Coolify)

Conflict: docker-compose.coolify.yml — both sides added env at the same spot;
took dev's superset (main's Directus/Blog vars + dev's P_DB vars).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

# Conflicts:
#	docker-compose.coolify.yml
2026-06-11 16:25:57 +03:00
c972a17670 feat(catalog): one-click report on empty/error catalog states — no form
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
'Çalışmadı mı? Bildir' opened the Sentry feedback dialog; most users
bail at a form. Now a single click captures a catalog_issue_reported
PostHog event with the vehicle/category context (vin, vehicle_label,
category_name, source, page) and flips to 'Bildirildi — teşekkürler'.
The identified person already carries the reporter's email; the Süper
Panel lists these under /analytics/reported-categories. The automatic
Sentry degradation report (with session replay) still fires via
useReportCatalogDegradation — unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 16:00:23 +03:00
fd789574fd fix(decode): mark pcat skip/transient in timings — stop false catalog-gap reads
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Cold-pool fast-fail (and transport blips) make pcat return null without
ever evaluating the VIN; EMEX then identifies the car and the row lands
as 'No catalog — identified as X' (pcat sub-timing 1-2ms). Downstream
catalog-gap detection can't tell these from true coverage gaps and
flags covered brands (Audi, 2026-06-11, 7 retries by one user).

- timings.pcat_skipped=1 on cold-pool fast-fail
- timings.pcat_transient=1 on transport failure (circuit-open skips
  already carry pcat_circuit_open)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-11 11:50:36 +03:00
4b5f3bb41e Merge pull request 'promote(emex): Floxy residential primary transport' (#125) from promote-emex-floxy into main 2026-06-10 19:24:21 +00:00
63908b1fca perf(emex): Floxy residential primary transport (was DataImpulse-first)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
EMEX egress was DataImpulse-first: fetchEmexHtml tried 3 dead-prone DataImpulse
ports before falling to the Floxy fallback, and the browser scraper launched on a
random DataImpulse port. With ~50% of DataImpulse ports dead, this burned the 25s
decode budget before reaching Floxy (observed: a Ford VIN where pcat correctly
returned null fast, then EMEX's DataImpulse retries ate the budget).

ssd tokens are replayed from emex's own HTML and are NOT strictly IP-bound on the
.aspx endpoints (the prior random-port-per-call primary proved that), so a sticky
Floxy IP across the flow is strictly safer.

- EMEX_PROXY_PROVIDER (default "floxy" | "dataimpulse" | "none"). floxy →
  fetchEmexHtml schedule = [floxy, floxy, dataimpulse] (Floxy sticky primary,
  rolls IP on transport failure, single DataImpulse last-ditch). dataimpulse →
  legacy [di,di,di,floxy,floxy]. Default HTTP agent (image-dims) follows provider.
- EmexBrowserService launches chromium on a sticky Floxy session (one residential
  exit IP for the browser lifetime; fresh id per relaunch) under the same flag.
- Reuses the existing EMEX_FLOXY_* config (same Floxy account pcat now uses).

Rollback: EMEX_PROXY_PROVIDER=dataimpulse. typecheck+biome+11 emex tests green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 22:23:36 +03:00
ef4ab9bcd5 perf(emex): Floxy residential primary transport (was DataImpulse-first)
EMEX egress was DataImpulse-first: fetchEmexHtml tried 3 dead-prone DataImpulse
ports before falling to the Floxy fallback, and the browser scraper launched on a
random DataImpulse port. With ~50% of DataImpulse ports dead, this burned the 25s
decode budget before reaching Floxy (observed: a Ford VIN where pcat correctly
returned null fast, then EMEX's DataImpulse retries ate the budget).

ssd tokens are replayed from emex's own HTML and are NOT strictly IP-bound on the
.aspx endpoints (the prior random-port-per-call primary proved that), so a sticky
Floxy IP across the flow is strictly safer.

- EMEX_PROXY_PROVIDER (default "floxy" | "dataimpulse" | "none"). floxy →
  fetchEmexHtml schedule = [floxy, floxy, dataimpulse] (Floxy sticky primary,
  rolls IP on transport failure, single DataImpulse last-ditch). dataimpulse →
  legacy [di,di,di,floxy,floxy]. Default HTTP agent (image-dims) follows provider.
- EmexBrowserService launches chromium on a sticky Floxy session (one residential
  exit IP for the browser lifetime; fresh id per relaunch) under the same flag.
- Reuses the existing EMEX_FLOXY_* config (same Floxy account pcat now uses).

Rollback: EMEX_PROXY_PROVIDER=dataimpulse. typecheck+biome+11 emex tests green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 22:18:29 +03:00
1392a642a9 Merge pull request 'promote(pcat): rotating Floxy residential transport + resilience' (#124) from promote-pcat-floxy into main 2026-06-10 18:58:32 +00:00
b886a05b5b perf(pcat): rotating Floxy residential transport + resilience (decode latency)
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
Prod decode was dominated by the DataImpulse proxy: ~50% of ports dead at any
moment, 57% of captured tokens discarded by validation, the dead-port slot
never evicted (invalidateSession only fired on 401/403, never on transport
timeout), and a cold pool blocked the user inline on a multi-second capture.
pcat sub-timing was p50 8.3s / p90 56s, blowing the 25s decode budget.

Verified on prod: the TWS- token is NOT request-time IP-bound (0 auth 401/403
across thousands of calls; a token captured on one IP replays 200 through
arbitrary fresh residential IPs). So the proxy can rotate freely.

- Transport swap to Floxy residential (PCAT_PROXY_PROVIDER=floxy|dataimpulse|none).
  Call/validate leg = a fresh exit IP per request (max IP diversity → min per-IP
  ban); capture leg = a sticky session per Playwright attempt. DataImpulse +
  direct kept as env fallbacks. Token decoupled from the slot (proxyPort removed).
- Prune 3 zero-yield JWT sites (knkauto/autodo.kz/flynestauto — 0 captures, ~80%
  of attempts); add PCAT_JWT_SITES env override.
- Lower catalog-scrape timeout 30s→10s (env-tunable) so a slow IP aborts within
  budget and the retry rotates to a fresh IP.
- Cold-pool fast-fail on the decode race: warm in background, let EMEX/PL24
  answer instead of blocking inline; treated as a transient miss (no neg-cache).
- Evict a token after N consecutive transport failures on rotating IPs (the
  dead-slot bug); reset on any 2xx.
- PCAT circuit breaker mirroring PL24 (transient-only; cold-skip neutral; skip
  marks outcome.transient so a pcat-only VIN isn't negative-cached 30m).

Typecheck + Biome + unit tests (categories/vehicles) green. Default flips pcat
to Floxy on next deploy; instant rollback via PCAT_PROXY_PROVIDER.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 21:56:45 +03:00
25ba37103d perf(pcat): rotating Floxy residential transport + resilience (decode latency)
Prod decode was dominated by the DataImpulse proxy: ~50% of ports dead at any
moment, 57% of captured tokens discarded by validation, the dead-port slot
never evicted (invalidateSession only fired on 401/403, never on transport
timeout), and a cold pool blocked the user inline on a multi-second capture.
pcat sub-timing was p50 8.3s / p90 56s, blowing the 25s decode budget.

Verified on prod: the TWS- token is NOT request-time IP-bound (0 auth 401/403
across thousands of calls; a token captured on one IP replays 200 through
arbitrary fresh residential IPs). So the proxy can rotate freely.

- Transport swap to Floxy residential (PCAT_PROXY_PROVIDER=floxy|dataimpulse|none).
  Call/validate leg = a fresh exit IP per request (max IP diversity → min per-IP
  ban); capture leg = a sticky session per Playwright attempt. DataImpulse +
  direct kept as env fallbacks. Token decoupled from the slot (proxyPort removed).
- Prune 3 zero-yield JWT sites (knkauto/autodo.kz/flynestauto — 0 captures, ~80%
  of attempts); add PCAT_JWT_SITES env override.
- Lower catalog-scrape timeout 30s→10s (env-tunable) so a slow IP aborts within
  budget and the retry rotates to a fresh IP.
- Cold-pool fast-fail on the decode race: warm in background, let EMEX/PL24
  answer instead of blocking inline; treated as a transient miss (no neg-cache).
- Evict a token after N consecutive transport failures on rotating IPs (the
  dead-slot bug); reset on any 2xx.
- PCAT circuit breaker mirroring PL24 (transient-only; cold-skip neutral; skip
  marks outcome.transient so a pcat-only VIN isn't negative-cached 30m).

Typecheck + Biome + unit tests (categories/vehicles) green. Default flips pcat
to Floxy on next deploy; instant rollback via PCAT_PROXY_PROVIDER.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 21:19:14 +03:00
a237e55874 docs: add repository index (PROJECT_INDEX.md + PROJECT_INDEX.json)
Token-efficient session bootstrap: module map, entry points, routes,
jobs, deps, test/deploy conventions. Regenerate via /sc:index-repo.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 19:25:35 +03:00
8b7dc83bd2 feat(p): reverse catalog on OEM detail — your vehicles using this code
New "Bu kod kataloğunuzda" section lists the user's decoded vehicles
whose parts include the queried OEM code, each linking to the schema
page that shows it. Pure sase data (parts.oem_code → vehicles, indexed
exact match) — no TecDoc / vehicle-structure dependency.
GET /parts/oem-vehicles?code=.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 15:26:48 +03:00
c2dfa5cd2e fix(decode): recognize VXK WMI as Opel (PSA/Stellantis-platform Corsa F, Mokka B)
Customer entered VXKUPHNKSP4032040 (2023 Corsa F) and got a bare
'Marka desteklenmiyor' — VXK was missing from all three WMI maps, so
identifyBasic could not even name the brand. PL24's opel_parts catalog
ends at CORSA-E (2015-2019, verified via the model-picker page), so the
catalog itself stays a gap, but the user now gets the honest
'Opel 2023 olarak tanındı, katalog henüz mevcut değil' message and the
miss is logged for the coverage backlog.

- WMI_BRAND_MAP + PL24_WMI_SERVICE_MAP + EMEX CATALOG_MAP: VXK -> Opel
- DECODE_CHAIN_VERSION 2 -> 3 (brand-mapping change; invalidates the
  stale negative cache for the customer's VIN)
- vin-cache-keys.spec: lock-key assertion checked the bare version char,
  which now appears inside the test VIN — assert the ✌️ segment instead

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 12:36:51 +03:00
0652fb3a18 Merge pull request 'promote(decode): opaque candidate keys + slim decode response' (#122) from promote-decode into main 2026-06-10 09:04:20 +00:00
7aafdc92b6 perf(decode): slim decode response to id + display summary
Some checks failed
QA Gate (P0/P1) / Test affected app (pull_request) Has been cancelled
POST /vehicles/decode returned the full vehicles row while every consumer
(search, landing, service-test) only navigates by id and re-fetches via
GET /vehicles/:id. That shipped the raw upstream decode payload to the
client on every decode — rawData is 19KB avg / 127KB p95 / 207KB max in
prod — plus the provider name, for nothing.

The response is now { id, brandName, model, year } on both the DB-hit and
fresh-decode paths. Frontend drops the (now absent) source property from
the vin_decode_success event; decode source remains queryable server-side
via query_logs.source.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
(cherry picked from commit 02c8321023)
2026-06-10 12:01:40 +03:00
6154656c3e refactor(decode): keep decode-source details server-side, opaque candidate keys
The multi-candidate decode response leaked provider internals (source name,
pcat car ids, EMEX _ssd/_vid/_quickGroupsUrl/catalogId) and made the client
carry them between requests: the frontend stored candidateSource and echoed
pcatCarId/emexCarIndex back on selection.

Now the candidate list returned to the client carries only display fields
(name, description, parameters) plus an opaque key, and the provider mapping
is stashed in Redis (vin:candidates:*, 30m TTL, resolve-cache fallback). The
pick request sends just { vin, candidate }. Legacy pcatCarId/emexCarIndex
body params still work for already-loaded bundles.

Also drops `source` from the public /vehicles/preview response — no consumer
used it, and provider names must never be public (same policy as
teaser-stats).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
(cherry picked from commit 2077a9724a)
2026-06-10 12:01:40 +03:00
02c8321023 perf(decode): slim decode response to id + display summary
POST /vehicles/decode returned the full vehicles row while every consumer
(search, landing, service-test) only navigates by id and re-fetches via
GET /vehicles/:id. That shipped the raw upstream decode payload to the
client on every decode — rawData is 19KB avg / 127KB p95 / 207KB max in
prod — plus the provider name, for nothing.

The response is now { id, brandName, model, year } on both the DB-hit and
fresh-decode paths. Frontend drops the (now absent) source property from
the vin_decode_success event; decode source remains queryable server-side
via query_logs.source.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 02:52:43 +03:00
2077a9724a refactor(decode): keep decode-source details server-side, opaque candidate keys
The multi-candidate decode response leaked provider internals (source name,
pcat car ids, EMEX _ssd/_vid/_quickGroupsUrl/catalogId) and made the client
carry them between requests: the frontend stored candidateSource and echoed
pcatCarId/emexCarIndex back on selection.

Now the candidate list returned to the client carries only display fields
(name, description, parameters) plus an opaque key, and the provider mapping
is stashed in Redis (vin:candidates:*, 30m TTL, resolve-cache fallback). The
pick request sends just { vin, candidate }. Legacy pcatCarId/emexCarIndex
body params still work for already-loaded bundles.

Also drops `source` from the public /vehicles/preview response — no consumer
used it, and provider names must never be public (same policy as
teaser-stats).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 02:37:02 +03:00
43dd4d38ac feat(surveys): entrance animation, step progress, Esc-to-close
Polish pass on the survey popover:
- Entrance: slide-up + fade on mount (300ms ease-out, respects
  prefers-reduced-motion via motion-reduce:transition-none).
- Step progress: "1 / N" indicator on multi-question surveys so users know
  how much is left.
- Esc closes the card (counts as a dismissal); listener attached once with a
  dismissRef so it captures current answers, not a stale first-render closure.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 01:28:45 +03:00
34cb361b67 fix(surveys): one-row rating scale, rating auto-advance, mobile spacing
Design review of the live survey popover surfaced three issues:
1. NPS 0–10 wrapped to two rows (flex-wrap + flex-1 made the 8/9/10 row
   stretch absurdly wide) — now a single equal-width grid row (grid-cols-N
   by scale; static map so Tailwind keeps the classes).
2. Single-question ratings forced a redundant "Devam" tap — tapping a score
   now auto-advances (450ms debounce to allow changing the pick) and the
   submit button is hidden on rating questions.
3. On mobile the card crowded the Chatwoot launcher — bumped to bottom-28
   (sm:bottom-24 keeps desktop unchanged).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 01:19:59 +03:00
c18c513139 feat(p): drop EAN from the OEM detail page
Remove the EAN line from the matched-article cards and stop fetching/
shipping eans in /p/oem (its only consumer was that line). Snapshot
still holds the data; the endpoint just no longer queries it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 01:17:22 +03:00
5a13f13e8e perf(p): instant OEM nav + trim 30x-bloated payload
OEM detail "slow load" was two things, not the query (DB 135ms / API
13-120ms):
1. The parts-panel link opened a NEW TAB → full SPA cold boot every
   click. Switch to in-app client navigation on plain click (real href
   kept, so ctrl/cmd/middle-click still opens a new tab).
2. The /p/oem response shipped each article's oeNumbers + compatible
   lists (up to 200 each × 60 articles) that the UI never renders — 96%
   of a 370 KB payload. Ship lean articles; aggregates already carry
   the cross-refs. 60-article code: 370 KB → ~12 KB.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 01:06:29 +03:00
f29730fae0 fix(surveys): hold captures until the survey event receiver is hooked
trial_urgency_banner_viewed fires ~1s into the page load and raced the lazy
surveys extension — events captured before its hook exists can never activate
an event-triggered survey (affects real users, not just e2e). capture() now
awaits surveysReceiverReady() (onSurveysLoaded, 3s safety timeout) and runs the
eligibility check after the event has gone through.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 00:55:06 +03:00
c1ae18e5f1 feat(p): brand/code search box on the OEM detail page
Long cross-reference lists (some parts have 90+ aftermarket numbers,
160+ OE codes) were hard to scan. Add a filter input that matches
across all three sections (matched articles, aftermarket equivalents,
OE cross-refs) by brand (substring) or code (normalised, so "1j0 973"
finds "1J0 973 702"). Clear button + "no results" state.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 00:51:13 +03:00
7f5db3234b fix(posthog): disable bot UA filter on staging so e2e can exercise surveys
posthog-js silently drops every capture when userAgentData.brands contains
HeadlessChrome (UA string/webdriver masking doesn't help), which also starves
event-triggered surveys in Playwright runs against dev.sase.tr. Production
keeps the bot filter.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 00:49:20 +03:00
72c3fe0dc2 feat(surveys): replace Formbricks with self-rendered PostHog API surveys
Formbricks CE turned out to gate person-based targeting (setUserId/attributes
→ 403 enterprise) — the exact flexibility surveys need. PostHog already holds
the person properties, so surveys move back there in API mode: posthog-js
evaluates eligibility (event triggers, targeting flags like
subscription_status=active, wait periods, per-distinct_id dedup) via
getActiveMatchingSurveys, and we render the popover ourselves — zero PostHog
branding, sase.tr dark-theme styling.

- lib/surveys.ts: display manager + capture payload builders that mirror
  posthog-js's own "survey shown/dismissed/sent" shapes exactly
  ($survey_response_<qid>, $set $survey_dismissed/<id>[/iter], seenSurvey_*,
  lastSeenSurveyDate) so the PostHog Surveys results UI works unchanged
- components/survey-popover.tsx: single_choice (+Diğer), open text, rating/NPS
- posthog.ts: capture/pageview/identify now schedule survey checks; register
  deploy_env (dev.sase.tr ships the key now → staging traffic is filterable)
- remove @formbricks/js, its CSP entries and build args

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 00:40:18 +03:00
7ce8e34d81 Merge pull request 'feat(blog): CMS cover images via MinIO, instant cache purge, drop embedded posts' (#121) from blog-cms-assets into main 2026-06-09 21:22:51 +00:00
4ffc3577a5 feat(p): link only OEM codes with a cross-reference match
Every OEM code was a link → unmatched codes (the majority — fasteners,
clips, body parts TecDoc doesn't carry) opened an empty "no
equivalents" page. Now the schema page batch-checks its codes against
the snapshot (POST /p/matched, one indexed query) and links ONLY the
matched ones; unmatched codes render as plain text. Copy stays for all.
No more dead-end empty pages from the parts panel.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 00:21:25 +03:00
572337ba62 feat(blog): CMS cover images via MinIO, instant cache purge, drop embedded posts
- cover_file uploads in Directus land in the public blog-assets MinIO bucket;
  API resolves them to storage.sase.tr URLs (Directus itself is Tailscale-only)
- POST /blog/cache/purge (automation token) lets a Directus Flow drop the
  30-min list cache the moment a post changes in the CMS
- blog list + detail pages now render purely from the API; the 4 hand-authored
  posts were migrated to Directus earlier and the JSX copies are removed

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 00:17:40 +03:00
07cf710bc3 fix(csp): allow Formbricks SDK in script-src
@formbricks/js is a loader shim that injects the real SDK as an external
script from anket.sase.tr — connect-src alone wasn't enough; the browser
blocked /js/formbricks.umd.cjs and no survey could render.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 00:03:05 +03:00
f955bb7ef8 fix(vehicles): canonicalise brand casing (case-insensitive brand match)
Decode persist looked brands up with a case-sensitive eq(), so an
uppercase decode string ("FORD") missed canonical "Ford" → brand_id
NULL + raw uppercase stored as brand_name, splitting one brand across
casing variants in analytics/catalog. Now matches brands
case-insensitively and stores the canonical name. Migration
0013_fix_brand_casing backfills existing rows (60 on prod, 1 on dev).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 23:47:33 +03:00
597391bbdf feat(surveys): Formbricks in-app survey bridge over PostHog events
Self-hosted Formbricks (anket.sase.tr) replaces PostHog surveys (free-tier
branding). PostHog stays the single instrumentation source: capture() forwards
allowlisted trigger events (trial_urgency_banner_viewed, subscription_cancelled,
onboarding_completed, vin_decode_error, empty_catalog_cta_clicked) to the
Formbricks SDK, identify/reset/people-properties mirror into Formbricks
attributes, and $pageview registers SPA route changes for no-code triggers.

- apps/web/src/lib/formbricks.ts: lazy fire-and-forget wrapper (inert without
  VITE_FORMBRICKS_APP_URL + VITE_FORMBRICKS_ENV_ID)
- CSP: allow anket.sase.tr in connect-src/img-src
- Dockerfile + compose: bake the two VITE_ build args

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-09 23:44:45 +03:00
4716232398 fix(p): natural empty-state copy (drop literal "P" from user text)
"P karşılığı bulunamadı" / "P kapsamında" read as nonsense — "P" is
the internal name, not a user-facing word. Use plain Turkish: "Bu OEM
kodu için uyumlu parça bulunamadı" and describe the catalog gap
without a brand or single-letter label.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 22:37:15 +03:00
e91af4b953 refactor(p): complete TecDoc → P content rename
Finishes 3b14431 (which only captured the file renames): apply the
identifier/endpoint/env/UI changes so the code matches the new paths
— PModule/PController/PSourceDbService, @Controller("p"), /p/oem,
config key `p`, P_DB_ENABLED/P_DB_URL, "P kataloğundan…" copy.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 22:30:46 +03:00
3b144313b3 refactor(p): rename TecDoc → P (parts) across the cross-reference feature
Drop the third-party brand name from the product surface and code.
The OEM detail feature is now "P" (short for parts) everywhere:
endpoint /p/oem, PModule/PController/PSourceDbService, config key
`p`, env P_DB_ENABLED/P_DB_URL, UI copy ("P kataloğundan…"), and the
unused tab label. Physical snapshot DB stays `td` (server-only).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 22:29:40 +03:00
aeb438e442 feat(blog): back blog module with central Directus CMS
Blog posts now live in the shared Directus instance (Coolify / Süper Panel
project) instead of the per-env blog_posts table, so prod and staging serve
identical content. API response shape is unchanged; Redis list cache and the
n8n automation endpoint keep working as before.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-09 21:13:03 +03:00
24e44a49f8 fix(tecdoc): only surface publicly-resolvable part images
The snapshot stores scrape-local '/_debug/...' image paths that 404
off-host. Filter the OEM lookup to http(s) image URLs and render the
thumbnail only when one exists, so the detail page shows clean
code-focused cards instead of a grid of broken-image placeholders.
Real CDN URLs will surface automatically once present.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 21:00:12 +03:00
eae373c553 fix(web): accuracy sweep — drop unbuilt-feature claims, VIN→şase, B2B framing
Full marketing-copy audit. Removes price-comparison claims (pricing
step, hero variant B, blog tip) that aren't built; normalises VIN→şase
across TR copy (plan features, e-commerce section, FAQ, blog, about) —
keeping VIN only in the educational "VIN nedir" post and the EN file;
de-consumerises framing (FAQ "aracınız", about "bireysel kullanıcılar").
Fabricated metrics/testimonials left untouched pending review.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-09 21:00:08 +03:00
fb2c28faf1 feat(tecdoc): OEM detail page with TecDoc cross-references
Resolve a catalog OEM code to its TecDoc equivalents on a new
/dashboard/oem/$code page: the aftermarket parts that carry it
(brand + article number + image + EAN), buyable supplier
substitutes, and OE cross-references (same part under other makes).

- API: TecdocModule (read-only postgres-js client to the imported
  `td` snapshot), GET /tecdoc/oem?code=. Normalisation-based match
  (TecDoc stores `1J0 973 702`, catalog gives `1J0973702`); exact
  match recovers ~1/10 vs normalised ~5/10 on real codes. Self-
  disables without TECDOC_DB_* env → { matched: false }.
- Web: OEM code in the parts panel is now a link (new tab) to the
  detail page; "N/A" stays plain text.
- Mirrors CatalogSourceDbModule (raw queries, no Drizzle modelling).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 20:52:51 +03:00
5e1675f95a fix(web): replace unbuilt feature claims with real capabilities
The landing OEM card, the manual-vs-Sase comparison table, and a blog
post advertised "price comparison" / "order history & part tracking" —
features that aren't built. Replaced with real capabilities
(exploded-diagram matching, interactive part diagrams, brand catalogs)
in both tr + en. Mirrors the same accuracy fix already shipped to the
Novu lifecycle emails.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-09 20:44:58 +03:00
d3ae278c75 feat(trial): value-moment upsell to convert engaged trials earlier
Only ~1.6% of trials reach checkout: the 30-day full-access trial shows the
(urgency) banner solely in its final 7 days, leaving ~3 weeks with no in-app
push, and the value moment (first few queries) goes unconverted.

Add TrialValueUpsell — a calm, value-framed banner that fires on PROVEN VALUE
(user has decoded >= 3 customer vehicles, read server-truth via /vehicles/history)
and only OUTSIDE the urgency window (days > 7), so it fills the gap without ever
overlapping or competing with the urgency banner. B2B copy: frames around
"müşteri araçlarını sınırsız sorgulama", never personal-vehicle wording.
Tracks viewed/cta_clicked/dismissed in PostHog; dismissal persists per trial.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 20:33:10 +03:00
cd54cf9443 fix(observability): harden Sentry init for catalog-degradation reporting
- initSentry() is now idempotent via a cached promise, and the degradation
  reporter / feedback opener await it — so an empty/error state that renders
  before the SDK's dynamic import resolves no longer drops the event (not missing
  the failure is the whole point of the auto-capture).
- environment now resolves hostname-first (sase.tr → production, dev.sase.tr →
  staging), so the compose-baked VITE_SENTRY_ENVIRONMENT default ("production")
  no longer makes dev browser events indistinguishable from prod.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 19:32:22 +03:00
f02f97f69f fix(payments): prevent paid-but-not-activated on Stripe webhook retry
handleCheckoutCompleted marked the payment 'completed' BEFORE activating the
subscription, but the idempotency guard keys off payment.status === 'completed'.
So if activateSubscription threw after the payment was marked completed, the
Stripe webhook retry would hit the guard, skip activation, and leave a
paid-but-never-activated subscription.

Fix: activate FIRST, then mark completed — a mid-activation failure leaves the
payment 'pending' so the retry re-runs cleanly. Guard activateSubscription with
an idempotency check (return early when already active) so a retry after a
successful activate but failed mark-completed cannot re-fire revenue events
(PostHog subscription_activated + Meta CAPI Purchase), re-consume referral
credit, or double-insert Full-plan brands. Admin manual-activate already
pre-rejects 'active', so legitimate first activations are unaffected.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 19:30:15 +03:00
11975d8b5a feat(payments): remove EFT/havale, Stripe-only checkout + Turkish locale
EFT/Havale was retired; Stripe is now the sole payment method. Remove the
EFT code surface (shared PaymentMethod "eft" + EftPaymentInput + eftReceiptUrl,
EFT_RECEIPT_REQUIRED error code, billing UI receipt/filter/label paths,
payments.service eft read paths). DB columns (eft_receipt_url, bank_account_id,
bank_accounts) are kept and marked @deprecated to preserve historical records
and avoid a destructive migration — same pattern as the retired iyzico column.

Faz 3 conversion lever: set locale "tr" on the Stripe Checkout session. The
audience is Turkish B2B and ~60% of sessions reached the foreign-language
hosted page but never started a payment intent (pure abandonment, not decline).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 19:17:10 +03:00
d6c88ede73 feat(observability): session-replay + in-app feedback on catalog UX failures
The two things only the browser can add on top of the backend catalog-degradation
reporting:

- Replay-on-failure: when the catalog UI renders empty-tree (decoded vehicle, no
  categories) or a drill loadError, capture a browser Sentry warning and flush the
  Session Replay → you can WATCH the user hit the dead-end (serkan's session,
  reproducible). Per-session deduped (one replay/session covers the whole journey).
- In-app feedback: a "Çalışmadı mı? Bildir" button on the empty-parts, empty-tree
  and loadError states opens the Sentry feedback dialog pre-tagged with the
  vehicle/category (+ session replay) — turns a parts shop's complaint into a
  structured, triageable report instead of an email.

Browser events are fingerprinted source="browser" so they form their own
"what users actually saw" issues (carrying replays) next to the server-side
detections. tsc + biome + web build clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-09 19:07:49 +03:00
224 changed files with 21717 additions and 1822 deletions

View File

@@ -21,6 +21,14 @@ COPY --from=deps /app/apps/web/node_modules ./apps/web/node_modules
COPY --from=deps /app/packages/shared/node_modules ./packages/shared/node_modules COPY --from=deps /app/packages/shared/node_modules ./packages/shared/node_modules
COPY --from=deps /app/packages/config/node_modules ./packages/config/node_modules COPY --from=deps /app/packages/config/node_modules ./packages/config/node_modules
COPY --from=deps /app/packages/ui/node_modules ./packages/ui/node_modules COPY --from=deps /app/packages/ui/node_modules ./packages/ui/node_modules
# Cache-bust: web-only commits were sticking to a cached build layer, so
# auto-deploys shipped a stale bundle (only --no-cache/force rebuilds picked
# them up). Referencing the commit SHA *before* the source COPY makes this layer
# (and therefore COPY + `pnpm build`) invalidate on every commit; the deps stage
# above stays cached on the lockfile, so installs aren't repeated.
ARG SOURCE_COMMIT=unknown
RUN echo "Building commit ${SOURCE_COMMIT}"
COPY . . COPY . .
# Vite env vars (baked at build time) # Vite env vars (baked at build time)
@@ -30,6 +38,8 @@ ARG VITE_POSTHOG_KEY=
ARG VITE_META_PIXEL_ID= ARG VITE_META_PIXEL_ID=
ARG VITE_CHATWOOT_BASE_URL= ARG VITE_CHATWOOT_BASE_URL=
ARG VITE_CHATWOOT_WEBSITE_TOKEN= ARG VITE_CHATWOOT_WEBSITE_TOKEN=
ARG VITE_GOOGLE_ADS_ID=
ARG VITE_GOOGLE_ADS_SIGNUP_LABEL=
RUN pnpm build RUN pnpm build

127
PROJECT_INDEX.json Normal file
View File

@@ -0,0 +1,127 @@
{
"project": "sase-v2 (sase.tr)",
"description": "Turkish B2B SaaS: VIN decode + OEM parts-catalog platform for parts dealers & repair shops",
"generated": "2026-06-10",
"generator": "/sc:index-repo",
"humanReadable": "PROJECT_INDEX.md",
"stack": {
"packageManager": "pnpm@10.29.3",
"node": "22 (alpine in Docker)",
"monorepo": "pnpm workspaces + turbo",
"language": "TypeScript",
"lint": "biome"
},
"workspaces": {
"apps/api": { "name": "api", "framework": "NestJS ^10.4", "role": "REST API :4000 + BullMQ worker (worker.ts)" },
"apps/web": { "name": "web", "framework": "React ^19 + TanStack Router ^1.120 + Vite", "role": "SPA dashboard + marketing pages" },
"packages/shared": { "name": "@sase/shared", "role": "zod schemas, types, constants (plans, error-codes, regex), utils (vin-validator, formatters, currency)" },
"packages/config": { "name": "@sase/config", "role": "shared config" },
"packages/ui": { "name": "@sase/ui", "role": "shadcn-style component library" }
},
"entryPoints": {
"api": "apps/api/src/main.ts",
"worker": "apps/api/src/worker.ts",
"web": "apps/web/src/main.tsx",
"migrations": "apps/api/src/database/migrate.ts"
},
"apiModules": {
"vehicles": "VIN decode orchestration; fallback chain Corgi→PartsCatalogs→PL24→EMEX→NHTSA; slim response + Redis candidate stash (vin-cache-keys.ts)",
"integrations/pl24": "PartsLink24 — shared P4 core + brand services: pl24.service, pl24-psa, pl24-volvo, pl24-ford, pl24-hyundai-kia, pl24-opel, pl24-ford-legacy, pl24-auth, parsers/",
"integrations/emex": "emexdwc.ae scrape — HTTP (undici ProxyAgent) primary, Playwright fallback (emex.browser.ts), emex-tree.parser",
"integrations/parts-catalogs": "PartsCatalogs API (pcat)",
"integrations/p": "OEM cross-reference 'P' — TecDoc-derived MySQL (P_DB_* env): p.controller, p-source-db.service",
"integrations/corgi": "offline WMI lookup (instant)",
"integrations/vin-api": "NHTSA fallback decoder",
"integrations/catalog-source-db": "upstream source-DB bridge",
"catalog": "catalog browse — catalog/pcat-catalog/emex-catalog controllers+services",
"categories": "category tree: partCount rollup, lazy drill/heal, PSA parent gates",
"parts": "part listings",
"brands": "brand canonicalization (case-insensitive lookup)",
"auth": "better-auth cookie sessions",
"users": "user management",
"referrals": "referral program",
"payments": "Stripe",
"subscriptions": "subscription lifecycle",
"plans": "plan definitions",
"jobs": "BullMQ — queues: catalog-prefetch, emex-scrape, translation, lifecycle-email, subscription-expiry, query-cleanup; processors mirror queues; prefetch-worker.service",
"posthog": "server-side feature-flag eval + kill-source-* switches",
"meta-capi": "Meta Conversions API (event_id dedup, fail-open)",
"notifications": "Novu lifecycle triggers",
"email": "Postal HTTP API ops mail",
"chatwoot": "support-desk integration",
"analytics": "product analytics endpoints",
"telemetry": "OpenTelemetry → OTLP exporters",
"common": "shared infra incl. catalog-degradation → Sentry (fingerprinted, Redis-deduped)",
"redis": "ioredis provider",
"storage": "S3/MinIO",
"translations": "OpenAI-backed translation",
"admin": "admin endpoints",
"internal-admin": "internal admin",
"blog": "blog content",
"changelog": "changelog content",
"demo": "public demo flow",
"contact": "contact form",
"config": "configuration.ts + env.validation.ts (zod; empty-string trap)",
"database": "Drizzle provider; schema split: core, pl24, emex, parts-catalogs, relations"
},
"webRoutes": {
"public": ["index", "pricing", "blog", "blog_/$slug", "demo", "demo_/categories_/$categoryId", "about", "contact", "kvkk", "privacy", "terms"],
"auth": ["login", "register", "forgot-password", "reset-password", "email-verified"],
"dashboard": [
"index", "search", "history", "settings", "billing", "changelog",
"vehicles_/$id", "vehicles_/$id/categories_/$categoryId",
"catalog/index", "catalog_/$brandName", "catalog_/$brandName_/$modelId", "catalog_/$brandName_/$modelId/categories_/$categoryId",
"catalog_/pcat/$catalogId[/_$modelId/_$carId[/groups/$groupId]]",
"catalog_/emex/$catalogCode[/_$vehicleId[/groups/$groupId]]",
"oem.$code", "subscription", "service-test"
],
"admin": ["admin/index", "admin/users", "admin/analytics", "admin/referrals", "admin/copy-logs"]
},
"webLib": ["api-client", "auth-client", "posthog", "surveys", "meta-pixel", "faro", "sentry", "chatwoot", "i18n", "vehicle", "toast", "user-settings", "keys", "category-icons", "emex-group-hierarchy", "recently-used-brands"],
"configFiles": {
"turbo.json": "task pipeline",
"biome.json": "lint/format",
"docker-compose.coolify.yml": "prod/staging: api + worker + sase-redis; env vars must be referenced as ${VAR} per service block",
"Dockerfile": "multi-stage node:22-alpine + Chromium; runtime imports must be in dependencies",
"ecosystem.config.js": "PM2",
"apps/api/src/config/env.validation.ts": "zod env gate"
},
"database": {
"primary": "PostgreSQL via drizzle-orm ^0.41",
"migrations": "apps/api/drizzle/ — 14 SQL files (00000013) + meta/_journal.json; custom SHA256-tracked runner (src/database/migrate.ts)",
"schemas": ["core", "pl24", "emex", "parts-catalogs", "relations"],
"secondary": "MySQL (mysql2) — TecDoc-derived 'P' OEM cross-ref db",
"cache": "Redis (sase-redis container) — BullMQ + VIN candidate stash + dedup"
},
"tests": {
"api": { "framework": "vitest", "count": 30, "pattern": "*.spec.ts", "run": "pnpm --filter api test" },
"web": { "framework": "vitest (no Playwright e2e)", "count": 24, "pattern": "*.test.ts(x)", "run": "pnpm --filter web test" },
"qa": "qa/post-deploy smoke checks"
},
"keyDependencies": {
"api": ["@nestjs/* ^10.4", "drizzle-orm + postgres", "better-auth", "bullmq + ioredis", "stripe", "posthog-node", "openai", "undici", "@sentry/nestjs", "@opentelemetry/*", "mysql2", "zod"],
"web": ["react ^19", "@tanstack/react-router + react-query", "zustand", "posthog-js", "@grafana/faro-web-sdk", "@sentry/react", "remotion", "better-auth", "zod"]
},
"commands": {
"install": "pnpm install",
"dev": "pnpm dev",
"build": "pnpm build",
"lint": "pnpm lint",
"typecheck": "pnpm typecheck",
"test": "pnpm test",
"dbMigrate": "pnpm --filter api db:migrate",
"dbStudio": "pnpm --filter api db:studio"
},
"deployment": {
"platform": "Coolify (cool.semih.ai)",
"staging": { "branch": "dev", "fqdn": "dev.sase.tr", "rule": "verify every change here first" },
"production": { "branch": "main", "fqdn": "sase.tr", "rule": "promote only after dev verified" },
"policy": "never edit on server/container; push-to-deploy via Gitea webhooks"
},
"docs": {
"assistant": ["CLAUDE.md", "AGENTS.md"],
"internal": "docs/ (INDEX.md, 00-testing.md, design-specs/, pl24-catalog/, product/, clarification/)",
"rootReports": ["upstream-prob.md", "undecoded-vin-rca.md", "posthog-max-leverage.md", "posthog-feature-roadmap.md", "katalogayapi.md", "knowledge.md", "ralpRapor.md", "emex-hierarchy-plan.md", "MANIFEST.md", "MANIFEST-FN-403.md", "RECOVERY-FN-412.md", "SCOPING-FN-411.md", "SASE_PRD_Hesap_Paylasimi_Onleme_v1.1.md"],
"bizWorkspace": "/home/s/ss/INDEX.md"
}
}

88
PROJECT_INDEX.md Normal file
View File

@@ -0,0 +1,88 @@
# Project Index: sase.tr (sase-v2)
Generated: 2026-06-10 · regenerate via `/sc:index-repo` · machine-readable twin: `PROJECT_INDEX.json`
Turkish B2B SaaS: VIN decode + OEM parts-catalog platform for parts dealers & repair shops.
pnpm 10.29.3 workspaces + Turbo · Node 22 · TypeScript · Biome.
## Structure
```
apps/api NestJS 10 backend — REST API (:4000) + separate BullMQ worker
apps/web React 19 + TanStack Router/Query + Vite SPA (Remotion video, i18n)
packages/shared @sase/shared — zod schemas, types, constants (plans, error-codes), utils (vin-validator, formatters)
packages/config @sase/config — shared config
packages/ui @sase/ui — shadcn-style components
scripts/ one-off ops/debug (emex scrapers, backfills, deploy.sh, pl24-catalog-explorer)
qa/post-deploy post-deploy smoke checks
docs/ internal docs — see docs/INDEX.md (testing, design-specs, pl24-catalog, product)
docker/ local dev compose (minio, nginx)
```
## Entry Points
- `apps/api/src/main.ts` — API bootstrap (also serves built web via ServeStatic; no nginx in prod)
- `apps/api/src/worker.ts` — BullMQ worker process (separate container)
- `apps/api/src/database/migrate.ts` — custom SHA256-tracked migration runner over `apps/api/drizzle/` (14 SQL migrations + `meta/_journal.json`)
- `apps/web/src/main.tsx` — SPA bootstrap (`routeTree.gen.ts` generated by `tsr generate`)
## API Modules (`apps/api/src/`)
- **vehicles/** — VIN decode orchestration; fallback chain Corgi → PartsCatalogs → PL24 → EMEX → NHTSA; slim decode response + Redis candidate stash (`vin-cache-keys.ts`)
- **integrations/pl24/** — PartsLink24: shared P4 core + per-brand services (`psa`, `volvo`, `ford`, `hyundai-kia`, `opel`, `ford-legacy`) + `parsers/`
- **integrations/emex/** — emexdwc.ae scrape: HTTP via undici ProxyAgent, Playwright fallback (`emex.browser.ts`), tree parser
- **integrations/parts-catalogs/** — PartsCatalogs API ("pcat")
- **integrations/p/** — OEM cross-reference ("P", TecDoc-derived MySQL db; `P_DB_*` env)
- **integrations/corgi/**, **vin-api/** — offline WMI lookup; NHTSA fallback
- **integrations/catalog-source-db/** — upstream source-DB bridge
- **catalog/** — catalog browse endpoints (`catalog`, `pcat-catalog`, `emex-catalog` controllers/services)
- **categories/**, **parts/**, **brands/** — category tree (partCount rollup, lazy drill/heal), parts, brand canonicalization
- **auth/** (better-auth cookie sessions), **users/**, **referrals/**
- **payments/** (Stripe), **subscriptions/**, **plans/**
- **jobs/** — BullMQ queues + processors: `catalog-prefetch`, `emex-scrape`, `translation`, `lifecycle-email`, `subscription-expiry`, `query-cleanup`
- **posthog/** — server-side flag eval + `kill-source-*` switches · **meta-capi/** — Meta Conversions API
- **notifications/** (Novu), **email/** (Postal), **chatwoot/**
- **analytics/**, **telemetry/** (OTel → OTLP), **common/** (incl. catalog-degradation → Sentry), **redis/**, **storage/** (S3/MinIO), **translations/** (OpenAI), **admin/**, **internal-admin/**, **blog/**, **changelog/**, **demo/**, **contact/**
- **config/** — `env.validation.ts` zod gate (beware `""` vs undefined), `configuration.ts`
- **database/** — Drizzle provider + `schema/` split: `core`, `pl24`, `emex`, `parts-catalogs`, `relations`
## Web (`apps/web/src/`)
- **routes/** — public: `index`, `pricing`, `blog`, `demo`, `about/contact/kvkk/privacy/terms` · `_auth/`: login, register, forgot/reset password, email-verified · `dashboard/`: `search` (main VIN flow), `vehicles_/$id[/categories_/$categoryId]`, catalog trees (`catalog_/$brandName/...`, `catalog_/pcat/...`, `catalog_/emex/...`), `oem.$code` (cross-ref), `subscription`, `billing`, `history`, `settings`, `service-test` (admin), `admin/*` (users, analytics, referrals, copy-logs)
- **lib/** — `api-client`, `auth-client`, `posthog`, `surveys` (API-mode survey renderer), `meta-pixel`, `faro`, `sentry`, `chatwoot`, `i18n`, `vehicle`
- **stores/** (zustand), **components/**, **remotion/**, **messages/** (i18n), **hooks/**
## Configuration
- `turbo.json`, `biome.json`, `tsconfig.json` — build/lint/typecheck pipeline
- `docker-compose.coolify.yml` — prod/staging: `api` + `worker` + `sase-redis` (Coolify env reaches a service ONLY if its `environment:` block references `${VAR}`)
- `Dockerfile` — multi-stage node:22-alpine + Chromium (runtime imports MUST be in `dependencies`)
- `ecosystem.config.js` — PM2 config
## Tests
- API: 30 `*.spec.ts` (Vitest) → `pnpm --filter api test`
- Web: 24 `*.test.ts(x)` (Vitest only; **no Playwright e2e**) → `pnpm --filter web test`; plus per-package `index.spec.ts`
- `qa/post-deploy/` smoke checks
## Key Dependencies
NestJS 10 · drizzle-orm 0.41 + postgres · better-auth · bullmq + ioredis · stripe · posthog-node · openai (translations) · undici (proxied fetch — global fetch lacks `dispatcher`) · Sentry + OpenTelemetry · mysql2 (P/TecDoc) ·· React 19 · TanStack Router/Query · zustand · posthog-js · @grafana/faro · remotion · zod throughout.
## Quick Start
1. `pnpm install`
2. `pnpm dev` (turbo: api :4000 + web :3000)
3. `pnpm lint` / `pnpm typecheck` / `pnpm test`
4. DB: `pnpm --filter api db:migrate` (custom runner) · `db:studio` · `db:seed`
## Deployment (Coolify — NEVER edit on server)
- Push `dev` → auto-deploys staging **dev.sase.tr** — verify every change here first
- Merge to `main` → auto-deploys prod **sase.tr** (only after dev verified)
## Docs Map
- `CLAUDE.md` / `AGENTS.md` — assistant instructions · `docs/INDEX.md` — internal docs index
- Root reports: `upstream-prob.md`, `undecoded-vin-rca.md`, `posthog-max-leverage.md`, `posthog-feature-roadmap.md`, `katalogayapi.md`, `knowledge.md`, `ralpRapor.md`, `emex-hierarchy-plan.md`, `MANIFEST*.md`, `RECOVERY-FN-412.md`, `SCOPING-FN-411.md`, `SASE_PRD_Hesap_Paylasimi_Onleme_v1.1.md`
- Business/ops workspace knowledge base: `/home/s/ss/INDEX.md`

View File

@@ -0,0 +1,28 @@
-- Canonicalise vehicle brand casing + backfill missing brand_id.
--
-- Root cause: the decode persist (vehicles.service.ts) looked brands up with a
-- case-SENSITIVE `eq(brands.name, brandName)`. A decode source that yields an
-- uppercase brand string (e.g. "FORD", "MERCEDES-BENZ") missed the canonical
-- "Ford" / "Mercedes-Benz" row → brand_id stayed NULL and the raw uppercase
-- string was stored as brand_name. That split the same brand across casing
-- variants in analytics + the catalog ("Ford" vs "FORD"). The companion code
-- fix makes the lookup case-insensitive and stores the canonical name; this
-- migration repairs the rows already written.
--
-- Backfill every vehicle whose brand_name matches a canonical brand
-- case-insensitively: set the FK and the canonical-cased name. (catalog_vehicles
-- is intentionally untouched — its brand_name namespace does not match the
-- brands table, so 0 rows would qualify.)
UPDATE "vehicles" v
SET brand_id = b.id, brand_name = b.name
FROM "brands" b
WHERE v.brand_id IS NULL AND lower(v.brand_name) = lower(b.name);
--> statement-breakpoint
-- Defensive: realign any vehicle whose brand_id is set but whose denormalised
-- brand_name has drifted from the canonical brands.name (none today, but keeps
-- the FK and the denormalised name consistent going forward).
UPDATE "vehicles" v
SET brand_name = b.name
FROM "brands" b
WHERE v.brand_id = b.id AND v.brand_name <> b.name;

View File

@@ -0,0 +1,23 @@
-- Proxy telemetry: one row per proxied upstream attempt (pcat call/capture/
-- validate, emex http). Lets the Süper Panel grade proxy providers per service
-- (success rate, ban rate, latency) and track banned residential exit IPs /
-- sticky sessions. Written fire-and-forget by ProxyTelemetryService; 30-day
-- retention enforced by the query-cleanup job.
CREATE TABLE "proxy_logs" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"service" varchar(30) NOT NULL,
"provider" varchar(20) NOT NULL,
"session_key" varchar(60),
"exit_ip" varchar(45),
"target_host" varchar(120),
"status_code" integer,
"error_kind" varchar(30),
"success" boolean DEFAULT false NOT NULL,
"banned" boolean DEFAULT false NOT NULL,
"duration_ms" integer,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE INDEX "proxy_logs_created_at_idx" ON "proxy_logs" ("created_at");
--> statement-breakpoint
CREATE INDEX "proxy_logs_banned_created_at_idx" ON "proxy_logs" ("banned","created_at");

View File

@@ -0,0 +1,37 @@
-- OEM community voting + gamification ledger.
-- oem_votes: one row per (user, oem_code) uyumlu/uyumsuz verdict cast from the
-- catalog part rows. Votes pool globally per code; part/vehicle/category are
-- analytics breadcrumbs only (catalog routes pass catalog_vehicles ids → no FK,
-- mirrors oem_code_copies). Re-votes update the row in place.
-- oem_vote_points: append-only, exactly one row per vote (vote_id unique),
-- written in the same transaction. points = 1 (vote) + 2 (agreed with strict
-- majority at cast time; first voter always 3). Never retro-adjusted.
CREATE TABLE "oem_votes" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"user_id" uuid NOT NULL,
"oem_code" varchar(100) NOT NULL,
"vote" varchar(12) NOT NULL,
"part_id" uuid,
"vehicle_id" uuid,
"category_id" uuid,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "oem_vote_points" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"user_id" uuid NOT NULL,
"vote_id" uuid NOT NULL,
"oem_code" varchar(100) NOT NULL,
"points" integer NOT NULL,
"correct" boolean DEFAULT false NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "oem_votes" ADD CONSTRAINT "oem_votes_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "oem_vote_points" ADD CONSTRAINT "oem_vote_points_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "oem_vote_points" ADD CONSTRAINT "oem_vote_points_vote_id_oem_votes_id_fk" FOREIGN KEY ("vote_id") REFERENCES "public"."oem_votes"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "oem_votes_user_oem_idx" ON "oem_votes" USING btree ("user_id","oem_code");--> statement-breakpoint
CREATE INDEX "oem_votes_oem_code_idx" ON "oem_votes" USING btree ("oem_code");--> statement-breakpoint
CREATE UNIQUE INDEX "oem_vote_points_vote_id_idx" ON "oem_vote_points" USING btree ("vote_id");--> statement-breakpoint
CREATE INDEX "oem_vote_points_user_id_idx" ON "oem_vote_points" USING btree ("user_id");

View File

@@ -0,0 +1,18 @@
-- Community OEM cross-reference suggestions, collected on the OEM detail page
-- ("muadil parça öner": brand + code). Primarily for codes the P snapshot has
-- no match for. suggested_code_norm (upper alphanumerics) powers grouping and
-- the per-user dedup; status is a moderation hook (active|hidden).
CREATE TABLE "oem_suggestions" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"user_id" uuid NOT NULL,
"oem_code" varchar(100) NOT NULL,
"brand" varchar(80) NOT NULL,
"suggested_code" varchar(100) NOT NULL,
"suggested_code_norm" varchar(100) NOT NULL,
"status" varchar(16) DEFAULT 'active' NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "oem_suggestions" ADD CONSTRAINT "oem_suggestions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "oem_suggestions_user_oem_code_idx" ON "oem_suggestions" USING btree ("user_id","oem_code","suggested_code_norm");--> statement-breakpoint
CREATE INDEX "oem_suggestions_oem_code_idx" ON "oem_suggestions" USING btree ("oem_code");

View File

@@ -0,0 +1,18 @@
-- Monthly Parça Uzmanları prizes: one row per (TR-month, rank). Written by the
-- expert-rewards cron (1st of month 00:00 Europe/Istanbul) when it closes the
-- finished season and grants the top 3 voters a subscription extension
-- (30/15/7 days). The unique index is the run-once guard — a re-run for the
-- same period inserts nothing, so days are never granted twice.
CREATE TABLE "oem_expert_rewards" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"period_start" timestamp with time zone NOT NULL,
"user_id" uuid NOT NULL,
"rank" integer NOT NULL,
"points" integer NOT NULL,
"reward_days" integer NOT NULL,
"granted_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "oem_expert_rewards" ADD CONSTRAINT "oem_expert_rewards_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "oem_expert_rewards_period_rank_idx" ON "oem_expert_rewards" USING btree ("period_start","rank");--> statement-breakpoint
CREATE INDEX "oem_expert_rewards_user_id_idx" ON "oem_expert_rewards" USING btree ("user_id");

View File

@@ -0,0 +1,25 @@
-- Tedarikçi fiyat geçmişi (parça kodu bazlı, takip MySQL'den beslenir).
-- part_price_tracks: hangi kod izleniyor (ilk seriyi API lazy-backfill yazar,
-- günlük cron sadece buradaki kodları tazeler). part_price_daily: (kod, kaynak,
-- gün) başına stoktaki tekliflerin p50/p95/p99'u + teklif sayısı. source şimdilik
-- hep 'supplier'; perakende verisi geldiğinde aynı tabloya 'retail' olarak girer.
-- Tedarikçi kimliği bilinçli olarak HİÇBİR kolonda yok.
CREATE TABLE "part_price_tracks" (
"code_norm" varchar(64) PRIMARY KEY NOT NULL,
"first_seen_at" timestamp with time zone DEFAULT now() NOT NULL,
"backfilled_at" timestamp with time zone,
"last_refreshed_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "part_price_daily" (
"code_norm" varchar(64) NOT NULL,
"source" varchar(16) DEFAULT 'supplier' NOT NULL,
"date" date NOT NULL,
"p50" numeric(15, 4),
"p95" numeric(15, 4),
"p99" numeric(15, 4),
"offer_count" integer DEFAULT 0 NOT NULL,
CONSTRAINT "part_price_daily_code_norm_source_date_pk" PRIMARY KEY("code_norm","source","date")
);
--> statement-breakpoint
CREATE INDEX "part_price_daily_code_date_idx" ON "part_price_daily" USING btree ("code_norm","date");

View File

@@ -0,0 +1,32 @@
-- Parça fiyat anahtarına marka eklendi: kısa sayısal kodlar (FEBI 27155 vs
-- GROS 27155 vs İBRAŞ 27155) markalar arası çakışıyor ve FARKLI fiziksel
-- parçaların fiyatları tek havuzda karışıyordu. Kimlik artık (kod, marka);
-- markasız izleme yalnızca uzun/benzersiz kodlar için ('' brand_norm).
-- Tablolar 0018'den bu yana yalnızca dev'de üç test kodu içeriyordu — veri
-- taşımak yerine drop+create (lazy-backfill ilk görüntülemede yeniden doldurur).
DROP TABLE IF EXISTS "part_price_daily";
--> statement-breakpoint
DROP TABLE IF EXISTS "part_price_tracks";
--> statement-breakpoint
CREATE TABLE "part_price_tracks" (
"code_norm" varchar(64) NOT NULL,
"brand_norm" varchar(64) DEFAULT '' NOT NULL,
"first_seen_at" timestamp with time zone DEFAULT now() NOT NULL,
"backfilled_at" timestamp with time zone,
"last_refreshed_at" timestamp with time zone,
CONSTRAINT "part_price_tracks_code_norm_brand_norm_pk" PRIMARY KEY("code_norm","brand_norm")
);
--> statement-breakpoint
CREATE TABLE "part_price_daily" (
"code_norm" varchar(64) NOT NULL,
"brand_norm" varchar(64) DEFAULT '' NOT NULL,
"source" varchar(16) DEFAULT 'supplier' NOT NULL,
"date" date NOT NULL,
"p50" numeric(15, 4),
"p95" numeric(15, 4),
"p99" numeric(15, 4),
"offer_count" integer DEFAULT 0 NOT NULL,
CONSTRAINT "part_price_daily_code_norm_brand_norm_source_date_pk" PRIMARY KEY("code_norm","brand_norm","source","date")
);
--> statement-breakpoint
CREATE INDEX "part_price_daily_code_date_idx" ON "part_price_daily" USING btree ("code_norm","brand_norm","date");

View File

@@ -0,0 +1,17 @@
-- Stripe recurring billing (Checkout mode:"subscription") bağlantı kolonları.
-- users.stripe_customer_id → sonraki checkout'lar aynı Stripe Customer'da
-- toplanır (kayıtlı kart + fatura geçmişi tek kayıtta).
-- user_subscriptions.stripe_subscription_id → yenileme faturaları
-- (invoice.paid/payment_failed webhook'ları) bizim abonelik satırına bu
-- kolondan çözülür; trial ve eski tek-seferlik satırlarda NULL kalır.
-- payments.stripe_invoice_id → invoice.paid webhook retry'larında mükerrer
-- gelir kaydını önleyen dedupe anahtarı.
ALTER TABLE "users" ADD COLUMN "stripe_customer_id" text;
--> statement-breakpoint
ALTER TABLE "user_subscriptions" ADD COLUMN "stripe_subscription_id" text;
--> statement-breakpoint
ALTER TABLE "payments" ADD COLUMN "stripe_invoice_id" text;
--> statement-breakpoint
CREATE INDEX "user_subscriptions_stripe_sub_idx" ON "user_subscriptions" USING btree ("stripe_subscription_id");
--> statement-breakpoint
CREATE INDEX "payments_stripe_invoice_id_idx" ON "payments" USING btree ("stripe_invoice_id");

View File

@@ -0,0 +1,30 @@
-- Vinpin ePER decode-oracle cache (feature-flagged: VINPIN_ENABLED).
-- One row per VIN. The vinpin-decode BullMQ job drives the Vinpin ePER web
-- catalog for Fiat VINs the normal chain (PL24/pcat/emex) can't identify, then
-- matches the decoded model to an EXISTING PL24 catalog_vehicle. Vinpin is ONLY
-- a decode oracle — parts are always served from PL24's existing catalog.
-- status: 'pending' | 'decoded' | 'not_found' | 'failed'.
CREATE TABLE IF NOT EXISTS "vinpin_decodes" (
"vin" text PRIMARY KEY NOT NULL,
"status" text DEFAULT 'pending' NOT NULL,
"brand_name" text,
"model" text,
"sincom" text,
"trim" text,
"model_year" text,
"engine" text,
"catalog_vehicle_id" uuid,
"raw" jsonb,
"attempts" integer DEFAULT 0 NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone,
"decoded_at" timestamp with time zone
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "vinpin_decodes" ADD CONSTRAINT "vinpin_decodes_catalog_vehicle_id_catalog_vehicles_id_fk" FOREIGN KEY ("catalog_vehicle_id") REFERENCES "public"."catalog_vehicles"("id") ON DELETE set null ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "vinpin_decodes_status_idx" ON "vinpin_decodes" USING btree ("status");

View File

@@ -0,0 +1,34 @@
-- Discount codes for checkout promotions. Each row mirrors a Stripe Coupon +
-- Promotion Code; Stripe enforces the redemption cap / expiry at checkout while
-- this table powers the admin panel's listing + usage tracking and lets us
-- preview a code without a per-render Stripe call. Amounts in kuruş (TRY
-- smallest unit), matching plans.price* / payments.amount.
CREATE TABLE IF NOT EXISTS "discount_codes" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"code" varchar(50) NOT NULL,
"stripe_coupon_id" text,
"stripe_promotion_code_id" text,
"discount_type" varchar(12) NOT NULL,
"percent_off" integer,
"amount_off" integer,
"duration" varchar(12) DEFAULT 'once' NOT NULL,
"duration_in_months" integer,
"max_redemptions" integer,
"times_redeemed" integer DEFAULT 0 NOT NULL,
"expires_at" timestamp with time zone,
"is_active" boolean DEFAULT true NOT NULL,
"note" text,
"created_by" uuid,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "discount_codes" ADD CONSTRAINT "discount_codes_created_by_users_id_fk"
FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE set null;
EXCEPTION WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "discount_codes_code_idx" ON "discount_codes" USING btree ("code");
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "discount_codes_active_idx" ON "discount_codes" USING btree ("is_active");

View File

@@ -0,0 +1,38 @@
-- Public API anahtarları — better-auth `apiKey` plugin'inin tablosu (model
-- "apikey", usePlural → "apikeys"). Kolon seti plugin şemasının birebir
-- karşılığı; "key" kolonu secret'ın SHA256 hash'i (düz metin sadece üretim
-- anında bir kez gösterilir). "metadata" JSON string (dailyDecodeLimit gibi
-- anahtar-bazlı limitler burada taşınır).
CREATE TABLE IF NOT EXISTS "apikeys" (
"id" text PRIMARY KEY NOT NULL,
"name" text,
"start" text,
"prefix" text,
"key" text NOT NULL,
"user_id" uuid NOT NULL,
"refill_interval" integer,
"refill_amount" integer,
"last_refill_at" timestamp with time zone,
"enabled" boolean DEFAULT true NOT NULL,
"rate_limit_enabled" boolean DEFAULT true NOT NULL,
"rate_limit_time_window" integer,
"rate_limit_max" integer,
"request_count" integer DEFAULT 0 NOT NULL,
"remaining" integer,
"last_request" timestamp with time zone,
"expires_at" timestamp with time zone,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
"permissions" text,
"metadata" text
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "apikeys" ADD CONSTRAINT "apikeys_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "apikeys_key_idx" ON "apikeys" ("key");
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "apikeys_user_id_idx" ON "apikeys" ("user_id");

View File

@@ -0,0 +1,70 @@
-- Unified cross-source parts taxonomy. The raw per-source category trees
-- (categories.parent_id chains + pl24/emex/pcat mirrors) are left untouched;
-- this adds an orthogonal canonical spine so every manufacturer's OEM microfiche
-- can be browsed/searched under one set of headings. See schema/core.ts for the
-- design rationale (name-level mapping, not row-level).
CREATE TABLE IF NOT EXISTS "canonical_categories" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"parent_id" uuid,
"slug" varchar(100) NOT NULL,
"name" varchar(200) NOT NULL,
"sort_order" integer DEFAULT 0 NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "canonical_categories_slug_idx" ON "canonical_categories" ("slug");
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "category_canonical_map" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"source" varchar(20) NOT NULL,
"normalized_name" varchar(500) NOT NULL,
"canonical_category_id" uuid NOT NULL,
"method" varchar(20) DEFAULT 'keyword' NOT NULL,
"confidence" numeric(4,3),
"is_manual" boolean DEFAULT false NOT NULL,
"ambiguous" boolean DEFAULT false NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "category_canonical_map" ADD CONSTRAINT "category_canonical_map_canonical_id_fk"
FOREIGN KEY ("canonical_category_id") REFERENCES "public"."canonical_categories"("id") ON DELETE cascade;
EXCEPTION WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "category_canonical_map_source_name_idx" ON "category_canonical_map" ("source", "normalized_name");
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "category_canonical_map_canonical_idx" ON "category_canonical_map" ("canonical_category_id");
--> statement-breakpoint
ALTER TABLE "categories" ADD COLUMN IF NOT EXISTS "canonical_category_id" uuid;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "categories_canonical_category_id_idx" ON "categories" ("canonical_category_id");
--> statement-breakpoint
-- Seed the top-level canonical taxonomy (18 buckets: aloparca's 17 retail tops
-- + "İç Döşeme ve Koltuk" which retail sites don't carry but OEM catalogs do —
-- ~16% of parts). Idempotent via ON CONFLICT on the unique slug.
INSERT INTO "canonical_categories" ("slug", "name", "sort_order") VALUES
('motor', 'Motor', 1),
('yakit-sistemi', 'Yakıt Sistemi', 2),
('atesleme-sistemi', 'Ateşleme Sistemi', 3),
('triger-zincir-rulman', 'Triger, Zincir ve Rulman', 4),
('sogutma', 'Soğutma ve Isıtma', 5),
('filtreler', 'Filtreler', 6),
('fren', 'Fren', 7),
('debriyaj', 'Debriyaj', 8),
('sanziman', 'Şanzıman ve Diferansiyel', 9),
('suspansiyon', 'Süspansiyon', 10),
('direksiyon', 'Direksiyon', 11),
('elektrik', 'Elektrik', 12),
('aydinlatma', 'Aydınlatma', 13),
('dis-kaporta', 'Dış Kaporta Aksamı', 14),
('tampon', 'Tampon ve Aksamı', 15),
('panjur-panel', 'Panjur ve Panel', 16),
('ic-doseme', 'İç Döşeme ve Koltuk', 17),
('yaglar-sivilar', 'Yağlar ve Sıvılar', 18)
ON CONFLICT ("slug") DO NOTHING;

View File

@@ -0,0 +1,7 @@
-- Two more canonical top buckets the OEM catalogs need but the retail-derived
-- seed lacked: "Jant ve Lastik" (pcat "Jantlar, Lastikler" appears on ~every
-- vehicle) and "Aksesuar". Idempotent via the unique slug.
INSERT INTO "canonical_categories" ("slug", "name", "sort_order") VALUES
('jant-lastik', 'Jant ve Lastik', 19),
('aksesuar', 'Aksesuar', 20)
ON CONFLICT ("slug") DO NOTHING;

View File

@@ -0,0 +1,294 @@
-- 0026: second-level canonical sub-categories (otoparcasan-derived)
ALTER TABLE "categories" ADD COLUMN IF NOT EXISTS "canonical_subcategory_id" uuid;
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "categories_canonical_subcategory_id_idx" ON "categories" ("canonical_subcategory_id");
--> statement-breakpoint
ALTER TABLE "category_canonical_map" ADD COLUMN IF NOT EXISTS "canonical_subcategory_id" uuid;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-abs', 'ABS Sensörü / Ünitesi', 1 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-balata', 'Balata', 2 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-disk', 'Fren Diski / Ayna', 3 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-kaliper', 'Kaliper', 4 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-kampana', 'Kampana', 5 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-merkez', 'Ana Merkez / Silindir', 6 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-guclendirici', 'Fren Güçlendirici (Servo)', 7 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-hidrolik', 'Hortum / Boru / Hidrolik', 8 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-elfren', 'El Freni', 9 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-musur', 'Fren Müşürü / İkaz', 10 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-vakum', 'Vakum Pompası', 11 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-pedal', 'Fren Pedalı', 12 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-donanim', 'Bağlantı ve Donanım', 13 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-balata', 'Debriyaj Balatası', 1 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-baski', 'Baskı', 2 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-set', 'Debriyaj Seti', 3 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-rulman', 'Debriyaj Rulmanı', 4 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-merkez', 'Debriyaj Merkezi', 5 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-volan', 'Volan', 6 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-catal', 'Çatal / Bilya', 7 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-tel', 'Debriyaj Teli / Pedalı', 8 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-donanim', 'Bağlantı ve Donanım', 9 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-silindirkapak', 'Silindir Kapağı', 1 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-piston', 'Piston / Segman / Gömlek', 2 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-krank', 'Krank / Biyel', 3 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-supap', 'Supap / Külbütör', 4 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-conta', 'Conta', 5 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-kece', 'Keçe', 6 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-karter', 'Karter', 7 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-yagpompa', 'Yağ Pompası', 8 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-takoz', 'Motor Takozu', 9 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-turbo', 'Turbo', 10 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-manifold', 'Manifold', 11 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-komple', 'Motor Komple / Blok', 12 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-donanim', 'Bağlantı ve Donanım', 13 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-enjektor', 'Enjektör', 1 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-pompa', 'Yakıt Pompası', 2 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-kelebek', 'Gaz Kelebeği', 3 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-depo', 'Yakıt Deposu / Şamandıra', 4 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-boru', 'Yakıt Borusu', 5 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-egr', 'EGR', 6 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-karburator', 'Karbüratör', 7 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-egzoz', 'Egzoz', 8 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-sistemi-donanim', 'Bağlantı ve Donanım', 9 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-buji', 'Buji', 1 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-bobin', 'Ateşleme Bobini', 2 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-kablo', 'Buji Kablosu', 3 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-distributor', 'Distribütör', 4 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'triger-kayis', 'Triger Kayışı / Seti', 1 FROM "canonical_categories" WHERE slug='triger-zincir-rulman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'triger-zincir', 'Triger Zinciri', 2 FROM "canonical_categories" WHERE slug='triger-zincir-rulman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'triger-gergi', 'Gergi / Rulman', 3 FROM "canonical_categories" WHERE slug='triger-zincir-rulman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-radyator', 'Su Radyatörü', 1 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-termostat', 'Termostat', 2 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-supompa', 'Devirdaim / Su Pompası', 3 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-klima', 'Klima (Kompresör/Radyatör)', 4 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-kalorifer', 'Kalorifer', 5 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-fan', 'Fan / Pervane', 6 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-hortum', 'Su Hortumu', 7 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-musur', 'Hararet / Fan Müşürü', 8 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-intercooler', 'Intercooler / Turbo Radyatör', 9 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-donanim', 'Bağlantı ve Donanım', 10 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-yag', 'Yağ Filtresi', 1 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-hava', 'Hava Filtresi', 2 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-yakit', 'Yakıt Filtresi', 3 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-polen', 'Polen / Kabin Filtresi', 4 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-diger', 'Diğer Filtre', 5 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-amortisor', 'Amortisör', 1 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-salincak', 'Salıncak', 2 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-rotil', 'Rotil', 3 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-rot', 'Rot / Rot Başı', 4 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-viraj', 'Viraj Demiri / Z Rot', 5 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-helezon', 'Helezon Yay', 6 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-rulman', 'Aks / Porya Rulmanı', 7 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-aks', 'Aks / Aktarma', 8 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-burc', 'Burç / Takoz', 9 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-bijon', 'Bijon', 10 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'suspansiyon-donanim', 'Bağlantı ve Donanım', 11 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-pompa', 'Direksiyon Pompası', 1 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-kutu', 'Direksiyon Kutusu / Kremayer', 2 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-airbag', 'Airbag', 3 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-koruk', 'Direksiyon Körüğü', 4 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-mil', 'Direksiyon Mili / Kolu', 5 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-vites', 'Vites Kolu / Mekanizması', 1 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-takoz', 'Şanzıman Takozu', 2 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-saft', 'Şaft / Mafsal', 3 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-diferansiyel', 'Diferansiyel', 4 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-sensor', 'Hız / Km Sensörü', 5 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-karter', 'Şanzıman Karteri / Yağ', 6 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-donanim', 'Bağlantı ve Donanım', 7 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-aku', 'Akü', 1 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-alternator', 'Alternatör', 2 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-mars', 'Marş Motoru', 3 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-sensor', 'Sensör', 4 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-sigorta', 'Sigorta / Röle', 5 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-motor', 'Cam / Silecek Motoru', 6 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-kontak', 'Kontak / Marş Anahtarı', 7 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-modul', 'Modül / Beyin / ECU', 8 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-kablo', 'Kablo / Tesisat', 9 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-donanim', 'Bağlantı ve Donanım', 10 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-far', 'Far', 1 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-stop', 'Stop Lambası', 2 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-sinyal', 'Sinyal', 3 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-sis', 'Sis Farı', 4 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-plaka', 'Plaka Lambası', 5 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-ic', 'İç / Tavan Lambası', 6 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-ampul', 'Ampul', 7 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-kapi', 'Kapı / Menteşe / Kilit', 1 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-ayna', 'Dikiz Aynası', 2 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-cam', 'Cam / Kriko / Fitil', 3 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-bagaj', 'Bagaj Kapağı', 4 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-kaput', 'Motor Kaputu', 5 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-kol', 'Kapı Kolu', 6 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-camurluk', 'Çamurluk', 1 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-panjur', 'Panjur / Ön Panel', 2 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-marspiyel', 'Marşpiyel', 3 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-farcerceve', 'Far Çerçevesi / Sacı', 4 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-travers', 'Travers / Takviye', 5 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-etek', 'Etek Sacı / Muhafaza', 6 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-tampon', 'Tampon', 1 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-demir', 'Tampon Demiri / Travers', 2 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-izgara', 'Izgara / Panjur', 3 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-braket', 'Braket / Sac', 4 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-cita', 'Çıta / Kaplama', 5 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-koltuk', 'Koltuk / Kılıf', 1 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-torpido', 'Torpido / Konsol', 2 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-tavan', 'Tavan Döşeme', 3 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-kemer', 'Emniyet Kemeri', 4 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-kolcak', 'Kolçak / Bardaklık', 5 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-paspas', 'Paspas / Halı', 6 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-motor', 'Motor Yağı', 1 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-sanziman', 'Şanzıman / Dif. Yağı', 2 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-fren', 'Fren Hidroliği', 3 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-direksiyon', 'Direksiyon Yağı', 4 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-antifriz', 'Antifriz', 5 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-gres', 'Gres', 6 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-adblue', 'AdBlue', 7 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-katki', 'Katkı / Sprey', 8 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-jant', 'Jant', 1 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-tire', 'Lastik', 2 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-kapak', 'Jant Kapağı / Bijon', 3 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-sibop', 'Sibop', 4 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-rulman', 'Teker Rulmanı / Göbek', 5 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-zincir', 'Kar Zinciri', 6 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint

View File

@@ -0,0 +1,301 @@
-- 0027: additional sub-leaves for weak buckets (yaglar/panjur/direksiyon)
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-abs', 'ABS Sensörü / Ünitesi', 1 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-balata', 'Balata', 2 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-disk', 'Fren Diski / Ayna', 3 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-kaliper', 'Kaliper', 4 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-kampana', 'Kampana', 5 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-merkez', 'Ana Merkez / Silindir', 6 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-guclendirici', 'Fren Güçlendirici (Servo)', 7 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-hidrolik', 'Hortum / Boru / Hidrolik', 8 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-elfren', 'El Freni', 9 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-musur', 'Fren Müşürü / İkaz', 10 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-vakum', 'Vakum Pompası', 11 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-pedal', 'Fren Pedalı', 12 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-donanim', 'Bağlantı ve Donanım', 13 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-balata', 'Debriyaj Balatası', 1 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-baski', 'Baskı', 2 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-set', 'Debriyaj Seti', 3 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-rulman', 'Debriyaj Rulmanı', 4 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-merkez', 'Debriyaj Merkezi / Hidrolik', 5 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-volan', 'Volan', 6 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-catal', 'Çatal / Bilya', 7 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-tel', 'Debriyaj Teli / Pedalı', 8 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-donanim', 'Bağlantı ve Donanım', 9 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-silindirkapak', 'Silindir Kapağı', 1 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-piston', 'Piston / Segman / Gömlek', 2 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-krank', 'Krank / Biyel', 3 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-supap', 'Supap / Külbütör', 4 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-conta', 'Conta', 5 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-kece', 'Keçe', 6 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-karter', 'Karter', 7 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-yagpompa', 'Yağ Pompası', 8 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-takoz', 'Motor Takozu', 9 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-turbo', 'Turbo', 10 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-manifold', 'Manifold', 11 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-komple', 'Motor Komple / Blok', 12 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-donanim', 'Bağlantı ve Donanım', 13 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-enjektor', 'Enjektör', 1 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-pompa', 'Yakıt Pompası', 2 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-kelebek', 'Gaz Kelebeği', 3 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-depo', 'Yakıt Deposu / Şamandıra', 4 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-boru', 'Yakıt Borusu', 5 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-egr', 'EGR', 6 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-karburator', 'Karbüratör', 7 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-egzoz', 'Egzoz', 8 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-sistemi-donanim', 'Bağlantı ve Donanım', 9 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-buji', 'Buji', 1 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-bobin', 'Ateşleme Bobini', 2 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-kablo', 'Buji Kablosu', 3 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-distributor', 'Distribütör', 4 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'triger-kayis', 'Triger Kayışı / Seti', 1 FROM "canonical_categories" WHERE slug='triger-zincir-rulman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'triger-zincir', 'Triger Zinciri', 2 FROM "canonical_categories" WHERE slug='triger-zincir-rulman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'triger-gergi', 'Gergi / Rulman', 3 FROM "canonical_categories" WHERE slug='triger-zincir-rulman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-radyator', 'Su Radyatörü', 1 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-termostat', 'Termostat', 2 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-supompa', 'Devirdaim / Su Pompası', 3 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-klima', 'Klima (Kompresör/Radyatör)', 4 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-kalorifer', 'Kalorifer', 5 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-fan', 'Fan / Pervane', 6 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-hortum', 'Su Hortumu', 7 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-musur', 'Hararet / Fan Müşürü', 8 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-intercooler', 'Intercooler / Turbo Radyatör', 9 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-donanim', 'Bağlantı ve Donanım', 10 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-yag', 'Yağ Filtresi', 1 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-hava', 'Hava Filtresi', 2 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-yakit', 'Yakıt Filtresi', 3 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-polen', 'Polen / Kabin Filtresi', 4 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-diger', 'Diğer Filtre', 5 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-amortisor', 'Amortisör', 1 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-salincak', 'Salıncak', 2 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-rotil', 'Rotil', 3 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-rot', 'Rot / Rot Başı', 4 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-viraj', 'Viraj Demiri / Z Rot', 5 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-helezon', 'Helezon Yay', 6 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-rulman', 'Aks / Porya Rulmanı', 7 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-aks', 'Aks / Aktarma', 8 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-burc', 'Burç / Takoz', 9 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-bijon', 'Bijon', 10 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'suspansiyon-donanim', 'Bağlantı ve Donanım', 11 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-simit', 'Direksiyon Simidi', 1 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-pompa', 'Direksiyon Pompası / Hidrolik', 2 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-kutu', 'Direksiyon Kutusu / Dişli / Kremayer', 3 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-airbag', 'Airbag', 4 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-koruk', 'Direksiyon Körüğü', 5 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-elektronik', 'Kumanda / Sensör', 6 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-mil', 'Direksiyon Mili / Boğaz / Göbek', 7 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-vites', 'Vites Kolu / Mekanizması', 1 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-takoz', 'Şanzıman Takozu', 2 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-saft', 'Şaft / Mafsal', 3 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-diferansiyel', 'Diferansiyel', 4 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-sensor', 'Hız / Km Sensörü', 5 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-karter', 'Şanzıman Karteri / Yağ', 6 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-donanim', 'Bağlantı ve Donanım', 7 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-aku', 'Akü', 1 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-alternator', 'Alternatör', 2 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-mars', 'Marş Motoru', 3 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-sensor', 'Sensör', 4 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-sigorta', 'Sigorta / Röle', 5 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-motor', 'Cam / Silecek Motoru', 6 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-kontak', 'Kontak / Marş Anahtarı', 7 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-modul', 'Modül / Beyin / ECU', 8 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-kablo', 'Kablo / Tesisat', 9 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-donanim', 'Bağlantı ve Donanım', 10 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-far', 'Far', 1 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-stop', 'Stop Lambası', 2 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-sinyal', 'Sinyal', 3 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-sis', 'Sis Farı', 4 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-plaka', 'Plaka Lambası', 5 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-ic', 'İç / Tavan Lambası', 6 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-ampul', 'Ampul', 7 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-kapi', 'Kapı / Menteşe / Kilit', 1 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-ayna', 'Dikiz Aynası', 2 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-cam', 'Cam / Kriko / Fitil', 3 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-bagaj', 'Bagaj Kapağı', 4 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-kaput', 'Motor Kaputu', 5 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-kol', 'Kapı Kolu', 6 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-camurluk', 'Çamurluk / Kanat', 1 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-kaput', 'Kaput / Kaporta', 2 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-tavan', 'Tavan', 3 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-sasi', 'Şasi / Alt Panel', 4 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-cam', 'Cam / Karo', 5 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-panjur', 'Panjur / Ön Panel', 6 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-marspiyel', 'Marşpiyel', 7 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-farcerceve', 'Far Çerçevesi / Sacı', 8 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-travers', 'Travers / Takviye', 9 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-etek', 'Etek Sacı / Muhafaza', 10 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-tampon', 'Tampon', 1 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-demir', 'Tampon Demiri / Travers', 2 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-izgara', 'Izgara / Panjur', 3 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-braket', 'Braket / Sac', 4 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-cita', 'Çıta / Kaplama', 5 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-koltuk', 'Koltuk / Kılıf', 1 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-torpido', 'Torpido / Konsol', 2 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-tavan', 'Tavan Döşeme', 3 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-kemer', 'Emniyet Kemeri', 4 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-kolcak', 'Kolçak / Bardaklık', 5 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-paspas', 'Paspas / Halı', 6 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-motor', 'Motor Yağı', 1 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-sanziman', 'Şanzıman / Dif. Yağı', 2 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-fren', 'Fren Hidroliği', 3 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-direksiyon', 'Direksiyon Yağı', 4 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-antifriz', 'Antifriz', 5 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-gres', 'Gres', 6 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-adblue', 'AdBlue', 7 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-katki', 'Katkı / Sprey', 8 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-sistemi', 'Yağlama Sistemi (Pompa/Çubuk/Boru)', 9 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-jant', 'Jant', 1 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-tire', 'Lastik', 2 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-kapak', 'Jant Kapağı / Bijon', 3 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-sibop', 'Sibop', 4 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-rulman', 'Teker Rulmanı / Göbek', 5 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-zincir', 'Kar Zinciri', 6 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint

View File

@@ -0,0 +1,305 @@
-- 0028: sub-leaves for queue-shrink additions (silecek, korna + kw)
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-abs', 'ABS Sensörü / Ünitesi', 1 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-balata', 'Balata', 2 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-disk', 'Fren Diski / Ayna', 3 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-kaliper', 'Kaliper', 4 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-kampana', 'Kampana', 5 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-merkez', 'Ana Merkez / Silindir', 6 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-guclendirici', 'Fren Güçlendirici (Servo)', 7 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-hidrolik', 'Hortum / Boru / Hidrolik', 8 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-elfren', 'El Freni', 9 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-musur', 'Fren Müşürü / İkaz', 10 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-vakum', 'Vakum Pompası', 11 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-pedal', 'Fren Pedalı', 12 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'fren-donanim', 'Bağlantı ve Donanım', 13 FROM "canonical_categories" WHERE slug='fren' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-balata', 'Debriyaj Balatası', 1 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-baski', 'Baskı', 2 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-set', 'Debriyaj Seti', 3 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-rulman', 'Debriyaj Rulmanı', 4 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-merkez', 'Debriyaj Merkezi / Hidrolik', 5 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-volan', 'Volan', 6 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-catal', 'Çatal / Bilya', 7 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-tel', 'Debriyaj Teli / Pedalı', 8 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'debriyaj-donanim', 'Bağlantı ve Donanım', 9 FROM "canonical_categories" WHERE slug='debriyaj' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-silindirkapak', 'Silindir Kapağı', 1 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-piston', 'Piston / Segman / Gömlek', 2 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-krank', 'Krank / Biyel', 3 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-supap', 'Supap / Külbütör', 4 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-conta', 'Conta', 5 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-kece', 'Keçe', 6 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-karter', 'Karter', 7 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-yagpompa', 'Yağ Pompası', 8 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-takoz', 'Motor Takozu', 9 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-turbo', 'Turbo', 10 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-manifold', 'Manifold', 11 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-komple', 'Motor Komple / Blok / Takım', 12 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'motor-donanim', 'Bağlantı ve Donanım', 13 FROM "canonical_categories" WHERE slug='motor' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-enjektor', 'Enjektör', 1 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-pompa', 'Yakıt Pompası', 2 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-kelebek', 'Gaz Kelebeği', 3 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-depo', 'Yakıt Deposu / Şamandıra', 4 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-boru', 'Yakıt Borusu', 5 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-egr', 'EGR', 6 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-karburator', 'Karbüratör', 7 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-egzoz', 'Egzoz / Lambda', 8 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yakit-sistemi-donanim', 'Bağlantı ve Donanım', 9 FROM "canonical_categories" WHERE slug='yakit-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-buji', 'Buji', 1 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-bobin', 'Ateşleme Bobini', 2 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-kablo', 'Buji Kablosu', 3 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'atesleme-distributor', 'Distribütör', 4 FROM "canonical_categories" WHERE slug='atesleme-sistemi' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'triger-kayis', 'Triger Kayışı / Seti', 1 FROM "canonical_categories" WHERE slug='triger-zincir-rulman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'triger-zincir', 'Triger Zinciri', 2 FROM "canonical_categories" WHERE slug='triger-zincir-rulman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'triger-gergi', 'Gergi / Rulman', 3 FROM "canonical_categories" WHERE slug='triger-zincir-rulman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-radyator', 'Radyatör / Soğutucu', 1 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-termostat', 'Termostat', 2 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-supompa', 'Devirdaim / Su Pompası', 3 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-klima', 'Klima (Kompresör/Radyatör)', 4 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-kalorifer', 'Kalorifer / Havalandırma / Isıtma', 5 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-fan', 'Fan / Pervane', 6 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-hortum', 'Su Hortumu', 7 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-musur', 'Hararet / Fan Müşürü', 8 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-intercooler', 'Intercooler / Turbo Radyatör', 9 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sogutma-donanim', 'Bağlantı ve Donanım', 10 FROM "canonical_categories" WHERE slug='sogutma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-yag', 'Yağ Filtresi', 1 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-hava', 'Hava Filtresi', 2 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-yakit', 'Yakıt Filtresi', 3 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-polen', 'Polen / Kabin Filtresi', 4 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'filtre-diger', 'Diğer Filtre', 5 FROM "canonical_categories" WHERE slug='filtreler' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-amortisor', 'Amortisör', 1 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-salincak', 'Salıncak', 2 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-rotil', 'Rotil', 3 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-rot', 'Rot / Rot Başı', 4 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-viraj', 'Viraj Demiri / Z Rot / Burkma', 5 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-helezon', 'Helezon Yay', 6 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-rulman', 'Aks / Porya Rulmanı', 7 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-aks', 'Aks / Aktarma', 8 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-burc', 'Burç / Takoz / Pim', 9 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'susp-bijon', 'Bijon', 10 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'suspansiyon-donanim', 'Bağlantı ve Donanım', 11 FROM "canonical_categories" WHERE slug='suspansiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-simit', 'Direksiyon Simidi', 1 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-pompa', 'Direksiyon Pompası / Hidrolik', 2 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-kutu', 'Direksiyon Kutusu / Dişli / Kremayer', 3 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-airbag', 'Airbag', 4 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-koruk', 'Direksiyon Körüğü', 5 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-elektronik', 'Kumanda / Sensör', 6 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'direksiyon-mil', 'Direksiyon Mili / Boğaz / Göbek', 7 FROM "canonical_categories" WHERE slug='direksiyon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-vites', 'Vites / İç Kontroller', 1 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-takoz', 'Şanzıman Takozu', 2 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-saft', 'Şaft / Mafsal', 3 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-diferansiyel', 'Diferansiyel', 4 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-sensor', 'Hız / Km Sensörü', 5 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-karter', 'Şanzıman Karteri / Yağ', 6 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'sanziman-donanim', 'Bağlantı ve Donanım', 7 FROM "canonical_categories" WHERE slug='sanziman' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-aku', 'Akü', 1 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-alternator', 'Alternatör', 2 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-mars', 'Marş Motoru', 3 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-sensor', 'Sensör', 4 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-sigorta', 'Sigorta / Röle', 5 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-motor', 'Cam / Silecek Motoru', 6 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-silecek', 'Silecek', 7 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-korna', 'Korna', 8 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-kontak', 'Kontak / Marş Anahtarı', 9 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-modul', 'Modül / Gösterge / Elektronik', 10 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-kablo', 'Kablo / Tesisat', 11 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'elektrik-donanim', 'Bağlantı ve Donanım', 12 FROM "canonical_categories" WHERE slug='elektrik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-far', 'Far', 1 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-stop', 'Stop Lambası', 2 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-sinyal', 'Sinyal', 3 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-sis', 'Sis Farı', 4 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-plaka', 'Plaka Lambası', 5 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-ic', 'İç / Tavan Lambası', 6 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ayd-ampul', 'Ampul', 7 FROM "canonical_categories" WHERE slug='aydinlatma' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-kapi', 'Kapı / Menteşe / Kilit', 1 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-ayna', 'Dikiz Aynası', 2 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-cam', 'Cam / Kriko / Fitil', 3 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-bagaj', 'Bagaj Kapağı', 4 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-kaput', 'Motor Kaputu', 5 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'disk-kol', 'Kapı Kolu', 6 FROM "canonical_categories" WHERE slug='dis-kaporta' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-camurluk', 'Çamurluk / Kanat', 1 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-kaput', 'Kaput / Kaporta', 2 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-tavan', 'Tavan', 3 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-sasi', 'Şasi / Kiriş / Alt Panel', 4 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-cam', 'Cam / Karo', 5 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-panjur', 'Panjur / Ön Panel', 6 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-marspiyel', 'Marşpiyel', 7 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-farcerceve', 'Far Çerçevesi / Sacı', 8 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-travers', 'Travers / Takviye', 9 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'panjur-etek', 'Etek Sacı / Muhafaza', 10 FROM "canonical_categories" WHERE slug='panjur-panel' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-tampon', 'Tampon', 1 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-demir', 'Tampon Demiri / Travers', 2 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-izgara', 'Izgara / Panjur', 3 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-braket', 'Braket / Sac', 4 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'tampon-cita', 'Çıta / Kaplama', 5 FROM "canonical_categories" WHERE slug='tampon' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-koltuk', 'Koltuk / Kılıf', 1 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-torpido', 'Torpido / Konsol', 2 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-tavan', 'Tavan Döşeme / Güneşlik', 3 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-kemer', 'Emniyet Kemeri', 4 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-kolcak', 'Kolçak / Bardaklık', 5 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'ic-paspas', 'Paspas / Halı', 6 FROM "canonical_categories" WHERE slug='ic-doseme' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-motor', 'Motor Yağı', 1 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-sanziman', 'Şanzıman / Dif. Yağı', 2 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-fren', 'Fren Hidroliği', 3 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-direksiyon', 'Direksiyon Yağı', 4 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-antifriz', 'Antifriz', 5 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-gres', 'Gres', 6 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-adblue', 'AdBlue', 7 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-katki', 'Katkı / Sprey', 8 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'yag-sistemi', 'Yağlama Sistemi (Pompa/Tava/Keçe)', 9 FROM "canonical_categories" WHERE slug='yaglar-sivilar' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-jant', 'Jant', 1 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-tire', 'Lastik', 2 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-kapak', 'Jant Kapağı / Bijon', 3 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-sibop', 'Sibop', 4 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-rulman', 'Teker Rulmanı / Göbek', 5 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint
INSERT INTO "canonical_categories" ("parent_id","slug","name","sort_order") SELECT id, 'jant-zincir', 'Kar Zinciri', 6 FROM "canonical_categories" WHERE slug='jant-lastik' AND parent_id IS NULL ON CONFLICT ("slug") DO NOTHING;
--> statement-breakpoint

View File

@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS "canonical_structure_template" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"source" varchar(20) NOT NULL,
"brand" varchar(120) NOT NULL,
"canonical_category_id" uuid NOT NULL,
"canonical_subcategory_id" uuid,
"vehicle_count" integer DEFAULT 0 NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "canonical_structure_template_source_brand_idx" ON "canonical_structure_template" ("source","brand");

View File

@@ -0,0 +1,91 @@
-- 0030: merge duplicate categories, then enforce uniqueness with NULLS NOT DISTINCT.
--
-- Root cause: categories_vehicle_name_source_idx (vehicle_id, catalog_vehicle_id,
-- name, source) treats NULLs as distinct, and emex/pcat rows have NULL
-- catalog_vehicle_id (pl24 rows have NULL vehicle_id) — so decode retries slipped
-- past every onConflictDoNothing and re-inserted the same category (+3k/day).
--
-- This migration (idempotent):
-- 1. Iteratively merges duplicate groups (same vehicle_id, catalog_vehicle_id,
-- name, source — NULLs equal): winner = most children, then most parts,
-- then oldest. Children are reparented (which can create next-level dupes —
-- hence the loop), parts are repointed with parts_dedup_idx-safe staging,
-- schema_pics / oem_code_copies / oem_votes are repointed, losers deleted.
-- 2. Swaps the unique index to NULLS NOT DISTINCT (PG15+; prod is PG17), which
-- makes all existing onConflictDoNothing inserts actually dedupe.
DO $$
DECLARE
merged integer;
pass integer := 0;
BEGIN
LOOP
pass := pass + 1;
CREATE TEMP TABLE dedup_map ON COMMIT DROP AS
WITH r AS (
SELECT id,
first_value(id) OVER w AS winner,
row_number() OVER w AS rn
FROM categories c
WINDOW w AS (
PARTITION BY vehicle_id, catalog_vehicle_id, name, source
ORDER BY (EXISTS(SELECT 1 FROM categories ch WHERE ch.parent_id = c.id)) DESC,
(SELECT count(*) FROM parts p WHERE p.category_id = c.id) DESC,
created_at, id
)
)
SELECT id AS loser, winner FROM r WHERE rn > 1;
SELECT count(*) INTO merged FROM dedup_map;
IF merged = 0 THEN
DROP TABLE dedup_map;
EXIT;
END IF;
RAISE NOTICE 'dedup pass %: merging % duplicate categories', pass, merged;
-- reparent children of losers onto the winner (may create next-level dupes)
UPDATE categories SET parent_id = m.winner FROM dedup_map m
WHERE categories.parent_id = m.loser;
-- repoint parts: stage with row_number over the TARGET dedup key so no two
-- moved rows collide with each other; skip ones colliding with the winner's
-- existing parts; delete the remainder (exact duplicates).
CREATE TEMP TABLE dedup_mv ON COMMIT DROP AS
SELECT p.id, m.winner,
row_number() OVER (
PARTITION BY p.vehicle_id, m.winner, p.oem_code, p.name, p."position"
ORDER BY p.id
) AS rn
FROM parts p JOIN dedup_map m ON m.loser = p.category_id;
UPDATE parts SET category_id = mv.winner FROM dedup_mv mv
WHERE parts.id = mv.id AND mv.rn = 1
AND NOT EXISTS (
SELECT 1 FROM parts w
WHERE w.category_id = mv.winner
AND w.vehicle_id IS NOT DISTINCT FROM parts.vehicle_id
AND w.oem_code IS NOT DISTINCT FROM parts.oem_code
AND w.name IS NOT DISTINCT FROM parts.name
AND w."position" IS NOT DISTINCT FROM parts."position"
);
DELETE FROM parts USING dedup_map m WHERE parts.category_id = m.loser;
UPDATE schema_pics SET category_id = m.winner FROM dedup_map m
WHERE schema_pics.category_id = m.loser;
UPDATE oem_code_copies SET category_id = m.winner FROM dedup_map m
WHERE oem_code_copies.category_id = m.loser;
UPDATE oem_votes SET category_id = m.winner FROM dedup_map m
WHERE oem_votes.category_id = m.loser;
DELETE FROM categories USING dedup_map m WHERE categories.id = m.loser;
DROP TABLE dedup_map;
DROP TABLE dedup_mv;
END LOOP;
RAISE NOTICE 'dedup complete after % pass(es)', pass;
END $$;
--> statement-breakpoint
DROP INDEX IF EXISTS "categories_vehicle_name_source_idx";
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "categories_vehicle_name_source_idx"
ON "categories" ("vehicle_id", "catalog_vehicle_id", "name", "source") NULLS NOT DISTINCT;

View File

@@ -0,0 +1,17 @@
-- 0031: brand-template ENTRY names — the browsable source-category names every
-- vehicle of (source, brand) shares. Lets a freshly-decoded vehicle show the
-- FULL canonical structure (named entries under each bucket) before its own
-- deep drill; only part counts/schemas are vehicle-specific. Rebuilt by the
-- global canonical-backfill run alongside canonical_structure_template.
CREATE TABLE IF NOT EXISTS "canonical_template_entries" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"source" varchar(20) NOT NULL,
"brand" varchar(120) NOT NULL,
"canonical_category_id" uuid NOT NULL,
"canonical_subcategory_id" uuid,
"name" varchar(500) NOT NULL,
"vehicle_count" integer DEFAULT 0 NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "canonical_template_entries_sb_idx" ON "canonical_template_entries" ("source","brand");

View File

@@ -92,6 +92,139 @@
"when": 1780581755559, "when": 1780581755559,
"tag": "0012_lifecycle_email_sent", "tag": "0012_lifecycle_email_sent",
"breakpoints": true "breakpoints": true
},
{
"idx": 13,
"version": "7",
"when": 1780600000000,
"tag": "0013_fix_brand_casing",
"breakpoints": true
},
{
"idx": 14,
"version": "7",
"when": 1781136000000,
"tag": "0014_proxy_logs",
"breakpoints": true
},
{
"idx": 15,
"version": "7",
"when": 1781222400000,
"tag": "0015_oem_votes",
"breakpoints": true
},
{
"idx": 16,
"version": "7",
"when": 1781308800000,
"tag": "0016_oem_suggestions",
"breakpoints": true
},
{
"idx": 17,
"version": "7",
"when": 1781395200000,
"tag": "0017_oem_expert_rewards",
"breakpoints": true
},
{
"idx": 18,
"version": "7",
"when": 1781481600000,
"tag": "0018_part_price_history",
"breakpoints": true
},
{
"idx": 19,
"version": "7",
"when": 1781485200000,
"tag": "0019_part_price_brand",
"breakpoints": true
},
{
"idx": 20,
"version": "7",
"when": 1781268490495,
"tag": "0020_stripe_recurring",
"breakpoints": true
},
{
"idx": 21,
"version": "7",
"when": 1781568000000,
"tag": "0021_vinpin_decodes",
"breakpoints": true
},
{
"idx": 22,
"version": "7",
"when": 1782652379976,
"tag": "0022_discount_codes",
"breakpoints": true
},
{
"idx": 23,
"version": "7",
"when": 1783084194145,
"tag": "0023_api_keys",
"breakpoints": true
},
{
"idx": 24,
"version": "7",
"when": 1783085194145,
"tag": "0024_canonical_categories",
"breakpoints": true
},
{
"idx": 25,
"version": "7",
"when": 1783086194145,
"tag": "0025_canonical_wheel_accessory",
"breakpoints": true
},
{
"idx": 26,
"version": "7",
"when": 1783087194145,
"tag": "0026_canonical_subcategories",
"breakpoints": true
},
{
"idx": 27,
"version": "7",
"when": 1783088194145,
"tag": "0027_canonical_subcategories_v2",
"breakpoints": true
},
{
"idx": 28,
"version": "7",
"when": 1783089194145,
"tag": "0028_canonical_subcat_v3",
"breakpoints": true
},
{
"idx": 29,
"version": "7",
"when": 1783090194145,
"tag": "0029_canonical_structure_template",
"breakpoints": true
},
{
"idx": 30,
"version": "7",
"when": 1783091194145,
"tag": "0030_categories_dedup_unique",
"breakpoints": true
},
{
"idx": 31,
"version": "7",
"when": 1783092194145,
"tag": "0031_canonical_template_entries",
"breakpoints": true
} }
] ]
} }

View File

@@ -65,6 +65,7 @@
"reflect-metadata": "^0.2.2", "reflect-metadata": "^0.2.2",
"rxjs": "^7.8.0", "rxjs": "^7.8.0",
"stripe": "^22.1.1", "stripe": "^22.1.1",
"tesseract.js": "^5.1.1",
"undici": "^7.22.0", "undici": "^7.22.0",
"zod": "^3.24.0" "zod": "^3.24.0"
}, },

View File

@@ -26,17 +26,24 @@ import { validate } from "./config/env.validation";
import { ContactModule } from "./contact/contact.module"; import { ContactModule } from "./contact/contact.module";
import { DatabaseModule } from "./database/database.module"; import { DatabaseModule } from "./database/database.module";
import { DemoModule } from "./demo/demo.module"; import { DemoModule } from "./demo/demo.module";
import { DiscountCodesModule } from "./discount-codes/discount-codes.module";
import { EmailModule } from "./email/email.module"; import { EmailModule } from "./email/email.module";
import { HealthController } from "./health.controller"; import { HealthController } from "./health.controller";
import { EmexModule } from "./integrations/emex/emex.module"; import { EmexModule } from "./integrations/emex/emex.module";
import { PModule } from "./integrations/p/p.module";
import { VinpinModule } from "./integrations/vinpin/vinpin.module";
import { InternalAdminModule } from "./internal-admin/internal-admin.module"; import { InternalAdminModule } from "./internal-admin/internal-admin.module";
import { JobsModule } from "./jobs/jobs.module"; import { JobsModule } from "./jobs/jobs.module";
import { MetaCapiModule } from "./meta-capi/meta-capi.module"; import { MetaCapiModule } from "./meta-capi/meta-capi.module";
import { NotificationsModule } from "./notifications/notifications.module"; import { NotificationsModule } from "./notifications/notifications.module";
import { OemSuggestionsModule } from "./oem-suggestions/oem-suggestions.module";
import { OemVotesModule } from "./oem-votes/oem-votes.module";
import { PartPricesModule } from "./part-prices/part-prices.module";
import { PartsModule } from "./parts/parts.module"; import { PartsModule } from "./parts/parts.module";
import { PaymentsModule } from "./payments/payments.module"; import { PaymentsModule } from "./payments/payments.module";
import { PlansModule } from "./plans/plans.module"; import { PlansModule } from "./plans/plans.module";
import { PostHogModule } from "./posthog/posthog.module"; import { PostHogModule } from "./posthog/posthog.module";
import { PublicApiModule } from "./public-api/public-api.module";
import { RedisModule } from "./redis/redis.module"; import { RedisModule } from "./redis/redis.module";
import { ReferralsModule } from "./referrals/referrals.module"; import { ReferralsModule } from "./referrals/referrals.module";
import { StorageModule } from "./storage/storage.module"; import { StorageModule } from "./storage/storage.module";
@@ -81,16 +88,22 @@ import { VehiclesModule } from "./vehicles/vehicles.module";
PlansModule, PlansModule,
SubscriptionsModule, SubscriptionsModule,
PaymentsModule, PaymentsModule,
DiscountCodesModule,
ReferralsModule, ReferralsModule,
VehiclesModule, VehiclesModule,
CategoriesModule, CategoriesModule,
DemoModule, DemoModule,
PartsModule, PartsModule,
PModule,
PartPricesModule,
JobsModule, JobsModule,
EmexModule, EmexModule,
VinpinModule,
TranslationsModule, TranslationsModule,
AdminModule, AdminModule,
AnalyticsModule, AnalyticsModule,
OemVotesModule,
OemSuggestionsModule,
CatalogModule, CatalogModule,
ChangelogModule, ChangelogModule,
ChatwootModule, ChatwootModule,
@@ -100,6 +113,7 @@ import { VehiclesModule } from "./vehicles/vehicles.module";
PostHogModule, PostHogModule,
TelemetryModule, TelemetryModule,
InternalAdminModule, InternalAdminModule,
PublicApiModule,
], ],
controllers: [HealthController], controllers: [HealthController],
providers: [ providers: [

View File

@@ -2,7 +2,7 @@ import { randomUUID } from "node:crypto";
import { generateReferralCode, normalizeName } from "@sase/shared"; import { generateReferralCode, normalizeName } from "@sase/shared";
import { betterAuth } from "better-auth"; import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { captcha } from "better-auth/plugins"; import { apiKey, captcha } from "better-auth/plugins";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { drizzle } from "drizzle-orm/postgres-js"; import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres"; import postgres from "postgres";
@@ -224,6 +224,21 @@ export function createAuth(
"http://localhost:4000", "http://localhost:4000",
], ],
plugins: [ plugins: [
// Public API anahtarları (api.sase.tr / /api/v1). Doğrulama ApiKeyGuard
// üzerinden auth.api.verifyApiKey ile yapılır; enableSessionForAPIKeys
// bilinçli olarak KAPALI — API anahtarı dashboard oturumu yerine geçmez,
// cookie tabanlı AuthGuard'ın davranışı değişmez.
apiKey({
defaultPrefix: "sase_",
enableMetadata: true,
// Anahtar başına istek tavanı (verify başına sayaç). Günlük decode
// kotasından ayrıdır — o, PublicApiQuotaService'te Redis ile tutulur.
rateLimit: {
enabled: true,
timeWindow: 60_000, // 1 dakika
maxRequests: 120,
},
}),
// Cloudflare Turnstile: yalnızca secret tanımlıysa aktif. sign-in/sign-up // Cloudflare Turnstile: yalnızca secret tanımlıysa aktif. sign-in/sign-up
// uçları "x-captcha-response" header'ındaki token ile doğrulanır. // uçları "x-captcha-response" header'ındaki token ile doğrulanır.
...(process.env.TURNSTILE_SECRET_KEY ...(process.env.TURNSTILE_SECRET_KEY
@@ -247,3 +262,42 @@ export function getAuth() {
} }
return authInstance; return authInstance;
} }
/** apiKey plugin'inin döndürdüğü anahtar kaydı (verify/create cevapları). */
export interface ApiKeyRecord {
id: string;
userId: string;
name: string | null;
start: string | null;
prefix: string | null;
enabled: boolean;
expiresAt: Date | null;
metadata: Record<string, unknown> | null;
}
/**
* apiKey plugin uçları. `authInstance` geniş `ReturnType<typeof betterAuth>`
* ile tiplendiği için plugin uçları tipte görünmüyor (runtime'da mevcutlar);
* bu erişimci onları dar ve doğru imzalarla expose eder.
*/
export function getApiKeyApi() {
return getAuth().api as unknown as {
verifyApiKey: (input: { body: { key: string } }) => Promise<{
valid: boolean;
error: { message?: string; code: string } | null;
key: ApiKeyRecord | null;
}>;
createApiKey: (input: {
body: {
userId: string;
name?: string;
prefix?: string;
expiresIn?: number;
metadata?: Record<string, unknown>;
rateLimitEnabled?: boolean;
rateLimitTimeWindow?: number;
rateLimitMax?: number;
};
}) => Promise<ApiKeyRecord & { key: string }>;
};
}

View File

@@ -52,10 +52,7 @@ export class BlogController {
// Automation endpoint — published by the Süper Panel content pipeline via n8n. // Automation endpoint — published by the Süper Panel content pipeline via n8n.
@Post("posts/internal") @Post("posts/internal")
@Public() @Public()
async createInternal( async createInternal(@Headers("authorization") authHeader: string, @Body() body: CreateBlogPost) {
@Headers("authorization") authHeader: string,
@Body() body: CreateBlogPost,
) {
assertAutomationToken(authHeader); assertAutomationToken(authHeader);
if (!body?.title || !body?.body_markdown) { if (!body?.title || !body?.body_markdown) {
throw new UnauthorizedException("title and body_markdown are required"); throw new UnauthorizedException("title and body_markdown are required");

View File

@@ -1,9 +1,4 @@
import { import { Injectable, Logger, NotFoundException, ServiceUnavailableException } from "@nestjs/common";
Injectable,
Logger,
NotFoundException,
ServiceUnavailableException,
} from "@nestjs/common";
import { RedisService } from "../redis/redis.service"; import { RedisService } from "../redis/redis.service";
import type { BlogPost, BlogPostListItem, CreateBlogPost } from "./blog.dto"; import type { BlogPost, BlogPostListItem, CreateBlogPost } from "./blog.dto";
@@ -56,10 +51,21 @@ function resolveCover(row: Pick<DirectusPost, "cover_image" | "cover_file">): st
// slug, but we slugify defensively (and to derive one if it's missing). // slug, but we slugify defensively (and to derive one if it's missing).
function slugify(input: string): string { function slugify(input: string): string {
const map: Record<string, string> = { const map: Record<string, string> = {
ç: "c", ğ: "g", ı: "i", İ: "i", ö: "o", ş: "s", ü: "u", ç: "c",
Ç: "c", Ğ: "g", Ö: "o", Ş: "s", Ü: "u", ğ: "g",
ı: "i",
İ: "i",
ö: "o",
ş: "s",
ü: "u",
Ç: "c",
Ğ: "g",
Ö: "o",
Ş: "s",
Ü: "u",
}; };
return input return (
input
.split("") .split("")
.map((c) => map[c] ?? c) .map((c) => map[c] ?? c)
.join("") .join("")
@@ -68,7 +74,8 @@ function slugify(input: string): string {
.replace(/[̀-ͯ]/g, "") .replace(/[̀-ͯ]/g, "")
.replace(/[^a-z0-9]+/g, "-") .replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "") .replace(/^-+|-+$/g, "")
.slice(0, 160) || "yazi"; .slice(0, 160) || "yazi"
);
} }
@Injectable() @Injectable()
@@ -95,7 +102,9 @@ export class BlogService {
}); });
if (!res.ok) { if (!res.ok) {
const detail = await res.text().catch(() => ""); const detail = await res.text().catch(() => "");
throw new Error(`Directus ${init.method ?? "GET"} ${path} -> ${res.status}: ${detail.slice(0, 300)}`); throw new Error(
`Directus ${init.method ?? "GET"} ${path} -> ${res.status}: ${detail.slice(0, 300)}`,
);
} }
const json = (await res.json()) as { data: T }; const json = (await res.json()) as { data: T };
return json.data; return json.data;
@@ -136,7 +145,8 @@ export class BlogService {
"filter[project][_eq]": DIRECTUS_PROJECT, "filter[project][_eq]": DIRECTUS_PROJECT,
"filter[status][_eq]": "published", "filter[status][_eq]": "published",
sort: "-published_at", sort: "-published_at",
fields: "slug,title,meta_description,tags,cover_image,cover_file.filename_disk,published_at", fields:
"slug,title,meta_description,tags,cover_image,cover_file.filename_disk,published_at",
limit: "-1", limit: "-1",
}), }),
); );

View File

@@ -81,6 +81,19 @@ export class CatalogController {
return this.catalogService.getCategoryTree(id, user.id, body, engine, gearbox, mainGroupsPath); return this.catalogService.getCategoryTree(id, user.id, body, engine, gearbox, mainGroupsPath);
} }
// Unified canonical browse for a catalog vehicle (pl24 flow).
@Get("vehicles/:id/canonical")
getCanonicalTree(
@Param("id") id: string,
@Query("body") body: string | undefined,
@Query("engine") engine: string | undefined,
@Query("gearbox") gearbox: string | undefined,
@Query("mgp") mainGroupsPath: string | undefined,
@CurrentUser() user: { id: string },
) {
return this.catalogService.getCanonicalTree(id, user.id, body, engine, gearbox, mainGroupsPath);
}
@Get("vehicles/:id/categories/:categoryId") @Get("vehicles/:id/categories/:categoryId")
getCategoryWithParts( getCategoryWithParts(
@Param("id") id: string, @Param("id") id: string,

View File

@@ -1,8 +1,16 @@
import { ForbiddenException, Inject, Injectable, Logger, NotFoundException } from "@nestjs/common"; import { ForbiddenException, Inject, Injectable, Logger, NotFoundException } from "@nestjs/common";
import { and, eq, inArray, or, sql } from "drizzle-orm"; import { and, eq, gte, inArray, isNotNull, isNull, or, sql } from "drizzle-orm";
import {
buildCanonicalInput,
groupCategoriesByCanonical,
overlayTemplate,
} from "../categories/canonical-grouping";
import { DATABASE, type Database } from "../database/database.provider"; import { DATABASE, type Database } from "../database/database.provider";
import { import {
brands, brands,
canonicalCategories,
canonicalStructureTemplate,
canonicalTemplateEntries,
catalogVehicles, catalogVehicles,
categories, categories,
parts, parts,
@@ -349,6 +357,130 @@ export class CatalogService {
/** /**
* Get category tree for a catalog vehicle (on-demand fetch from PL24). * Get category tree for a catalog vehicle (on-demand fetch from PL24).
*/ */
/**
* Unified canonical browse for a catalog vehicle (pl24 flow) — the
* catalog_vehicle_id counterpart of CategoriesService.getCanonicalTree.
* Folds the vehicle's part-holding leaf categories into the 20 canonical
* buckets so pl24 brands (VW, Opel, Renault…) navigate through the same
* headings as the emex/pcat vehicle_id flow. Part counts are joined by
* category_id (pl24 parts often carry vehicle_id, not catalog_vehicle_id).
*/
async getCanonicalTree(
catalogVehicleId: string,
userId: string,
body = "_all_",
engine = "_all_",
gearbox = "_all_",
mainGroupsPath?: string,
) {
// Populate the raw source tree (lazy fetch + cache) and enforce brand access.
await this.getCategoryTree(catalogVehicleId, userId, body, engine, gearbox, mainGroupsPath);
const buckets = await this.db
.select({
id: canonicalCategories.id,
slug: canonicalCategories.slug,
name: canonicalCategories.name,
})
.from(canonicalCategories)
.where(isNull(canonicalCategories.parentId))
.orderBy(canonicalCategories.sortOrder);
const subLeaves = await this.db
.select({
id: canonicalCategories.id,
parentId: canonicalCategories.parentId,
slug: canonicalCategories.slug,
name: canonicalCategories.name,
})
.from(canonicalCategories)
.where(isNotNull(canonicalCategories.parentId))
.orderBy(canonicalCategories.sortOrder);
const cats = await this.db
.select({
id: categories.id,
name: categories.name,
source: categories.source,
canonicalId: categories.canonicalCategoryId,
subId: categories.canonicalSubcategoryId,
parentId: categories.parentId,
})
.from(categories)
.where(eq(categories.catalogVehicleId, catalogVehicleId));
const partCounts = new Map<string, number>();
const catIds = cats.map((c) => c.id);
if (catIds.length > 0) {
const rows = await this.db
.select({ categoryId: parts.categoryId, count: sql<number>`count(*)::int` })
.from(parts)
.where(inArray(parts.categoryId, catIds))
.groupBy(parts.categoryId);
for (const r of rows) partCounts.set(r.categoryId, r.count);
}
// Show each bucket's natural sub-category level (see buildCanonicalInput).
const {
input: shown,
counts: shownCounts,
hasChildren,
} = buildCanonicalInput(cats, partCounts, buckets);
const groups = groupCategoriesByCanonical(shown, shownCounts, buckets, {
requireParts: false,
subLeaves,
hasChildren,
});
// Overlay the per-brand structure template (see CategoriesService.getCanonicalTree).
const cv = await this.db
.select({ brand: catalogVehicles.brandName, source: catalogVehicles.source })
.from(catalogVehicles)
.where(eq(catalogVehicles.id, catalogVehicleId))
.limit(1);
const brand = cv[0]?.brand;
const source = cats[0]?.source ?? cv[0]?.source;
if (!brand || !source) return groups;
const template = await this.db
.select({
bucketId: canonicalStructureTemplate.canonicalCategoryId,
subId: canonicalStructureTemplate.canonicalSubcategoryId,
})
.from(canonicalStructureTemplate)
.where(
and(
eq(canonicalStructureTemplate.source, source),
eq(canonicalStructureTemplate.brand, brand),
gte(canonicalStructureTemplate.vehicleCount, 2),
),
);
const templateEntriesRaw = await this.db
.select({
bucketId: canonicalTemplateEntries.canonicalCategoryId,
subId: canonicalTemplateEntries.canonicalSubcategoryId,
name: canonicalTemplateEntries.name,
vehicleCount: canonicalTemplateEntries.vehicleCount,
})
.from(canonicalTemplateEntries)
.where(
and(
eq(canonicalTemplateEntries.source, source),
eq(canonicalTemplateEntries.brand, brand),
gte(canonicalTemplateEntries.vehicleCount, 2),
),
);
// Cap placeholders at the MOST-SHARED 40 per bucket — some brands carry
// hundreds of model-variant names (pl24 BMW elektrik: 500+) that would
// drown the view; the widely-shared ones are the real "brand structure".
templateEntriesRaw.sort((a, z) => z.vehicleCount - a.vehicleCount);
const perBucket = new Map<string, number>();
const templateEntries = templateEntriesRaw.filter((e) => {
const n = (perBucket.get(e.bucketId) ?? 0) + 1;
perBucket.set(e.bucketId, n);
return n <= 40;
});
return overlayTemplate(groups, template, buckets, subLeaves, templateEntries);
}
async getCategoryTree( async getCategoryTree(
catalogVehicleId: string, catalogVehicleId: string,
userId: string, userId: string,

View File

@@ -0,0 +1,368 @@
import { classifyNode, foldName, mapToCanonical } from "../jobs/canonical-lexicon";
import { mapToSubcategory } from "../jobs/canonical-sublexicon";
// Shared logic for the unified canonical browse, used by both the vehicle_id
// flow (CategoriesService) and the catalog_vehicle_id flow (CatalogService):
// fold a vehicle's part-holding leaf categories into the 20 canonical buckets.
// Bucketing prefers the materialized categories.canonical_category_id and falls
// back to on-the-fly mapping (same canonical-lexicon the backfill uses) for
// categories decoded since the last backfill; anything unbucketed lands in "Diğer".
export interface CanonicalBucket {
id: string;
slug: string;
name: string;
}
export interface SubLeafRef {
id: string;
parentId: string | null;
slug: string;
name: string;
}
export interface RawCategory {
id: string;
name: string;
source: string;
canonicalId: string | null;
subId?: string | null;
}
interface CanonicalLeaf {
id: string;
name: string;
source: string;
partCount: number;
// Template placeholder: the brand is known to have this entry but THIS
// vehicle's row isn't drilled yet — rendered disabled ("dolduruluyor").
unavailable?: boolean;
}
export interface TemplateEntry {
bucketId: string;
subId: string | null;
name: string;
}
export interface CanonicalSubgroup {
canonical: { id: string | null; slug: string; name: string };
partCount: number;
categories: CanonicalLeaf[];
}
export interface CanonicalGroup {
canonical: { id: string | null; slug: string; name: string };
partCount: number;
categories: CanonicalLeaf[];
// Present only when sub-leaves are supplied: the bucket's leaves split one
// level finer (Fren → Balata/Disk/…). Leaves with no sub-match collect under
// a "genel" subgroup so nothing is hidden.
subgroups?: CanonicalSubgroup[];
}
type LeafWithSub = CanonicalLeaf & { subId: string | null };
// Build the canonical-view input from a vehicle's raw category tree. Shows each
// bucket's NATURAL sub-category level: the children of every bucket "entry"
// container ("Fren sistemi" → Disk frenler / Fren Kaliyeri / El fren sistemi …),
// with SUBTREE part counts, drilled or not — matching the source catalog. So
// clicking a bucket lands on its sub-categories, not a redundant "Fren sistemi"
// hop, and partially-drilled vehicles still show every sub-category (0 parça
// until drilled). Cross-bucket children ("Debriyaj sistemi" under Fren) are
// handled by their own entry → nothing double-shows. Bucketing is materialized
// (canonicalId) with an on-the-fly fallback for freshly-decoded categories.
export function buildCanonicalInput<
T extends { id: string; name: string; canonicalId: string | null; parentId: string | null },
>(
cats: T[],
partCounts: Map<string, number>,
buckets: CanonicalBucket[],
): { input: T[]; counts: Map<string, number>; hasChildren: Set<string> } {
const hasChildren = new Set(cats.map((c) => c.parentId).filter((p): p is string => !!p));
const childrenOf = new Map<string, string[]>();
for (const c of cats)
if (c.parentId) {
const a = childrenOf.get(c.parentId) ?? [];
a.push(c.id);
childrenOf.set(c.parentId, a);
}
const subMemo = new Map<string, number>();
const subtreeParts = (id: string): number => {
const m = subMemo.get(id);
if (m !== undefined) return m;
let s = partCounts.get(id) ?? 0;
for (const ch of childrenOf.get(id) ?? []) s += subtreeParts(ch);
subMemo.set(id, s);
return s;
};
const catById = new Map(cats.map((c) => [c.id, c]));
const bucketSlugToId = new Map(buckets.map((b) => [b.slug, b.id]));
const bucketOf = (c: T): string | null => {
if (c.canonicalId) return c.canonicalId;
const f = foldName(c.name);
if (classifyNode(f) !== "part") return null;
const { slug } = mapToCanonical(f);
return slug ? (bucketSlugToId.get(slug) ?? null) : null;
};
const input: T[] = [];
const counts = new Map<string, number>();
const push = (c: T) => {
if (counts.has(c.id)) return;
counts.set(c.id, subtreeParts(c.id));
input.push(c);
};
for (const c of cats) {
const b = bucketOf(c);
if (!b) continue;
const p = c.parentId ? catById.get(c.parentId) : null;
if (p && bucketOf(p) === b) continue; // not a bucket entry — shown via its parent
const sameKids = (childrenOf.get(c.id) ?? [])
.map((id) => catById.get(id))
.filter((k): k is T => !!k && bucketOf(k) === b);
if (sameKids.length > 0) for (const k of sameKids) push(k);
else push(c); // leaf entry (İç Aydınlatma Sistemi) or no same-bucket child
}
return { input, counts, hasChildren };
}
export interface TemplateRow {
bucketId: string;
subId: string | null;
}
// Overlay the per-brand structure template onto a vehicle's grouped tree so the
// FULL canonical structure shows immediately: any bucket / sub-leaf the brand is
// known to have but this vehicle hasn't deep-drilled yet is added as an empty
// (partCount 0) entry. As the prefetch backfill drills, these fill with real
// source categories + parts. Never removes the vehicle's own data.
export function overlayTemplate(
groups: CanonicalGroup[],
template: TemplateRow[],
buckets: CanonicalBucket[],
subLeaves: SubLeafRef[],
// Entry-level template: the brand's shared source-category NAMES per bucket/
// sub. Names this vehicle doesn't have yet are added as disabled placeholders
// so the FULL browsable structure shows immediately after decode ("yapı aynı,
// sadece parça kodları/şemalar araca özel").
entries: TemplateEntry[] = [],
): CanonicalGroup[] {
if (template.length === 0 && entries.length === 0) return groups;
const bucketById = new Map(buckets.map((b) => [b.id, b]));
const subById = new Map(subLeaves.map((s) => [s.id, s]));
const byBucket = new Map(groups.map((g) => [g.canonical.id, g]));
const templBuckets = new Set<string>();
const templSubsByBucket = new Map<string, Set<string>>();
for (const t of template) {
templBuckets.add(t.bucketId);
if (t.subId) {
const set = templSubsByBucket.get(t.bucketId) ?? new Set<string>();
set.add(t.subId);
templSubsByBucket.set(t.bucketId, set);
}
}
const result = [...groups];
for (const bid of templBuckets) {
let g = byBucket.get(bid);
if (!g) {
const b = bucketById.get(bid);
if (!b) continue;
g = { canonical: { id: b.id, slug: b.slug, name: b.name }, partCount: 0, categories: [] };
result.push(g);
byBucket.set(bid, g);
}
const wantSubs = templSubsByBucket.get(bid);
if (!wantSubs || wantSubs.size === 0) continue;
if (!g.subgroups) g.subgroups = [];
const present = new Set(g.subgroups.map((sg) => sg.canonical.id).filter(Boolean));
for (const sid of wantSubs) {
if (present.has(sid)) continue;
const s = subById.get(sid);
if (!s) continue;
g.subgroups.push({
canonical: { id: s.id, slug: s.slug, name: s.name },
partCount: 0,
categories: [],
});
}
g.subgroups.sort((a, z) => z.partCount - a.partCount);
}
// Merge entry-level template names as placeholders (skip ones the vehicle
// already shows anywhere in the bucket — matched on folded name).
if (entries.length > 0) {
let tplSeq = 0;
for (const e of entries) {
let g = byBucket.get(e.bucketId);
if (!g) {
const b = bucketById.get(e.bucketId);
if (!b) continue;
g = { canonical: { id: b.id, slug: b.slug, name: b.name }, partCount: 0, categories: [] };
result.push(g);
byBucket.set(e.bucketId, g);
}
const have = new Set<string>();
for (const c of g.categories) have.add(foldName(c.name));
for (const sg of g.subgroups ?? []) for (const c of sg.categories) have.add(foldName(c.name));
if (have.has(foldName(e.name))) continue;
const leaf: CanonicalLeaf = {
id: `tpl-${++tplSeq}`,
name: e.name,
source: "template",
partCount: 0,
unavailable: true,
};
const sub = e.subId ? subById.get(e.subId) : undefined;
if (sub) {
if (!g.subgroups) g.subgroups = [];
let sg = g.subgroups.find((x) => x.canonical.id === sub.id);
if (!sg) {
sg = {
canonical: { id: sub.id, slug: sub.slug, name: sub.name },
partCount: 0,
categories: [],
};
g.subgroups.push(sg);
}
sg.categories.push(leaf);
} else {
g.categories.push(leaf);
}
}
}
const order = new Map(buckets.map((b, i) => [b.id, i]));
result.sort(
(a, z) => (order.get(a.canonical.id ?? "") ?? 999) - (order.get(z.canonical.id ?? "") ?? 999),
);
return result;
}
export function groupCategoriesByCanonical(
cats: RawCategory[],
partCounts: Map<string, number>,
buckets: CanonicalBucket[],
// When false, categories with no parts are still included (used for the
// root-level fallback on freshly-decoded vehicles whose leaves/parts aren't
// populated yet). Default true keeps the part-holding-leaf behavior.
// subLeaves: supply the canonical_categories sub-rows (parentId set) to emit
// a second level (subgroups) inside each bucket.
// hasChildren: ids of branch nodes; used to dedupe duplicate categories
// (some decodes emit a root twice — keep the populated/drilled copy).
opts: { requireParts?: boolean; subLeaves?: SubLeafRef[]; hasChildren?: Set<string> } = {},
): CanonicalGroup[] {
const requireParts = opts.requireParts ?? true;
const hasChildren = opts.hasChildren ?? new Set<string>();
// Collapse leaves that share a folded name, keeping the richest copy (most
// parts, then has-children). Fixes "Motor, Motor" from duplicate roots.
const dedupe = (leaves: LeafWithSub[]): LeafWithSub[] => {
const best = new Map<string, LeafWithSub>();
const rank = (l: LeafWithSub) => l.partCount * 2 + (hasChildren.has(l.id) ? 1 : 0);
for (const l of leaves) {
const key = foldName(l.name);
const cur = best.get(key);
if (!cur || rank(l) > rank(cur)) best.set(key, l);
}
return [...best.values()];
};
const slugToBucket = new Map(buckets.map((b) => [b.slug, b]));
const idToBucket = new Map(buckets.map((b) => [b.id, b]));
const subById = new Map((opts.subLeaves ?? []).map((s) => [s.id, s]));
const subSlugToId = new Map((opts.subLeaves ?? []).map((s) => [s.slug, s.id]));
const emitSubs = (opts.subLeaves?.length ?? 0) > 0;
const grouped = new Map<string, { partCount: number; categories: LeafWithSub[] }>();
const other: { partCount: number; categories: LeafWithSub[] } = { partCount: 0, categories: [] };
for (const c of cats) {
const partCount = partCounts.get(c.id) ?? 0;
if (requireParts && partCount === 0) continue; // only clickable, part-holding leaves
let bucket = c.canonicalId ? idToBucket.get(c.canonicalId) : undefined;
const folded = foldName(c.name);
if (!bucket) {
// Skip vehicle-selector / code nodes entirely (Opel model roots like
// "A05 zafira-b 2005-2015") so they don't clutter the "Diğer" bucket;
// only genuine unmapped part-categories fall through to "Diğer".
if (classifyNode(folded) !== "part") continue;
const { slug } = mapToCanonical(folded);
if (slug) bucket = slugToBucket.get(slug);
}
// sub: prefer materialized subId, else map on-the-fly within the bucket.
let subId = c.subId ?? null;
if (!subId && emitSubs && bucket) {
const subSlug = mapToSubcategory(folded, bucket.slug);
if (subSlug) subId = subSlugToId.get(subSlug) ?? null;
}
const leaf: LeafWithSub = { id: c.id, name: c.name, source: c.source, partCount, subId };
const target = bucket ? (grouped.get(bucket.id) ?? { partCount: 0, categories: [] }) : other;
target.categories.push(leaf);
target.partCount += partCount;
if (bucket) grouped.set(bucket.id, target);
}
const strip = (l: LeafWithSub): CanonicalLeaf => ({
id: l.id,
name: l.name,
source: l.source,
partCount: l.partCount,
});
const buildSubgroups = (leaves: LeafWithSub[]): CanonicalSubgroup[] => {
const bySub = new Map<string, { partCount: number; categories: CanonicalLeaf[] }>();
const genel: CanonicalSubgroup = {
canonical: { id: null, slug: "genel", name: "Genel" },
partCount: 0,
categories: [],
};
for (const l of leaves) {
const sub = l.subId ? subById.get(l.subId) : undefined;
if (!sub) {
genel.categories.push(strip(l));
genel.partCount += l.partCount;
continue;
}
const g = bySub.get(sub.id) ?? { partCount: 0, categories: [] };
g.categories.push(strip(l));
g.partCount += l.partCount;
bySub.set(sub.id, g);
}
const subs: CanonicalSubgroup[] = [...bySub.entries()].map(([id, g]) => {
const s = subById.get(id) as SubLeafRef;
g.categories.sort((a, z) => z.partCount - a.partCount);
return { canonical: { id: s.id, slug: s.slug, name: s.name }, ...g };
});
subs.sort((a, z) => z.partCount - a.partCount);
if (genel.categories.length > 0) {
genel.categories.sort((a, z) => z.partCount - a.partCount);
subs.push(genel);
}
return subs;
};
const result: CanonicalGroup[] = buckets
.filter((b) => grouped.has(b.id))
.map((b) => {
const g = grouped.get(b.id) as { partCount: number; categories: LeafWithSub[] };
const deduped = dedupe(g.categories);
const subgroups = emitSubs ? buildSubgroups(deduped) : undefined;
const categories = deduped.map(strip).sort((a, z) => z.partCount - a.partCount);
return {
canonical: { id: b.id, slug: b.slug, name: b.name },
partCount: g.partCount,
categories,
...(subgroups ? { subgroups } : {}),
};
});
if (other.categories.length > 0) {
const categories = dedupe(other.categories)
.map(strip)
.sort((a, z) => z.partCount - a.partCount);
result.push({
canonical: { id: null, slug: "diger", name: "Diğer" },
partCount: other.partCount,
categories,
});
}
return result;
}

View File

@@ -5,9 +5,19 @@ import { CategoriesService } from "./categories.service";
export class CategoriesController { export class CategoriesController {
constructor(private categoriesService: CategoriesService) {} constructor(private categoriesService: CategoriesService) {}
// Sources this vehicle has (default/eper first) — drives the source switcher.
// Declared before "tree/:vehicleId" so the literal segment wins the match.
@Get("tree/:vehicleId/sources")
async getCatalogSources(@Param("vehicleId") vehicleId: string) {
return this.categoriesService.getCatalogSources(vehicleId);
}
@Get("tree/:vehicleId") @Get("tree/:vehicleId")
async getCategoryTree(@Param("vehicleId") vehicleId: string) { async getCategoryTree(
return this.categoriesService.getCategoryTree(vehicleId); @Param("vehicleId") vehicleId: string,
@Query("source") source?: string,
) {
return this.categoriesService.getCategoryTree(vehicleId, source);
} }
@Get("search/:vehicleId") @Get("search/:vehicleId")
@@ -15,6 +25,27 @@ export class CategoriesController {
return this.categoriesService.searchCatalog(vehicleId, q ?? ""); return this.categoriesService.searchCatalog(vehicleId, q ?? "");
} }
// The 20-bucket unified taxonomy (browse landing / filter chips). Declared
// before ":id" so the literal path isn't captured by the id param route.
@Get("canonical")
async getCanonicalCategories() {
return this.categoriesService.getCanonicalCategories();
}
// Full 2-level canonical taxonomy (buckets + sub-leaves + population counts) —
// the source-independent structure exposed to B2C API consumers. Declared
// before "canonical/:vehicleId" so the literal isn't captured as a vehicleId.
@Get("canonical/taxonomy")
async getCanonicalTaxonomy() {
return this.categoriesService.getCanonicalTaxonomy();
}
// A vehicle's catalog folded into the canonical buckets (unified browse).
@Get("canonical/:vehicleId")
async getCanonicalTree(@Param("vehicleId") vehicleId: string) {
return this.categoriesService.getCanonicalTree(vehicleId);
}
@Get(":id/children") @Get(":id/children")
async getChildren(@Param("id") id: string) { async getChildren(@Param("id") id: string) {
return this.categoriesService.getChildren(id); return this.categoriesService.getChildren(id);

View File

@@ -320,6 +320,84 @@ describe("CategoriesService", () => {
"drill-load-error", "drill-load-error",
); );
}); });
it("routes a P4 Ford VIN node (json-vin-sub-group.action, positional link_wid) to children, not an empty leaf", async () => {
// link_wid "1" is a positional code (not a *Group* table) so the link_wid
// group-drill branch misses it; the path is …group.action (not /psa/, not
// vin-group.action) so the PSA / Volvo branches miss it too. Before the
// dedicated gate these fell through to the leaf parts path → silent empty
// panel (Ford/Nissan/Opel analog of the PSA-parent-gate gap).
const category = {
id: "ford1",
name: "elektrik sistemi",
nameOriginal: "elektrik sistemi",
parentId: null,
vehicleId: "v1",
source: "pl24",
linkPath: "/ford/fordp_parts/json-vin-sub-group.action?mainGroupId=CAP1&vin=WF0FXX",
linkWid: "1",
hasSubgroups: null,
hasParts: null,
};
let selectCall = 0;
const db = {
select: vi.fn().mockImplementation(() => {
selectCall++;
const captured = selectCall;
const c: Record<string, any> = {};
c.from = vi.fn().mockReturnValue(c);
c.where = vi.fn().mockImplementation(() => (captured === 2 ? [] : c));
c.limit = vi.fn().mockReturnValue([category]);
return c;
}),
execute: vi.fn().mockResolvedValue([]),
};
const { service } = createService(db);
const discovered = [{ id: "sg1", name: "kablolar", children: undefined }];
vi.spyOn(service, "getChildren").mockResolvedValue(discovered as any);
const result = await service.getCategoryWithParts("ford1");
expect(service.getChildren).toHaveBeenCalledWith("ford1");
expect(result.parts).toEqual([]);
expect((result as { children?: unknown }).children).toEqual(discovered);
});
it("surfaces loadError when a P4 group.action drill comes back empty (not a silent leaf)", async () => {
const category = {
id: "ford2",
name: "kaporta",
nameOriginal: "kaporta",
parentId: null,
vehicleId: "v1",
source: "pl24",
linkPath: "/ford/fordp_parts/json-vin-sub-group.action?mainGroupId=GDB&vin=WF0FXX",
linkWid: "2",
hasSubgroups: null,
hasParts: null,
};
let selectCall = 0;
const db = {
select: vi.fn().mockImplementation(() => {
selectCall++;
const captured = selectCall;
const c: Record<string, any> = {};
c.from = vi.fn().mockReturnValue(c);
c.where = vi.fn().mockImplementation(() => (captured === 2 ? [] : c));
c.limit = vi.fn().mockReturnValue([category]);
return c;
}),
execute: vi.fn().mockResolvedValue([]),
};
const { service } = createService(db);
vi.spyOn(service, "getChildren").mockResolvedValue([] as any);
const result = await service.getCategoryWithParts("ford2");
expect(service.getChildren).toHaveBeenCalledWith("ford2");
expect((result as { loadError?: boolean }).loadError).toBe(true);
expect((result as { children?: unknown }).children).toBeUndefined();
});
}); });
describe("getAncestors", () => { describe("getAncestors", () => {

View File

@@ -1,11 +1,19 @@
import { Inject, Injectable, Logger, NotFoundException } from "@nestjs/common"; import { Inject, Injectable, Logger, NotFoundException } from "@nestjs/common";
import { and, eq, ilike, inArray, isNull, or, sql } from "drizzle-orm"; import { and, eq, gte, ilike, inArray, isNotNull, isNull, or, sql } from "drizzle-orm";
import { import {
type CatalogDegradationKind, type CatalogDegradationKind,
reportCatalogDegradation, reportCatalogDegradation,
} from "../common/catalog-degradation"; } from "../common/catalog-degradation";
import { DATABASE, type Database } from "../database/database.provider"; import { DATABASE, type Database } from "../database/database.provider";
import { categories, parts, schemaPics, vehicles } from "../database/schema/core"; import {
canonicalCategories,
canonicalStructureTemplate,
canonicalTemplateEntries,
categories,
parts,
schemaPics,
vehicles,
} from "../database/schema/core";
import { EmexSourceDbService } from "../integrations/catalog-source-db/emex-source-db.service"; import { EmexSourceDbService } from "../integrations/catalog-source-db/emex-source-db.service";
import { PcatSourceDbService } from "../integrations/catalog-source-db/pcat-source-db.service"; import { PcatSourceDbService } from "../integrations/catalog-source-db/pcat-source-db.service";
import { EmexService } from "../integrations/emex/emex.service"; import { EmexService } from "../integrations/emex/emex.service";
@@ -14,9 +22,15 @@ import { PcatGroup } from "../integrations/parts-catalogs/parts-catalogs.types";
import { PL24FordLegacyService } from "../integrations/pl24/pl24-ford-legacy.service"; import { PL24FordLegacyService } from "../integrations/pl24/pl24-ford-legacy.service";
import { PL24PsaService } from "../integrations/pl24/pl24-psa.service"; import { PL24PsaService } from "../integrations/pl24/pl24-psa.service";
import { PL24Service } from "../integrations/pl24/pl24.service"; import { PL24Service } from "../integrations/pl24/pl24.service";
import { classifyNode, foldName, mapToCanonical } from "../jobs/canonical-lexicon";
import { RedisService } from "../redis/redis.service"; import { RedisService } from "../redis/redis.service";
import { StorageService } from "../storage/storage.service"; import { StorageService } from "../storage/storage.service";
import { TranslationsService } from "../translations/translations.service"; import { TranslationsService } from "../translations/translations.service";
import {
buildCanonicalInput,
groupCategoriesByCanonical,
overlayTemplate,
} from "./canonical-grouping";
@Injectable() @Injectable()
export class CategoriesService { export class CategoriesService {
@@ -36,8 +50,8 @@ export class CategoriesService {
private emexSourceDb: EmexSourceDbService, private emexSourceDb: EmexSourceDbService,
) {} ) {}
async getCategoryTree(vehicleId: string) { async getCategoryTree(vehicleId: string, sourceFilter?: string) {
const cacheKey = `cat:tree:${vehicleId}`; const cacheKey = `cat:tree:${vehicleId}${sourceFilter ? `:${sourceFilter}` : ""}`;
const cached = await this.redis.getJson<any[]>(cacheKey); const cached = await this.redis.getJson<any[]>(cacheKey);
if (cached) return cached; if (cached) return cached;
@@ -120,6 +134,10 @@ export class CategoriesService {
externalId: s.code, externalId: s.code,
linkPath: s.linkPath || null, linkPath: s.linkPath || null,
linkWid: null as string | null, linkWid: null as string | null,
// PSA scopes are always parents (they hold main-groups, never direct
// parts) — flag them so the tree UI signposts them as folders and the
// prefetch worker pre-drills them instead of treating them as leaves.
hasSubgroups: true,
source: "pl24" as const, source: "pl24" as const,
})); }));
dbCategories = await this.db dbCategories = await this.db
@@ -181,6 +199,10 @@ export class CategoriesService {
externalId: c.code, externalId: c.code,
linkPath: c.linkPath || null, linkPath: c.linkPath || null,
linkWid: c.linkWid || null, linkWid: c.linkWid || null,
// Mark P4 VIN/legacy group-drill nodes (…group.action) as parents so
// the tree UI shows them as folders and prefetch pre-drills them. Leaf
// pages (image-board.action/bom) stay null (unknown→leaf downstream).
hasSubgroups: c.linkPath?.includes("group.action") ? true : null,
source: "pl24" as const, source: "pl24" as const,
})); }));
@@ -435,8 +457,23 @@ export class CategoriesService {
partCountRows.map((r) => [r.categoryId, r.count]), partCountRows.map((r) => [r.categoryId, r.count]),
); );
// Default catalog source for a multi-source vehicle: prefer eper-pekidi — the
// native OEM ePER tree Fiat dealers already know, fully populated with parts +
// diagrams — over parts-catalogs' broader-but-lazy (drill-on-demand) folders,
// which is where dealers hit gaps (a motor OEM missing under "Motor"). pcat is
// never dropped: the ?source override + /sources endpoint keep it one tap away.
const presentSources = [...new Set(dbCategories.map((c) => c.source))];
const activeSource =
sourceFilter && presentSources.includes(sourceFilter)
? sourceFilter
: presentSources.includes("eper-pekidi")
? "eper-pekidi"
: null;
const forTree = activeSource
? dbCategories.filter((c) => c.source === activeSource)
: dbCategories;
// Build tree // Build tree
const tree = this.buildTree(dbCategories, directPartCounts); const tree = this.buildTree(forTree, directPartCounts);
// Cache a populated tree for an hour; an EMPTY tree (transient decode/proxy // Cache a populated tree for an hour; an EMPTY tree (transient decode/proxy
// failure) only for 60s so a blip doesn't poison the catalog for an hour — // failure) only for 60s so a blip doesn't poison the catalog for an hour —
// it self-heals on the next request after the source recovers, while still // it self-heals on the next request after the source recovers, while still
@@ -458,6 +495,210 @@ export class CategoriesService {
return tree; return tree;
} }
/**
* The catalog sources a decoded vehicle has categories from, ordered with the
* default first (eper-pekidi — the dealer-familiar native ePER catalog). Drives
* the per-vehicle source switcher (ePER ⇄ Diğer); a single source → no switcher.
*/
async getCatalogSources(vehicleId: string): Promise<string[]> {
const rows = await this.db
.selectDistinct({ source: categories.source })
.from(categories)
.where(eq(categories.vehicleId, vehicleId));
const present = rows.map((r) => r.source);
const order = ["eper-pekidi", "parts-catalogs", "emex", "pl24"];
return present.sort((a, b) => {
const ia = order.indexOf(a);
const ib = order.indexOf(b);
return (ia === -1 ? 99 : ia) - (ib === -1 ? 99 : ib);
});
}
/**
* The unified canonical taxonomy (20 top buckets), source/brand-independent.
* Used for the browse landing + as the fixed set of headings every vehicle's
* catalog folds into.
*/
async getCanonicalCategories() {
return this.db
.select({
id: canonicalCategories.id,
slug: canonicalCategories.slug,
name: canonicalCategories.name,
})
.from(canonicalCategories)
.where(isNull(canonicalCategories.parentId))
.orderBy(canonicalCategories.sortOrder);
}
/**
* Full 2-level canonical taxonomy — the source-independent category structure
* a B2C API consumer browses by: 20 top buckets, each with its curated
* sub-leaves (Fren → Balata/Disk/Kaliper…). `mappedCategories` shows how many
* source categories currently fall in each node (population signal); sub-leaves
* with 0 are still listed so the contract is stable. Sub-mapping is completed
* over time in the super-panel, so counts only grow.
*/
async getCanonicalTaxonomy() {
const nodes = await this.db
.select({
id: canonicalCategories.id,
parentId: canonicalCategories.parentId,
slug: canonicalCategories.slug,
name: canonicalCategories.name,
sortOrder: canonicalCategories.sortOrder,
})
.from(canonicalCategories)
.orderBy(canonicalCategories.sortOrder);
// population per node: top buckets count canonical_category_id, sub-leaves
// count canonical_subcategory_id. One grouped scan each (both columns indexed).
const topCounts = await this.db
.select({ id: categories.canonicalCategoryId, n: sql<number>`count(*)::int` })
.from(categories)
.where(isNotNull(categories.canonicalCategoryId))
.groupBy(categories.canonicalCategoryId);
const subCounts = await this.db
.select({ id: categories.canonicalSubcategoryId, n: sql<number>`count(*)::int` })
.from(categories)
.where(isNotNull(categories.canonicalSubcategoryId))
.groupBy(categories.canonicalSubcategoryId);
const countById = new Map<string, number>();
for (const r of [...topCounts, ...subCounts]) if (r.id) countById.set(r.id, r.n);
const subsByParent = new Map<string, typeof nodes>();
for (const n of nodes) {
if (!n.parentId) continue;
const arr = subsByParent.get(n.parentId) ?? [];
arr.push(n);
subsByParent.set(n.parentId, arr);
}
return nodes
.filter((n) => !n.parentId)
.map((b) => ({
id: b.id,
slug: b.slug,
name: b.name,
mappedCategories: countById.get(b.id) ?? 0,
subcategories: (subsByParent.get(b.id) ?? []).map((s) => ({
id: s.id,
slug: s.slug,
name: s.name,
mappedCategories: countById.get(s.id) ?? 0,
})),
}));
}
/**
* Unified browse: a vehicle's part-holding leaf categories grouped under the
* 20 canonical buckets, so VW and Opel (whose raw OEM trees differ wildly) are
* navigated through one identical set of headings. Each entry lists the source
* leaf categories that fall in that bucket for this vehicle, with part counts;
* the UI drills into a leaf via the existing /categories/:id endpoint.
*
* Bucketing uses the materialized categories.canonical_category_id when set,
* falling back to on-the-fly mapping (shared canonical-lexicon) for categories
* decoded since the last backfill. Categories with no bucket land under "Diğer"
* so nothing is hidden.
*/
async getCanonicalTree(vehicleId: string) {
// Ensure the vehicle's raw source tree is populated (lazy decode + cache).
await this.getCategoryTree(vehicleId);
const buckets = await this.getCanonicalCategories();
const subLeaves = await this.db
.select({
id: canonicalCategories.id,
parentId: canonicalCategories.parentId,
slug: canonicalCategories.slug,
name: canonicalCategories.name,
})
.from(canonicalCategories)
.where(isNotNull(canonicalCategories.parentId))
.orderBy(canonicalCategories.sortOrder);
const cats = await this.db
.select({
id: categories.id,
name: categories.name,
source: categories.source,
canonicalId: categories.canonicalCategoryId,
subId: categories.canonicalSubcategoryId,
parentId: categories.parentId,
})
.from(categories)
.where(eq(categories.vehicleId, vehicleId));
const partCountRows = await this.db
.select({ categoryId: parts.categoryId, count: sql<number>`count(*)::int` })
.from(parts)
.where(eq(parts.vehicleId, vehicleId))
.groupBy(parts.categoryId);
const partCounts = new Map(partCountRows.map((r) => [r.categoryId, r.count]));
const {
input: shown,
counts: shownCounts,
hasChildren,
} = buildCanonicalInput(cats, partCounts, buckets);
const groups = groupCategoriesByCanonical(shown, shownCounts, buckets, {
requireParts: false,
subLeaves,
hasChildren,
});
// Overlay the per-brand structure template: buckets / sub-leaves this brand
// is known to have but this vehicle hasn't deep-drilled yet appear as empty
// entries, so the FULL canonical structure shows immediately after decode.
const veh = await this.db
.select({ brand: vehicles.brandName, source: vehicles.source })
.from(vehicles)
.where(eq(vehicles.id, vehicleId))
.limit(1);
const brand = veh[0]?.brand;
const source = cats[0]?.source ?? veh[0]?.source;
if (!brand || !source) return groups;
const template = await this.db
.select({
bucketId: canonicalStructureTemplate.canonicalCategoryId,
subId: canonicalStructureTemplate.canonicalSubcategoryId,
})
.from(canonicalStructureTemplate)
.where(
and(
eq(canonicalStructureTemplate.source, source),
eq(canonicalStructureTemplate.brand, brand),
gte(canonicalStructureTemplate.vehicleCount, 2),
),
);
const templateEntriesRaw = await this.db
.select({
bucketId: canonicalTemplateEntries.canonicalCategoryId,
subId: canonicalTemplateEntries.canonicalSubcategoryId,
name: canonicalTemplateEntries.name,
vehicleCount: canonicalTemplateEntries.vehicleCount,
})
.from(canonicalTemplateEntries)
.where(
and(
eq(canonicalTemplateEntries.source, source),
eq(canonicalTemplateEntries.brand, brand),
gte(canonicalTemplateEntries.vehicleCount, 2),
),
);
// Cap placeholders at the MOST-SHARED 40 per bucket — some brands carry
// hundreds of model-variant names (pl24 BMW elektrik: 500+) that would
// drown the view; the widely-shared ones are the real "brand structure".
templateEntriesRaw.sort((a, z) => z.vehicleCount - a.vehicleCount);
const perBucket = new Map<string, number>();
const templateEntries = templateEntriesRaw.filter((e) => {
const n = (perBucket.get(e.bucketId) ?? 0) + 1;
perBucket.set(e.bucketId, n);
return n <= 40;
});
return overlayTemplate(groups, template, buckets, subLeaves, templateEntries);
}
/** /**
* Get sub-categories (sub-groups) for a category. * Get sub-categories (sub-groups) for a category.
* Fetches from PL24 on-demand if not cached. * Fetches from PL24 on-demand if not cached.
@@ -1123,6 +1364,39 @@ export class CategoriesService {
// No subgroups → leaf; fall through to the parts path below. // No subgroups → leaf; fall through to the parts path below.
} }
// PL24 P4 VIN / legacy group node whose link_wid is a positional code
// ("1","2","CAP1"…) rather than a *Group* table, so the link_wid branch above
// misses it, and which is neither PSA- nor Volvo-special. Its linkPath is a
// sub-group / main-group drill action (…group.action — never a parts leaf like
// image-board.action). This is the Ford / Nissan / Opel / Hyundai-Kia analog of
// the PSA-parent-gate gap: without it these VIN scopes ("mekanik", "kaporta",
// "Elektrikli aksam"…) fell through every parent branch to the leaf parts path,
// fetched no parts and rendered a silent empty panel — confirmed the dominant
// current "0 parça" cluster. Volvo's vin-group.action keeps its own branch above
// (it may legitimately bottom out in parts), so exclude it here. Drill on demand:
// subgroups → parent; an empty drill is a transient upstream failure → retryable
// load error (a group node is never legitimately a parts leaf).
if (
category.source === "pl24" &&
category.linkPath?.includes("group.action") &&
!category.linkPath.includes("vin-group.action") &&
category.vehicleId
) {
const p4Children = await this.getChildren(categoryId);
const base = {
id: category.id,
name: category.name,
description: category.nameOriginal || null,
parentId: category.parentId || null,
parts: [],
schemaPics: [],
hotspots: [],
};
return p4Children.length > 0
? { ...base, children: p4Children }
: { ...base, loadError: true };
}
// EMEX Vehicle.aspx group node (linkWid="emex-group") — a parent, never a // EMEX Vehicle.aspx group node (linkWid="emex-group") — a parent, never a
// parts leaf. Return its children (seeded sub-groups, or Unit leaves drilled // parts leaf. Return its children (seeded sub-groups, or Unit leaves drilled
// on demand by getChildren). Mirrors the pl24 group-node branch above; an // on demand by getChildren). Mirrors the pl24 group-node branch above; an

View File

@@ -0,0 +1,96 @@
import {
type CanActivate,
type ExecutionContext,
HttpException,
HttpStatus,
Inject,
Injectable,
UnauthorizedException,
} from "@nestjs/common";
import { eq } from "drizzle-orm";
import { type ApiKeyRecord, getApiKeyApi } from "../../auth/auth";
import { DATABASE, type Database } from "../../database/database.provider";
import { users } from "../../database/schema/core";
export type VerifiedApiKey = ApiKeyRecord;
/**
* Public API (/api/v1) kimlik doğrulaması. Cookie tabanlı AuthGuard'dan
* tamamen ayrı: anahtar `Authorization: Bearer sase_...` veya `x-api-key`
* header'ında gelir, better-auth apiKey plugin'i ile doğrulanır (hash
* karşılaştırma + enabled/expiry/anahtar-başına rate-limit). Geçerliyse
* anahtarın sahibi kullanıcı yüklenir ve `request.user` + `request.apiKey`
* doldurulur — böylece mevcut servisler (decodeVin'in abonelik/marka
* yetki kontrolü dahil) hiçbir değişiklik olmadan çalışır.
*
* Controller'lar bu guard'ı `@Public()` ile birlikte kullanmalı; @Public
* global AuthGuard'ı atlatır, kimlik işini bu guard devralır.
*/
@Injectable()
export class ApiKeyGuard implements CanActivate {
constructor(@Inject(DATABASE) private readonly db: Database) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
const rawKey = this.extractKey(request.headers);
if (!rawKey) {
throw new UnauthorizedException(
"API anahtarı gerekli. Anahtarınızı 'Authorization: Bearer <anahtar>' header'ında gönderin.",
);
}
let result: Awaited<ReturnType<ReturnType<typeof getApiKeyApi>["verifyApiKey"]>>;
try {
result = await getApiKeyApi().verifyApiKey({ body: { key: rawKey } });
} catch {
throw new UnauthorizedException("Geçersiz API anahtarı.");
}
if (!result.valid || !result.key) {
const code = result.error?.code ?? "INVALID_API_KEY";
if (code === "RATE_LIMITED" || code === "USAGE_EXCEEDED" || code === "TOO_MANY_REQUESTS") {
throw new HttpException(
"İstek limiti aşıldı. Lütfen bekleyip tekrar deneyin.",
HttpStatus.TOO_MANY_REQUESTS,
);
}
if (code === "KEY_EXPIRED") {
throw new UnauthorizedException("API anahtarının süresi dolmuş.");
}
if (code === "KEY_DISABLED") {
throw new UnauthorizedException("API anahtarı devre dışı bırakılmış.");
}
throw new UnauthorizedException("Geçersiz API anahtarı.");
}
const key = result.key;
const [user] = await this.db.select().from(users).where(eq(users.id, key.userId)).limit(1);
if (!user) {
throw new UnauthorizedException("API anahtarının sahibi bulunamadı.");
}
if (user.status !== "active") {
throw new UnauthorizedException(
"Hesabınız askıya alınmış durumda. Destek ile iletişime geçin.",
);
}
request.user = user;
request.apiKey = key;
return true;
}
/** Authorization: Bearer <key> (tercih edilen) veya x-api-key header'ı. */
private extractKey(headers: Record<string, unknown>): string | null {
const authHeader = headers.authorization;
if (typeof authHeader === "string" && authHeader.length > 0) {
const match = authHeader.match(/^Bearer\s+(.+)$/i);
const value = (match ? match[1] : authHeader).trim();
if (value.length > 0) return value;
}
const apiKeyHeader = headers["x-api-key"];
if (typeof apiKeyHeader === "string" && apiKeyHeader.trim().length > 0) {
return apiKeyHeader.trim();
}
return null;
}
}

View File

@@ -1,10 +1,10 @@
import { timingSafeEqual } from "node:crypto";
import { import {
type CanActivate, type CanActivate,
type ExecutionContext, type ExecutionContext,
Injectable, Injectable,
UnauthorizedException, UnauthorizedException,
} from "@nestjs/common"; } from "@nestjs/common";
import { timingSafeEqual } from "node:crypto";
@Injectable() @Injectable()
export class InternalTokenGuard implements CanActivate { export class InternalTokenGuard implements CanActivate {

View File

@@ -88,6 +88,22 @@ export default () => ({
.map((s) => s.trim()) .map((s) => s.trim())
.filter(Boolean), .filter(Boolean),
}, },
p: {
// Read-only lookup against the imported P snapshot (db `td`). When
// enabled + url set, the OEM detail page resolves a part's OEM code to
// P aftermarket equivalents + OE cross-references. Disabled → endpoint
// returns { matched: false } and the UI shows an empty state.
enabled: process.env.P_DB_ENABLED === "true",
url: process.env.P_DB_URL,
},
supplierPrices: {
// Tedarikçi fiyat geçmişi (takip MySQL, Tailscale). Kapalı/boş URL →
// part-prices uçları { matched: false } / boş batch döner; UI fiyat
// bölümünü hiç render etmez. Tedarikçi kimliği API cevabına asla çıkmaz —
// yalnızca stoktaki tekliflerin p50/p95/p99 dağılımı servis edilir.
enabled: process.env.SUPPLIER_PRICE_DB_ENABLED === "true",
url: process.env.SUPPLIER_PRICE_DB_URL,
},
otel: { otel: {
enabled: process.env.OTEL_ENABLED === "true", enabled: process.env.OTEL_ENABLED === "true",
endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT, endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,

View File

@@ -1,10 +1,12 @@
import { import {
boolean, boolean,
date,
index, index,
integer, integer,
jsonb, jsonb,
numeric, numeric,
pgTable, pgTable,
primaryKey,
text, text,
timestamp, timestamp,
uniqueIndex, uniqueIndex,
@@ -37,6 +39,10 @@ export const users = pgTable(
// subscription yet (referrer had no active/trial sub at grant time). // subscription yet (referrer had no active/trial sub at grant time).
// Consumed when the user next starts a trial or activates a subscription. // Consumed when the user next starts a trial or activates a subscription.
referralCreditDays: integer("referral_credit_days").default(0).notNull(), referralCreditDays: integer("referral_credit_days").default(0).notNull(),
// Stripe Customer backing this user's recurring billing. Set on the first
// completed checkout and reused on later checkouts so saved cards and
// invoices stay on one customer record.
stripeCustomerId: text("stripe_customer_id"),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(), createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
}, },
@@ -110,6 +116,46 @@ export const verifications = pgTable(
(table) => [index("verifications_identifier_idx").on(table.identifier)], (table) => [index("verifications_identifier_idx").on(table.identifier)],
); );
// ─── Better Auth: API Keys (public API) ──────────────
// Backing table for the better-auth `apiKey` plugin (model "apikey", plural).
// Column set mirrors the plugin's schema exactly — the drizzle adapter matches
// by property name, so renaming a property here breaks key verification.
// `key` holds the SHA256 hash of the secret; the plaintext is shown once at
// creation. `metadata` is a JSON string (enableMetadata) carrying
// per-key overrides such as dailyDecodeLimit.
export const apikeys = pgTable(
"apikeys",
{
id: text("id").primaryKey(),
name: text("name"),
start: text("start"),
prefix: text("prefix"),
key: text("key").notNull(),
userId: uuid("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
refillInterval: integer("refill_interval"),
refillAmount: integer("refill_amount"),
lastRefillAt: timestamp("last_refill_at", { withTimezone: true }),
enabled: boolean("enabled").default(true).notNull(),
rateLimitEnabled: boolean("rate_limit_enabled").default(true).notNull(),
rateLimitTimeWindow: integer("rate_limit_time_window"),
rateLimitMax: integer("rate_limit_max"),
requestCount: integer("request_count").default(0).notNull(),
remaining: integer("remaining"),
lastRequest: timestamp("last_request", { withTimezone: true }),
expiresAt: timestamp("expires_at", { withTimezone: true }),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
permissions: text("permissions"),
metadata: text("metadata"),
},
(table) => [
index("apikeys_key_idx").on(table.key),
index("apikeys_user_id_idx").on(table.userId),
],
);
// ─── Brands ───────────────────────────────────────── // ─── Brands ─────────────────────────────────────────
export const brands = pgTable( export const brands = pgTable(
"brands", "brands",
@@ -153,12 +199,17 @@ export const userSubscriptions = pgTable(
startDate: timestamp("start_date", { withTimezone: true }), startDate: timestamp("start_date", { withTimezone: true }),
endDate: timestamp("end_date", { withTimezone: true }), endDate: timestamp("end_date", { withTimezone: true }),
cancelledAt: timestamp("cancelled_at", { withTimezone: true }), cancelledAt: timestamp("cancelled_at", { withTimezone: true }),
// Stripe Subscription id for recurring (mode:"subscription") billing.
// NULL for trials and legacy one-time purchases. Renewal invoices resolve
// our row through this.
stripeSubscriptionId: text("stripe_subscription_id"),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(), createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(), updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
}, },
(table) => [ (table) => [
index("user_subscriptions_user_id_idx").on(table.userId), index("user_subscriptions_user_id_idx").on(table.userId),
index("user_subscriptions_status_idx").on(table.status), index("user_subscriptions_status_idx").on(table.status),
index("user_subscriptions_stripe_sub_idx").on(table.stripeSubscriptionId),
], ],
); );
@@ -184,7 +235,9 @@ export const userBrands = pgTable(
], ],
); );
// ─── Bank Accounts (EFT/Havale destination accounts, one active at a time) ──── // ─── Bank Accounts ───────────────────────────────────
// @deprecated EFT/Havale was retired (Stripe is the sole payment method). Table
// kept only to preserve historical FK integrity; no rows are created anymore.
export const bankAccounts = pgTable( export const bankAccounts = pgTable(
"bank_accounts", "bank_accounts",
{ {
@@ -225,7 +278,12 @@ export const payments = pgTable(
iyzicoPaymentId: text("iyzico_payment_id"), iyzicoPaymentId: text("iyzico_payment_id"),
stripeSessionId: text("stripe_session_id"), stripeSessionId: text("stripe_session_id"),
stripePaymentIntentId: text("stripe_payment_intent_id"), stripePaymentIntentId: text("stripe_payment_intent_id"),
// Stripe Invoice behind a recurring charge (renewals). Dedupe key against
// invoice.paid webhook retries.
stripeInvoiceId: text("stripe_invoice_id"),
/** @deprecated EFT/Havale retired (Stripe-only). Kept for historical data. */
bankAccountId: uuid("bank_account_id").references(() => bankAccounts.id), bankAccountId: uuid("bank_account_id").references(() => bankAccounts.id),
/** @deprecated EFT/Havale retired (Stripe-only). Kept for historical data. */
eftReceiptUrl: text("eft_receipt_url"), eftReceiptUrl: text("eft_receipt_url"),
adminNote: text("admin_note"), adminNote: text("admin_note"),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(), createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
@@ -235,6 +293,7 @@ export const payments = pgTable(
index("payments_user_id_idx").on(table.userId), index("payments_user_id_idx").on(table.userId),
index("payments_status_idx").on(table.status), index("payments_status_idx").on(table.status),
index("payments_stripe_session_id_idx").on(table.stripeSessionId), index("payments_stripe_session_id_idx").on(table.stripeSessionId),
index("payments_stripe_invoice_id_idx").on(table.stripeInvoiceId),
], ],
); );
@@ -318,6 +377,32 @@ export const catalogVehicles = pgTable(
], ],
); );
// ─── Vinpin decodes (ePER decode-oracle cache — one row per VIN) ──
// Feature-flagged (VINPIN_ENABLED). Populated by the vinpin-decode BullMQ job
// which drives the Vinpin ePER web catalog for Fiat VINs the normal decode chain
// (PL24/pcat/emex) can't identify. On success the decoded model is matched to an
// EXISTING PL24 catalog_vehicle (catalog_vehicle_id) and parts are served from
// there — Vinpin is ONLY a decode oracle, never a parts source.
export const vinpinDecodes = pgTable("vinpin_decodes", {
vin: text("vin").primaryKey(),
// 'pending' | 'decoded' | 'not_found' | 'failed'
status: text("status").notNull().default("pending"),
brandName: text("brand_name"),
model: text("model"),
sincom: text("sincom"),
trim: text("trim"),
modelYear: text("model_year"),
engine: text("engine"),
catalogVehicleId: uuid("catalog_vehicle_id").references(() => catalogVehicles.id, {
onDelete: "set null",
}),
raw: jsonb("raw"),
attempts: integer("attempts").notNull().default(0),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }),
decodedAt: timestamp("decoded_at", { withTimezone: true }),
});
// ─── Vehicles (shared config — one record per VIN) ── // ─── Vehicles (shared config — one record per VIN) ──
export const vehicles = pgTable( export const vehicles = pgTable(
"vehicles", "vehicles",
@@ -386,12 +471,27 @@ export const categories = pgTable(
hasParts: boolean("has_parts"), hasParts: boolean("has_parts"),
unavailable: boolean("unavailable").default(false).notNull(), unavailable: boolean("unavailable").default(false).notNull(),
source: varchar("source", { length: 20 }).default("pl24").notNull(), source: varchar("source", { length: 20 }).default("pl24").notNull(),
// Materialized result of the name-level canonical mapping (see
// category_canonical_map). Nullable: null = not yet mapped / unmappable.
// Lets callers filter/browse this raw node under the unified taxonomy
// without walking the map table per row.
canonicalCategoryId: uuid("canonical_category_id"),
// Second canonical level: a canonical_categories row whose parentId ==
// canonicalCategoryId (e.g. Fren → Balata). Nullable: null = mapped to the
// top bucket only / no sub-leaf matched → SP manual review completes these.
canonicalSubcategoryId: uuid("canonical_subcategory_id"),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(), createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
}, },
(table) => [ (table) => [
index("categories_vehicle_id_idx").on(table.vehicleId), index("categories_vehicle_id_idx").on(table.vehicleId),
index("categories_catalog_vehicle_id_idx").on(table.catalogVehicleId), index("categories_catalog_vehicle_id_idx").on(table.catalogVehicleId),
index("categories_parent_id_idx").on(table.parentId), index("categories_parent_id_idx").on(table.parentId),
index("categories_canonical_category_id_idx").on(table.canonicalCategoryId),
index("categories_canonical_subcategory_id_idx").on(table.canonicalSubcategoryId),
// NULLS NOT DISTINCT since migration 0030 (raw SQL — drizzle's uniqueIndex
// can't express it): emex/pcat rows have NULL catalog_vehicle_id (pl24 rows
// NULL vehicle_id), and with default NULLs-distinct semantics decode retries
// slipped past onConflictDoNothing and duplicated whole category trees.
uniqueIndex("categories_vehicle_name_source_idx").on( uniqueIndex("categories_vehicle_name_source_idx").on(
table.vehicleId, table.vehicleId,
table.catalogVehicleId, table.catalogVehicleId,
@@ -613,3 +713,339 @@ export const emexCategoryTranslations = pgTable(
}, },
(table) => [uniqueIndex("emex_translations_original_name_idx").on(table.originalName)], (table) => [uniqueIndex("emex_translations_original_name_idx").on(table.originalName)],
); );
// ─── Proxy Logs ──────────────────────────────────────
// One row per proxied upstream attempt (pcat call/capture/validate, emex http).
// Written fire-and-forget by ProxyTelemetryService so the Süper Panel can grade
// proxy providers (success/ban/latency per service) and track banned exit IPs.
// `banned` = HTTP 403/429 from the upstream (IP-level block signal); `session_key`
// identifies a sticky Floxy session or a DataImpulse port so bans are attributable
// even when the exact exit IP is unknown (rotating call legs).
export const proxyLogs = pgTable(
"proxy_logs",
{
id: uuid("id").primaryKey().defaultRandom(),
// Which integration leg made the request: pcat_call | pcat_capture |
// pcat_validate | emex_http
service: varchar("service", { length: 30 }).notNull(),
// floxy | dataimpulse | none (direct)
provider: varchar("provider", { length: 20 }).notNull(),
// Sticky Floxy session id ("s-ab12cd34") or DataImpulse port ("di:10042");
// null on per-request rotating legs (exit IP changes every call).
sessionKey: varchar("session_key", { length: 60 }),
// Resolved residential exit IP (sticky sessions only — one cheap ipify probe
// per session). Null when rotation makes the IP unknowable.
exitIp: varchar("exit_ip", { length: 45 }),
targetHost: varchar("target_host", { length: 120 }),
statusCode: integer("status_code"),
// Transport failure class when no HTTP answer arrived:
// timeout | connect | reset | dns | aborted | transport
errorKind: varchar("error_kind", { length: 30 }),
success: boolean("success").default(false).notNull(),
banned: boolean("banned").default(false).notNull(),
durationMs: integer("duration_ms"),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [
index("proxy_logs_created_at_idx").on(table.createdAt),
index("proxy_logs_banned_created_at_idx").on(table.banned, table.createdAt),
],
);
// ─── OEM Votes (community compatibility verdicts) ─────
// One row per (user, oemCode): a parts seller's uyumlu/uyumsuz verdict on an
// OEM code, cast from the catalog part rows. Votes pool globally per code —
// the part/vehicle/category columns are analytics breadcrumbs only (mirrors
// oem_code_copies: catalog routes pass catalog_vehicles ids here, so no FK).
// Re-voting updates `vote` in place; points are only ever granted on the
// first insert (see oem_vote_points).
export const oemVotes = pgTable(
"oem_votes",
{
id: uuid("id").primaryKey().defaultRandom(),
userId: uuid("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
oemCode: varchar("oem_code", { length: 100 }).notNull(),
// compatible | incompatible
vote: varchar("vote", { length: 12 }).notNull(),
partId: uuid("part_id"),
vehicleId: uuid("vehicle_id"),
categoryId: uuid("category_id"),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [
uniqueIndex("oem_votes_user_oem_idx").on(table.userId, table.oemCode),
index("oem_votes_oem_code_idx").on(table.oemCode),
],
);
// ─── OEM Vote Points (gamification ledger) ────────────
// Append-only: exactly one row per oem_votes row (vote_id unique), written in
// the same transaction as the vote insert. `points` = 1 for voting +2 when
// the vote agreed with the strict majority at cast time (first voter on a
// code always agrees with themselves → 3). Awards are never retro-adjusted
// when the majority later flips. Leaderboard = SUM(points) per user.
export const oemVotePoints = pgTable(
"oem_vote_points",
{
id: uuid("id").primaryKey().defaultRandom(),
userId: uuid("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
voteId: uuid("vote_id")
.notNull()
.references(() => oemVotes.id, { onDelete: "cascade" }),
oemCode: varchar("oem_code", { length: 100 }).notNull(),
points: integer("points").notNull(),
correct: boolean("correct").default(false).notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [
uniqueIndex("oem_vote_points_vote_id_idx").on(table.voteId),
index("oem_vote_points_user_id_idx").on(table.userId),
],
);
// ─── OEM Suggestions (community cross-references) ─────
// Crowd-sourced equivalents for an OEM code: "this code's compatible part is
// <brand> <suggestedCode>", collected on the OEM detail page — primarily for
// codes the P snapshot can't match. suggestedCodeNorm (upper, alphanumerics
// only) powers grouping and the per-user dedup constraint; display keeps the
// submitted casing. `status` is a moderation hook (active|hidden) — no rows
// are hidden automatically today.
export const oemSuggestions = pgTable(
"oem_suggestions",
{
id: uuid("id").primaryKey().defaultRandom(),
userId: uuid("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
oemCode: varchar("oem_code", { length: 100 }).notNull(),
brand: varchar("brand", { length: 80 }).notNull(),
suggestedCode: varchar("suggested_code", { length: 100 }).notNull(),
suggestedCodeNorm: varchar("suggested_code_norm", { length: 100 }).notNull(),
status: varchar("status", { length: 16 }).default("active").notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [
uniqueIndex("oem_suggestions_user_oem_code_idx").on(
table.userId,
table.oemCode,
table.suggestedCodeNorm,
),
index("oem_suggestions_oem_code_idx").on(table.oemCode),
],
);
// ─── OEM Expert Rewards (monthly leaderboard prizes) ──
// One row per (TR-month, rank): the expert-rewards cron (1st of month 00:00
// Europe/Istanbul) closes the finished season, e-mails the top 3 voters to
// admin@sase.tr (name/e-mail/points + prize 30/15/7 days — EXPERT_REWARD_
// LADDER) and records them here. Subscription days are NOT granted
// automatically — the admin applies them manually from the mail. The unique
// index doubles as the run-once guard: a second run for the same period
// inserts nothing and sends no second mail.
export const oemExpertRewards = pgTable(
"oem_expert_rewards",
{
id: uuid("id").primaryKey().defaultRandom(),
// TR-ayının başlangıcı (UTC timestamptz) — ödüllendirilen sezon.
periodStart: timestamp("period_start", { withTimezone: true }).notNull(),
userId: uuid("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
rank: integer("rank").notNull(),
points: integer("points").notNull(),
rewardDays: integer("reward_days").notNull(),
grantedAt: timestamp("granted_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [
uniqueIndex("oem_expert_rewards_period_rank_idx").on(table.periodStart, table.rank),
index("oem_expert_rewards_user_id_idx").on(table.userId),
],
);
// ─── Part Price History (tedarikçi fiyat verisi) ──────
// Parça bazlı piyasa fiyat geçmişi; kaynak takip MySQL'i (Tailscale). Kimlik
// (kod, marka): kısa sayısal kodlar markalar arası çakışır (FEBI 27155 ≠
// GROS 27155) — marka olmadan percentile anlamsızlaşır; markasız ('' brand)
// izleme yalnızca uzun/benzersiz kodlar içindir (OEM detayın ana kodu).
// tracks = izlenen parçalar: ilk seriyi API lazy-backfill yazar, günlük cron
// yalnızca buradakilere bugünün satırını ekler. daily = (kod, marka, kaynak,
// gün) başına stoktaki tekliflerin p50/p95/p99 + teklif sayısı; sıfır-teklif
// günleri null fiyat + offerCount 0 ile saklanır. source şimdilik hep
// 'supplier'; perakende geldiğinde aynı tabloya 'retail' girer. Tedarikçi
// kimliği bilinçli olarak hiçbir yerde tutulmaz.
export const partPriceTracks = pgTable(
"part_price_tracks",
{
codeNorm: varchar("code_norm", { length: 64 }).notNull(),
brandNorm: varchar("brand_norm", { length: 64 }).default("").notNull(),
firstSeenAt: timestamp("first_seen_at", { withTimezone: true }).defaultNow().notNull(),
backfilledAt: timestamp("backfilled_at", { withTimezone: true }),
lastRefreshedAt: timestamp("last_refreshed_at", { withTimezone: true }),
},
(table) => [primaryKey({ columns: [table.codeNorm, table.brandNorm] })],
);
export const partPriceDaily = pgTable(
"part_price_daily",
{
codeNorm: varchar("code_norm", { length: 64 }).notNull(),
brandNorm: varchar("brand_norm", { length: 64 }).default("").notNull(),
source: varchar("source", { length: 16 }).default("supplier").notNull(),
date: date("date").notNull(),
p50: numeric("p50", { precision: 15, scale: 4 }),
p95: numeric("p95", { precision: 15, scale: 4 }),
p99: numeric("p99", { precision: 15, scale: 4 }),
offerCount: integer("offer_count").default(0).notNull(),
},
(table) => [
primaryKey({ columns: [table.codeNorm, table.brandNorm, table.source, table.date] }),
index("part_price_daily_code_date_idx").on(table.codeNorm, table.brandNorm, table.date),
],
);
// ─── Discount Codes (Stripe-backed checkout promotions) ───────────────────
// Admin-generated promo codes. Each row MIRRORS a Stripe Coupon + Promotion
// Code pair: Stripe is the source of truth for redemption (it enforces the cap
// + expiry at checkout and owns the canonical times_redeemed), while this row
// lets the admin panel list/track codes and lets us preview/validate a code at
// checkout without a per-render Stripe call. Amounts are in kuruş (TRY smallest
// unit), matching plans.price* / payments.amount.
export const discountCodes = pgTable(
"discount_codes",
{
id: uuid("id").primaryKey().defaultRandom(),
// Customer-facing code, stored UPPERCASE; matched case-insensitively.
code: varchar("code", { length: 50 }).notNull(),
// Stripe linkage — both set together at creation. Checkout attaches the
// promotion code id; deactivation archives it on Stripe too.
stripeCouponId: text("stripe_coupon_id"),
stripePromotionCodeId: text("stripe_promotion_code_id"),
// 'percentage' (percentOff 1..100) | 'fixed' (amountOff in kuruş, TRY).
discountType: varchar("discount_type", { length: 12 }).notNull(),
percentOff: integer("percent_off"),
amountOff: integer("amount_off"),
// How long the discount rides a subscription, mirroring Stripe Coupon.duration:
// 'once' (first invoice only), 'repeating' (durationInMonths invoices),
// 'forever' (every renewal).
duration: varchar("duration", { length: 12 }).default("once").notNull(),
durationInMonths: integer("duration_in_months"),
// Total redemption cap across all users (null = unlimited). Stripe enforces.
maxRedemptions: integer("max_redemptions"),
// Mirror of Stripe's times_redeemed; bumped from the checkout webhook and
// reconcilable from Stripe.
timesRedeemed: integer("times_redeemed").default(0).notNull(),
// Code stops working after this instant (null = no expiry). Stripe enforces.
expiresAt: timestamp("expires_at", { withTimezone: true }),
// Admin soft-toggle. Deactivating also archives the Stripe promotion code.
isActive: boolean("is_active").default(true).notNull(),
// Free-text admin note (campaign name, who it's for…).
note: text("note"),
createdBy: uuid("created_by").references(() => users.id, { onDelete: "set null" }),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [
uniqueIndex("discount_codes_code_idx").on(table.code),
index("discount_codes_active_idx").on(table.isActive),
],
);
// ─── Canonical Categories (unified cross-source taxonomy) ──────────────────
// Single manufacturer-independent parts taxonomy that every source's OEM
// microfiche tree maps INTO. The raw per-source trees (`categories.parent_id`
// chains, pl24/emex/pcat mirrors) stay untouched; this is an orthogonal spine
// so VW, Opel, Ford etc. can be browsed/searched under one set of headings.
// Seeded from the Turkish retail taxonomy (aloparca 17 tops) extended with
// OEM-only tops the retail sites don't sell (interior trim/koltuk, body).
// Two levels: parentId NULL = top bucket, else a sub-category under a top.
export const canonicalCategories = pgTable(
"canonical_categories",
{
id: uuid("id").primaryKey().defaultRandom(),
parentId: uuid("parent_id"),
slug: varchar("slug", { length: 100 }).notNull(),
name: varchar("name", { length: 200 }).notNull(),
sortOrder: integer("sort_order").default(0).notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [uniqueIndex("canonical_categories_slug_idx").on(table.slug)],
);
// ─── Category → Canonical map (NAME-level, not row-level) ──────────────────
// Mapping key is (source, normalized_name), NOT category.id — because the same
// category name repeats across thousands of vehicles (704k rows collapse to
// ~111k distinct names), so mapping at the name level is ~6× cheaper and a
// single human decision fans out to every matching row. `method` records how
// the row was produced (keyword | fuzzy | embedding | manual) and `confidence`
// gates auto-apply vs review queue; `isManual` protects human-approved rows
// from being overwritten by re-runs (same pattern as emex_category_translations).
export const categoryCanonicalMap = pgTable(
"category_canonical_map",
{
id: uuid("id").primaryKey().defaultRandom(),
source: varchar("source", { length: 20 }).notNull(),
normalizedName: varchar("normalized_name", { length: 500 }).notNull(),
canonicalCategoryId: uuid("canonical_category_id")
.notNull()
.references(() => canonicalCategories.id, { onDelete: "cascade" }),
// Second level materialized alongside the top bucket (name→bucket→sub is
// deterministic). Nullable: no sub-leaf matched → served at top level.
canonicalSubcategoryId: uuid("canonical_subcategory_id"),
method: varchar("method", { length: 20 }).default("keyword").notNull(),
confidence: numeric("confidence", { precision: 4, scale: 3 }),
isManual: boolean("is_manual").default(false).notNull(),
ambiguous: boolean("ambiguous").default(false).notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [
uniqueIndex("category_canonical_map_source_name_idx").on(table.source, table.normalizedName),
index("category_canonical_map_canonical_idx").on(table.canonicalCategoryId),
],
);
// ─── Canonical structure template (per source+brand) ──────────────────────
// The canonical STRUCTURE (which buckets/sub-leaves a platform has) is shared
// across vehicles of the same brand — only the parts differ. This table records,
// per (source, brand), each (bucket, sub-leaf) seen across already-drilled
// vehicles with a vehicle_count (support). On decode the serving overlays the
// brand template so the user sees the FULL canonical structure instantly, before
// this vehicle's own tree is deep-drilled by the prefetch backfill. Rebuilt at
// the end of each global canonical-backfill run (derived, so full-refresh).
// Entry-level companion of canonicalStructureTemplate: the browsable source-
// category NAMES every vehicle of (source, brand) shares under a bucket/sub.
// A freshly-decoded vehicle renders these immediately (placeholder until its
// own row exists) — only parts/schemas are vehicle-specific.
export const canonicalTemplateEntries = pgTable(
"canonical_template_entries",
{
id: uuid("id").primaryKey().defaultRandom(),
source: varchar("source", { length: 20 }).notNull(),
brand: varchar("brand", { length: 120 }).notNull(),
canonicalCategoryId: uuid("canonical_category_id").notNull(),
canonicalSubcategoryId: uuid("canonical_subcategory_id"),
name: varchar("name", { length: 500 }).notNull(),
vehicleCount: integer("vehicle_count").default(0).notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [index("canonical_template_entries_sb_idx").on(table.source, table.brand)],
);
export const canonicalStructureTemplate = pgTable(
"canonical_structure_template",
{
id: uuid("id").primaryKey().defaultRandom(),
source: varchar("source", { length: 20 }).notNull(),
brand: varchar("brand", { length: 120 }).notNull(),
canonicalCategoryId: uuid("canonical_category_id").notNull(),
canonicalSubcategoryId: uuid("canonical_subcategory_id"),
vehicleCount: integer("vehicle_count").default(0).notNull(),
updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
},
(table) => [index("canonical_structure_template_source_brand_idx").on(table.source, table.brand)],
);

View File

@@ -0,0 +1,58 @@
import { BadRequestException, Body, Controller, Get, Param, Patch, Post } from "@nestjs/common";
import { CurrentUser } from "../common/decorators/current-user.decorator";
import { Roles } from "../common/decorators/roles.decorator";
import { type CreateDiscountInput, DiscountCodesService } from "./discount-codes.service";
interface AuthedUser {
id: string;
email: string;
role: string;
}
@Controller("discount-codes")
export class DiscountCodesController {
constructor(private discountCodes: DiscountCodesService) {}
// ── Any logged-in user: validate + price a code on the checkout page ──
// No @Roles → only the global AuthGuard applies (must be signed in).
@Post("validate")
async validate(
@Body() body: { code: string; planKey: string; billingPeriod: "monthly" | "yearly" },
) {
if (!body?.code || !body?.planKey || !body?.billingPeriod) {
throw new BadRequestException("code, planKey ve billingPeriod zorunlu");
}
return this.discountCodes.preview(body.code, body.planKey, body.billingPeriod);
}
// ── Admin-only management ──
@Roles("admin")
@Get()
async list() {
return this.discountCodes.list();
}
@Roles("admin")
@Get(":id")
async getOne(@Param("id") id: string) {
return this.discountCodes.getOne(id);
}
@Roles("admin")
@Post()
async create(@CurrentUser() user: AuthedUser, @Body() body: CreateDiscountInput) {
return this.discountCodes.create(body, user.id);
}
@Roles("admin")
@Patch(":id/deactivate")
async deactivate(@Param("id") id: string) {
return this.discountCodes.deactivate(id);
}
@Roles("admin")
@Post("sync")
async sync() {
return this.discountCodes.syncRedemptions();
}
}

View File

@@ -0,0 +1,14 @@
import { Module } from "@nestjs/common";
import { StripeModule } from "../payments/stripe/stripe.module";
import { DiscountCodesController } from "./discount-codes.controller";
import { DiscountCodesService } from "./discount-codes.service";
// One-way dependency on StripeModule (StripeService creates the Stripe
// Coupon/Promotion objects + owns checkout-time preview). StripeModule does NOT
// import us back, so there is no cycle.
@Module({
imports: [StripeModule],
controllers: [DiscountCodesController],
providers: [DiscountCodesService],
})
export class DiscountCodesModule {}

View File

@@ -0,0 +1,237 @@
import { BadRequestException, Inject, Injectable, Logger, NotFoundException } from "@nestjs/common";
import { desc, eq } from "drizzle-orm";
import { DATABASE, type Database } from "../database/database.provider";
import { discountCodes } from "../database/schema/core";
import { StripeService } from "../payments/stripe/stripe.service";
export type DiscountType = "percentage" | "fixed";
export type DiscountDuration = "once" | "repeating" | "forever";
export interface CreateDiscountInput {
/** Optional custom code; auto-generated when omitted. */
code?: string;
discountType: DiscountType;
/** 1..100, required for `percentage`. */
percentOff?: number;
/** kuruş (TRY smallest unit), required for `fixed`. */
amountOff?: number;
duration?: DiscountDuration;
/** required when duration === "repeating" */
durationInMonths?: number;
/** total redemption cap (null/omitted = unlimited) */
maxRedemptions?: number;
/** ISO date string (null/omitted = no expiry) */
expiresAt?: string;
note?: string;
}
type DiscountRow = typeof discountCodes.$inferSelect;
// 3..50 chars, starts alphanumeric, then alphanumeric / dash / underscore.
const CODE_RE = /^[A-Z0-9][A-Z0-9_-]{2,49}$/;
// Crockford-ish alphabet — no 0/O/1/I confusion in shared codes.
const CODE_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
@Injectable()
export class DiscountCodesService {
private readonly logger = new Logger(DiscountCodesService.name);
constructor(
@Inject(DATABASE) private db: Database,
private stripe: StripeService,
) {}
private generateCode(): string {
let s = "";
for (let i = 0; i < 8; i++) {
s += CODE_ALPHABET[Math.floor(Math.random() * CODE_ALPHABET.length)];
}
return `SASE${s}`;
}
private computeStatus(row: DiscountRow): "active" | "inactive" | "expired" | "exhausted" {
if (!row.isActive) return "inactive";
if (row.expiresAt && row.expiresAt.getTime() < Date.now()) return "expired";
if (row.maxRedemptions != null && row.timesRedeemed >= row.maxRedemptions) return "exhausted";
return "active";
}
async create(input: CreateDiscountInput, createdBy: string | null) {
const discountType = input.discountType;
if (discountType !== "percentage" && discountType !== "fixed") {
throw new BadRequestException("discountType 'percentage' veya 'fixed' olmalı");
}
let percentOff: number | null = null;
let amountOff: number | null = null;
if (discountType === "percentage") {
percentOff = Number(input.percentOff);
if (!Number.isInteger(percentOff) || percentOff < 1 || percentOff > 100) {
throw new BadRequestException("Yüzde indirim 1-100 arası tam sayı olmalı");
}
} else {
amountOff = Number(input.amountOff);
if (!Number.isInteger(amountOff) || amountOff < 1) {
throw new BadRequestException("Tutar indirimi (kuruş) 1'den büyük tam sayı olmalı");
}
}
const duration: DiscountDuration = input.duration ?? "once";
if (duration !== "once" && duration !== "repeating" && duration !== "forever") {
throw new BadRequestException("Geçersiz süre türü");
}
let durationInMonths: number | null = null;
if (duration === "repeating") {
durationInMonths = Number(input.durationInMonths);
if (!Number.isInteger(durationInMonths) || durationInMonths < 1 || durationInMonths > 36) {
throw new BadRequestException("Tekrar eden indirimde ay sayısı 1-36 arası olmalı");
}
}
let maxRedemptions: number | null = null;
if (
input.maxRedemptions !== undefined &&
input.maxRedemptions !== null &&
String(input.maxRedemptions) !== ""
) {
maxRedemptions = Number(input.maxRedemptions);
if (!Number.isInteger(maxRedemptions) || maxRedemptions < 1) {
throw new BadRequestException("Kullanım limiti 1'den büyük tam sayı olmalı");
}
}
let expiresAt: Date | null = null;
if (input.expiresAt) {
const d = new Date(input.expiresAt);
if (Number.isNaN(d.getTime())) {
throw new BadRequestException("Geçersiz son kullanma tarihi");
}
if (d.getTime() < Date.now()) {
throw new BadRequestException("Son kullanma tarihi gelecekte olmalı");
}
expiresAt = d;
}
let code = (input.code ?? "").trim().toUpperCase();
if (code) {
if (!CODE_RE.test(code)) {
throw new BadRequestException("Kod 3-50 karakter olmalı; harf, rakam, - ve _ içerebilir");
}
} else {
code = this.generateCode();
}
// Local uniqueness check BEFORE creating Stripe objects, so a duplicate code
// doesn't leave an orphan Stripe coupon behind a failing row insert.
const [existing] = await this.db
.select({ id: discountCodes.id })
.from(discountCodes)
.where(eq(discountCodes.code, code))
.limit(1);
if (existing) throw new BadRequestException(`'${code}' kodu zaten mevcut`);
let couponId: string;
let promotionCodeId: string;
try {
const created = await this.stripe.createPromotion({
code,
discountType,
percentOff: percentOff ?? undefined,
amountOff: amountOff ?? undefined,
duration,
durationInMonths: durationInMonths ?? undefined,
maxRedemptions: maxRedemptions ?? undefined,
expiresAt: expiresAt ?? undefined,
});
couponId = created.couponId;
promotionCodeId = created.promotionCodeId;
} catch (err) {
const msg = (err as Error).message || String(err);
if (msg.toLowerCase().includes("already exists")) {
throw new BadRequestException(`'${code}' kodu Stripe'da zaten kullanımda`);
}
this.logger.error(`Stripe promotion create failed for ${code}: ${msg}`);
throw new BadRequestException(`İndirim kodu oluşturulamadı: ${msg}`);
}
const [row] = await this.db
.insert(discountCodes)
.values({
code,
stripeCouponId: couponId,
stripePromotionCodeId: promotionCodeId,
discountType,
percentOff,
amountOff,
duration,
durationInMonths,
maxRedemptions,
expiresAt,
note: input.note?.slice(0, 500) ?? null,
createdBy,
})
.returning();
this.logger.log(`Discount code created: ${code} (${discountType}) by ${createdBy ?? "?"}`);
return { ...row, status: this.computeStatus(row) };
}
async list() {
const rows = await this.db.select().from(discountCodes).orderBy(desc(discountCodes.createdAt));
return rows.map((r) => ({ ...r, status: this.computeStatus(r) }));
}
async getOne(id: string) {
const [row] = await this.db
.select()
.from(discountCodes)
.where(eq(discountCodes.id, id))
.limit(1);
if (!row) throw new NotFoundException("İndirim kodu bulunamadı");
return { ...row, status: this.computeStatus(row) };
}
async deactivate(id: string) {
const [row] = await this.db
.select()
.from(discountCodes)
.where(eq(discountCodes.id, id))
.limit(1);
if (!row) throw new NotFoundException("İndirim kodu bulunamadı");
// Must archive on Stripe too: checkout resolves the live promotion code, so
// a code left active there would keep working after a local-only deactivate.
if (row.stripePromotionCodeId) {
await this.stripe.deactivatePromotion(row.stripePromotionCodeId);
}
await this.db
.update(discountCodes)
.set({ isActive: false, updatedAt: new Date() })
.where(eq(discountCodes.id, id));
this.logger.log(`Discount code deactivated: ${row.code}`);
return { success: true };
}
/** Reconcile mirrored redemption counts from Stripe (panel "sync" button). */
async syncRedemptions() {
const rows = await this.db.select().from(discountCodes);
let updated = 0;
for (const row of rows) {
if (!row.stripePromotionCodeId) continue;
const count = await this.stripe.getPromotionRedemptions(row.stripePromotionCodeId);
if (count != null && count !== row.timesRedeemed) {
await this.db
.update(discountCodes)
.set({ timesRedeemed: count, updatedAt: new Date() })
.where(eq(discountCodes.id, row.id));
updated++;
}
}
return { synced: rows.length, updated };
}
/** Validate + price a code for the checkout page. */
preview(code: string, planKey: string, billingPeriod: "monthly" | "yearly") {
return this.stripe.previewDiscount(code, planKey, billingPeriod);
}
}

View File

@@ -14,6 +14,11 @@
import { Injectable, Logger, type OnModuleDestroy, type OnModuleInit } from "@nestjs/common"; import { Injectable, Logger, type OnModuleDestroy, type OnModuleInit } from "@nestjs/common";
import { ConfigService } from "@nestjs/config"; import { ConfigService } from "@nestjs/config";
import { Browser, BrowserContext, Page } from "playwright"; import { Browser, BrowserContext, Page } from "playwright";
import { ProxyHealthService } from "../proxy-telemetry/proxy-health.service";
import {
classifyTransportError,
isProxyConnectFailure,
} from "../proxy-telemetry/proxy-telemetry.service";
const SESSION_TTL_MS = 25 * 60 * 1000; // 25 minutes const SESSION_TTL_MS = 25 * 60 * 1000; // 25 minutes
const MAX_CONCURRENT_PAGES = 3; const MAX_CONCURRENT_PAGES = 3;
@@ -61,22 +66,52 @@ export class EmexBrowserService implements OnModuleInit, OnModuleDestroy {
private launching: Promise<void> | null = null; private launching: Promise<void> | null = null;
private readonly useProxy: boolean; private readonly useProxy: boolean;
// "floxy" residential (default) | "dataimpulse" legacy | "none".
private readonly proxyProvider: "floxy" | "dataimpulse" | "none";
private readonly proxyHost: string; private readonly proxyHost: string;
private readonly proxyPortStart: number; private readonly proxyPortStart: number;
private readonly proxyPortEnd: number; private readonly proxyPortEnd: number;
private readonly proxyUsername: string; private readonly proxyUsername: string;
private readonly proxyPassword: string; private readonly proxyPassword: string;
// Floxy residential — a sticky session pins ONE exit IP for the browser's whole
// lifetime (relaunch mints a fresh session id), which suits the chained scrape.
private readonly floxyHost: string;
private readonly floxyPort: number;
private readonly floxyUser: string;
private readonly floxyPass: string;
private readonly floxyLifetime: number;
private startedAt = 0; private startedAt = 0;
// Provider the live browser was actually launched with. Tracks failover: when
// the configured provider is "floxy" but the shared health gate is tripped,
// (re)launch picks "dataimpulse" instead. Whole-browser scope because Playwright
// binds the proxy at launch — a relaunch is what swaps the exit.
private activeProvider: "floxy" | "dataimpulse" | "none" = "none";
constructor(private configService: ConfigService) { constructor(
private configService: ConfigService,
private readonly proxyHealth: ProxyHealthService,
) {
this.semaphore = new Semaphore(MAX_CONCURRENT_PAGES); this.semaphore = new Semaphore(MAX_CONCURRENT_PAGES);
this.useProxy = this.configService.get<string>("EMEX_USE_PROXY", "true") === "true"; this.useProxy = this.configService.get<string>("EMEX_USE_PROXY", "true") === "true";
const rawProvider = this.configService
.get<string>("EMEX_PROXY_PROVIDER", "floxy")
.toLowerCase();
this.proxyProvider = !this.useProxy
? "none"
: rawProvider === "dataimpulse" || rawProvider === "none"
? (rawProvider as "dataimpulse" | "none")
: "floxy";
this.proxyHost = this.configService.get<string>("EMEX_PROXY_HOST", "74.81.81.81"); this.proxyHost = this.configService.get<string>("EMEX_PROXY_HOST", "74.81.81.81");
this.proxyPortStart = this.configService.get<number>("EMEX_PROXY_PORT_START", 10001); this.proxyPortStart = this.configService.get<number>("EMEX_PROXY_PORT_START", 10001);
this.proxyPortEnd = this.configService.get<number>("EMEX_PROXY_PORT_END", 10099); this.proxyPortEnd = this.configService.get<number>("EMEX_PROXY_PORT_END", 10099);
this.proxyUsername = this.configService.get<string>("EMEX_PROXY_USER", "1726bbe361918676d44e"); this.proxyUsername = this.configService.get<string>("EMEX_PROXY_USER", "1726bbe361918676d44e");
this.proxyPassword = this.configService.get<string>("EMEX_PROXY_PASS", "f11c7b6128cc86c6"); this.proxyPassword = this.configService.get<string>("EMEX_PROXY_PASS", "f11c7b6128cc86c6");
this.floxyHost = this.configService.get<string>("EMEX_FLOXY_HOST", "residential.floxy.io");
this.floxyPort = Number(this.configService.get("EMEX_FLOXY_PORT", 12321)) || 12321;
this.floxyUser = this.configService.get<string>("EMEX_FLOXY_USER", "cac4b0d96a80");
this.floxyPass = this.configService.get<string>("EMEX_FLOXY_PASS", "dfc38fe467a3");
this.floxyLifetime = Number(this.configService.get("EMEX_FLOXY_LIFETIME", 300)) || 300;
} }
async onModuleInit(): Promise<void> { async onModuleInit(): Promise<void> {
@@ -184,14 +219,38 @@ export class EmexBrowserService implements OnModuleInit, OnModuleDestroy {
const executablePath = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH; const executablePath = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH;
if (executablePath) launchOptions.executablePath = executablePath; if (executablePath) launchOptions.executablePath = executablePath;
if (this.useProxy) { // Pick the provider for THIS launch: honour the configured one, but while the
// shared Floxy-health gate is tripped, a "floxy" config launches on DataImpulse.
this.activeProvider = this.pickLaunchProvider();
if (this.activeProvider === "floxy") {
// Sticky Floxy session for the browser's lifetime → one residential exit IP
// for the whole chained scrape; a fresh id is minted on each (re)launch.
const sid = Math.random().toString(36).slice(2, 10);
const pass =
this.floxyLifetime > 0
? `${this.floxyPass}_session-${sid}_lifetime-${this.floxyLifetime}`
: this.floxyPass;
launchOptions.proxy = {
server: `http://${this.floxyHost}:${this.floxyPort}`,
username: this.floxyUser,
password: pass,
};
this.logger.log(
`Using Floxy residential proxy: ${this.floxyHost}:${this.floxyPort} (sticky ${this.floxyLifetime}s)`,
);
} else if (this.activeProvider === "dataimpulse") {
const port = this.randomProxyPort(); const port = this.randomProxyPort();
launchOptions.proxy = { launchOptions.proxy = {
server: `http://${this.proxyHost}:${port}`, server: `http://${this.proxyHost}:${port}`,
username: this.proxyUsername, username: this.proxyUsername,
password: this.proxyPassword, password: this.proxyPassword,
}; };
this.logger.log(`Using proxy: ${this.proxyHost}:${port}`); this.logger.log(
`Using DataImpulse proxy: ${this.proxyHost}:${port}${
this.proxyProvider === "floxy" ? " (Floxy failover)" : ""
}`,
);
} }
this.browser = await chromium.launch(launchOptions); this.browser = await chromium.launch(launchOptions);
@@ -232,22 +291,67 @@ export class EmexBrowserService implements OnModuleInit, OnModuleDestroy {
await this.launchBrowser(); await this.launchBrowser();
} }
/** Provider for the next launch: DataImpulse while the Floxy gate is tripped. */
private pickLaunchProvider(): "floxy" | "dataimpulse" | "none" {
if (this.proxyProvider === "floxy" && this.proxyHealth.isFloxyDown()) {
return "dataimpulse"; // DI creds are always present (env defaults) when proxy on
}
return this.proxyProvider;
}
private async relaunchBrowser(): Promise<void> {
await this.closeBrowser();
await this.launchBrowser();
}
/** /**
* Visit baseUrl to establish/refresh ASP.NET session cookie * Trip Floxy failover from a scrape-level transport failure (a connect error
* that surfaced AFTER the session was established, so ensureSession's own gate
* didn't catch it). Opens the shared cooldown and forces the next acquirePage
* to re-establish the session — which switches the browser to DataImpulse.
*/
async tripFloxyFailover(reason?: string): Promise<void> {
this.proxyHealth.reportFloxyFailure(reason);
if (this.activeProvider === "floxy") {
this.sessionExpiry = 0; // force ensureSession → provider switch on next acquire
}
}
/**
* Visit baseUrl to establish/refresh ASP.NET session cookie. Doubles as the
* Floxy health gate for the browser path: only at a session boundary, switch
* the browser's proxy to match pickLaunchProvider() (fail over to DataImpulse
* when Floxy is down, probe Floxy again once the gate clears). A connect
* failure during the probe trips the gate and retries once on DataImpulse.
*/ */
private async ensureSession(): Promise<void> { private async ensureSession(): Promise<void> {
// Only re-evaluate the provider when establishing a fresh session, so an
// in-flight scrape on a working browser is never interrupted mid-flow.
if (Date.now() >= this.sessionExpiry && this.proxyProvider !== "none") {
const desired = this.pickLaunchProvider();
if (this.browser?.isConnected() && desired !== this.activeProvider) {
this.logger.log(`EMEX switching browser proxy: ${this.activeProvider}${desired}`);
await this.relaunchBrowser();
}
}
if (Date.now() < this.sessionExpiry) return; if (Date.now() < this.sessionExpiry) return;
this.logger.log("Establishing EMEX session..."); // Up to 2 attempts: a Floxy tunnel failure trips the gate, relaunches on
// DataImpulse, and tries once more so the caller still gets a live session.
for (let attempt = 1; attempt <= 2; attempt++) {
if (!this.context) throw new Error("EMEX browser context not initialized"); if (!this.context) throw new Error("EMEX browser context not initialized");
const ctx = this.context; const ctx = this.context;
const page = await ctx.newPage(); const page = await ctx.newPage();
try { try {
this.logger.log(`Establishing EMEX session (${this.activeProvider})...`);
await page.goto(EMEX_BASE_URL, { await page.goto(EMEX_BASE_URL, {
waitUntil: "networkidle", waitUntil: "networkidle",
timeout: 30000, timeout: 30000,
}); });
if (this.activeProvider === "floxy") this.proxyHealth.reportFloxySuccess();
const cookies = await ctx.cookies(); const cookies = await ctx.cookies();
const session = cookies.find((c) => c.name === "ASP.NET_SessionId"); const session = cookies.find((c) => c.name === "ASP.NET_SessionId");
if (session) { if (session) {
@@ -258,8 +362,23 @@ export class EmexBrowserService implements OnModuleInit, OnModuleDestroy {
// Still set a short TTL to avoid hammering // Still set a short TTL to avoid hammering
this.sessionExpiry = Date.now() + 60_000; this.sessionExpiry = Date.now() + 60_000;
} }
return;
} catch (err) {
await page.close().catch(() => {});
const canFailover =
attempt < 2 &&
this.proxyProvider === "floxy" &&
this.activeProvider === "floxy" &&
isProxyConnectFailure(err);
if (canFailover) {
this.proxyHealth.reportFloxyFailure(`emex session: ${classifyTransportError(err)}`);
await this.relaunchBrowser(); // gate now tripped → relaunch picks DataImpulse
continue;
}
throw err;
} finally { } finally {
await page.close(); await page.close().catch(() => {});
}
} }
} }

View File

@@ -1,8 +1,10 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { ProxyTelemetryModule } from "../proxy-telemetry/proxy-telemetry.module";
import { EmexBrowserService } from "./emex.browser"; import { EmexBrowserService } from "./emex.browser";
import { EmexService } from "./emex.service"; import { EmexService } from "./emex.service";
@Module({ @Module({
imports: [ProxyTelemetryModule],
providers: [EmexBrowserService, EmexService], providers: [EmexBrowserService, EmexService],
exports: [EmexService], exports: [EmexService],
}) })

View File

@@ -18,6 +18,12 @@ function makeConfig(overrides: Record<string, string>): ConfigService {
const browser = {} as never; const browser = {} as never;
const redis = { set: vi.fn() } as never; const redis = { set: vi.fn() } as never;
const posthog = {} as never; const posthog = {} as never;
const proxyTelemetry = { record: vi.fn() } as never;
const proxyHealth = {
isFloxyDown: vi.fn(() => false),
reportFloxyFailure: vi.fn(),
reportFloxySuccess: vi.fn(),
} as never;
describe("EmexService proxy-port coercion (regression)", () => { describe("EmexService proxy-port coercion (regression)", () => {
it("does not throw 'Invalid URL' when ports arrive as strings over a real range", () => { it("does not throw 'Invalid URL' when ports arrive as strings over a real range", () => {
@@ -28,7 +34,9 @@ describe("EmexService proxy-port coercion (regression)", () => {
EMEX_PROXY_PORT_END: "10099", EMEX_PROXY_PORT_END: "10099",
}); });
// Pre-fix: threw "Invalid URL" here (port "4510001" > 65535). // Pre-fix: threw "Invalid URL" here (port "4510001" > 65535).
expect(() => new EmexService(config, browser, redis, posthog)).not.toThrow(); expect(
() => new EmexService(config, browser, redis, posthog, proxyTelemetry, proxyHealth),
).not.toThrow();
}); });
it("constructs with a single-port range (string env)", () => { it("constructs with a single-port range (string env)", () => {
@@ -37,7 +45,9 @@ describe("EmexService proxy-port coercion (regression)", () => {
EMEX_PROXY_PORT_START: "823", EMEX_PROXY_PORT_START: "823",
EMEX_PROXY_PORT_END: "823", EMEX_PROXY_PORT_END: "823",
}); });
expect(() => new EmexService(config, browser, redis, posthog)).not.toThrow(); expect(
() => new EmexService(config, browser, redis, posthog, proxyTelemetry, proxyHealth),
).not.toThrow();
}); });
it("falls back to a valid default when the port env is garbage", () => { it("falls back to a valid default when the port env is garbage", () => {
@@ -46,11 +56,15 @@ describe("EmexService proxy-port coercion (regression)", () => {
EMEX_PROXY_PORT_START: "not-a-number", EMEX_PROXY_PORT_START: "not-a-number",
EMEX_PROXY_PORT_END: "999999", EMEX_PROXY_PORT_END: "999999",
}); });
expect(() => new EmexService(config, browser, redis, posthog)).not.toThrow(); expect(
() => new EmexService(config, browser, redis, posthog, proxyTelemetry, proxyHealth),
).not.toThrow();
}); });
it("constructs cleanly with the proxy disabled", () => { it("constructs cleanly with the proxy disabled", () => {
const config = makeConfig({ EMEX_USE_PROXY: "false" }); const config = makeConfig({ EMEX_USE_PROXY: "false" });
expect(() => new EmexService(config, browser, redis, posthog)).not.toThrow(); expect(
() => new EmexService(config, browser, redis, posthog, proxyTelemetry, proxyHealth),
).not.toThrow();
}); });
}); });

View File

@@ -23,6 +23,12 @@ import { ProxyAgent } from "undici";
import { isBackfillContext } from "../../jobs/prefetch-context"; import { isBackfillContext } from "../../jobs/prefetch-context";
import { PostHogService } from "../../posthog/posthog.service"; import { PostHogService } from "../../posthog/posthog.service";
import { RedisService } from "../../redis/redis.service"; import { RedisService } from "../../redis/redis.service";
import { ProxyHealthService } from "../proxy-telemetry/proxy-health.service";
import {
ProxyTelemetryService,
classifyTransportError,
isProxyConnectFailure,
} from "../proxy-telemetry/proxy-telemetry.service";
import { parseUnitLeaves, parseVehicleTree } from "./emex-tree.parser"; import { parseUnitLeaves, parseVehicleTree } from "./emex-tree.parser";
import { EmexBrowserService } from "./emex.browser"; import { EmexBrowserService } from "./emex.browser";
import { createEmptyDecodedVehicle, mapEmexResponse } from "./emex.mapper"; import { createEmptyDecodedVehicle, mapEmexResponse } from "./emex.mapper";
@@ -132,12 +138,18 @@ export class EmexService {
private floxySessionId = Math.random().toString(36).slice(2, 10); private floxySessionId = Math.random().toString(36).slice(2, 10);
private floxySessionBornMs = Date.now(); private floxySessionBornMs = Date.now();
private readonly emexDirectFallback: boolean; private readonly emexDirectFallback: boolean;
// Transport provider for EMEX egress (HTTP + image-dims). "floxy" residential
// is primary by default (sticky session pins one exit IP across a chained flow),
// DataImpulse is the last-ditch fallback. See constructor.
private readonly emexProxyProvider: "floxy" | "dataimpulse" | "none";
constructor( constructor(
private configService: ConfigService, private configService: ConfigService,
private browserService: EmexBrowserService, private browserService: EmexBrowserService,
private redis: RedisService, private redis: RedisService,
private posthog: PostHogService, private posthog: PostHogService,
private proxyTelemetry: ProxyTelemetryService,
private proxyHealth: ProxyHealthService,
) { ) {
// __dirname is apps/api/src/integrations/emex/ or dist/integrations/emex/ // __dirname is apps/api/src/integrations/emex/ or dist/integrations/emex/
// Scraper lives at <monorepo-root>/scripts/emex-vin-scraper.js // Scraper lives at <monorepo-root>/scripts/emex-vin-scraper.js
@@ -169,15 +181,11 @@ export class EmexService {
portStart: toPort("EMEX_PROXY_PORT_START", 10001), portStart: toPort("EMEX_PROXY_PORT_START", 10001),
portEnd: toPort("EMEX_PROXY_PORT_END", 10099), portEnd: toPort("EMEX_PROXY_PORT_END", 10099),
}; };
// Default agent for the low-stakes image-dims path; fetchEmexHtml builds a
// fresh agent per request so a flaky port can't pin every call.
this.proxyAgent = this.newProxyAgent();
this.logger.log( this.logger.log(
`EMEX HTTP proxy enabled: ${this.emexProxy.host}:${this.emexProxy.portStart}-${this.emexProxy.portEnd}`, `EMEX DataImpulse fallback enabled: ${this.emexProxy.host}:${this.emexProxy.portStart}-${this.emexProxy.portEnd}`,
); );
} else { } else {
this.emexProxy = null; this.emexProxy = null;
this.proxyAgent = null;
} }
// Floxy residential fallback for when the DataImpulse pool flakes (connect // Floxy residential fallback for when the DataImpulse pool flakes (connect
@@ -189,8 +197,8 @@ export class EmexService {
this.emexFloxy = { this.emexFloxy = {
host: this.configService.get<string>("EMEX_FLOXY_HOST", "residential.floxy.io"), host: this.configService.get<string>("EMEX_FLOXY_HOST", "residential.floxy.io"),
port: Number.isInteger(fport) && fport >= 1 && fport <= 65535 ? fport : 12321, port: Number.isInteger(fport) && fport >= 1 && fport <= 65535 ? fport : 12321,
user: this.configService.get<string>("EMEX_FLOXY_USER", "d739255e819b"), user: this.configService.get<string>("EMEX_FLOXY_USER", "cac4b0d96a80"),
pass: this.configService.get<string>("EMEX_FLOXY_PASS", "9092873ba4e0"), pass: this.configService.get<string>("EMEX_FLOXY_PASS", "dfc38fe467a3"),
lifetime: Number.isInteger(flife) && flife >= 0 ? flife : 300, lifetime: Number.isInteger(flife) && flife >= 0 ? flife : 300,
}; };
this.logger.log( this.logger.log(
@@ -200,6 +208,28 @@ export class EmexService {
this.emexFloxy = null; this.emexFloxy = null;
} }
// Transport provider. EMEX_PROXY_PROVIDER: "floxy" (default) → Floxy residential
// sticky primary + a single DataImpulse last-ditch fallback; "dataimpulse" →
// legacy DataImpulse-first order; "none" → direct. ssd tokens are replayed from
// emex's own HTML and aren't strictly IP-bound on the .aspx endpoints (the old
// random-port-per-call primary proved that), but a sticky Floxy IP across the
// chained flow is still the safest choice — and avoids DataImpulse's ~50% dead
// ports that were eating the decode budget before reaching the Floxy fallback.
this.emexProxyProvider = !useProxy
? "none"
: (() => {
const p = this.configService.get<string>("EMEX_PROXY_PROVIDER", "floxy").toLowerCase();
return p === "dataimpulse" || p === "none" ? p : "floxy";
})();
// Default agent for the low-stakes image-dims path; fetchEmexHtml builds a
// fresh agent per request so a flaky exit can't pin every call.
this.proxyAgent =
this.emexProxyProvider === "none"
? null
: (this.newProxyAgent(this.emexProxyProvider === "dataimpulse" ? "dataimpulse" : "floxy") ??
this.newProxyAgent("dataimpulse"));
this.logger.log(`EMEX transport provider: ${this.emexProxyProvider}`);
this.logger.log(`EMEX Service initialized with scraper path: ${this.scraperPath}`); this.logger.log(`EMEX Service initialized with scraper path: ${this.scraperPath}`);
} }
@@ -347,29 +377,56 @@ export class EmexService {
} }
private async fetchEmexHtml(url: string): Promise<string> { private async fetchEmexHtml(url: string): Promise<string> {
// Attempt schedule: DataImpulse (rotating port, fresh agent each try to dodge // Attempt schedule by provider. floxy (default): Floxy residential sticky
// a flaky sticky port — ~42% blip rate, undecoded-vin-rca.md EMEX #1) first, // primary (one exit IP across the flow; rolls to a fresh IP on transport
// then the Floxy residential fallback when the DataImpulse pool throws // failure), then a single DataImpulse last-ditch try — this avoids DataImpulse's
// transport errors. A definitive HTTP answer (e.g. 404) stops the schedule — // ~50% dead ports that used to burn the decode budget before reaching Floxy.
// it's a real result, and a different proxy IP must not "retry" it away. // dataimpulse (legacy): DataImpulse-first (rotating port each try), Floxy after.
// A definitive HTTP answer (e.g. 404) stops the schedule — it's a real result,
// and a different proxy IP must not "retry" it away.
const schedule: Array<"dataimpulse" | "floxy"> = []; const schedule: Array<"dataimpulse" | "floxy"> = [];
if (this.emexProxyProvider === "floxy") {
if (this.emexFloxy && this.proxyHealth.isFloxyDown() && this.emexProxy) {
// Floxy gate tripped → DataImpulse-first (rotating ports cover its ~50%
// dead ports), with one Floxy probe last so a recovery is still detected.
schedule.push("dataimpulse", "dataimpulse", "floxy");
} else {
if (this.emexFloxy) schedule.push("floxy", "floxy");
if (this.emexProxy) schedule.push("dataimpulse");
}
} else if (this.emexProxyProvider === "dataimpulse") {
if (this.emexProxy) schedule.push("dataimpulse", "dataimpulse", "dataimpulse"); if (this.emexProxy) schedule.push("dataimpulse", "dataimpulse", "dataimpulse");
if (this.emexFloxy) schedule.push("floxy", "floxy"); if (this.emexFloxy) schedule.push("floxy", "floxy");
}
const maxAttempts = schedule.length || 1; // 0 → single proxy-less attempt const maxAttempts = schedule.length || 1; // 0 → single proxy-less attempt
let lastErr: unknown; let lastErr: unknown;
for (let attempt = 1; attempt <= maxAttempts; attempt++) { for (let attempt = 1; attempt <= maxAttempts; attempt++) {
const provider = schedule[attempt - 1]; // undefined when no proxy → direct const provider = schedule[attempt - 1]; // undefined when no proxy → direct
const agent = provider ? this.newProxyAgent(provider) : null; const agent = provider ? this.newProxyAgent(provider) : null;
const attemptStart = Date.now();
// Sticky Floxy sessions are the attributable identity here; DataImpulse
// rotates a random port per agent, so there is nothing stable to pin.
const sessionKey = provider === "floxy" ? `s-${this.floxySessionId}` : null;
try { try {
const res = await fetch(url, { const res = await fetch(url, {
headers: { "User-Agent": EMEX_UA, Accept: "text/html,application/xhtml+xml" }, headers: { "User-Agent": EMEX_UA, Accept: "text/html,application/xhtml+xml" },
signal: AbortSignal.timeout(this.timeout), signal: AbortSignal.timeout(this.timeout),
...(agent ? { dispatcher: agent } : {}), ...(agent ? { dispatcher: agent } : {}),
} as RequestInit); } as RequestInit);
this.proxyTelemetry.record({
service: "emex_http",
provider: provider ?? "none",
sessionKey,
targetHost: new URL(url).hostname,
statusCode: res.status,
success: res.ok,
durationMs: Date.now() - attemptStart,
});
if (!res.ok) { if (!res.ok) {
throw new Error(`EMEX HTTP ${res.status} for ${url}`); throw new Error(`EMEX HTTP ${res.status} for ${url}`);
} }
if (provider === "floxy") { if (provider === "floxy") {
this.proxyHealth.reportFloxySuccess(); // Floxy answered — clear cooldown
this.logger.log(`EMEX fetch via Floxy fallback succeeded for ${url}`); this.logger.log(`EMEX fetch via Floxy fallback succeeded for ${url}`);
} }
return await res.text(); return await res.text();
@@ -378,6 +435,19 @@ export class EmexService {
const e = err as Error & { cause?: unknown }; const e = err as Error & { cause?: unknown };
// A real HTTP response ("EMEX HTTP 404") is a definitive answer — never retry. // A real HTTP response ("EMEX HTTP 404") is a definitive answer — never retry.
const httpAnswer = /^EMEX HTTP \d/.test(e.message); const httpAnswer = /^EMEX HTTP \d/.test(e.message);
// HTTP answers were already recorded right after fetch; only transport
// failures (no answer at all) still need a row.
if (!httpAnswer) {
this.proxyTelemetry.record({
service: "emex_http",
provider: provider ?? "none",
sessionKey,
targetHost: new URL(url).hostname,
errorKind: classifyTransportError(e),
success: false,
durationMs: Date.now() - attemptStart,
});
}
const transient = const transient =
!httpAnswer && !httpAnswer &&
(e.name === "TimeoutError" || (e.name === "TimeoutError" ||
@@ -386,6 +456,11 @@ export class EmexService {
/fetch failed|ECONNRESET|ECONNREFUSED|EAI_AGAIN|socket hang up|other side closed|terminated|UND_ERR/i.test( /fetch failed|ECONNRESET|ECONNREFUSED|EAI_AGAIN|socket hang up|other side closed|terminated|UND_ERR/i.test(
`${e.message} ${String(e.cause ?? "")}`, `${e.message} ${String(e.cause ?? "")}`,
)); ));
// A Floxy connect failure (dead exit / tunnel refused) trips the shared
// gate so EMEX + pcat fail over to DataImpulse for the cooldown window.
if (provider === "floxy" && isProxyConnectFailure(e)) {
this.proxyHealth.reportFloxyFailure(`emex http: ${classifyTransportError(e)}`);
}
if (!transient || attempt === maxAttempts) break; if (!transient || attempt === maxAttempts) break;
// A Floxy transport failure means the current sticky exit IP is dead — // A Floxy transport failure means the current sticky exit IP is dead —
// roll to a fresh session so the next Floxy attempt gets a new IP. // roll to a fresh session so the next Floxy attempt gets a new IP.
@@ -849,6 +924,12 @@ export class EmexService {
} catch (error) { } catch (error) {
const err = error as Error; const err = error as Error;
// Floxy tunnel dead mid-scrape → trip failover so the next acquirePage
// re-establishes the session on DataImpulse.
if (isProxyConnectFailure(err)) {
void this.browserService.tripFloxyFailover(`emex decode: ${classifyTransportError(err)}`);
}
if ( if (
err instanceof BadRequestException || err instanceof BadRequestException ||
err instanceof ServiceUnavailableException || err instanceof ServiceUnavailableException ||
@@ -992,6 +1073,10 @@ export class EmexService {
return { parts: [], schemaImageUrl: null, hotspots: [], schemaWidth: 0, schemaHeight: 0 }; return { parts: [], schemaImageUrl: null, hotspots: [], schemaWidth: 0, schemaHeight: 0 };
} catch (error) { } catch (error) {
const err = error as Error; const err = error as Error;
// Floxy tunnel dead mid-scrape → trip failover for the next acquirePage.
if (isProxyConnectFailure(err)) {
void this.browserService.tripFloxyFailover(`emex parts: ${classifyTransportError(err)}`);
}
this.logger.error(`Failed to fetch category parts: ${err.message}`); this.logger.error(`Failed to fetch category parts: ${err.message}`);
return { parts: [], schemaImageUrl: null, hotspots: [], schemaWidth: 0, schemaHeight: 0 }; return { parts: [], schemaImageUrl: null, hotspots: [], schemaWidth: 0, schemaHeight: 0 };
} finally { } finally {

View File

@@ -348,6 +348,7 @@ export const CATALOG_MAP: Record<string, CatalogEntry> = {
// Opel // Opel
W0L: { code: "GM_OP201809", brand: "Opel" }, W0L: { code: "GM_OP201809", brand: "Opel" },
W0V: { code: "GM_OP201809", brand: "Opel" }, // newer Stellantis-era WMI W0V: { code: "GM_OP201809", brand: "Opel" }, // newer Stellantis-era WMI
VXK: { code: "GM_OP201809", brand: "Opel" }, // PSA/Stellantis-platform Opel (Corsa F, Mokka B)
// Chevrolet // Chevrolet
KL1: { code: "GM_C201809", brand: "Chevrolet" }, KL1: { code: "GM_C201809", brand: "Chevrolet" },
// SsangYong // SsangYong

View File

@@ -0,0 +1,262 @@
import { Injectable, Logger, type OnModuleDestroy, type OnModuleInit } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import postgres, { type Sql } from "postgres";
import type { PArticle, PCompatible, POeNumber, POemResult } from "./p.types";
/**
* Read-only lookup against the imported P snapshot (db `td` — a selective
* copy of articles + OE numbers + aftermarket compatibility + images/eans; the
* 29 GB vehicle-fitment table is intentionally excluded). Given an OEM code from
* the sase catalog, returns the P articles that carry it as an OE number,
* with their aftermarket equivalents and OE cross-references.
*
* Matching is normalisation-based, not exact: P stores OE codes with
* spaces/dashes (`1J0 973 702`) while the catalog gives `1J0973702`, so both
* sides are reduced to `[A-Z0-9]` uppercase before comparison (a precomputed
* `code_norm` column, indexed, holds the P side). Exact matching recovers
* almost nothing — verified ~1/10 vs normalised ~5/10 on real catalog codes.
*
* Never throws: disabled feature, too-short code, connection blip or no match
* all collapse to `matched: false` so the UI has a single empty-state path.
*/
@Injectable()
export class PSourceDbService implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(PSourceDbService.name);
private sql: Sql | null = null;
private enabled = false;
// Short normalised codes (e.g. "NA" from "N/A", single digits) collide across
// unrelated parts — refuse to match below this length. Real OE numbers are 5+.
private static readonly MIN_NORM_LEN = 5;
private static readonly MAX_ARTICLES = 60;
private static readonly MAX_AGG = 300;
constructor(private readonly config: ConfigService) {}
onModuleInit() {
const enabled = this.config.get<boolean>("p.enabled");
const url = this.config.get<string>("p.url");
if (!enabled || !url) {
this.logger.log(`[p] disabled (enabled=${enabled}, urlSet=${Boolean(url)})`);
return;
}
this.sql = postgres(url, {
max: 5,
idle_timeout: 30,
connect_timeout: 10,
prepare: false,
});
this.enabled = true;
this.logger.log("[p] connected, OEM cross-reference lookup enabled");
}
async onModuleDestroy() {
if (this.sql) {
await this.sql.end({ timeout: 5 });
this.sql = null;
}
}
/** `1J0 973 702` / `1j0-973-702` → `1J0973702`. Used for both the input code
* and JS-side dedupe; the P side is matched against the stored
* `code_norm` (built with the identical rule at import time). */
private static norm(code: string): string {
return code.toUpperCase().replace(/[^A-Z0-9]/g, "");
}
/**
* Batch membership test for the schema page: given the OEM codes in a parts
* list, return the subset that has at least one OE cross-reference in the
* snapshot. The panel links only these (and renders the rest as plain text),
* so a click never lands on an empty "no equivalents" detail page. One indexed
* query (`code_norm` btree). Fail-open to [] (→ no links) when disabled / on
* error, echoing back the caller's original spelling for the matched codes.
*/
async matchedCodes(rawCodes: string[]): Promise<string[]> {
if (!this.enabled || !this.sql || !rawCodes?.length) return [];
const rawByNorm = new Map<string, string>();
for (const raw of rawCodes) {
const n = PSourceDbService.norm(raw ?? "");
if (n.length >= PSourceDbService.MIN_NORM_LEN && !rawByNorm.has(n)) rawByNorm.set(n, raw);
}
if (rawByNorm.size === 0) return [];
try {
const norms = [...rawByNorm.keys()];
const rows = await this.sql<Array<{ code_norm: string }>>`
SELECT DISTINCT code_norm FROM article_oe_numbers
WHERE code_norm IN ${this.sql(norms)}
`;
return rows
.map((r) => rawByNorm.get(r.code_norm))
.filter((c): c is string => c !== undefined);
} catch (err) {
this.logger.warn(
`[p] matchedCodes failed (${rawByNorm.size} codes): ${(err as Error).message}`,
);
return [];
}
}
async lookupByOem(rawCode: string): Promise<POemResult | null> {
const query = (rawCode ?? "").trim();
const queryNorm = PSourceDbService.norm(query);
const miss: POemResult = {
query,
queryNorm,
matched: false,
articles: [],
aftermarketParts: [],
oeCrossReferences: [],
truncated: false,
};
if (!this.enabled || !this.sql) return miss;
if (queryNorm.length < PSourceDbService.MIN_NORM_LEN) return miss;
try {
const rows = await this.sql<
Array<{
id: string;
brand: string;
article_number: string;
name: string | null;
spare_info: string | null;
oe_numbers: POeNumber[];
compatible: PCompatible[];
images: Array<{ url: string; thumb: string | null }>;
}>
>`
WITH hit AS (
SELECT DISTINCT article_id
FROM article_oe_numbers
WHERE code_norm = ${queryNorm}
LIMIT ${PSourceDbService.MAX_ARTICLES}
)
SELECT
a.id::text AS id,
b.name AS brand,
-- Snapshot'ta kolonlar şaşırtmacalı: articles.article_number %96
-- upstream'in sayısal scrape-ID'si, GERÇEK üretici parça kodu ise
-- articles.name kolonunda ("0 092 S40 300", "JTE2311"; 1,1M satırda
-- yalnızca 176 istisna). Kullanıcıya kopyalanabilir gerçek kodu servis
-- et; name'i ayrıca döndürme (UI'daki ikincil satır zaten kod ile
-- aynıysa gizleniyordu).
COALESCE(NULLIF(a.name, ''), a.article_number) AS article_number,
NULL AS name,
a.spare_info AS spare_info,
COALESCE((
SELECT json_agg(json_build_object('brand', o.brand, 'code', o.code))
FROM (
SELECT DISTINCT brand, code FROM article_oe_numbers
WHERE article_id = a.id ORDER BY brand LIMIT 200
) o
), '[]') AS oe_numbers,
COALESCE((
SELECT json_agg(json_build_object('brand', c.compatible_brand, 'article', c.compatible_article))
FROM (
SELECT DISTINCT compatible_brand, compatible_article FROM article_compatibility
WHERE article_id = a.id ORDER BY compatible_brand LIMIT 200
) c
), '[]') AS compatible,
COALESCE((
SELECT json_agg(json_build_object('url', i.image_url, 'thumb', i.thumb_url) ORDER BY i.sort_order)
FROM (
-- Only publicly-resolvable URLs. The current snapshot stores
-- scrape-local '/_debug/...' paths (404 off-host) — filtering them
-- here keeps the API contract honest so the UI shows no broken
-- thumbnails; real CDN URLs surface automatically once present.
SELECT image_url, thumb_url, sort_order FROM article_images
WHERE article_id = a.id AND image_url LIKE 'http%'
ORDER BY sort_order LIMIT 8
) i
), '[]') AS images
FROM hit
JOIN articles a ON a.id = hit.article_id
JOIN article_brands b ON b.id = a.brand_id
ORDER BY b.name, COALESCE(NULLIF(a.name, ''), a.article_number)
`;
if (rows.length === 0) return miss;
let truncated = rows.length >= PSourceDbService.MAX_ARTICLES;
const articles: PArticle[] = rows.map((r) => {
if (r.oe_numbers.length >= 200 || r.compatible.length >= 200) truncated = true;
return {
id: r.id,
brand: r.brand,
articleNumber: r.article_number,
name: r.name,
spareInfo: r.spare_info,
images: r.images,
oeNumbers: r.oe_numbers,
compatible: r.compatible,
};
});
// ── Aggregate: buyable aftermarket part numbers ──────────────────────
// The matched articles are themselves aftermarket parts; their
// compatibility rows add equivalent numbers from other supplier brands.
const afterSeen = new Set<string>();
const aftermarketParts: POemResult["aftermarketParts"] = [];
const pushAfter = (brand: string, articleNumber: string, thumb: string | null) => {
const key = `${brand.toUpperCase().trim()}${PSourceDbService.norm(articleNumber)}`;
if (afterSeen.has(key) || !articleNumber.trim()) return;
afterSeen.add(key);
if (aftermarketParts.length < PSourceDbService.MAX_AGG) {
aftermarketParts.push({ brand, articleNumber, thumb });
} else {
truncated = true;
}
};
for (const a of articles) {
pushAfter(a.brand, a.articleNumber, a.images[0]?.thumb ?? a.images[0]?.url ?? null);
}
for (const a of articles) {
for (const c of a.compatible) pushAfter(c.brand, c.article, null);
}
// ── Aggregate: OE cross-references (same part, other makes) ───────────
// Exclude restatements of the queried code itself (same normalised code).
const oeSeen = new Set<string>();
const oeCrossReferences: POeNumber[] = [];
for (const a of articles) {
for (const oe of a.oeNumbers) {
const codeNorm = PSourceDbService.norm(oe.code);
if (codeNorm === queryNorm) continue;
const key = `${oe.brand.toUpperCase().trim()}${codeNorm}`;
if (oeSeen.has(key)) continue;
oeSeen.add(key);
if (oeCrossReferences.length < PSourceDbService.MAX_AGG) {
oeCrossReferences.push(oe);
} else {
truncated = true;
}
}
}
return {
query,
queryNorm,
matched: true,
// Ship lean articles — the per-article oeNumbers/compatible lists were
// only needed above to build the aggregates; sending them too bloated
// heavy responses ~30x (370 KB → ~12 KB for a 60-article code).
articles: articles.map((a) => ({
id: a.id,
brand: a.brand,
articleNumber: a.articleNumber,
name: a.name,
spareInfo: a.spareInfo,
images: a.images,
})),
aftermarketParts,
oeCrossReferences,
truncated,
};
} catch (err) {
this.logger.warn(`[p] lookup failed (oem=${query}): ${(err as Error).message}`);
return miss;
}
}
}

View File

@@ -0,0 +1,28 @@
import { Body, Controller, Get, Post, Query } from "@nestjs/common";
import { PSourceDbService } from "./p-source-db.service";
@Controller("p")
export class PController {
constructor(private readonly p: PSourceDbService) {}
/**
* Resolve an OEM code from the catalog to its P equivalents.
* `GET /p/oem?code=1J0973702` → { matched, articles, aftermarketParts,
* oeCrossReferences }. Always 200 with `matched: false` on any miss.
*/
@Get("oem")
async oem(@Query("code") code: string) {
return this.p.lookupByOem(code ?? "");
}
/**
* Batch: which of these OEM codes have a P cross-reference. The schema page
* sends the codes it's about to render and links only the returned ones, so
* unmatched codes stay plain text and never open an empty detail page.
* `POST /p/matched { codes: [...] }` → { matched: [...] }.
*/
@Post("matched")
async matched(@Body("codes") codes: string[]) {
return { matched: await this.p.matchedCodes(Array.isArray(codes) ? codes : []) };
}
}

View File

@@ -0,0 +1,15 @@
import { Module } from "@nestjs/common";
import { PSourceDbService } from "./p-source-db.service";
import { PController } from "./p.controller";
/**
* OEM cross-reference lookup against the imported P snapshot (db `td`).
* Raw read-only queries — intentionally no Drizzle schema modelling, mirroring
* CatalogSourceDbModule. Self-disables when P_DB_* env is unset.
*/
@Module({
controllers: [PController],
providers: [PSourceDbService],
exports: [PSourceDbService],
})
export class PModule {}

View File

@@ -0,0 +1,57 @@
/** An OE (original-equipment) number cross-reference: the same physical part as
* catalogued by a vehicle manufacturer (e.g. VAG `1J0 973 702`). */
export interface POeNumber {
brand: string;
code: string;
}
/** An aftermarket equivalent: a buyable part number from a supplier brand
* (e.g. FEBI BILSTEIN `171903`). */
export interface PCompatible {
brand: string;
article: string;
}
export interface PImage {
url: string;
thumb: string | null;
}
/** One P article whose OE number list contains the queried OEM code. Internal
* shape — `oeNumbers`/`compatible` feed the aggregates and are NOT shipped to
* the client (they're large and unused there; see PArticleSummary). */
export interface PArticle {
id: string;
brand: string;
articleNumber: string;
name: string | null;
spareInfo: string | null;
images: PImage[];
oeNumbers: POeNumber[];
compatible: PCompatible[];
}
/** What the OEM detail page actually renders per matched article — without the
* heavy per-article OE/compat lists (those are deduped into the top-level
* aggregates). Trimming them cut a 60-article payload from ~370 KB to ~12 KB. */
export type PArticleSummary = Omit<PArticle, "oeNumbers" | "compatible">;
/** Response of the OEM detail lookup. `matched: false` covers every miss —
* feature disabled, code too short, or no P article carries that OE
* number — so the UI has a single empty-state path. */
export interface POemResult {
query: string;
queryNorm: string;
matched: boolean;
/** Distinct articles whose OE list contains the queried code (lean — per-article
* OE/compat lists are aggregated below, not duplicated here). */
articles: PArticleSummary[];
/** Deduped buyable aftermarket part numbers across all matched articles
* (the matched articles themselves + their compatibility entries). */
aftermarketParts: Array<{ brand: string; articleNumber: string; thumb: string | null }>;
/** Deduped OE cross-references across all matched articles, excluding the
* queried code itself — i.e. the same part's numbers under other makes. */
oeCrossReferences: POeNumber[];
/** True when any per-article list or the article set hit its cap. */
truncated: boolean;
}

View File

@@ -0,0 +1,87 @@
import { ConfigService } from "@nestjs/config";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { PartsCatalogsAuthService } from "./parts-catalogs-auth.service";
import type { JwtSlot } from "./parts-catalogs.types";
function makeService(env: Record<string, string> = {}) {
const config = {
get: <T>(key: string, def?: T) => (key in env ? (env[key] as unknown as T) : (def as T)),
} as unknown as ConfigService;
const redis = {} as never;
const telemetry = { record: vi.fn() } as never;
const health = {
isFloxyDown: () => false,
reportFloxyFailure: vi.fn(),
reportFloxySuccess: vi.fn(),
} as never;
return new PartsCatalogsAuthService(config, redis, telemetry, health);
}
function slot(capturedAt: number): JwtSlot {
return {
jwt: {
raw: `TWS-${capturedAt}`,
exp: 0,
apiPath: "",
guiVersion: "3",
userId: "",
origin: "",
referer: "",
},
siteUsed: "https://x.test/#/catalogs",
capturedAt,
refreshTimer: null,
failCount: 0,
} as JwtSlot;
}
describe("PartsCatalogsAuthService", () => {
let svc: PartsCatalogsAuthService;
beforeEach(() => {
svc = makeService();
});
describe("captureAllowDirect (env)", () => {
it("defaults on when a proxy provider is active", () => {
expect(makeService()["captureAllowDirect"]).toBe(true);
expect(makeService({ PCAT_PROXY_PROVIDER: "dataimpulse" })["captureAllowDirect"]).toBe(true);
});
it("off when explicitly disabled", () => {
expect(makeService({ PCAT_CAPTURE_ALLOW_DIRECT: "false" })["captureAllowDirect"]).toBe(false);
});
it("off when there is no proxy to fall back from (provider none)", () => {
expect(makeService({ PCAT_USE_PROXY: "false" })["captureAllowDirect"]).toBe(false);
});
});
describe("enforcePoolCap", () => {
it("trims to MAX_POOL_SIZE keeping the freshest slots", () => {
// 9 slots, capturedAt 1..9 (9 newest). Cap is 5.
svc["pool"] = [3, 1, 8, 2, 9, 5, 4, 7, 6].map(slot);
svc["enforcePoolCap"]();
const kept = svc["pool"].map((s) => s.capturedAt).sort((a, b) => a - b);
expect(svc["pool"].length).toBe(5);
expect(kept).toEqual([5, 6, 7, 8, 9]); // 5 newest survive
});
it("is a no-op at or under the cap", () => {
svc["pool"] = [1, 2, 3].map(slot);
svc["enforcePoolCap"]();
expect(svc["pool"].length).toBe(3);
});
it("clears refresh timers on evicted slots", () => {
const all = [1, 2, 3, 4, 5, 6, 7].map((c) => {
const s = slot(c);
s.refreshTimer = setTimeout(() => {}, 60_000);
return s;
});
const [oldest1, oldest2] = all; // capturedAt 1 and 2 — the two evicted
svc["pool"] = [...all];
svc["enforcePoolCap"]();
expect(oldest1.refreshTimer).toBeNull();
expect(oldest2.refreshTimer).toBeNull();
// Survivors keep their timers.
expect(svc["pool"].every((s) => s.refreshTimer !== null)).toBe(true);
for (const s of svc["pool"]) if (s.refreshTimer) clearTimeout(s.refreshTimer);
});
});
});

View File

@@ -1,12 +1,16 @@
/** /**
* Parts-Catalogs Auth Service — v3 token warm pool via Playwright + DataImpulse proxy * Parts-Catalogs Auth Service — v3 token warm pool via Playwright + rotating proxy
* *
* Tokens are captured by navigating to partner sites that embed the v3 widget. * Tokens are captured by navigating to partner sites that embed the v3 widget.
* The widget calls /v3/api/proxy/* with `x-api-key: TWS-{UUID}` and four other * The widget calls /v3/api/proxy/* with `x-api-key: TWS-{UUID}` and four other
* X-* headers (api-path, gui-version, user-id, origin, referer); we intercept * X-* headers (api-path, gui-version, user-id, origin, referer); we intercept
* all of them so backend requests can replay the exact header set. * all of them so backend requests can replay the exact header set.
* *
* Tokens are IP-bound — same proxy port must be reused for the API calls. * Transport (PCAT_PROXY_PROVIDER): "floxy" residential (default) rotates a fresh
* exit IP per call (the token is NOT IP-bound — see parts-catalogs.types.ts — so
* max IP diversity = min per-IP ban), with a sticky IP only per Playwright
* capture; "dataimpulse" is the legacy port-bound datacenter proxy; "none" calls
* direct from the server IP.
* *
* Warm pool behavior: * Warm pool behavior:
* 24/7 → proactive: maintain >= 1 slot, auto-refresh ~9.5min before expiry. * 24/7 → proactive: maintain >= 1 slot, auto-refresh ~9.5min before expiry.
@@ -19,29 +23,39 @@ import { Injectable, Logger, type OnModuleDestroy, type OnModuleInit } from "@ne
import { ConfigService } from "@nestjs/config"; import { ConfigService } from "@nestjs/config";
import { Browser, BrowserContext } from "playwright"; import { Browser, BrowserContext } from "playwright";
import { RedisService } from "../../redis/redis.service"; import { RedisService } from "../../redis/redis.service";
import { ProxyHealthService } from "../proxy-telemetry/proxy-health.service";
import {
ProxyTelemetryService,
classifyTransportError,
isProxyConnectFailure,
resolveExitIp,
} from "../proxy-telemetry/proxy-telemetry.service";
import { JwtSlot, PcatJwtToken, PcatSession } from "./parts-catalogs.types"; import { JwtSlot, PcatJwtToken, PcatSession } from "./parts-catalogs.types";
// Shared single-slot cache so dev + prod (and any restarted container) can // Shared single-slot cache so dev + prod (and any restarted container) can
// inherit a freshly-captured warm slot instead of paying the Playwright // inherit a freshly-captured warm token instead of paying the Playwright
// capture cost from cold. Token is still IP-bound to its proxyPort; if a // capture cost from cold. The token is portable (not IP-bound), so any
// reader on a different IP gets 401/403, invalidateSession's existing // container can serve it through its own rotating call-leg IPs; if it's dead
// fallback re-captures locally. So worst case = today's behavior. // at the source, invalidateSession drops it and the next read re-captures.
const REDIS_SLOT_KEY = "pcat:jwt:slot"; const REDIS_SLOT_KEY = "pcat:jwt:slot";
const REDIS_SAFETY_BUFFER_S = 60; // don't serve from Redis if <60s of life left const REDIS_SAFETY_BUFFER_S = 60; // don't serve from Redis if <60s of life left
const TOKEN_TTL = 600; // seconds — TWS- has no built-in expiry, refresh aggressively const TOKEN_TTL = 600; // seconds — TWS- has no built-in expiry, refresh aggressively
const REFRESH_BUFFER = 90; // Refresh 90s before expiry const REFRESH_BUFFER = 90; // Refresh 90s before expiry
const CAPTURE_POLL_INTERVAL = 500; // ms const CAPTURE_POLL_INTERVAL = 500; // ms
// Two-armed post-goto poll: a healthy goto means the widget JS already loaded // Post-goto poll. We now navigate with `domcontentloaded` (reliable, doesn't
// and the API call typically fires within ~1-2s — short cap is enough. A // depend on networkidle settling on JS-heavy catalog sites), then poll for the
// failed goto means we lost the page, but the widget request may still have // widget's token call. Measured through Floxy residential 2026-06-11: DOM ready
// gone through before the navigation timeout — give a brief grace window // ~7s, the /v3/api/proxy token call fires ~11.8s (≈5s after DOM). The poll must
// and bail. Old single-armed 20s cap was the dominant cost on failed-site // comfortably cover that gap, so 12s — too short and every capture misses the
// attempts (e.g. 10s page timeout + 20s blind poll = 30s wasted). // token (seen at the old 5s cap: "No token after 12s", whole pool starved).
const CAPTURE_POLL_AFTER_OK = 10; // 10 × 500ms = 5s after a healthy goto const CAPTURE_POLL_AFTER_OK = Number(process.env.PCAT_CAPTURE_POLL_OK) || 24; // 24 × 500ms = 12s
const CAPTURE_POLL_AFTER_FAIL = 4; // 4 × 500ms = 2s grace after goto failure const CAPTURE_POLL_AFTER_FAIL = 8; // 8 × 500ms = 4s grace after goto failure
const PAGE_TIMEOUT = 10_000; // 10s — healthy partner sites load in <5s through // 25s — residential exit IPs are slow; the token call fired at ~11.8s in the
// the proxy; a longer wait only prolongs dead-port connects in capture retries. // 2026-06-11 measurement, so 10s timed out before it. Capture is background
// (warm pool, never blocks a user with cold-pool fast-fail), so a longer cap is
// cheap insurance against residential variance. Env-tunable for future rotation.
const PAGE_TIMEOUT = Number(process.env.PCAT_PAGE_TIMEOUT_MS) || 25_000;
const CONTEXT_CLOSE_TIMEOUT = 5_000; const CONTEXT_CLOSE_TIMEOUT = 5_000;
const SITE_COOLDOWN = 10 * 60 * 1000; // 10 min per site const SITE_COOLDOWN = 10 * 60 * 1000; // 10 min per site
const MAX_POOL_SIZE = 5; const MAX_POOL_SIZE = 5;
@@ -62,30 +76,48 @@ const RPM_PER_SLOT = 6; // 1 token per 6 req/min
* Each site uses a different proxy port (IP) to avoid rate limiting. * Each site uses a different proxy port (IP) to avoid rate limiting.
*/ */
// Order matters: round-robin starts from index 0, so the first site is the one // Order matters: round-robin starts from index 0, so the first site is the one
// a cold pool waits on. Sites that consistently load in <10s through the proxy // a cold pool waits on. Sites that consistently load fast stay at the top so
// stay at the top so initial JWT capture finishes in seconds. // initial JWT capture finishes in seconds.
// (e-acca.com was previously included last but removed — the current DataImpulse // Pruned 2026-06-10 to the 6 sites that actually yielded captures on prod —
// rotating proxy 74.81.81.81:10000-10999 cannot reach it; page.goto always // knkauto.ru / autodo.kz / flynestauto.com produced 0 captures yet consumed
// blocked until PAGE_TIMEOUT instead of failing fast like the other sites.) // ~80% of capture attempts (the per-site cooldown kept forcing the retry loop
const JWT_SITES = [ // onto them). Override the list at runtime with PCAT_JWT_SITES (comma-separated)
// without a deploy, e.g. to give dev a disjoint subset.
const DEFAULT_JWT_SITES = [
"https://www.alkatalog.com/cats/#/catalogs", "https://www.alkatalog.com/cats/#/catalogs",
"https://auto-komplekt.ru/goodvin#/catalogs", "https://auto-komplekt.ru/goodvin#/catalogs",
"https://www.autotrade.md/cats/#/catalogs", "https://www.autotrade.md/cats/#/catalogs",
"https://www.e-trak.ru/cats/#/catalogs", "https://www.e-trak.ru/cats/#/catalogs",
"https://www.autopolyus.ru/cats/#/catalogs", "https://www.autopolyus.ru/cats/#/catalogs",
"https://knkauto.ru/goodvin#/catalogs",
"https://www.autodo.kz/#/catalogs",
"https://avtoman124.ru/goodvin#/catalogs", "https://avtoman124.ru/goodvin#/catalogs",
"https://flynestauto.com/auto-parts-oem-catalog",
]; ];
// DataImpulse proxy defaults (port-based IP rotation) // DataImpulse proxy defaults (port-based IP rotation) — legacy. Half its ports
// were dead at any moment (measured 50% connect-timeout), which drove the bulk of
// pcat decode latency + failures. Kept selectable via PCAT_PROXY_PROVIDER for
// instant rollback, but Floxy is the default.
const DI_HOST = "gw.dataimpulse.com"; const DI_HOST = "gw.dataimpulse.com";
const DI_PORT_MIN = 10000; const DI_PORT_MIN = 10000;
const DI_PORT_MAX = 10999; const DI_PORT_MAX = 10999;
const DI_DEFAULT_USER = "1726bbe361918676d44e"; const DI_DEFAULT_USER = "1726bbe361918676d44e";
const DI_DEFAULT_PASS = "78ebc3d881de6ec0"; const DI_DEFAULT_PASS = "78ebc3d881de6ec0";
// Floxy residential defaults (session-based IP rotation; same account EMEX uses).
// Call leg → plain creds = a fresh residential exit IP per request (max IP
// diversity → minimal per-IP ban exposure, since each IP sees ~1 request).
// Capture leg → a sticky session id pins one IP for the duration of a single
// Playwright capture so the partner site sees a coherent session.
const FLOXY_HOST = "residential.floxy.io";
const FLOXY_PORT = 12321;
// Floxy account creds (overridable via PCAT_FLOXY_USER/PASS). Rotated 2026-06-11
// after the prior account (d739255e819b) hit HTTP 402 (balance exhausted).
const FLOXY_DEFAULT_USER = "cac4b0d96a80";
const FLOXY_DEFAULT_PASS = "dfc38fe467a3";
const FLOXY_CAPTURE_LIFETIME_S = 60; // sticky window for one capture attempt
type ProxyProvider = "floxy" | "dataimpulse" | "none";
type ProxyLeg = "call" | "capture";
/** Simple counting semaphore (same pattern as EmexBrowserService) */ /** Simple counting semaphore (same pattern as EmexBrowserService) */
class Semaphore { class Semaphore {
private current = 0; private current = 0;
@@ -119,18 +151,20 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
private browser: Browser | null = null; private browser: Browser | null = null;
private launching: Promise<void> | null = null; private launching: Promise<void> | null = null;
// Concurrent captures capped at JWT_SITES.length — the real bottleneck is // Active JWT capture sites (PCAT_JWT_SITES override or DEFAULT_JWT_SITES),
// how many distinct partner sites we can drive in parallel (one capture per // resolved in the constructor.
// site, since each site = a different IP / cookie origin). Ports are private readonly jwtSites: string[];
// ~unlimited (10000-10999) and Playwright contexts are isolated; the // Concurrent captures capped at jwtSites.length — the real bottleneck is how
// synchronous siteIndex round-robin hands each concurrent capture a // many distinct partner sites we can drive in parallel (one capture per site,
// different site so they don't collide. // since each site = a different cookie origin). Playwright contexts are
private readonly semaphore = new Semaphore(JWT_SITES.length); // isolated; the synchronous siteIndex round-robin hands each concurrent
// capture a different site so they don't collide. Initialized in constructor.
private readonly semaphore: Semaphore;
// Pool state // Pool state
private pool: JwtSlot[] = []; private pool: JwtSlot[] = [];
private siteLastUsedAt = new Map<string, number>(); private siteLastUsedAt = new Map<string, number>();
private siteIndex = 0; // round-robin across JWT_SITES private siteIndex = 0; // round-robin across jwtSites
private requestRoundRobin = 0; // round-robin across pool slots private requestRoundRobin = 0; // round-robin across pool slots
// Business hours scheduling // Business hours scheduling
@@ -139,20 +173,143 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
// RPM tracking // RPM tracking
private requestTimestamps: number[] = []; private requestTimestamps: number[] = [];
// Config // Transport config — see ProxyProvider. The TWS- token is NOT request-time
private readonly useProxy: boolean; // IP-bound (parts-catalogs.types.ts), so the call leg rotates exit IPs per
private readonly proxyHost: string; // request and only the capture leg holds a sticky IP per attempt.
private readonly proxyUser: string; private readonly proxyProvider: ProxyProvider;
private readonly proxyPass: string; // Floxy residential
private readonly floxyHost: string;
private readonly floxyPort: number;
private readonly floxyUser: string;
private readonly floxyPass: string;
private readonly floxyCaptureLifetime: number;
// DataImpulse legacy (port-rotated)
private readonly diHost: string;
private readonly diUser: string;
private readonly diPass: string;
// When a proxied capture dies on a tunnel/connect failure (residential balance
// exhausted, DataImpulse ports dead, both providers down), retry that capture
// DIRECT from the server IP instead of starving the warm pool. Safe because
// the TWS- token is not IP-bound and capture is low-frequency (≤ MAX_POOL_SIZE
// refreshes / ~8.5min). This is the survival path for a full proxy outage —
// the exact failure seen 2026-06-11 (Floxy 402) and 2026-07-03 (Floxy tunnel
// down). Disable with PCAT_CAPTURE_ALLOW_DIRECT=false.
private readonly captureAllowDirect: boolean;
constructor( constructor(
private configService: ConfigService, private configService: ConfigService,
private readonly redis: RedisService, private readonly redis: RedisService,
private readonly proxyTelemetry: ProxyTelemetryService,
private readonly proxyHealth: ProxyHealthService,
) { ) {
this.useProxy = this.configService.get<string>("PCAT_USE_PROXY", "true") === "true"; const cfg = this.configService;
this.proxyHost = this.configService.get<string>("PCAT_PROXY_HOST", DI_HOST);
this.proxyUser = this.configService.get<string>("PCAT_PROXY_USER", DI_DEFAULT_USER); // Active capture sites: PCAT_JWT_SITES override (comma-separated) or the
this.proxyPass = this.configService.get<string>("PCAT_PROXY_PASS", DI_DEFAULT_PASS); // pruned default. Empty/blank entries are dropped; falls back to default if
// the override resolves to nothing.
const sitesRaw = cfg.get<string>("PCAT_JWT_SITES", "");
const parsedSites = sitesRaw
.split(",")
.map((s) => s.trim())
.filter((s) => s.length > 0);
this.jwtSites = parsedSites.length > 0 ? parsedSites : DEFAULT_JWT_SITES;
this.semaphore = new Semaphore(this.jwtSites.length);
// Provider selection. Back-compat: PCAT_USE_PROXY=false still forces direct.
const rawProvider = cfg.get<string>("PCAT_PROXY_PROVIDER", "floxy").toLowerCase();
const useProxy = cfg.get<string>("PCAT_USE_PROXY", "true") === "true";
this.proxyProvider = !useProxy
? "none"
: rawProvider === "dataimpulse" || rawProvider === "none"
? (rawProvider as ProxyProvider)
: "floxy";
const toInt = (key: string, def: number): number => {
const n = Number(cfg.get(key, def));
return Number.isInteger(n) && n >= 0 ? n : def;
};
const toPort = (key: string, def: number): number => {
const n = Number(cfg.get(key, def));
return Number.isInteger(n) && n >= 1 && n <= 65535 ? n : def;
};
this.floxyHost = cfg.get<string>("PCAT_FLOXY_HOST", FLOXY_HOST);
this.floxyPort = toPort("PCAT_FLOXY_PORT", FLOXY_PORT);
this.floxyUser = cfg.get<string>("PCAT_FLOXY_USER", FLOXY_DEFAULT_USER);
this.floxyPass = cfg.get<string>("PCAT_FLOXY_PASS", FLOXY_DEFAULT_PASS);
this.floxyCaptureLifetime = toInt("PCAT_FLOXY_CAPTURE_LIFETIME", FLOXY_CAPTURE_LIFETIME_S);
this.diHost = cfg.get<string>("PCAT_PROXY_HOST", DI_HOST);
this.diUser = cfg.get<string>("PCAT_PROXY_USER", DI_DEFAULT_USER);
this.diPass = cfg.get<string>("PCAT_PROXY_PASS", DI_DEFAULT_PASS);
this.captureAllowDirect =
cfg.get<string>("PCAT_CAPTURE_ALLOW_DIRECT", "true") !== "false" &&
this.proxyProvider !== "none";
this.logger.log(
this.proxyProvider === "floxy"
? `PCAT transport: floxy residential ${this.floxyHost}:${this.floxyPort} (call=rotating, capture=sticky ${this.floxyCaptureLifetime}s)`
: this.proxyProvider === "dataimpulse"
? `PCAT transport: dataimpulse ${this.diHost}:${DI_PORT_MIN}-${DI_PORT_MAX}`
: "PCAT transport: direct (no proxy)",
);
}
/**
* Build a proxy for one leg. "call" rotates the exit IP per invocation (the
* token is not IP-bound, so each decode/scrape/validate request gets a fresh
* residential IP → max IP diversity, min ban). "capture" pins a sticky IP for
* the lifetime of a single Playwright capture so the partner site sees a
* coherent session. Returns nulls when provider is "none" (direct).
*/
private buildProxy(leg: ProxyLeg): {
proxyUrl: string | null;
proxyConfig: { server: string; username: string; password: string } | null;
// Telemetry identity: sticky Floxy session id or DataImpulse port. Null on
// rotating legs (exit IP changes per request — nothing stable to attribute).
sessionKey: string | null;
// The provider actually used for this leg (may differ from the configured
// provider when Floxy is in failover). Drives accurate telemetry + which
// provider's health to report success/failure against.
provider: ProxyProvider;
} {
if (this.proxyProvider === "none")
return { proxyUrl: null, proxyConfig: null, sessionKey: null, provider: "none" };
// Floxy-primary but the shared health gate is tripped (Floxy down — balance
// or tunnel) → fail over to the DataImpulse datacenter pool for this leg so
// captures/calls keep landing instead of dying on a dead residential proxy.
const useDataImpulse =
this.proxyProvider === "dataimpulse" ||
(this.proxyProvider === "floxy" && this.proxyHealth.isFloxyDown());
if (useDataImpulse) {
const port = this.allocatePort();
const server = `http://${this.diHost}:${port}`;
return {
proxyUrl: `http://${this.diUser}:${this.diPass}@${this.diHost}:${port}`,
proxyConfig: { server, username: this.diUser, password: this.diPass },
sessionKey: `di:${port}`,
provider: "dataimpulse",
};
}
// floxy
let password = this.floxyPass;
let sessionKey: string | null = null;
if (leg === "capture" && this.floxyCaptureLifetime > 0) {
const sid = Math.random().toString(36).slice(2, 10);
password = `${this.floxyPass}_session-${sid}_lifetime-${this.floxyCaptureLifetime}`;
sessionKey = `s-${sid}`;
}
const server = `http://${this.floxyHost}:${this.floxyPort}`;
return {
proxyUrl: `http://${this.floxyUser}:${password}@${this.floxyHost}:${this.floxyPort}`,
proxyConfig: { server, username: this.floxyUser, password },
sessionKey,
provider: "floxy",
};
} }
async onModuleInit(): Promise<void> { async onModuleInit(): Promise<void> {
@@ -193,10 +350,14 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
// ─── Public API ─────────────────────────────────────────── // ─── Public API ───────────────────────────────────────────
/** /**
* Acquire a matched JWT + proxy session from the pool. * Acquire a JWT session from the pool.
* Returns a valid slot (round-robin) or captures on-demand if pool is empty. * Returns a valid slot (round-robin) or, if the pool is empty, captures
* on-demand. With `blockOnEmpty: false` it does NOT block on a cold pool —
* it kicks off a background warm-up and returns null so a latency-sensitive
* caller (the decode race) can let a faster source answer instead of eating
* the multi-second capture cost inline.
*/ */
async acquireSession(): Promise<PcatSession> { async acquireSession(opts?: { blockOnEmpty?: boolean }): Promise<PcatSession | null> {
this.trackRequest(); this.trackRequest();
// Try to find a valid slot in the pool // Try to find a valid slot in the pool
@@ -207,14 +368,26 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
return this.slotToSession(slot); return this.slotToSession(slot);
} }
// No valid slot — capture on-demand // No valid slot. Latency-sensitive callers fast-fail and warm in background.
if (opts?.blockOnEmpty === false) {
this.logger.log("JWT pool empty — fast-fail (warming in background)");
this.captureToPool().catch((err) => {
this.logger.warn(`Background warm capture failed: ${(err as Error).message}`);
});
return null;
}
// Capture on-demand (blocks the caller).
this.logger.log("JWT pool empty — capturing on-demand..."); this.logger.log("JWT pool empty — capturing on-demand...");
const newSlot = await this.captureToPool(); const newSlot = await this.captureToPool();
return this.slotToSession(newSlot); return this.slotToSession(newSlot);
} }
/** /**
* Invalidate a session after 401/403, remove the slot, and capture a replacement. * Invalidate a session (401/403, or N consecutive transport failures on
* rotating IPs = a token that's dead at the source), remove the slot, and
* capture a replacement only if the pool is now empty — a surviving sibling
* plus the scheduled refresh/scale-up covers the rest without a capture storm.
*/ */
async invalidateSession(session: PcatSession): Promise<void> { async invalidateSession(session: PcatSession): Promise<void> {
const idx = this.pool.indexOf(session._slot); const idx = this.pool.indexOf(session._slot);
@@ -222,17 +395,18 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
this.clearSlotTimer(this.pool[idx]); this.clearSlotTimer(this.pool[idx]);
this.pool.splice(idx, 1); this.pool.splice(idx, 1);
this.logger.log( this.logger.log(
`JWT pool: slot invalidated (port ${session._slot.proxyPort}), ${this.pool.length} remaining`, `JWT pool: slot invalidated (${new URL(session._slot.siteUsed).hostname}), ${this.pool.length} remaining`,
); );
} }
// The Redis-shared slot may point at a now-401'd JWT (e.g. IP rotated // The Redis-shared slot may point at this same now-dead token. Drop it so
// away on the proxy). Drop it so other containers re-capture instead of // a cold-booting container re-captures instead of hydrating the dead token.
// inheriting the same dead slot.
this.deleteFromRedis().catch(() => {}); this.deleteFromRedis().catch(() => {});
// Capture replacement in background (don't block the caller's retry) // Only force an immediate replacement when the pool emptied; otherwise let
if (this.isBusinessHours() || this.pool.length === 0) { // the surviving slot serve and the refresh/scale-up timers backfill, so a
// burst of evictions can't stampede the capture pipeline.
if (this.pool.length === 0) {
this.captureToPool().catch((err) => { this.captureToPool().catch((err) => {
this.logger.error(`Replacement capture failed: ${(err as Error).message}`); this.logger.error(`Replacement capture failed: ${(err as Error).message}`);
}); });
@@ -241,6 +415,20 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
// ─── Pool management ────────────────────────────────────── // ─── Pool management ──────────────────────────────────────
/**
* Trim the pool to MAX_POOL_SIZE, evicting the oldest slots (by capturedAt) and
* clearing their refresh timers. Idempotent; a no-op when at/under the cap.
*/
private enforcePoolCap(): void {
if (this.pool.length <= MAX_POOL_SIZE) return;
this.pool.sort((a, b) => a.capturedAt - b.capturedAt); // oldest first
const evicted = this.pool.splice(0, this.pool.length - MAX_POOL_SIZE);
for (const s of evicted) this.clearSlotTimer(s);
this.logger.warn(
`JWT pool: over cap, evicted ${evicted.length} oldest slot(s), size now ${this.pool.length}`,
);
}
private pickValidSlot(): JwtSlot | null { private pickValidSlot(): JwtSlot | null {
const now = Math.floor(Date.now() / 1000); const now = Math.floor(Date.now() / 1000);
// Remove expired slots // Remove expired slots
@@ -262,16 +450,10 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
} }
private slotToSession(slot: JwtSlot): PcatSession { private slotToSession(slot: JwtSlot): PcatSession {
const proxyUrl = this.useProxy // Fresh rotating call-leg proxy per request — and per retry, since
? `http://${this.proxyUser}:${this.proxyPass}@${this.proxyHost}:${slot.proxyPort}` // fetchWithAuth re-acquires the session each attempt → a new exit IP each
: null; // time, so a slow/blocked residential IP is shed on the next try.
const proxyConfig = this.useProxy const { proxyUrl, proxyConfig } = this.buildProxy("call");
? {
server: `http://${this.proxyHost}:${slot.proxyPort}`,
username: this.proxyUser,
password: this.proxyPass,
}
: null;
return { return {
apiKey: slot.jwt.raw, apiKey: slot.jwt.raw,
apiPath: slot.jwt.apiPath, apiPath: slot.jwt.apiPath,
@@ -294,17 +476,16 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
const maxRetries = 4; const maxRetries = 4;
for (let attempt = 0; attempt < maxRetries; attempt++) { for (let attempt = 0; attempt < maxRetries; attempt++) {
const port = this.allocatePort();
const siteUrl = this.getAvailableSite(); const siteUrl = this.getAvailableSite();
const jwt = await this.attemptCapture(siteUrl, port); const jwt = await this.attemptCapture(siteUrl);
if (jwt) { if (jwt) {
// Validate the JWT+proxy combo against the real upstream before // Validate the token against the real upstream (on a fresh rotating
// publishing the slot — see VALIDATION_VIN comment above. // call-leg IP) before publishing the slot — see VALIDATION_VIN.
const valid = await this.validateSlot(jwt, port); const valid = await this.validateSlot(jwt);
if (!valid) { if (!valid) {
this.logger.warn( this.logger.warn(
`JWT capture attempt ${attempt + 1}/${maxRetries} validation failed (port ${port}, ${new URL(siteUrl).hostname}), trying next site...`, `JWT capture attempt ${attempt + 1}/${maxRetries} validation failed (${new URL(siteUrl).hostname}), trying next site...`,
); );
// Mark site as used anyway so we rotate away rather than retry it. // Mark site as used anyway so we rotate away rather than retry it.
this.siteLastUsedAt.set(siteUrl, Date.now()); this.siteLastUsedAt.set(siteUrl, Date.now());
@@ -313,17 +494,24 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
const slot: JwtSlot = { const slot: JwtSlot = {
jwt, jwt,
proxyPort: port,
siteUsed: siteUrl, siteUsed: siteUrl,
capturedAt: Date.now(), capturedAt: Date.now(),
refreshTimer: null, refreshTimer: null,
failCount: 0,
}; };
// Mark site as used // Mark site as used
this.siteLastUsedAt.set(siteUrl, Date.now()); this.siteLastUsedAt.set(siteUrl, Date.now());
// Add to pool // Add to pool, then enforce the hard cap. A slot-accounting race in the
// refresh path (concurrent captures via the semaphore, or a refresh
// whose old slot was already removed) could otherwise let the pool grow
// past MAX_POOL_SIZE — observed at 32-39 on prod, which multiplies
// capture load and partner-site ban exposure for no benefit. Evicting
// the oldest slots keeps the freshest MAX_POOL_SIZE and makes the
// documented invariant hold regardless of the race.
this.pool.push(slot); this.pool.push(slot);
this.enforcePoolCap();
// Publish to Redis so other containers can hydrate from this slot. // Publish to Redis so other containers can hydrate from this slot.
this.persistToRedis(slot).catch(() => {}); this.persistToRedis(slot).catch(() => {});
@@ -352,13 +540,14 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
} }
/** /**
* Probe the just-captured token + proxy port with a single cheap upstream * Probe the just-captured token with a single cheap upstream call (/car/info
* call (/car/info with a public demo VIN). Returns true iff the upstream * with a public demo VIN) on a fresh rotating call-leg IP — the SAME transport
* accepts the combo with 2xx within VALIDATION_TIMEOUT_MS. A failure here * a real request will use. Returns true iff upstream accepts it with 2xx within
* (proxy dead, IP blocked, token 401/403) is far cheaper to absorb at * VALIDATION_TIMEOUT_MS. Validating on the call-leg transport (not the capture
* capture time than to inherit when a real user clicks a category. * IP) is what makes the check meaningful: it rejects a token only if it can't
* be served the way it will actually be served.
*/ */
private async validateSlot(jwt: PcatJwtToken, port: number): Promise<boolean> { private async validateSlot(jwt: PcatJwtToken): Promise<boolean> {
const url = `${PCAT_API_BASE}/car/info?q=${VALIDATION_VIN}`; const url = `${PCAT_API_BASE}/car/info?q=${VALIDATION_VIN}`;
const fetchOptions: RequestInit & { dispatcher?: any } = { const fetchOptions: RequestInit & { dispatcher?: any } = {
method: "GET", method: "GET",
@@ -375,17 +564,35 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
}, },
signal: AbortSignal.timeout(VALIDATION_TIMEOUT_MS), signal: AbortSignal.timeout(VALIDATION_TIMEOUT_MS),
}; };
if (this.useProxy) { const { proxyUrl } = this.buildProxy("call");
if (proxyUrl) {
const { ProxyAgent } = await import("undici"); const { ProxyAgent } = await import("undici");
fetchOptions.dispatcher = new ProxyAgent({ fetchOptions.dispatcher = new ProxyAgent({
uri: `http://${this.proxyUser}:${this.proxyPass}@${this.proxyHost}:${port}`, uri: proxyUrl,
connect: { timeout: 6_000 }, connect: { timeout: 6_000 },
}); });
} }
const start = Date.now();
try { try {
const r = await fetch(url, fetchOptions); const r = await fetch(url, fetchOptions);
this.proxyTelemetry.record({
service: "pcat_validate",
provider: this.proxyProvider,
targetHost: "gui.parts-catalogs.com",
statusCode: r.status,
success: r.ok,
durationMs: Date.now() - start,
});
return r.ok; return r.ok;
} catch (err) { } catch (err) {
this.proxyTelemetry.record({
service: "pcat_validate",
provider: this.proxyProvider,
targetHost: "gui.parts-catalogs.com",
errorKind: classifyTransportError(err),
success: false,
durationMs: Date.now() - start,
});
this.logger.debug(`Slot validation threw: ${(err as Error).message}`); this.logger.debug(`Slot validation threw: ${(err as Error).message}`);
return false; return false;
} }
@@ -539,15 +746,16 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
private getAvailableSite(): string { private getAvailableSite(): string {
const now = Date.now(); const now = Date.now();
const sites = this.jwtSites;
// Try round-robin, preferring sites not on cooldown // Try round-robin, preferring sites not on cooldown
for (let i = 0; i < JWT_SITES.length; i++) { for (let i = 0; i < sites.length; i++) {
const idx = (this.siteIndex + i) % JWT_SITES.length; const idx = (this.siteIndex + i) % sites.length;
const site = JWT_SITES[idx]; const site = sites[idx];
const lastUsed = this.siteLastUsedAt.get(site) || 0; const lastUsed = this.siteLastUsedAt.get(site) || 0;
if (now - lastUsed >= SITE_COOLDOWN) { if (now - lastUsed >= SITE_COOLDOWN) {
this.siteIndex = (idx + 1) % JWT_SITES.length; this.siteIndex = (idx + 1) % sites.length;
return site; return site;
} }
} }
@@ -555,15 +763,15 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
// All on cooldown — pick the one with oldest usage // All on cooldown — pick the one with oldest usage
let oldestIdx = 0; let oldestIdx = 0;
let oldestTime = Number.POSITIVE_INFINITY; let oldestTime = Number.POSITIVE_INFINITY;
for (let i = 0; i < JWT_SITES.length; i++) { for (let i = 0; i < sites.length; i++) {
const lastUsed = this.siteLastUsedAt.get(JWT_SITES[i]) || 0; const lastUsed = this.siteLastUsedAt.get(sites[i]) || 0;
if (lastUsed < oldestTime) { if (lastUsed < oldestTime) {
oldestTime = lastUsed; oldestTime = lastUsed;
oldestIdx = i; oldestIdx = i;
} }
} }
this.siteIndex = (oldestIdx + 1) % JWT_SITES.length; this.siteIndex = (oldestIdx + 1) % sites.length;
return JWT_SITES[oldestIdx]; return sites[oldestIdx];
} }
private allocatePort(): number { private allocatePort(): number {
@@ -572,19 +780,52 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
// ─── JWT capture via Playwright ─────────────────────────── // ─── JWT capture via Playwright ───────────────────────────
private async attemptCapture(siteUrl: string, port: number): Promise<PcatJwtToken | null> { private async attemptCapture(siteUrl: string, forceDirect = false): Promise<PcatJwtToken | null> {
let context: BrowserContext | null = null; let context: BrowserContext | null = null;
const startTime = Date.now(); const startTime = Date.now();
try { // Sticky capture proxy: one residential exit IP pinned for the lifetime of
// Build context options with proxy // this capture so the partner site + widget see a coherent session.
const contextOptions: Record<string, unknown> = {}; // forceDirect (set on retry after a proxy tunnel failure) skips the proxy —
if (this.useProxy) { // see captureAllowDirect: the token is not IP-bound so a direct capture is a
contextOptions.proxy = { // safe last resort that keeps the pool alive through a full proxy outage.
server: `http://${this.proxyHost}:${port}`, const { proxyConfig, proxyUrl, sessionKey, provider } = forceDirect
username: this.proxyUser, ? {
password: this.proxyPass, proxyConfig: null,
proxyUrl: null as string | null,
sessionKey: null as string | null,
provider: "none" as ProxyProvider,
}
: this.buildProxy("capture");
// Set when a proxied navigation dies on a tunnel/connect failure and direct
// capture is allowed — we fall through to a direct retry instead of counting
// this as a dead site.
let retryDirect = false;
// The session is sticky, so a parallel ipify probe exits from the SAME IP
// the capture will use — that's what makes banned-IP tracking concrete.
// Resolves in ~1-2s while the capture itself takes 7s+; never throws.
const exitIpPromise = proxyUrl ? resolveExitIp(proxyUrl) : Promise.resolve(null);
const logCapture = (ok: boolean, errorKind?: string): void => {
void exitIpPromise.then((exitIp) =>
this.proxyTelemetry.record({
service: "pcat_capture",
provider,
sessionKey,
exitIp,
targetHost: new URL(siteUrl).hostname,
errorKind: errorKind ?? null,
success: ok,
durationMs: Date.now() - startTime,
}),
);
}; };
try {
const contextOptions: Record<string, unknown> = {};
if (proxyConfig) {
contextOptions.proxy = proxyConfig;
} }
if (!this.browser) throw new Error("PCAT browser not initialized"); if (!this.browser) throw new Error("PCAT browser not initialized");
@@ -640,12 +881,16 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
return route.continue(); return route.continue();
}); });
// Navigate — networkidle waits for widget JS to load + make API calls // Navigate with domcontentloaded (not networkidle): on JS-heavy catalog
// sites through a slow residential proxy networkidle often never settles
// and the goto times out before the widget fires its token call. DOM-ready
// is reliable (~7s measured); the request listener + post-goto poll then
// catch the /v3/api/proxy token call (~11.8s measured).
let gotoOk = true; let gotoOk = true;
try { try {
await page.goto(siteUrl, { await page.goto(siteUrl, {
timeout: PAGE_TIMEOUT, timeout: PAGE_TIMEOUT,
waitUntil: "networkidle", waitUntil: "domcontentloaded",
}); });
} catch (navErr) { } catch (navErr) {
// Navigation may timeout but JWT could still be captured if the // Navigation may timeout but JWT could still be captured if the
@@ -653,8 +898,21 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
// constants above. // constants above.
gotoOk = false; gotoOk = false;
this.logger.debug(`Navigation ended: ${(navErr as Error).message?.slice(0, 80)}`); this.logger.debug(`Navigation ended: ${(navErr as Error).message?.slice(0, 80)}`);
const connectFailed = isProxyConnectFailure(navErr);
// A tunnel/connect failure here (not a plain slow-site timeout) means the
// Floxy exit is dead — trip the shared gate so this + EMEX fail over to
// DataImpulse for the cooldown window.
if (provider === "floxy" && connectFailed) {
this.proxyHealth.reportFloxyFailure(`pcat capture: ${classifyTransportError(navErr)}`);
}
// Proxy tunnel is dead (all providers). Rather than return an empty
// capture and starve the pool, retry this same site direct.
if (connectFailed && proxyConfig && this.captureAllowDirect && !forceDirect) {
retryDirect = true;
}
} }
if (!retryDirect) {
// Poll for token — short cap after healthy goto, brief grace after fail // Poll for token — short cap after healthy goto, brief grace after fail
const pollMax = gotoOk ? CAPTURE_POLL_AFTER_OK : CAPTURE_POLL_AFTER_FAIL; const pollMax = gotoOk ? CAPTURE_POLL_AFTER_OK : CAPTURE_POLL_AFTER_FAIL;
for (let i = 0; i < pollMax; i++) { for (let i = 0; i < pollMax; i++) {
@@ -665,15 +923,33 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
const elapsed = Date.now() - startTime; const elapsed = Date.now() - startTime;
if (capturedToken) { if (capturedToken) {
this.logger.log(`Token captured in ${elapsed}ms from ${new URL(siteUrl).hostname}`); logCapture(true);
// Floxy answered — clear any failover cooldown so we resume residential.
if (provider === "floxy") this.proxyHealth.reportFloxySuccess();
this.logger.log(
`Token captured in ${elapsed}ms from ${new URL(siteUrl).hostname}${forceDirect ? " (direct)" : ""}`,
);
return capturedToken; return capturedToken;
} }
// Widget never fired its token call — on a sticky residential IP this is
// the classic "partner site blocked/ratelimited this exit IP" signature.
logCapture(false, "capture_timeout");
this.logger.debug(`No token after ${elapsed}ms from ${siteUrl}`); this.logger.debug(`No token after ${elapsed}ms from ${siteUrl}`);
return null; return null;
}
} catch (err) { } catch (err) {
logCapture(false, classifyTransportError(err));
const connectFailed = isProxyConnectFailure(err);
if (provider === "floxy" && connectFailed) {
this.proxyHealth.reportFloxyFailure(`pcat capture: ${classifyTransportError(err)}`);
}
if (connectFailed && proxyConfig && this.captureAllowDirect && !forceDirect) {
retryDirect = true;
} else {
this.logger.warn(`Token capture error: ${(err as Error).message}`); this.logger.warn(`Token capture error: ${(err as Error).message}`);
return null; return null;
}
} finally { } finally {
if (context) { if (context) {
try { try {
@@ -686,6 +962,13 @@ export class PartsCatalogsAuthService implements OnModuleInit, OnModuleDestroy {
} }
} }
} }
// Reached only when a proxied attempt hit a dead tunnel and direct is
// allowed — retry the same site straight from the server IP.
this.logger.warn(
`Capture proxy tunnel dead (${provider}) for ${new URL(siteUrl).hostname} — retrying direct`,
);
return this.attemptCapture(siteUrl, true);
} }
// ─── Browser lifecycle ─────────────────────────────────── // ─── Browser lifecycle ───────────────────────────────────

View File

@@ -1,8 +1,10 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { ProxyTelemetryModule } from "../proxy-telemetry/proxy-telemetry.module";
import { PartsCatalogsAuthService } from "./parts-catalogs-auth.service"; import { PartsCatalogsAuthService } from "./parts-catalogs-auth.service";
import { PartsCatalogsService } from "./parts-catalogs.service"; import { PartsCatalogsService } from "./parts-catalogs.service";
@Module({ @Module({
imports: [ProxyTelemetryModule],
providers: [PartsCatalogsAuthService, PartsCatalogsService], providers: [PartsCatalogsAuthService, PartsCatalogsService],
exports: [PartsCatalogsService], exports: [PartsCatalogsService],
}) })

View File

@@ -2,14 +2,22 @@
* Parts-Catalogs API Service — HTTP client for parts-catalogs.com v3 * Parts-Catalogs API Service — HTTP client for parts-catalogs.com v3
* *
* Calls the v3 widget proxy (gui.parts-catalogs.com/v3/api/proxy/*) with the * Calls the v3 widget proxy (gui.parts-catalogs.com/v3/api/proxy/*) with the
* captured TWS- token + supporting X-* headers. Requests must go through the * captured TWS- token + supporting X-* headers. The token is portable (NOT
* same DataImpulse proxy port that captured the token (IP-bound). * IP-bound), so each request rotates a fresh exit IP via the auth service's
* call-leg proxy (PartsCatalogsAuthService.buildProxy).
*/ */
import { Injectable, Logger } from "@nestjs/common"; import { Injectable, Logger } from "@nestjs/common";
import { isBackfillContext } from "../../jobs/prefetch-context"; import { isBackfillContext } from "../../jobs/prefetch-context";
import { PostHogService } from "../../posthog/posthog.service"; import { PostHogService } from "../../posthog/posthog.service";
import { RedisService } from "../../redis/redis.service"; import { RedisService } from "../../redis/redis.service";
import { ProxyHealthService } from "../proxy-telemetry/proxy-health.service";
import {
ProxyTelemetryService,
classifyTransportError,
describeProxyUrl,
isProxyConnectFailure,
} from "../proxy-telemetry/proxy-telemetry.service";
import { PartsCatalogsAuthService } from "./parts-catalogs-auth.service"; import { PartsCatalogsAuthService } from "./parts-catalogs-auth.service";
import { import {
PcatCar, PcatCar,
@@ -20,19 +28,38 @@ import {
} from "./parts-catalogs.types"; } from "./parts-catalogs.types";
const API_BASE = "https://gui.parts-catalogs.com/v3/api/proxy"; const API_BASE = "https://gui.parts-catalogs.com/v3/api/proxy";
const REQUEST_TIMEOUT = Number(process.env.PCAT_REQUEST_TIMEOUT_MS) || 30_000; // Catalog scrape (groups2/parts2). Lowered 30s→10s (2026-06-10): the old 30s
// default could blow the caller's 25s decode budget on a single slow attempt,
// and through a rotating residential proxy a healthy call answers in 1-4s. A
// slow/blocked exit IP now aborts at 10s and the retry re-acquires a fresh IP.
const REQUEST_TIMEOUT = Number(process.env.PCAT_REQUEST_TIMEOUT_MS) || 10_000;
const MAX_RETRIES = Number(process.env.PCAT_MAX_RETRIES) || 2; const MAX_RETRIES = Number(process.env.PCAT_MAX_RETRIES) || 2;
// A dead DataImpulse proxy port otherwise stalls for undici's 10s default connect // undici's ProxyAgent connect.timeout bounds the connect to the proxy entry.
// timeout before the retry rotates to a fresh port — three of those blow the
// caller's 25s decode budget. Fail fast so retries reach a live port in time.
const PROXY_CONNECT_TIMEOUT = Number(process.env.PCAT_PROXY_CONNECT_TIMEOUT_MS) || 6_000; const PROXY_CONNECT_TIMEOUT = Number(process.env.PCAT_PROXY_CONNECT_TIMEOUT_MS) || 6_000;
// VIN decode (/car/info) answers in <1s on a healthy proxy, so a long timeout only // VIN decode (/car/info) answers in 1-4s through the rotating residential proxy.
// prolongs dead-port connects. undici's ProxyAgent connect.timeout does NOT bound the // A short per-call timeout + retries means a slow exit IP aborts fast and the
// connection to the proxy itself (it stays at undici's 10s default), so we bound it at // next attempt re-acquires the session → a fresh IP, all within the 25s budget.
// the fetch level: a short per-call timeout + extra retries makes a stuck DataImpulse
// port abort fast and rotate to a live one within the caller's 25s decode budget.
const DECODE_REQUEST_TIMEOUT = Number(process.env.PCAT_DECODE_TIMEOUT_MS) || 6_000; const DECODE_REQUEST_TIMEOUT = Number(process.env.PCAT_DECODE_TIMEOUT_MS) || 6_000;
const DECODE_MAX_RETRIES = Number(process.env.PCAT_DECODE_MAX_RETRIES) || 4; const DECODE_MAX_RETRIES = Number(process.env.PCAT_DECODE_MAX_RETRIES) || 4;
// Evict a pooled token after this many consecutive transport failures (reset on
// any 2xx). Through the rotating residential proxy each attempt hits a different
// exit IP, so N failures in a row on N different IPs ⇒ the token is dead at the
// source, not a flaky IP. Pre-rotation this was the "dead slot never evicted"
// bug: invalidateSession only fired on 401/403, so a bad token was served until TTL.
const TRANSPORT_FAIL_EVICT_THRESHOLD = Number(process.env.PCAT_TRANSPORT_FAIL_EVICT) || 2;
/**
* Thrown when a fast-fail (blockOnEmpty:false) caller hits a cold pool. NOT a
* "VIN not found" — pcat was skipped while the pool warmed, so the caller must
* treat it as a transient miss (don't poison the negative cache) and let
* another source answer.
*/
class PcatColdPoolError extends Error {
constructor() {
super("pcat cold pool — skipped to avoid inline capture");
this.name = "PcatColdPoolError";
}
}
@Injectable() @Injectable()
export class PartsCatalogsService { export class PartsCatalogsService {
@@ -42,6 +69,8 @@ export class PartsCatalogsService {
private authService: PartsCatalogsAuthService, private authService: PartsCatalogsAuthService,
private redis: RedisService, private redis: RedisService,
private posthog: PostHogService, private posthog: PostHogService,
private proxyTelemetry: ProxyTelemetryService,
private proxyHealth: ProxyHealthService,
) {} ) {}
/** Mark parts-catalogs as actively used (5min TTL) to defer prefetch worker */ /** Mark parts-catalogs as actively used (5min TTL) to defer prefetch worker */
@@ -62,7 +91,8 @@ export class PartsCatalogsService {
async decodeVin( async decodeVin(
vin: string, vin: string,
signal?: AbortSignal, signal?: AbortSignal,
outcome?: { transient: boolean }, outcome?: { transient: boolean; coldSkip?: boolean },
opts?: { blockOnEmpty?: boolean },
): Promise<PcatVinResult | null> { ): Promise<PcatVinResult | null> {
if (!(await this.posthog.isSourceLive("parts-catalogs"))) { if (!(await this.posthog.isSourceLive("parts-catalogs"))) {
this.logger.warn("parts-catalogs disabled by kill switch (kill-source-parts-catalogs)"); this.logger.warn("parts-catalogs disabled by kill switch (kill-source-parts-catalogs)");
@@ -72,6 +102,7 @@ export class PartsCatalogsService {
const data = await this.fetchWithAuth("/car/info", { q: vin }, signal, { const data = await this.fetchWithAuth("/car/info", { q: vin }, signal, {
timeoutMs: DECODE_REQUEST_TIMEOUT, timeoutMs: DECODE_REQUEST_TIMEOUT,
maxRetries: DECODE_MAX_RETRIES, maxRetries: DECODE_MAX_RETRIES,
blockOnEmpty: opts?.blockOnEmpty,
}); });
if (!data || typeof data !== "object") { if (!data || typeof data !== "object") {
@@ -105,9 +136,23 @@ export class PartsCatalogsService {
return { cars }; return { cars };
} catch (err) { } catch (err) {
const e = err as Error & { cause?: unknown }; const e = err as Error & { cause?: unknown };
// Distinguish a transient transport blip (proxy timeout / dropped DataImpulse // Cold-pool fast-fail: pcat was skipped (pool warming), NOT a definitive
// connection) from a genuine "not found". The caller uses `outcome.transient` // miss. Mark transient so the orchestrator doesn't negative-cache a VIN
// to decide whether to poison the negative cache — a blip must not. // pcat hasn't actually evaluated, and stay quiet (this is expected).
if (e instanceof PcatColdPoolError) {
if (outcome) {
outcome.transient = true;
// A deliberate cold-pool skip is not a transport fault — flag it so the
// circuit breaker doesn't count it (the pool is already warming).
outcome.coldSkip = true;
}
this.logger.debug(`pcat decode skipped for ${vin}: cold pool (warming)`);
return null;
}
// Distinguish a transient transport blip (proxy timeout / dropped
// connection) from a genuine "not found". The caller uses
// `outcome.transient` to decide whether to poison the negative cache — a
// blip must not.
if ( if (
outcome && outcome &&
(e.name === "TimeoutError" || (e.name === "TimeoutError" ||
@@ -212,7 +257,7 @@ export class PartsCatalogsService {
endpoint: string, endpoint: string,
params?: Record<string, string>, params?: Record<string, string>,
externalSignal?: AbortSignal, externalSignal?: AbortSignal,
opts?: { timeoutMs?: number; maxRetries?: number }, opts?: { timeoutMs?: number; maxRetries?: number; blockOnEmpty?: boolean },
): Promise<any> { ): Promise<any> {
const maxRetries = opts?.maxRetries ?? MAX_RETRIES; const maxRetries = opts?.maxRetries ?? MAX_RETRIES;
const timeoutMs = opts?.timeoutMs ?? REQUEST_TIMEOUT; const timeoutMs = opts?.timeoutMs ?? REQUEST_TIMEOUT;
@@ -223,7 +268,12 @@ export class PartsCatalogsService {
if (externalSignal?.aborted) { if (externalSignal?.aborted) {
throw new Error("Request aborted by caller"); throw new Error("Request aborted by caller");
} }
session = await this.authService.acquireSession(); session = await this.authService.acquireSession({ blockOnEmpty: opts?.blockOnEmpty });
if (!session) {
// Cold pool + fast-fail: warming in the background; don't retry, let the
// caller fall through to another source.
throw new PcatColdPoolError();
}
const url = new URL(`${API_BASE}${endpoint}`); const url = new URL(`${API_BASE}${endpoint}`);
if (params) { if (params) {
@@ -232,6 +282,7 @@ export class PartsCatalogsService {
} }
} }
const attemptStart = Date.now();
try { try {
const signals = [AbortSignal.timeout(timeoutMs)]; const signals = [AbortSignal.timeout(timeoutMs)];
if (externalSignal) signals.push(externalSignal); if (externalSignal) signals.push(externalSignal);
@@ -262,7 +313,24 @@ export class PartsCatalogsService {
const response = await fetch(url.toString(), fetchOptions); const response = await fetch(url.toString(), fetchOptions);
// An HTTP answer arrived — the proxy leg worked at transport level;
// log the upstream's verdict (403/429 are the IP-ban signals).
this.proxyTelemetry.record({
service: "pcat_call",
...describeProxyUrl(session.proxyUrl),
targetHost: url.hostname,
statusCode: response.status,
success: response.ok,
durationMs: Date.now() - attemptStart,
});
if (response.ok) { if (response.ok) {
// Token proven good on this IP — clear any accrued failure streak.
session._slot.failCount = 0;
// Floxy answered at transport level — clear any failover cooldown.
if (describeProxyUrl(session.proxyUrl).provider === "floxy") {
this.proxyHealth.reportFloxySuccess();
}
return await response.json(); return await response.json();
} }
@@ -280,6 +348,23 @@ export class PartsCatalogsService {
throw new Error(`HTTP ${response.status} from ${endpoint}: ${text.slice(0, 200)}`); throw new Error(`HTTP ${response.status} from ${endpoint}: ${text.slice(0, 200)}`);
} catch (err) { } catch (err) {
const e = err as Error & { cause?: unknown }; const e = err as Error & { cause?: unknown };
// Transport failure (no HTTP answer) — the try block already logged any
// real HTTP response before rethrowing it as `Error: HTTP <code> …`.
if (!/^HTTP \d/.test(e.message ?? "")) {
this.proxyTelemetry.record({
service: "pcat_call",
...describeProxyUrl(session.proxyUrl),
targetHost: url.hostname,
errorKind: classifyTransportError(e),
success: false,
durationMs: Date.now() - attemptStart,
});
// Floxy exit refused/closed the CONNECT → trip the shared gate so the
// next capture/call (and EMEX) fails over to DataImpulse.
if (describeProxyUrl(session.proxyUrl).provider === "floxy" && isProxyConnectFailure(e)) {
this.proxyHealth.reportFloxyFailure(`pcat call: ${classifyTransportError(e)}`);
}
}
// Retry transient TRANSPORT failures only: request timeouts and undici // Retry transient TRANSPORT failures only: request timeouts and undici
// network errors ("TypeError: fetch failed" — a dropped/reset DataImpulse // network errors ("TypeError: fetch failed" — a dropped/reset DataImpulse
// proxy connection). A definitive HTTP response (e.g. 400 "list of parts // proxy connection). A definitive HTTP response (e.g. 400 "list of parts
@@ -291,17 +376,29 @@ export class PartsCatalogsService {
/fetch failed|ECONNRESET|ECONNREFUSED|EAI_AGAIN|socket hang up|other side closed|terminated|UND_ERR/i.test( /fetch failed|ECONNRESET|ECONNREFUSED|EAI_AGAIN|socket hang up|other side closed|terminated|UND_ERR/i.test(
`${e.message} ${String(e.cause ?? "")}`, `${e.message} ${String(e.cause ?? "")}`,
); );
if (transient && attempt < maxRetries) { if (transient) {
// A token that keeps failing transport across rotating IPs is dead at
// the source — evict it so it stops being round-robin-served (the old
// 401/403-only eviction never caught this). Only when a proxy is in use;
// direct-mode transport errors are upstream, not token/IP problems.
if (session.proxyUrl) {
session._slot.failCount = (session._slot.failCount ?? 0) + 1;
if (session._slot.failCount >= TRANSPORT_FAIL_EVICT_THRESHOLD) {
await this.authService.invalidateSession(session);
}
}
if (attempt < maxRetries) {
this.logger.warn( this.logger.warn(
`Transient transport error on ${endpoint} (attempt ${attempt + 1}/${maxRetries + 1}): ${e.message}${ `Transient transport error on ${endpoint} (attempt ${attempt + 1}/${maxRetries + 1}): ${e.message}${
e.cause ? ` [cause: ${String(e.cause)}]` : "" e.cause ? ` [cause: ${String(e.cause)}]` : ""
}`, }`,
); );
// Brief backoff so a momentarily-flaky proxy port can recover; the next // Brief backoff so a momentarily-flaky exit IP can recover; the next
// loop iteration re-acquires a session (round-robin across the pool). // loop iteration re-acquires a session (a fresh rotating IP).
await new Promise((r) => setTimeout(r, 400 * (attempt + 1))); await new Promise((r) => setTimeout(r, 400 * (attempt + 1)));
continue; continue;
} }
}
throw err; throw err;
} }
} }

View File

@@ -1,7 +1,12 @@
/** /**
* Captured from a parts-catalogs.com v3 widget request. * Captured from a parts-catalogs.com v3 widget request.
* Token + the supporting X-* headers the widget sends with every API call. * Token + the supporting X-* headers the widget sends with every API call.
* IP-bound (must be reused with the same proxy port that captured it). *
* NOT request-time IP-bound. Verified on prod 2026-06-10: 0 auth 401/403 across
* thousands of proxied calls, and a token captured on one exit IP replays with a
* 200 through arbitrary other IPs (incl. fresh residential ones). Upstream issues
* a stable shared token per origin. So the call leg rotates exit IPs freely; only
* the capture (Playwright) leg holds a sticky IP for the duration of one capture.
*/ */
export interface PcatJwtToken { export interface PcatJwtToken {
raw: string; // x-api-key value, e.g. "TWS-016EA7BE-..." raw: string; // x-api-key value, e.g. "TWS-016EA7BE-..."
@@ -15,10 +20,14 @@ export interface PcatJwtToken {
export interface JwtSlot { export interface JwtSlot {
jwt: PcatJwtToken; jwt: PcatJwtToken;
proxyPort: number;
siteUsed: string; siteUsed: string;
capturedAt: number; capturedAt: number;
refreshTimer: ReturnType<typeof setTimeout> | null; refreshTimer: ReturnType<typeof setTimeout> | null;
// Consecutive transport failures across requests (reset on any 2xx). The slot
// is evicted once this crosses a threshold — a token that keeps failing on
// fresh rotating IPs is dead at the source, not a flaky exit IP. Optional so
// slots persisted to Redis by older builds still hydrate.
failCount?: number;
} }
export interface PcatSession { export interface PcatSession {

View File

@@ -91,6 +91,20 @@ describe("PL24FordLegacyService.parseFordGroupsFromHtml — nav-crumb junk filte
expect(groups.some((g) => g.linkPath.includes("eu.nissan.biz"))).toBe(false); expect(groups.some((g) => g.linkPath.includes("eu.nissan.biz"))).toBe(false);
}); });
it("appends the Hyundai illustration description to the bare figure code", () => {
// Real Hyundai row shape: first <td> is the figure code (in a link), the
// second <td> is the human description — which the parser used to drop.
const html = [
"<table>",
'<tr class="tc-data-row" url="image-board.action?mainGroup=EN&subGroup=20-201A&illustration=1&lang=tr">' +
'<td class="identifier"><a href="image-board.action?subGroup=20-201A">20-201A .1</a></td>' +
"<td>SUB ENGINE ASSY</td><td></td></tr>",
"</table>",
].join("");
const groups = pg.parseFordGroupsFromHtml(html, "hyundai_parts", "");
expect(groups[0]?.nameTr).toBe("20-201A .1 SUB ENGINE ASSY");
});
it("keeps Volvo vin-group.action?...group1=... (real category) but drops the bare breadcrumb", () => { it("keeps Volvo vin-group.action?...group1=... (real category) but drops the bare breadcrumb", () => {
const html = [ const html = [
'<tr class="tc-data-row" url="vin-group.action?group1=2&lang=tr" caption="Motor"><td>Motor</td></tr>', '<tr class="tc-data-row" url="vin-group.action?group1=2&lang=tr" caption="Motor"><td>Motor</td></tr>',

View File

@@ -3663,9 +3663,10 @@ export class PL24FordLegacyService {
seen.add(url); seen.add(url);
const captionMatch = segment.match(/\bcaption="([^"]+)"/); const captionMatch = segment.match(/\bcaption="([^"]+)"/);
// Try to extract any text from <td> elements, including those with nested tags // Collect all <td> texts (nested tags stripped). The first non-empty one
let tdName = ""; // is the primary name for most brands.
const tdPattern = /<td[^>]*>([\s\S]*?)<\/td>/g; const tdPattern = /<td[^>]*>([\s\S]*?)<\/td>/g;
const tdTexts: string[] = [];
for (const tdMatch2 of segment.matchAll(tdPattern)) { for (const tdMatch2 of segment.matchAll(tdPattern)) {
const text = tdMatch2[1] const text = tdMatch2[1]
.replace(/<[^>]+>/g, " ") .replace(/<[^>]+>/g, " ")
@@ -3674,10 +3675,22 @@ export class PL24FordLegacyService {
.replace(/\xa0/g, " ") .replace(/\xa0/g, " ")
.replace(/\s+/g, " ") .replace(/\s+/g, " ")
.trim(); .trim();
if (text.length >= 3) { if (text.length >= 2) tdTexts.push(text);
tdName = text;
break;
} }
let tdName = tdTexts.find((t) => t.length >= 3) ?? "";
// Hyundai/Kia illustration rows put a bare figure code in the first cell
// ("09-091 .1") and the human description in the next ("SUB ENGINE ASSY").
// When the chosen name is code-only, append the first later cell that has
// real words so the leaf is nameable (and canonically mappable via ENGINE).
// Figure code like "09-091", "20-201A .1" (optional letter suffix + " .N"),
// or a purely numeric/dotted code.
const isCodeOnly = (s: string) => {
const t = s.trim();
return /^\d{1,3}-\d{1,4}[a-z]?(\s*\.\s*\d+)?$/i.test(t) || /^[\d.\s-]+$/.test(t);
};
if (tdName && isCodeOnly(tdName)) {
const desc = tdTexts.find((t) => t !== tdName && /[a-zA-ZğüşıöçĞÜŞİÖÇ]{3,}/.test(t));
if (desc) tdName = `${tdName} ${desc}`.replace(/\s+/g, " ").trim();
} }
// Caption may contain HTML-encoded content (e.g. &lt;div class="restrictionBlock"...&gt;) // Caption may contain HTML-encoded content (e.g. &lt;div class="restrictionBlock"...&gt;)

View File

@@ -565,6 +565,7 @@ export const PL24_WMI_SERVICE_MAP: Record<string, string> = {
// Opel / Vauxhall // Opel / Vauxhall
W0L: "opel_parts", // Opel AG (Germany) W0L: "opel_parts", // Opel AG (Germany)
W0V: "opel_parts", // Opel (newer Stellantis-era WMI) W0V: "opel_parts", // Opel (newer Stellantis-era WMI)
VXK: "opel_parts", // PSA/Stellantis-platform Opel (Corsa F, Mokka B — France/Spain plants)
// Citroën (PSA) // Citroën (PSA)
VF7: "citroen_parts", // Citroën SA (France) VF7: "citroen_parts", // Citroën SA (France)

View File

@@ -0,0 +1,76 @@
import type { ConfigService } from "@nestjs/config";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ProxyHealthService } from "./proxy-health.service";
import { isProxyConnectFailure } from "./proxy-telemetry.service";
function makeConfig(overrides: Record<string, unknown> = {}): ConfigService {
return {
get: vi.fn((key: string, def?: unknown) =>
Object.hasOwn(overrides, key) ? overrides[key] : def,
),
} as unknown as ConfigService;
}
describe("ProxyHealthService — Floxy failover gate", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it("starts healthy (Floxy not down)", () => {
const h = new ProxyHealthService(makeConfig());
expect(h.isFloxyDown()).toBe(false);
});
it("trips for the cooldown window then self-heals when it lapses", () => {
const h = new ProxyHealthService(makeConfig({ FLOXY_FAILOVER_COOLDOWN_MS: 180_000 }));
h.reportFloxyFailure("test");
expect(h.isFloxyDown()).toBe(true);
expect(h.cooldownRemainingSec()).toBe(180);
vi.advanceTimersByTime(179_000);
expect(h.isFloxyDown()).toBe(true);
vi.advanceTimersByTime(2_000); // past the 180s window
expect(h.isFloxyDown()).toBe(false);
});
it("a Floxy success clears the cooldown immediately", () => {
const h = new ProxyHealthService(makeConfig({ FLOXY_FAILOVER_COOLDOWN_MS: 180_000 }));
h.reportFloxyFailure();
expect(h.isFloxyDown()).toBe(true);
h.reportFloxySuccess();
expect(h.isFloxyDown()).toBe(false);
});
it("re-reporting a failure extends the window", () => {
const h = new ProxyHealthService(makeConfig({ FLOXY_FAILOVER_COOLDOWN_MS: 100_000 }));
h.reportFloxyFailure();
vi.advanceTimersByTime(90_000);
h.reportFloxyFailure(); // extends from now
vi.advanceTimersByTime(20_000); // 110s since first failure, 20s since second
expect(h.isFloxyDown()).toBe(true);
});
});
describe("isProxyConnectFailure", () => {
it("matches proxy/tunnel connection failures (the Floxy-down signal)", () => {
for (const msg of [
"page.goto: net::ERR_TUNNEL_CONNECTION_FAILED at https://emexdwc.ae/",
"net::ERR_PROXY_CONNECTION_FAILED",
"TypeError: fetch failed",
"connect ECONNREFUSED 1.2.3.4:12321",
"Client network socket disconnected (ECONNRESET)",
]) {
expect(isProxyConnectFailure(new Error(msg)), msg).toBe(true);
}
});
it("does NOT match a plain slow-site navigation timeout", () => {
const e = new Error("Timeout 30000ms exceeded.");
e.name = "TimeoutError";
expect(isProxyConnectFailure(e)).toBe(false);
});
});

View File

@@ -0,0 +1,68 @@
import { Injectable, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
/**
* Shared Floxy-health gate. Floxy is the residential primary for EMEX + pcat
* egress (HTTP fetch, Playwright capture, browser scrape). When Floxy goes dark
* — balance exhausted (proxy returns 402) or the entry node refuses CONNECT
* (`ERR_TUNNEL_CONNECTION_FAILED` / `fetch failed`) — every consumer otherwise
* keeps hammering the dead proxy and the decode success rate collapses (this
* happened 2026-06-11/13: ~17k tunnel failures/24h, decode 78% → 45%).
*
* This singleton is the one place that answers "should we route through Floxy
* right now?". Any consumer that sees a Floxy transport failure trips the gate;
* for the cooldown window all consumers fail over to the DataImpulse datacenter
* pool instead. The gate self-heals: once the cooldown lapses, the next request
* probes Floxy again and `reportFloxySuccess()` clears it (or re-trips if still
* down). A live Floxy answer at any time clears the cooldown immediately.
*
* In-memory only and deliberately dependency-light (no DB, no Redis): a single
* api/worker process is the egress unit, and a stale cooldown is self-correcting
* within one window. Cooldown is tunable via FLOXY_FAILOVER_COOLDOWN_MS.
*/
@Injectable()
export class ProxyHealthService {
private readonly logger = new Logger(ProxyHealthService.name);
private readonly cooldownMs: number;
private floxyDownUntil = 0;
constructor(private readonly config: ConfigService) {
const raw = Number(this.config.get("FLOXY_FAILOVER_COOLDOWN_MS", 180_000));
this.cooldownMs = Number.isFinite(raw) && raw >= 0 ? raw : 180_000;
}
/** True while Floxy is in cooldown — consumers should route via DataImpulse. */
isFloxyDown(): boolean {
return Date.now() < this.floxyDownUntil;
}
/**
* Floxy transport failure observed — open (or extend) the cooldown so every
* consumer fails over to DataImpulse. Logged once per outage, not per failure,
* so a 6k-failure storm doesn't flood the logs.
*/
reportFloxyFailure(reason?: string): void {
const wasDown = this.isFloxyDown();
this.floxyDownUntil = Date.now() + this.cooldownMs;
if (!wasDown) {
this.logger.warn(
`Floxy proxy unhealthy${reason ? ` (${reason})` : ""} — failing over to DataImpulse for ${Math.round(
this.cooldownMs / 1000,
)}s`,
);
}
}
/** A Floxy request just succeeded — Floxy is healthy again, clear the gate. */
reportFloxySuccess(): void {
if (this.isFloxyDown()) {
this.logger.log("Floxy proxy recovered — resuming Floxy-primary routing");
}
this.floxyDownUntil = 0;
}
/** Seconds remaining in the current cooldown (0 when healthy). */
cooldownRemainingSec(): number {
return Math.max(0, Math.ceil((this.floxyDownUntil - Date.now()) / 1000));
}
}

View File

@@ -0,0 +1,12 @@
import { Module } from "@nestjs/common";
import { ProxyHealthService } from "./proxy-health.service";
import { ProxyTelemetryService } from "./proxy-telemetry.service";
// Imported by PartsCatalogsModule and EmexModule; Nest module caching makes the
// services singletons (one shared flush buffer / one shared Floxy-health gate)
// across both.
@Module({
providers: [ProxyTelemetryService, ProxyHealthService],
exports: [ProxyTelemetryService, ProxyHealthService],
})
export class ProxyTelemetryModule {}

View File

@@ -0,0 +1,169 @@
import { Inject, Injectable, Logger, OnModuleDestroy } from "@nestjs/common";
import { DATABASE, type Database } from "../../database/database.provider";
import { proxyLogs } from "../../database/schema/core";
/**
* Proxy telemetry — fire-and-forget logging of every proxied upstream attempt
* (pcat call/capture/validate, emex http) into `proxy_logs`, so the Süper Panel
* can grade proxy providers per service and track banned exit IPs / sessions.
*
* Design constraints:
* - MUST never affect the request path: record() only pushes to an in-memory
* buffer; a timer flushes batches, and every DB error is swallowed.
* - Bounded memory: the buffer is capped; when full, oldest rows are dropped.
*/
export type ProxyServiceLeg = "pcat_call" | "pcat_capture" | "pcat_validate" | "emex_http";
export type ProxyProviderName = "floxy" | "dataimpulse" | "none";
export interface ProxyLogEvent {
service: ProxyServiceLeg;
provider: ProxyProviderName;
sessionKey?: string | null;
exitIp?: string | null;
targetHost?: string | null;
statusCode?: number | null;
errorKind?: string | null;
success: boolean;
durationMs?: number | null;
}
const FLUSH_INTERVAL_MS = 5_000;
const FLUSH_BATCH_SIZE = 50;
const BUFFER_CAP = 500;
// Upstream statuses that signal an IP-level block (not an auth/data problem):
// 403 = forbidden (WAF/geo/IP ban), 429 = rate limited. 402 is the proxy
// provider itself rejecting (balance exhausted — Floxy did this 2026-06-10);
// counted as banned=false but worth surfacing via status_code.
export function isBanStatus(status: number | null | undefined): boolean {
return status === 403 || status === 429;
}
/** Classify a transport error (no HTTP answer) into a coarse error_kind. */
export function classifyTransportError(err: unknown): string {
const e = err as Error & { cause?: unknown };
const text = `${e?.name ?? ""} ${e?.message ?? ""} ${String(e?.cause ?? "")}`;
if (/TimeoutError|UND_ERR_CONNECT_TIMEOUT|UND_ERR_HEADERS_TIMEOUT/i.test(text)) return "timeout";
if (/AbortError|aborted/i.test(text)) return "aborted";
if (/ECONNREFUSED|UND_ERR_SOCKET|connect/i.test(text)) return "connect";
if (/ECONNRESET|socket hang up|other side closed|terminated/i.test(text)) return "reset";
if (/EAI_AGAIN|ENOTFOUND/i.test(text)) return "dns";
return "transport";
}
/**
* True when an error means we could not even reach the upstream THROUGH the
* proxy — the proxy entry node refused/closed/reset the CONNECT, or undici/
* Chromium reported a tunnel/proxy connection failure ("fetch failed",
* ERR_TUNNEL_CONNECTION_FAILED, ERR_PROXY_CONNECTION_FAILED, ECONNREFUSED…).
* This is the "the proxy itself is dead" signal that should trip Floxy failover.
*
* Deliberately EXCLUDES a plain navigation/request TimeoutError with no proxy
* marker: a slow-but-alive residential exit shouldn't trip failover — only a
* genuine connection failure should.
*/
export function isProxyConnectFailure(err: unknown): boolean {
const e = err as Error & { cause?: unknown };
const text = `${e?.name ?? ""} ${e?.message ?? ""} ${String(e?.cause ?? "")}`;
return /ERR_TUNNEL_CONNECTION_FAILED|ERR_PROXY_CONNECTION_FAILED|ERR_SOCKS_CONNECTION_FAILED|NS_ERROR_PROXY|ECONNREFUSED|ECONNRESET|UND_ERR_SOCKET|UND_ERR_CONNECT_TIMEOUT|socket hang up|other side closed|tunnel|proxy connection|fetch failed/i.test(
text,
);
}
/**
* Derive provider + session key from a proxy URL the way the integrations
* build them: Floxy passwords may embed `_session-<id>_lifetime-<n>` (sticky);
* DataImpulse rotates by port, so the port IS the session identity.
*/
export function describeProxyUrl(proxyUrl: string | null | undefined): {
provider: ProxyProviderName;
sessionKey: string | null;
} {
if (!proxyUrl) return { provider: "none", sessionKey: null };
try {
const u = new URL(proxyUrl);
if (/floxy/i.test(u.hostname)) {
const m = decodeURIComponent(u.password).match(/_session-([A-Za-z0-9]+)/);
return { provider: "floxy", sessionKey: m ? `s-${m[1]}` : null };
}
return { provider: "dataimpulse", sessionKey: `di:${u.port}` };
} catch {
return { provider: "none", sessionKey: null };
}
}
/**
* Resolve the residential exit IP of a (sticky) proxy session with one cheap
* probe through the same proxy. Only meaningful for sticky sessions — on a
* rotating leg the probe and the real request exit from different IPs.
*/
export async function resolveExitIp(proxyUrl: string, timeoutMs = 5_000): Promise<string | null> {
try {
const { ProxyAgent } = await import("undici");
const res = await fetch("https://api.ipify.org?format=text", {
dispatcher: new ProxyAgent({ uri: proxyUrl, connect: { timeout: timeoutMs } }),
signal: AbortSignal.timeout(timeoutMs),
} as unknown as RequestInit);
if (!res.ok) return null;
const ip = (await res.text()).trim();
return /^[0-9a-fA-F.:]{3,45}$/.test(ip) ? ip : null;
} catch {
return null;
}
}
@Injectable()
export class ProxyTelemetryService implements OnModuleDestroy {
private readonly logger = new Logger(ProxyTelemetryService.name);
private buffer: ProxyLogEvent[] = [];
private flushTimer: NodeJS.Timeout | null = null;
private flushing = false;
constructor(@Inject(DATABASE) private readonly db: Database) {
this.flushTimer = setInterval(() => void this.flush(), FLUSH_INTERVAL_MS);
// Never keep the process alive just to flush telemetry.
this.flushTimer.unref?.();
}
/** Queue one proxied-attempt record. Never throws, never blocks. */
record(event: ProxyLogEvent): void {
if (this.buffer.length >= BUFFER_CAP) this.buffer.shift();
this.buffer.push(event);
if (this.buffer.length >= FLUSH_BATCH_SIZE) void this.flush();
}
private async flush(): Promise<void> {
if (this.flushing || this.buffer.length === 0) return;
this.flushing = true;
const batch = this.buffer.splice(0, FLUSH_BATCH_SIZE * 2);
try {
await this.db.insert(proxyLogs).values(
batch.map((e) => ({
service: e.service,
provider: e.provider,
sessionKey: e.sessionKey ?? null,
exitIp: e.exitIp ?? null,
targetHost: e.targetHost?.slice(0, 120) ?? null,
statusCode: e.statusCode ?? null,
errorKind: e.errorKind ?? null,
success: e.success,
banned: isBanStatus(e.statusCode),
durationMs: e.durationMs != null ? Math.round(e.durationMs) : null,
})),
);
} catch (err) {
// Telemetry must never matter more than the product. Drop the batch.
this.logger.debug(
`proxy_logs flush failed (${batch.length} rows): ${(err as Error).message}`,
);
} finally {
this.flushing = false;
}
}
async onModuleDestroy(): Promise<void> {
if (this.flushTimer) clearInterval(this.flushTimer);
await this.flush();
}
}

View File

@@ -0,0 +1,446 @@
/**
* Vinpin ePER Driver — warm-session browser driver (decode-oracle only).
*
* Drives the Vinpin ePER (Fiat) web catalog to decode a VIN the normal chain
* (PL24/pcat/emex) couldn't identify. Modeled on emex.browser.ts: ONE persistent
* Chromium + context + page kept alive for the app lifetime, lazy-launched,
* auto-recovering on disconnect.
*
* Differences from emex.browser:
* - The ePER UI is a pixel CANVAS (Blast/Horizon) with no stable selectors and
* no DOM-readable text, so navigation is coordinate-driven AND every screen
* transition is verified with a VISION LOOP: screenshot → OCR → check keyword
* → retry on miss (see vinpin.ocr + VINPIN_OCR keyword sets).
* - A single Vinpin seat → a hard mutex serializes decodes (one at a time).
* - The decode-modal text is read by OCR'ing a screenshot of the modal region
* (clipboard is unreliable on the Blast canvas — proven dead end).
*
* SAFETY CONTRACT: this driver must NEVER break its caller. When VINPIN_ENABLED
* is false, creds are missing, the browser fails to launch, or anything throws,
* `decode()` returns null (logged) — it never propagates.
*
* ⚠️ RUNTIME NOTE (Docker/xvfb): the Blast canvas was only VERIFIED working
* HEADFUL (chromium under a virtual display) with software GL — so the deployed
* worker should set VINPIN_HEADFUL=true and run under `xvfb-run -a` (a 1600x900
* screen). Launch honours PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH like emex. OCR
* additionally wants `ffmpeg` on PATH for upscaling (soft dep — degrades
* gracefully) and lets tesseract.js fetch+cache eng traineddata on first use.
*/
import { Injectable, Logger, type OnModuleDestroy } from "@nestjs/common";
import type { Browser, BrowserContext, Page } from "playwright";
import {
VINPIN_COORDS,
VINPIN_MODAL_REGION,
VINPIN_OCR,
VINPIN_VIEWPORT,
VINPIN_WAITS,
} from "./vinpin.constants";
import { ocrRegion, terminateOcr } from "./vinpin.ocr";
import { type VinpinParsed, isUsableParse, parseVinpinModal } from "./vinpin.parser";
export interface VinpinDecodeResult {
model: string | null;
sincom: string | null;
trim: string | null;
modelYear: string | null;
engine: string | null;
/** Opaque raw capture (modal text + meta) for debugging/audit. */
raw: unknown;
}
interface VinpinConfig {
enabled: boolean;
url: string;
user: string | undefined;
pass: string | undefined;
maxAttempts: number;
headful: boolean;
}
function readConfig(): VinpinConfig {
return {
enabled: process.env.VINPIN_ENABLED === "true",
url: process.env.VINPIN_URL || "https://login.tr.vinpin.online",
user: process.env.VINPIN_USER || undefined,
pass: process.env.VINPIN_PASS || undefined,
maxAttempts: Number(process.env.VINPIN_DECODE_MAX_ATTEMPTS) || 3,
headful: process.env.VINPIN_HEADFUL === "true",
};
}
@Injectable()
export class VinpinDriverService implements OnModuleDestroy {
private readonly logger = new Logger(VinpinDriverService.name);
private browser: Browser | null = null;
private context: BrowserContext | null = null;
private page: Page | null = null;
private loggedIn = false;
// Single Vinpin seat → serialize all decodes through one promise chain.
private lock: Promise<unknown> = Promise.resolve();
async onModuleDestroy(): Promise<void> {
await this.close();
await terminateOcr();
}
/**
* Decode a VIN through Vinpin ePER. Returns the parsed vehicle, or null when
* disabled / not found / on any failure (never throws to the caller).
*/
async decode(vin: string): Promise<VinpinDecodeResult | null> {
const cfg = readConfig();
if (!cfg.enabled) {
this.logger.debug("decode skipped — VINPIN_ENABLED is false");
return null;
}
if (!cfg.user || !cfg.pass) {
this.logger.warn("decode skipped — VINPIN_USER/VINPIN_PASS not set");
return null;
}
// Serialize behind the single-seat lock.
const run = this.lock.then(() => this.decodeLocked(vin, cfg));
// Keep the chain alive even if this run rejects (it shouldn't — defensive).
this.lock = run.catch(() => undefined);
return run;
}
/** Health snapshot for monitoring. */
healthCheck(): { browserConnected: boolean; loggedIn: boolean } {
return {
browserConnected: this.browser?.isConnected() ?? false,
loggedIn: this.loggedIn,
};
}
// ─── Private ─────────────────────────────────────────────
private async decodeLocked(vin: string, cfg: VinpinConfig): Promise<VinpinDecodeResult | null> {
for (let attempt = 1; attempt <= cfg.maxAttempts; attempt++) {
try {
await this.ensureReady(cfg);
const page = this.page;
if (!page) throw new Error("Vinpin page not initialized");
const text = await this.runVinFlow(page, vin);
const parsed = parseVinpinModal(text);
if (isUsableParse(parsed)) {
this.logger.log(
`decoded ${vin} → model="${parsed.model}" sincom="${parsed.sincom}" (attempt ${attempt})`,
);
return this.toResult(parsed, text);
}
this.logger.warn(
`attempt ${attempt}/${cfg.maxAttempts}: no usable decode for ${vin} (text len ${text?.length ?? 0})`,
);
// Empty/garbled — re-establish the session on the next attempt.
await this.close();
} catch (err) {
const e = err as Error;
this.logger.warn(`attempt ${attempt}/${cfg.maxAttempts} failed for ${vin}: ${e.message}`);
// Hard failure → tear the browser down so the next attempt re-establishes.
await this.close();
}
}
this.logger.warn(`giving up on ${vin} after ${cfg.maxAttempts} attempts → not found`);
return null;
}
private toResult(parsed: VinpinParsed, rawText: string | null): VinpinDecodeResult {
return {
model: parsed.model,
sincom: parsed.sincom,
trim: parsed.trim,
modelYear: parsed.modelYear,
engine: parsed.engine,
raw: { source: "vinpin-eper", text: rawText },
};
}
/**
* Ensure the browser+context+page are alive AND we're logged in and sitting on
* the Fiat ePER search page. (Re)establishes whatever is missing.
*/
private async ensureReady(cfg: VinpinConfig): Promise<void> {
if (!this.browser?.isConnected() || !this.context || !this.page || this.page.isClosed()) {
await this.launch(cfg);
}
if (!this.loggedIn) {
await this.establishSession(cfg);
}
}
private async launch(cfg: VinpinConfig): Promise<void> {
await this.close();
const { chromium } = await import("playwright");
const launchOptions: Record<string, unknown> = {
headless: !cfg.headful,
args: [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
"--ignore-certificate-errors",
// Software GL so the Blast/Horizon canvas renders without a real GPU
// (verified working under xvfb). Without these the catalog canvas stays
// blank and OCR reads nothing.
"--use-gl=angle",
"--use-angle=swiftshader",
"--enable-unsafe-swiftshader",
`--window-size=${VINPIN_VIEWPORT.width},${VINPIN_VIEWPORT.height}`,
],
};
const executablePath = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH;
if (executablePath) launchOptions.executablePath = executablePath;
this.browser = await chromium.launch(launchOptions);
this.context = await this.browser.newContext({
viewport: { width: VINPIN_VIEWPORT.width, height: VINPIN_VIEWPORT.height },
ignoreHTTPSErrors: true,
permissions: ["clipboard-read", "clipboard-write"],
userAgent:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
});
this.page = await this.context.newPage();
this.loggedIn = false;
this.browser.on("disconnected", () => {
this.logger.warn("Vinpin browser disconnected — will relaunch on next decode");
this.browser = null;
this.context = null;
this.page = null;
this.loggedIn = false;
});
this.logger.log(`Vinpin browser launched (headless=${!cfg.headful})`);
}
/**
* Log in and navigate to the Fiat ePER search page, VISION-VERIFYING each step.
* Coordinates/waits verified live @ 1600x900.
*/
private async establishSession(cfg: VinpinConfig): Promise<void> {
const page = this.page;
if (!page) throw new Error("Vinpin page not initialized");
// a. Login (reliable DOM path).
await page.goto(cfg.url, { waitUntil: "domcontentloaded", timeout: 60_000 });
await page.waitForTimeout(VINPIN_WAITS.afterGoto);
// First visible text input = username.
const userInput = page.locator('input[type="text"]:visible, input:not([type]):visible').first();
await userInput.fill(cfg.user ?? "");
await page
.locator('input[type="password"]')
.first()
.fill(cfg.pass ?? "");
const submit = page.locator('button:has-text("Oturum"), [type="submit"]').first();
await submit.click();
await page.waitForTimeout(VINPIN_WAITS.afterLogin);
// a2. Bring VinPower up. The PERMANENT (paid) seat does NOT auto-launch — it
// lands on the Horizon app launcher and needs the VINPIN app clicked. The
// TRIAL seat auto-launches straight to the brand grid. Detect & reconcile.
await this.ensureBrandGrid(page, cfg);
// b. Open Fiat ePER — canvas click is flaky, so retry + OCR-verify it opened.
let opened = false;
for (let i = 0; i < 4 && !opened; i++) {
// A deliberate mousedown/up (not a synthetic click) lands on the canvas tile.
await page.mouse.move(VINPIN_COORDS.fiatBrand.x, VINPIN_COORDS.fiatBrand.y);
await page.waitForTimeout(150);
await page.mouse.down();
await page.waitForTimeout(80);
await page.mouse.up();
await page.waitForTimeout(VINPIN_WAITS.afterFiatOpen);
const t = await ocrRegion(page);
opened = VINPIN_OCR.eperOpen.test(t);
if (!opened) this.logger.warn(`Fiat ePER open attempt ${i + 1}/4 — not yet (retrying)`);
}
if (!opened) throw new Error("Fiat ePER did not open after retries");
// c. Dismiss the spurious Russian-language dialog (click its OK button).
await page.mouse.click(VINPIN_COORDS.langDialogDismiss.x, VINPIN_COORDS.langDialogDismiss.y);
await page.waitForTimeout(VINPIN_WAITS.afterLangDismiss);
// d. Reach the Spare Parts CATALOGUE (carries the VIN identification panel).
// The seat opens either straight onto the catalogue or onto the portal
// home — detect which, and click the Spare Parts tile when on the portal.
let onCatalogue = false;
for (let i = 0; i < 4 && !onCatalogue; i++) {
const t = await ocrRegion(page);
onCatalogue = VINPIN_OCR.catalogueReady.test(t);
if (onCatalogue) break;
this.logger.warn(`not on Spare Parts catalogue (attempt ${i + 1}/4) — opening it`);
await page.mouse.click(VINPIN_COORDS.sparePartsTile.x, VINPIN_COORDS.sparePartsTile.y);
await page.waitForTimeout(VINPIN_WAITS.afterSearchOpen);
}
if (!onCatalogue) throw new Error("could not reach Spare Parts Catalogue (VIN panel)");
this.loggedIn = true;
this.logger.log("Vinpin session established (Fiat ePER Spare Parts catalogue)");
}
/**
* Bring the session to the VinPower brand grid (the state the Fiat-tile flow
* expects). Backward-compatible across both seats:
* - TRIAL seat auto-launches VinPower → the grid is already up → no-op.
* - PERMANENT seat lands on the Horizon HTML-Access app launcher → click the
* "VINPIN" app to launch VinPower, satisfy its secondary login (username
* pre-filled, password blank), and wait for the grid.
* OCR-gated with a small retry budget. Never throws — if the grid can't be
* confirmed, the caller's own ePER-open retry loop still runs.
*/
private async ensureBrandGrid(page: Page, cfg: VinpinConfig): Promise<void> {
for (let i = 0; i < 4; i++) {
const t = await ocrRegion(page);
if (VINPIN_OCR.brandGrid.test(t)) {
if (i > 0) this.logger.log("VinPower brand grid up");
return;
}
// If VinPower's own login dialog is up, satisfy it (never re-click the app
// tile mid-login — that would double-launch).
if (await this.submitVinPowerLogin(page, cfg)) {
await page.waitForTimeout(VINPIN_WAITS.afterVinPowerLogin);
continue;
}
// On the launcher → click the VINPIN app to launch VinPower.
if (VINPIN_OCR.launcher.test(t)) {
this.logger.log(`app-launcher detected — launching VINPIN app (try ${i + 1}/4)`);
await page.mouse.click(VINPIN_COORDS.vinpinApp.x, VINPIN_COORDS.vinpinApp.y);
await page.waitForTimeout(VINPIN_WAITS.afterVinpinLaunch);
continue;
}
// Neither grid, login, nor launcher — a "Disconnected" dialog or a blank
// connecting screen. Dismiss any dialog and wait for the next transition.
await this.dismissDisconnected(page);
await page.waitForTimeout(VINPIN_WAITS.afterVinpinLaunch);
}
this.logger.warn("VinPower brand grid not confirmed — letting the ePER-open loop try anyway");
}
/**
* Satisfy VinPower's own login dialog (permanent seat, shown after launching
* the VINPIN app: username pre-filled, password blank). Returns true when it
* found & submitted the dialog. DOM path (the dialog is real HTML in the
* webclient frame) with a coordinate fallback. No dialog → false (trial seat /
* already authenticated).
*/
private async submitVinPowerLogin(page: Page, cfg: VinpinConfig): Promise<boolean> {
for (const frame of page.frames()) {
try {
const pw = frame.locator('input[type="password"]:visible').first();
if ((await pw.count()) === 0 || !(await pw.isVisible().catch(() => false))) continue;
await pw.fill(cfg.pass ?? "");
const btn = frame.locator('button:has-text("Oturum"), [type="submit"]').first();
if ((await btn.count()) > 0) {
await btn.click().catch(() => {});
} else {
await page.mouse.click(VINPIN_COORDS.vinpowerSubmit.x, VINPIN_COORDS.vinpowerSubmit.y);
}
this.logger.log("submitted VinPower login dialog");
return true;
} catch {
// ignore — try the next frame
}
}
return false;
}
/** Dismiss the Horizon "Disconnected" dialog if present (best-effort). */
private async dismissDisconnected(page: Page): Promise<void> {
try {
const btn = page.locator('button:has-text("Close"), button:has-text("Kapat")').first();
if ((await btn.count()) > 0 && (await btn.isVisible().catch(() => false))) {
await btn.click().catch(() => {});
}
} catch {
// ignore
}
}
/**
* Run one VIN decode on an already-established warm session. Enters the VIN in
* the right-panel field, submits, and OCRs the decode-modal region. Returns the
* modal text, or null on a genuine "vehicle not found" (which it also clears so
* the next decode in the warm session is unaffected).
*/
private async runVinFlow(page: Page, vin: string): Promise<string | null> {
// Clear any leftover alert/modal from a previous decode in this warm session.
await page.keyboard.press("Escape").catch(() => {});
await page.waitForTimeout(VINPIN_WAITS.afterAlertDismiss);
// Focus + clear the VIN field, then type the VIN.
await page.mouse.dblclick(VINPIN_COORDS.vinField.x, VINPIN_COORDS.vinField.y);
await page.waitForTimeout(350);
await page.keyboard.press("Control+A").catch(() => {});
await page.keyboard.press("Delete").catch(() => {});
await page.waitForTimeout(200);
await page.keyboard.type(vin, { delay: 50 });
await page.waitForTimeout(400);
// Submit (arrow) and wait for the decode modal.
await page.mouse.click(VINPIN_COORDS.vinSubmitArrow.x, VINPIN_COORDS.vinSubmitArrow.y);
await page.waitForTimeout(VINPIN_WAITS.afterVinSubmit);
const text = await this.extractModalText(page);
// Genuine not-found: ePER shows a "vehicles not found" alert over the bare
// catalogue (no model/prod-date). Dismiss it and report null cleanly.
const hasResult = Boolean(text) && VINPIN_OCR.modalHit.test(text ?? "");
if (!hasResult) {
await page.mouse
.click(VINPIN_COORDS.notFoundOk.x, VINPIN_COORDS.notFoundOk.y)
.catch(() => {});
await page.keyboard.press("Escape").catch(() => {});
await page.keyboard.press("Enter").catch(() => {});
await page.waitForTimeout(VINPIN_WAITS.afterAlertDismiss);
return null;
}
return text;
}
/**
* Read the decode-modal text by OCR'ing a screenshot of the modal region. The
* Blast canvas exposes no DOM text and the clipboard is unreliable, so the
* screenshot is the only dependable read channel (see vinpin.ocr).
*/
private async extractModalText(page: Page): Promise<string | null> {
const text = await ocrRegion(page, VINPIN_MODAL_REGION);
return text.length > 0 ? text : null;
}
private async close(): Promise<void> {
this.loggedIn = false;
try {
if (this.page && !this.page.isClosed()) await this.page.close();
} catch {
// ignore
}
try {
if (this.context) await this.context.close();
} catch {
// ignore
}
try {
if (this.browser) await this.browser.close();
} catch {
// ignore
}
this.page = null;
this.context = null;
this.browser = null;
}
}
// ─── Worker singleton ────────────────────────────────────────
// The vinpin-decode BullMQ processor runs in the standalone worker (no Nest DI),
// so it grabs a process-wide singleton instead of a provider. One instance =
// one warm browser = the single Vinpin seat.
let workerSingleton: VinpinDriverService | null = null;
export function getVinpinDriver(): VinpinDriverService {
if (!workerSingleton) workerSingleton = new VinpinDriverService();
return workerSingleton;
}

View File

@@ -0,0 +1,137 @@
/**
* Vinpin ePER driver constants.
*
* The screen coordinates below are calibrated for a 1600x900 viewport against
* the Vinpin ePER (Fiat) web catalog rendered inside its Blast/Horizon canvas.
* The UI is a pixel canvas with no stable DOM selectors, so navigation is
* coordinate-driven.
*
* These were VERIFIED LIVE against a real Vinpin trial seat (2026-06-26) with a
* vision loop (screenshot + OCR), not guessed — every transition is OCR-checked
* at runtime and retried, so minor drift self-heals. Re-verify the Fiat-tile
* coordinate if the brand-grid layout ever changes.
*/
/** Viewport the coordinates are calibrated for. */
export const VINPIN_VIEWPORT = { width: 1600, height: 900 } as const;
/** Coordinate map (x, y) for the ePER canvas. Verified live @ 1600x900. */
export const VINPIN_COORDS = {
/** "VINPIN" app tile on the Horizon HTML-Access launcher ("Available" list).
* The PERMANENT (paid) seat lands on this launcher after login instead of
* auto-launching VinPower — clicking this tile launches VinPower.
* Tuned for the permanent seat @ 1600x900 (2026-07-01). */
vinpinApp: { x: 40, y: 256 },
/** VinPower's own login dialog (shown after launching the app from the
* launcher): password field + "Oturum aç". DOM-filled when reachable; these
* are the coordinate fallback. Tuned for the permanent seat @ 1600x900. */
vinpowerPassField: { x: 800, y: 465 },
vinpowerSubmit: { x: 800, y: 629 },
/** Fiat brand tile on the post-login brand grid (row 2, col 2). */
fiatBrand: { x: 230, y: 104 },
/** "OK" button of the spurious Russian-language dialog shown on ePER open. */
langDialogDismiss: { x: 746, y: 454 },
/** "Spare Parts" catalog tile on the ePER portal home ("Доступные каталоги").
* Opens the Spare Parts Catalogue that carries the VIN identification panel.
* (The seat may open straight onto the catalogue or onto this portal — the
* driver detects which and clicks here only when stuck on the portal.) */
sparePartsTile: { x: 214, y: 330 },
/** Right-panel "Идентификация автомобиля → VIN" input field. */
vinField: { x: 1200, y: 189 },
/** Decode arrow (→) next to the VIN field. */
vinSubmitArrow: { x: 1283, y: 189 },
/** "OK" button of the "vehicles not found" alert (genuine not-found result). */
notFoundOk: { x: 816, y: 448 },
} as const;
/** Screenshot clip (px) of the decode-result modal body, fed to OCR. Captures
* the model line ("6J - TIPO - EGEA (2015-2021)"), SINCOM, trim, prod-date and
* the echoed VIN. Verified live. */
export const VINPIN_MODAL_REGION = { x: 250, y: 120, width: 820, height: 480 } as const;
/** OCR keyword sets for state detection (case-insensitive). */
export const VINPIN_OCR = {
/** Horizon HTML-Access app launcher (permanent seat, no auto-launch). Its
* "Available / Running / No Running Items" chrome is absent from every
* VinPower screen, so these tokens uniquely identify the launcher. */
launcher: /No Running Items|Available|Running/i,
/** VinPower brand grid (many brand tiles). These multi-brand tokens appear
* ONLY on the grid — not on the launcher, the ePER catalogue, or the
* connecting/blank screens — so they confirm VinPower actually launched. */
brandGrid: /SsangYong|General Motors|Volkswagen|TecDoc|Mitsubishi|Dongfeng/i,
/** ePER (Fiat Dealer) window is open. */
eperOpen: /Dealer|Pycc|Русск|Spare|Поиск|Запчаст|EPER/i,
/** We're on the Spare Parts CATALOGUE (left model tree visible) — the page
* that carries the right-panel VIN identification field. Discriminated from
* the portal home by the model-tree tokens (which OCR cleanly in Latin),
* since the portal shows only "Accessories / Service Packages / REMAN". */
catalogueReady:
/Spare Parts Catalogue|GRANDE PANDA|124 SPIDER|TOPOLINO|TIPO|EGEA|DOBLO|ULYSSE|SEDICI/i,
/** The decode modal actually carries a result (a model / prod-date / MVS). */
modalHit:
/MVS|найден|Prod\.?\s*date|TIPO|EGEA|DOBLO|PALIO|PANDA|PUNTO|LINEA|DUCATO|QUBO|FIORINO|ULYSSE/i,
/** Genuine "no vehicle found" outcome (the catalog has no record). */
notFound: /не\s*найден|not\s*found|Catalogue/i,
} as const;
/** Wait budgets (ms) for each step. Verified live @ 1600x900. */
export const VINPIN_WAITS = {
afterGoto: 7_000,
afterLogin: 20_000,
/** After clicking the launcher's VINPIN app: wait for VinPower to connect and
* raise its login dialog (permanent seat only). */
afterVinpinLaunch: 14_000,
/** After submitting VinPower's login: wait for the brand grid to render. */
afterVinPowerLogin: 18_000,
afterFiatOpen: 12_000,
afterLangDismiss: 2_500,
afterSearchOpen: 5_000,
afterVinSubmit: 7_000,
afterAlertDismiss: 700,
} as const;
/**
* Known Fiat model tokens used to extract the model name from the decode modal
* text. The modal interleaves Cyrillic boilerplate, a platform code, the model
* name, a SINCOM code and a trim string; matching against a known-token list is
* far more robust than positional parsing. Extend as new models appear.
*/
export const FIAT_MODEL_TOKENS = [
"TIPO",
"EGEA",
"LINEA",
"DOBLO",
"FIORINO",
"PUNTO",
"PANDA",
"BRAVO",
"STILO",
"ALBEA",
"PALIO",
"SIENA",
"MAREA",
"500",
"500L",
"500X",
"QUBO",
"DUCATO",
"SCUDO",
"TALENTO",
"FREEMONT",
"CROMA",
"IDEA",
"SEDICI",
"MULTIPLA",
] as const;
/**
* Brands for which the Vinpin fallback is enabled. Fiat-only for now (the
* Egea/NM4356 no-catalog cluster); a Set so it's trivially extensible later.
* Compared case-insensitively against the canonical browse brand.
*/
export const VINPIN_BRAND_ALLOWLIST = new Set<string>(["fiat"]);
export function isVinpinBrandAllowed(brand: string | null | undefined): boolean {
if (!brand) return false;
return VINPIN_BRAND_ALLOWLIST.has(brand.trim().toLowerCase());
}

View File

@@ -0,0 +1,88 @@
import { describe, expect, it } from "vitest";
import {
type CatalogCandidate,
modelTokens,
parseYearRange,
pickBestCatalogMatch,
} from "./vinpin.matcher";
describe("modelTokens", () => {
it("drops parenthetical year ranges and bare years, keeps model tokens", () => {
expect(modelTokens("TIPO - EGEA (2015-2021)")).toEqual(["TIPO", "EGEA"]);
expect(modelTokens("Tipo-Egea MCA (2020-....)")).toEqual(["TIPO", "EGEA", "MCA"]);
expect(modelTokens("DOBLO REST. 2005 (2005-2016)")).toEqual(["DOBLO", "REST"]);
});
it("keeps engine displacements as tokens (only 1950-2039 count as years)", () => {
// The 4-digit engine sizes must survive — dropping them made the ancient
// "TIPO 1100-1370-1580" collapse to just ["TIPO"] and win the tiebreak.
expect(modelTokens("TIPO 1100-1370-1580 (1987-1993)")).toEqual([
"TIPO",
"1100",
"1370",
"1580",
]);
expect(modelTokens("TIPO 1750-2000 (1990-1993)")).toEqual(["TIPO", "1750"]); // 2000 = year-range
});
});
describe("parseYearRange", () => {
it("parses closed and open-ended ranges", () => {
expect(parseYearRange("(2015-2021)")).toEqual({ start: 2015, end: 2021 });
expect(parseYearRange("Tipo-Egea MCA (2020-....)")).toEqual({ start: 2020, end: null });
});
});
describe("pickBestCatalogMatch", () => {
const candidates: CatalogCandidate[] = [
{ id: "egea-mca", model: "Tipo-Egea MCA (2020-....)", year: "2020-" },
{ id: "egea", model: "TIPO - EGEA (2015-2021)", year: "2015-2021" },
{ id: "linea", model: "LINEA (2007-2017)", year: "2007-2017" },
{ id: "doblo", model: "DOBLO REST. 2005 (2005-2016)", year: "2005-2016" },
];
it("matches TIPO-EGEA 2017 to the year-overlapping Egea catalog (not the 2020 MCA)", () => {
const id = pickBestCatalogMatch({ model: "TIPO-EGEA", modelYear: "2017" }, candidates);
expect(id).toBe("egea");
});
it("prefers the MCA Egea for a 2021+ car", () => {
const id = pickBestCatalogMatch({ model: "TIPO-EGEA", modelYear: "2022" }, candidates);
expect(id).toBe("egea-mca");
});
it("matches a single-token model (LINEA)", () => {
const id = pickBestCatalogMatch({ model: "LINEA", modelYear: "2010" }, candidates);
expect(id).toBe("linea");
});
it("returns null when no candidate contains all decoded model tokens", () => {
const id = pickBestCatalogMatch({ model: "PANDA", modelYear: "2018" }, candidates);
expect(id).toBeNull();
});
it("returns null for an empty model", () => {
expect(pickBestCatalogMatch({ model: null, modelYear: "2018" }, candidates)).toBeNull();
});
it("never falls back onto an ancient catalog for a bare, year-less model", () => {
const withAncient: CatalogCandidate[] = [
...candidates,
{ id: "tipo-1987", model: "TIPO 1100-1370-1580 (1987-1993)", year: "1987-1993" },
];
// Bare "TIPO", no year (the exact shape of the NM4131 false positive that
// used to land on the 1987 Tipo). Must resolve to a modern Egea instead.
const id = pickBestCatalogMatch({ model: "TIPO", modelYear: null }, withAncient);
expect(id).not.toBe("tipo-1987");
expect(["egea", "egea-mca"]).toContain(id);
});
it("breaks a score tie toward the newer catalog", () => {
const puntos: CatalogCandidate[] = [
{ id: "punto-old", model: "PUNTO (1993-1999)", year: "1993-1999" },
{ id: "punto-new", model: "PUNTO (2012-2018)", year: "2012-2018" },
];
// Identical tokens + no year → equal score → recency decides.
expect(pickBestCatalogMatch({ model: "PUNTO", modelYear: null }, puntos)).toBe("punto-new");
});
});

View File

@@ -0,0 +1,148 @@
/**
* Vinpin catalog matcher — maps a Vinpin-decoded Fiat model (e.g. "TIPO-EGEA")
* + model year onto an EXISTING PL24 catalog_vehicles row, so the parts can be
* served from PL24's catalog. Vinpin only identifies the car; PL24 already has
* the Egea/Linea/Doblo/etc. catalogs.
*
* The pure picker (`pickBestCatalogMatch`) is unit-tested directly; the
* @Injectable wrapper runs the DB query (brand='Fiat', source='pl24') and hands
* the rows to the picker.
*/
import { Inject, Injectable, Logger } from "@nestjs/common";
import { and, eq, sql } from "drizzle-orm";
import { DATABASE, type Database } from "../../database/database.provider";
import { catalogVehicles } from "../../database/schema/core";
export interface CatalogCandidate {
id: string;
model: string | null;
year: string | null;
}
export interface DecodedForMatch {
model: string | null;
modelYear: string | null;
}
/** Tokenize a model string into upper-case model tokens, dropping parenthetical
* groups (year ranges) and bare 4-digit *years*. "TIPO - EGEA (2015-2021)" →
* ["TIPO","EGEA"]. Keeps "500"/"500X" AND engine displacements like 1100 / 1370
* / 1580 / 2400 — only 1950-2039 (plausible car years) are treated as years.
* Dropping displacements made ancient catalogs ("TIPO 1100-1370-1580") collapse
* to just ["TIPO"], looking falsely specific and winning the fewest-extra-tokens
* tiebreak over the modern Egea. */
export function modelTokens(s: string | null | undefined): string[] {
if (!s) return [];
return s
.toUpperCase()
.replace(/\([^)]*\)/g, " ") // drop parenthetical year ranges
.replace(/\b(19[5-9]\d|20[0-3]\d)\b/g, " ") // drop bare YEARS 1950-2039; keep displacements
.replace(/[^A-Z0-9]+/g, " ")
.split(" ")
.map((t) => t.trim())
.filter((t) => t.length >= 2);
}
/** Parse a year range from free text. Returns {start, end} where end===null
* means open-ended ("(2020-....)"). */
export function parseYearRange(
s: string | null | undefined,
): { start: number; end: number | null } | null {
if (!s) return null;
const m = s.match(/(\d{4})\s*[-–—]\s*(\d{4}|\.+|present|now)?/i);
if (!m) {
const single = s.match(/\b(\d{4})\b/);
if (single) {
const y = Number(single[1]);
return { start: y, end: y };
}
return null;
}
const start = Number(m[1]);
const endRaw = m[2];
const end = endRaw && /^\d{4}$/.test(endRaw) ? Number(endRaw) : null;
return { start, end };
}
function yearInRange(year: number, range: { start: number; end: number | null }): boolean {
if (year < range.start) return false;
if (range.end !== null && year > range.end) return false;
return true;
}
/**
* Pick the best catalog_vehicles row for a decoded model + year. A candidate
* qualifies only when EVERY decoded model token appears in the candidate's
* model tokens (subset match). Among qualifiers, prefer year-range overlap,
* then the most specific (fewest extra tokens). Returns the id or null.
*/
export function pickBestCatalogMatch(
decoded: DecodedForMatch,
candidates: CatalogCandidate[],
): string | null {
const decTokens = modelTokens(decoded.model);
if (decTokens.length === 0) return null;
const decYear = decoded.modelYear ? Number.parseInt(decoded.modelYear, 10) : null;
let best: { id: string; score: number; startYear: number } | null = null;
for (const cand of candidates) {
const candText = `${cand.model ?? ""} ${cand.year ?? ""}`;
const candTokens = modelTokens(candText);
const candSet = new Set(candTokens);
// Subset requirement — all decoded model tokens must be present.
if (!decTokens.every((t) => candSet.has(t))) continue;
let score = 1000; // base for any qualifying candidate
const range = parseYearRange(candText);
if (decYear && Number.isFinite(decYear) && range) {
score += yearInRange(decYear, range) ? 500 : -200;
}
// Tiebreak: fewer extra tokens = more specific match.
const extra = candTokens.filter((t) => !decTokens.includes(t)).length;
score -= extra;
// Final tiebreak on equal score: prefer the newer catalog. Without a year
// signal an ambiguous model (e.g. bare "TIPO") must never fall back onto an
// ancient generation — modern is the overwhelmingly likelier intent.
const startYear = range?.start ?? 0;
if (!best || score > best.score || (score === best.score && startYear > best.startYear)) {
best = { id: cand.id, score, startYear };
}
}
return best?.id ?? null;
}
@Injectable()
export class VinpinCatalogMatcher {
private readonly logger = new Logger(VinpinCatalogMatcher.name);
constructor(@Inject(DATABASE) private readonly db: Database) {}
/**
* Find the best PL24 Fiat catalog_vehicle for a decoded model + year.
* Returns the catalog_vehicle id, or null when nothing matches.
*/
async match(decoded: DecodedForMatch): Promise<string | null> {
if (!decoded.model) return null;
const rows = await this.db
.select({
id: catalogVehicles.id,
model: catalogVehicles.model,
year: catalogVehicles.year,
})
.from(catalogVehicles)
.where(
and(sql`lower(${catalogVehicles.brandName}) = 'fiat'`, eq(catalogVehicles.source, "pl24")),
);
const id = pickBestCatalogMatch(decoded, rows as CatalogCandidate[]);
this.logger.log(
`match model="${decoded.model}" year=${decoded.modelYear ?? "?"}${id ?? "null"} (over ${rows.length} Fiat catalog vehicles)`,
);
return id;
}
}

View File

@@ -0,0 +1,16 @@
import { Module } from "@nestjs/common";
import { VinpinDriverService } from "./vinpin-driver.service";
import { VinpinCatalogMatcher } from "./vinpin.matcher";
/**
* Vinpin ePER decode-oracle module. Feature-flagged (VINPIN_ENABLED) — every
* component is a strict no-op when the flag is off. The actual browser driving
* happens in the standalone worker (vinpin-decode processor) via the module-level
* singleton; this NestModule wires the services into the API graph for DI
* completeness and future direct use (e.g. a health endpoint or admin trigger).
*/
@Module({
providers: [VinpinDriverService, VinpinCatalogMatcher],
exports: [VinpinDriverService, VinpinCatalogMatcher],
})
export class VinpinModule {}

View File

@@ -0,0 +1,146 @@
/**
* Vinpin OCR — the reliable read channel for the Blast/Horizon pixel canvas.
*
* The ePER catalog renders into a canvas (no DOM text, clipboard is intermittent),
* so the ONLY dependable way to read the decode modal is a screenshot + OCR.
*
* Pipeline: `page.screenshot({ clip })` (native crop) → optional ffmpeg 3× upscale
* (greatly improves small-text accuracy; degrades gracefully to raw pixels when
* ffmpeg is absent) → tesseract.js (pure-JS, no system binary). The tesseract
* worker is a lazily-created process-wide singleton (eng traineddata cached on
* first use), reused across every decode in the warm session.
*
* Everything here is failure-tolerant: any error → "" so the driver treats it as
* "no decode" and never throws to its caller.
*
* ⚠️ RUNTIME NOTE: `ffmpeg` is a SOFT dependency (upscale only). tesseract.js
* downloads `eng.traineddata.gz` once (network) and caches it under
* VINPIN_OCR_CACHE (default os.tmpdir()/vinpin-tess). Bundle the traineddata in
* the image if the worker has no egress.
*/
import { spawn } from "node:child_process";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Logger } from "@nestjs/common";
import type { Page } from "playwright";
const logger = new Logger("VinpinOcr");
export interface OcrClip {
x: number;
y: number;
width: number;
height: number;
}
// Minimal structural type for the tesseract.js worker we use (avoids a hard type
// dep / `any` while keeping it decoupled from the lib's exported types).
interface TessWorker {
recognize(image: Buffer): Promise<{ data: { text: string } }>;
terminate(): Promise<unknown>;
}
let workerPromise: Promise<TessWorker> | null = null;
async function getWorker(): Promise<TessWorker> {
if (!workerPromise) {
workerPromise = (async () => {
// Dynamic import so a missing/broken OCR dep can never crash module load.
const tesseract = (await import("tesseract.js")) as unknown as {
createWorker: (
lang: string,
oem: number,
opts: { cachePath: string; gzip: boolean },
) => Promise<TessWorker>;
};
const cachePath = process.env.VINPIN_OCR_CACHE || join(tmpdir(), "vinpin-tess");
return tesseract.createWorker("eng", 1, { cachePath, gzip: true });
})().catch((err) => {
workerPromise = null; // allow a later retry
throw err;
});
}
return workerPromise;
}
/**
* Crop a `region` out of a full-page PNG and upscale `scale`× via ffmpeg.
*
* IMPORTANT: we crop from a FULL screenshot with ffmpeg rather than using
* Playwright's `screenshot({ clip })`. Clip-capture of the Blast compositor
* canvas intermittently returns a BLANK image under headful/xvfb even when the
* full screenshot is fine (verified failure mode) — full-frame capture is
* reliable. Falls back to the untouched PNG when ffmpeg is missing/errors.
*/
function cropScale(png: Buffer, region: OcrClip | undefined, scale: number): Promise<Buffer> {
if (!region && scale <= 1) return Promise.resolve(png);
const filters: string[] = [];
if (region) filters.push(`crop=${region.width}:${region.height}:${region.x}:${region.y}`);
if (scale > 1) filters.push(`scale=iw*${scale}:ih*${scale}:flags=lanczos`);
filters.push("format=gray");
return new Promise((resolve) => {
try {
const ff = spawn("ffmpeg", [
"-loglevel",
"error",
"-i",
"pipe:0",
"-vf",
filters.join(","),
"-f",
"image2pipe",
"-vcodec",
"png",
"pipe:1",
]);
const chunks: Buffer[] = [];
ff.stdout.on("data", (c: Buffer) => chunks.push(c));
ff.on("error", () => resolve(png)); // ffmpeg not installed
ff.on("close", (code) => resolve(code === 0 && chunks.length ? Buffer.concat(chunks) : png));
ff.stdin.on("error", () => {});
ff.stdin.end(png);
} catch {
resolve(png);
}
});
}
/** Full-page screenshot → (optional ffmpeg crop) + upscale → OCR. Returns
* whitespace-collapsed text, or "" on any failure. `clip` undefined = OCR the
* whole frame at 1× (state detection). */
export async function ocrRegion(page: Page, clip?: OcrClip, scale = 3): Promise<string> {
try {
const full = await page.screenshot({ fullPage: false });
const img = await cropScale(full, clip, clip ? scale : 1);
// Opt-in diagnostics: dump the OCR'd image (great for tuning coords in prod).
const dbg = process.env.VINPIN_DEBUG_DIR;
if (dbg) {
try {
const { writeFileSync } = await import("node:fs");
const tag = clip ? `clip-${clip.x}x${clip.y}` : "full";
writeFileSync(join(dbg, `vinpin-ocr-${tag}-${Date.now()}.png`), img);
} catch {
// ignore
}
}
const worker = await getWorker();
const { data } = await worker.recognize(img);
return (data.text || "").replace(/\s+/g, " ").trim();
} catch (err) {
logger.warn(`ocrRegion failed: ${(err as Error).message}`);
return "";
}
}
/** Tear down the shared tesseract worker (best-effort). */
export async function terminateOcr(): Promise<void> {
if (!workerPromise) return;
try {
const w = await workerPromise;
await w.terminate();
} catch {
// ignore
}
workerPromise = null;
}

View File

@@ -0,0 +1,52 @@
import { describe, expect, it } from "vitest";
import { isUsableParse, parseVinpinModal } from "./vinpin.parser";
const SAMPLE =
"MVS, найденные по vin NM435600006H43436 6J - TIPO - EGEA (2015-2021) 356G37001162 - 3V HIGH PLUS 1.6 120CV D5 CM E6 GS TR Двигатель: 8165300 Автомобиль: 279476 Prod. date: 2/8/2017 Vin: NM435600006H43436";
describe("parseVinpinModal", () => {
it("parses the real ePER modal sample (TIPO-EGEA / 356G37001162 / 2017)", () => {
const p = parseVinpinModal(SAMPLE);
expect(p.model).toBe("TIPO-EGEA");
expect(p.sincom).toBe("356G37001162");
expect(p.modelYear).toBe("2017"); // from Prod. date, not the (2015-2021) range
expect(p.engine).toBe("8165300");
expect(p.trim).toContain("HIGH PLUS");
expect(isUsableParse(p)).toBe(true);
});
it("does not mistake the VIN or short engine number for a SINCOM code", () => {
const p = parseVinpinModal(SAMPLE);
expect(p.sincom).not.toContain("NM4356");
expect(p.sincom).not.toBe("8165300");
});
it("falls back to the year-range start when no production date is present", () => {
const p = parseVinpinModal("9F - LINEA (2007-2017) 323A11005000 - 1.3 MJET");
expect(p.model).toBe("LINEA");
expect(p.sincom).toBe("323A11005000");
expect(p.modelYear).toBe("2007");
});
it("returns all-null for empty/garbage input and reports unusable", () => {
const p = parseVinpinModal("");
expect(p.model).toBeNull();
expect(p.sincom).toBeNull();
expect(isUsableParse(p)).toBe(false);
expect(isUsableParse(parseVinpinModal(null))).toBe(false);
});
it("rejects a stale-breadcrumb model with no SINCOM (vehicle-not-found false positive)", () => {
// Real garbled OCR of a failed NM4131 decode: the "не найден" alert is
// mangled ("He HaaeHs") so it slips the not-found check, and the screen
// still shows the operator's last catalog ("FIAT » TIPO - EGEA"). A model
// token is present but there is NO SINCOM → must be treated as unusable so
// it is not false-mapped onto the ancient "TIPO 1987-1993" catalog.
const p = parseVinpinModal(
"Fiat Dealer Spare Parts FIAT TIPO - EGBA 500 HYBRID TIPO-EGEA MCA (2020) DOBLO FREEMONT LINEA IDEA To yKasaHHeIM N3paMETpaM 3BTOMOBHMN He HaaeHs",
);
expect(p.model).not.toBeNull(); // breadcrumb model still extracted…
expect(p.sincom).toBeNull(); // …but no SINCOM
expect(isUsableParse(p)).toBe(false); // → not a real decode
});
});

View File

@@ -0,0 +1,108 @@
/**
* Pure parser for the Vinpin ePER decode-result modal text.
*
* Sample modal text (clipboard dump of the result panel):
* "MVS, найденные по vin NM435600006H43436 6J - TIPO - EGEA (2015-2021)
* 356G37001162 - 3V HIGH PLUS 1.6 120CV D5 CM E6 GS TR
* Двигатель: 8165300 Автомобиль: 279476 Prod. date: 2/8/2017
* Vin: NM435600006H43436"
*
* → { model: "TIPO-EGEA", sincom: "356G37001162", trim: "3V HIGH PLUS 1.6 …",
* modelYear: "2017", engine: "8165300" }
*
* Kept dependency-free and side-effect-free so it can be unit-tested directly.
*/
import { FIAT_MODEL_TOKENS } from "./vinpin.constants";
export interface VinpinParsed {
model: string | null;
sincom: string | null;
trim: string | null;
modelYear: string | null;
engine: string | null;
}
// SINCOM: 3 digits then 8-10 alphanumerics (e.g. 356G37001162). The leading
// \b avoids matching inside the VIN (which begins with letters → no boundary
// before a digit run) and rejects the short engine/vehicle numbers.
const SINCOM_RE = /\b\d{3}[A-Z0-9]{8,10}\b/;
// Engine number after the Cyrillic "Двигатель:" (or an "Engine:" fallback).
const ENGINE_RE = /(?:Двигатель|Engine)\s*:\s*(\d+)/i;
// Production date → take the year. e.g. "Prod. date: 2/8/2017".
const PROD_DATE_YEAR_RE = /Prod\.?\s*date\s*:\s*\d{1,2}\/\d{1,2}\/(\d{4})/i;
// Model-year range fallback, e.g. "(2015-2021)".
const YEAR_RANGE_RE = /\((\d{4})\s*-\s*\d{4}\)/;
/**
* Extract the model name as the run of consecutive known Fiat model tokens
* (joined by "-"). "6J - TIPO - EGEA" → "TIPO-EGEA"; "LINEA" → "LINEA".
*/
function extractModel(text: string): string | null {
const upper = text.toUpperCase();
// Tokenize on whitespace and dashes; keep alphanumeric tokens.
const tokens = upper.split(/[^A-Z0-9]+/).filter(Boolean);
const known = new Set<string>(FIAT_MODEL_TOKENS as readonly string[]);
const collected: string[] = [];
for (const tok of tokens) {
if (known.has(tok)) {
collected.push(tok);
} else if (collected.length > 0) {
// Stop at the first non-model token AFTER we've started collecting, so we
// capture the contiguous model phrase and not a later stray token.
break;
}
}
if (collected.length === 0) return null;
// De-dup consecutive repeats while preserving order.
const deduped = collected.filter((t, i) => i === 0 || t !== collected[i - 1]);
return deduped.join("-");
}
/**
* Extract the trim/description: the text between the SINCOM code and the
* "Двигатель"/"Engine" marker, with a leading "- " stripped.
*/
function extractTrim(text: string, sincom: string | null): string | null {
if (!sincom) return null;
const sincomIdx = text.indexOf(sincom);
if (sincomIdx === -1) return null;
let tail = text.slice(sincomIdx + sincom.length);
const stopMatch = tail.match(/Двигатель|Engine|Автомобиль|Prod\.?\s*date|Vin\s*:/i);
if (stopMatch && stopMatch.index !== undefined) {
tail = tail.slice(0, stopMatch.index);
}
const trim = tail.replace(/^[\s\-–—]+/, "").trim();
return trim.length > 0 ? trim : null;
}
export function parseVinpinModal(rawText: string | null | undefined): VinpinParsed {
const text = (rawText ?? "").replace(/\s+/g, " ").trim();
if (!text) {
return { model: null, sincom: null, trim: null, modelYear: null, engine: null };
}
const sincom = text.match(SINCOM_RE)?.[0] ?? null;
const model = extractModel(text);
const engine = text.match(ENGINE_RE)?.[1] ?? null;
const modelYear = text.match(PROD_DATE_YEAR_RE)?.[1] ?? text.match(YEAR_RANGE_RE)?.[1] ?? null;
const trim = extractTrim(text, sincom);
return { model, sincom, trim, modelYear, engine };
}
/**
* A parse is usable only when we got a SINCOM code — the trim identifier that a
* genuine ePER decode always emits ("356G37001162"). A model *alone* is a stale
* breadcrumb: when the VIN search returns "vehicle not found" (a TR NM4 VIN ePER
* can't resolve), the screen still shows whatever catalog the operator was last
* browsing (e.g. "FIAT » TIPO - EGEA"), and the OCR of the Cyrillic "не найден"
* alert garbles enough to slip past the not-found check. Grabbing that
* breadcrumb model ("TIPO"/"PANDA") and matching it to a catalog is a false
* positive that lands users on the wrong (often ancient) catalog. Requiring the
* SINCOM cleanly separates the two: every genuine decode carries one; the
* breadcrumb false positives never do.
*/
export function isUsableParse(p: VinpinParsed): boolean {
return Boolean(p.sincom);
}

View File

@@ -0,0 +1,314 @@
import {
BadRequestException,
Body,
Controller,
Get,
HttpCode,
HttpStatus,
Inject,
NotFoundException,
Param,
Patch,
Post,
Query,
UseGuards,
} from "@nestjs/common";
import { desc, eq, inArray } from "drizzle-orm";
import { getApiKeyApi } from "../auth/auth";
import { Public } from "../common/decorators/public.decorator";
import { InternalTokenGuard } from "../common/guards/internal-token.guard";
import { DATABASE, type Database } from "../database/database.provider";
import { apikeys, users } from "../database/schema/core";
import { PublicApiQuotaService } from "../public-api/public-api-quota.service";
// better-auth createApiKey `expiresIn`'i SANİYE bekler (ms değil).
const DAY_SECONDS = 24 * 60 * 60;
/**
* Public API anahtarı yönetimi — kapalı beta döneminde anahtarlar self-service
* değil, buradan (Süper Panel / curl + x-internal-token) elle üretilir.
* Anahtarın düz metni YALNIZCA create cevabında döner; DB'de hash saklanır.
*/
@Controller("internal/admin/api-keys")
@Public()
@UseGuards(InternalTokenGuard)
export class ApiKeysAdminController {
constructor(
@Inject(DATABASE) private readonly db: Database,
private readonly quota: PublicApiQuotaService,
) {}
/**
* Anahtar üret. Kullanıcı `userId` veya `email` ile seçilir (anahtar o
* kullanıcının aboneliği/marka yetkisiyle çalışır). `dailyDecodeLimit`
* anahtar metadata'sına yazılır; verilmezse env/100 varsayılanı geçerli.
*
* `publishable: true` → widget anahtarı (sase_pk_): müşteri sayfasında
* görünür, /widget shell'i yalnız bunları kabul eder ve `domains`
* listesinden frame-ancestors CSP kilidi üretir. domains zorunludur
* (boş liste = hiçbir 3. parti sitede render olmaz, sadece test).
*/
@Post()
@HttpCode(HttpStatus.CREATED)
async create(
@Body()
body: {
userId?: string;
email?: string;
name?: string;
dailyDecodeLimit?: number;
expiresInDays?: number;
rateLimitMax?: number;
test?: boolean;
publishable?: boolean;
domains?: string[];
},
) {
const user = await this.resolveUser(body);
if (
body.dailyDecodeLimit !== undefined &&
(typeof body.dailyDecodeLimit !== "number" || body.dailyDecodeLimit <= 0)
) {
throw new BadRequestException("dailyDecodeLimit pozitif bir sayı olmalı");
}
if (body.publishable && body.domains !== undefined && !Array.isArray(body.domains)) {
throw new BadRequestException('domains bir dizi olmalı (ör. ["musteri.com"])');
}
if (body.domains && !body.publishable) {
throw new BadRequestException("domains yalnız publishable anahtarlarda kullanılır");
}
const metadata: Record<string, unknown> = {};
if (body.dailyDecodeLimit) metadata.dailyDecodeLimit = Math.floor(body.dailyDecodeLimit);
if (body.publishable) {
metadata.publishable = true;
metadata.domains = (body.domains ?? []).filter((d) => typeof d === "string" && d.trim());
}
const created = await getApiKeyApi().createApiKey({
body: {
userId: user.id,
name: body.name ?? `${user.email} public-api`,
prefix: body.publishable ? "sase_pk_" : body.test ? "sase_test_" : "sase_live_",
...(body.expiresInDays ? { expiresIn: body.expiresInDays * DAY_SECONDS } : {}),
...(body.rateLimitMax
? { rateLimitEnabled: true, rateLimitTimeWindow: 60_000, rateLimitMax: body.rateLimitMax }
: {}),
...(Object.keys(metadata).length ? { metadata } : {}),
},
});
return {
id: created.id,
// Düz metin anahtar — sadece bu cevapta görünür, bir daha üretilemez.
key: created.key,
start: created.start,
user: { id: user.id, email: user.email },
name: created.name,
expiresAt: created.expiresAt,
dailyDecodeLimit: body.dailyDecodeLimit ?? null,
...(body.publishable ? { publishable: true, domains: metadata.domains } : {}),
};
}
/**
* Anahtarları listele (hash asla dönmez). userId/email verilirse o
* kullanıcıya filtrelenir; verilmezse TÜM anahtarlar döner (Süper Panel
* genel görünümü). Her satırda sahibi kullanıcı da bulunur.
*/
@Get()
async list(@Query("userId") userId?: string, @Query("email") email?: string) {
const user = userId || email ? await this.resolveUser({ userId, email }) : null;
const rows = await this.db
.select({
id: apikeys.id,
name: apikeys.name,
start: apikeys.start,
prefix: apikeys.prefix,
enabled: apikeys.enabled,
rateLimitMax: apikeys.rateLimitMax,
requestCount: apikeys.requestCount,
lastRequest: apikeys.lastRequest,
expiresAt: apikeys.expiresAt,
createdAt: apikeys.createdAt,
metadata: apikeys.metadata,
userId: apikeys.userId,
userEmail: users.email,
})
.from(apikeys)
.innerJoin(users, eq(apikeys.userId, users.id))
.where(user ? eq(apikeys.userId, user.id) : undefined)
.orderBy(desc(apikeys.createdAt));
return {
...(user ? { user: { id: user.id, email: user.email } } : {}),
keys: rows,
};
}
/**
* Günlük decode-kotası anlık durumu (Redis sayaçları). `ids` virgüllü
* liste; verilmezse tüm anahtarlar. Kota günü TR gecesi döndüğü için
* `used` yalnız bugünü yansıtır — tarihçe tutulmaz.
*/
@Get("usage")
async usage(@Query("ids") ids?: string) {
const idList = (ids ?? "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
const rows = await this.db
.select({ id: apikeys.id, metadata: apikeys.metadata, enabled: apikeys.enabled })
.from(apikeys)
.where(idList.length ? inArray(apikeys.id, idList) : undefined);
const items = await Promise.all(
rows.map(async (row) => {
const limit = this.quota.resolveLimit(parseKeyMetadata(row.metadata));
const state = await this.quota.getState(row.id, limit);
return { id: row.id, enabled: row.enabled, ...state };
}),
);
return { items };
}
/**
* Anahtar ayarlarını güncelle. Alan verilmezse dokunulmaz; `null`
* geçilen alan temizlenir (dailyDecodeLimit → env varsayılanına döner,
* expiresInDays → süresiz, rateLimitMax → plugin varsayılanı 120/dk).
*/
@Patch(":id")
async update(
@Param("id") id: string,
@Body()
body: {
name?: string;
dailyDecodeLimit?: number | null;
domains?: string[];
rateLimitMax?: number | null;
expiresInDays?: number | null;
},
) {
const [row] = await this.db.select().from(apikeys).where(eq(apikeys.id, id)).limit(1);
if (!row) throw new NotFoundException("API anahtarı bulunamadı");
const metadata = parseKeyMetadata(row.metadata);
let metadataTouched = false;
const set: Partial<typeof apikeys.$inferInsert> = { updatedAt: new Date() };
if (body.name !== undefined) {
const name = String(body.name).trim();
if (!name || name.length > 32) {
throw new BadRequestException("name 1-32 karakter olmalı");
}
set.name = name;
}
if (body.dailyDecodeLimit !== undefined) {
if (body.dailyDecodeLimit === null) {
// JSON.stringify undefined alanı düşürür → env varsayılanına döner.
metadata.dailyDecodeLimit = undefined;
} else if (typeof body.dailyDecodeLimit !== "number" || body.dailyDecodeLimit <= 0) {
throw new BadRequestException("dailyDecodeLimit pozitif bir sayı veya null olmalı");
} else {
metadata.dailyDecodeLimit = Math.floor(body.dailyDecodeLimit);
}
metadataTouched = true;
}
if (body.domains !== undefined) {
if (!metadata.publishable) {
throw new BadRequestException("domains yalnız publishable anahtarlarda kullanılır");
}
if (!Array.isArray(body.domains)) {
throw new BadRequestException('domains bir dizi olmalı (ör. ["musteri.com"])');
}
metadata.domains = body.domains.filter((d) => typeof d === "string" && d.trim());
metadataTouched = true;
}
if (body.rateLimitMax !== undefined) {
if (body.rateLimitMax === null) {
set.rateLimitMax = null;
set.rateLimitTimeWindow = null;
} else if (typeof body.rateLimitMax !== "number" || body.rateLimitMax <= 0) {
throw new BadRequestException("rateLimitMax pozitif bir sayı veya null olmalı");
} else {
set.rateLimitEnabled = true;
set.rateLimitTimeWindow = 60_000;
set.rateLimitMax = Math.floor(body.rateLimitMax);
}
}
if (body.expiresInDays !== undefined) {
if (body.expiresInDays === null) {
set.expiresAt = null;
} else if (typeof body.expiresInDays !== "number" || body.expiresInDays <= 0) {
throw new BadRequestException("expiresInDays pozitif bir sayı veya null olmalı");
} else {
set.expiresAt = new Date(Date.now() + body.expiresInDays * DAY_SECONDS * 1000);
}
}
// better-auth şema transform'u tek JSON.stringify yazar — aynı format.
if (metadataTouched) set.metadata = JSON.stringify(metadata);
const [updated] = await this.db.update(apikeys).set(set).where(eq(apikeys.id, id)).returning({
id: apikeys.id,
name: apikeys.name,
enabled: apikeys.enabled,
rateLimitMax: apikeys.rateLimitMax,
expiresAt: apikeys.expiresAt,
metadata: apikeys.metadata,
});
return updated;
}
/** Anahtarı devre dışı bırak (geri açmak için enable). */
@Post(":id/disable")
@HttpCode(HttpStatus.OK)
async disable(@Param("id") id: string) {
return this.setEnabled(id, false);
}
@Post(":id/enable")
@HttpCode(HttpStatus.OK)
async enable(@Param("id") id: string) {
return this.setEnabled(id, true);
}
private async setEnabled(id: string, enabled: boolean) {
const [row] = await this.db
.update(apikeys)
.set({ enabled, updatedAt: new Date() })
.where(eq(apikeys.id, id))
.returning({ id: apikeys.id, enabled: apikeys.enabled });
if (!row) throw new NotFoundException("API anahtarı bulunamadı");
return row;
}
private async resolveUser(input: { userId?: string; email?: string }) {
if (!input.userId && !input.email) {
throw new BadRequestException("userId veya email gerekli");
}
const [user] = await this.db
.select({ id: users.id, email: users.email })
.from(users)
.where(input.userId ? eq(users.id, input.userId) : eq(users.email, input.email as string))
.limit(1);
if (!user) throw new NotFoundException("Kullanıcı bulunamadı");
return user;
}
}
/** apikeys.metadata text kolonu — better-auth tek JSON.stringify ile yazar. */
function parseKeyMetadata(raw: string | null): Record<string, unknown> {
if (!raw) return {};
try {
let value: unknown = JSON.parse(raw);
// Bazı better-auth sürümleri çift stringify yapabiliyor; toleranslı ol.
if (typeof value === "string") value = JSON.parse(value);
return value && typeof value === "object" ? (value as Record<string, unknown>) : {};
} catch {
return {};
}
}

View File

@@ -71,10 +71,7 @@ export class BillingController {
@Post(":id/cancel") @Post(":id/cancel")
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
async cancel( async cancel(@Param("id") id: string, @Body() body: { reason?: string; founderId?: string }) {
@Param("id") id: string,
@Body() body: { reason?: string; founderId?: string },
) {
this.requireFounder(body); this.requireFounder(body);
this.requireReason(body, "cancel"); this.requireReason(body, "cancel");
return this.billing.cancel({ return this.billing.cancel({
@@ -86,10 +83,7 @@ export class BillingController {
@Post(":id/resume") @Post(":id/resume")
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
async resume( async resume(@Param("id") id: string, @Body() body: { reason?: string; founderId?: string }) {
@Param("id") id: string,
@Body() body: { reason?: string; founderId?: string },
) {
this.requireFounder(body); this.requireFounder(body);
this.requireReason(body, "resume"); this.requireReason(body, "resume");
return this.billing.resume({ return this.billing.resume({

View File

@@ -31,8 +31,8 @@ export class BillingService {
reason: string; reason: string;
founderId: string; founderId: string;
}) { }) {
if (!Number.isFinite(input.days) || input.days <= 0 || input.days > 90) { if (!Number.isFinite(input.days) || input.days <= 0 || input.days > 3650) {
throw new BadRequestException("days must be 1..90"); throw new BadRequestException("days must be 1..3650");
} }
const [sub] = await this.db const [sub] = await this.db
.select() .select()
@@ -126,9 +126,7 @@ export class BillingService {
.limit(1); .limit(1);
if (!sub) throw new NotFoundException("Subscription bulunamadı"); if (!sub) throw new NotFoundException("Subscription bulunamadı");
if (sub.status !== "active" && sub.status !== "trial") { if (sub.status !== "active" && sub.status !== "trial") {
throw new ConflictException( throw new ConflictException(`'${sub.status}' subscription için plan değişimi yapılamaz`);
`'${sub.status}' subscription için plan değişimi yapılamaz`,
);
} }
if (sub.planId === input.newPlanId) { if (sub.planId === input.newPlanId) {
throw new ConflictException("Subscription zaten bu planda"); throw new ConflictException("Subscription zaten bu planda");
@@ -234,16 +232,10 @@ export class BillingService {
.limit(1); .limit(1);
if (!sub) throw new NotFoundException("Subscription bulunamadı"); if (!sub) throw new NotFoundException("Subscription bulunamadı");
if (sub.status !== "active" && sub.status !== "trial") { if (sub.status !== "active" && sub.status !== "trial") {
throw new ConflictException( throw new ConflictException(`'${sub.status}' subscription için brand atama yapılamaz`);
`'${sub.status}' subscription için brand atama yapılamaz`,
);
} }
const [plan] = await this.db const [plan] = await this.db.select().from(plans).where(eq(plans.id, sub.planId)).limit(1);
.select()
.from(plans)
.where(eq(plans.id, sub.planId))
.limit(1);
if (!plan) throw new NotFoundException("Plan bulunamadı"); if (!plan) throw new NotFoundException("Plan bulunamadı");
// brandCount 0 = "all brands" (Full plan): don't accept a custom list; // brandCount 0 = "all brands" (Full plan): don't accept a custom list;
@@ -282,9 +274,7 @@ export class BillingService {
.from(userBrands) .from(userBrands)
.where(eq(userBrands.subscriptionId, input.subscriptionId)); .where(eq(userBrands.subscriptionId, input.subscriptionId));
await this.db await this.db.delete(userBrands).where(eq(userBrands.subscriptionId, input.subscriptionId));
.delete(userBrands)
.where(eq(userBrands.subscriptionId, input.subscriptionId));
if (input.brandIds.length > 0) { if (input.brandIds.length > 0) {
await this.db.insert(userBrands).values( await this.db.insert(userBrands).values(

View File

@@ -1,3 +1,4 @@
import { createHmac, timingSafeEqual } from "node:crypto";
import { import {
Body, Body,
Controller, Controller,
@@ -15,7 +16,6 @@ import {
} from "@nestjs/common"; } from "@nestjs/common";
import { ConfigService } from "@nestjs/config"; import { ConfigService } from "@nestjs/config";
import type { Request, Response } from "express"; import type { Request, Response } from "express";
import { createHmac, timingSafeEqual } from "node:crypto";
import { Public } from "../common/decorators/public.decorator"; import { Public } from "../common/decorators/public.decorator";
import { InternalTokenGuard } from "../common/guards/internal-token.guard"; import { InternalTokenGuard } from "../common/guards/internal-token.guard";
import { ImpersonationService } from "./impersonation.service"; import { ImpersonationService } from "./impersonation.service";
@@ -66,9 +66,8 @@ export class ImpersonationController {
ttlMinutes: body.ttlMinutes ?? 15, ttlMinutes: body.ttlMinutes ?? 15,
reason, reason,
ipAddress: this.getIp(req), ipAddress: this.getIp(req),
userAgent: typeof req.headers["user-agent"] === "string" userAgent:
? req.headers["user-agent"] typeof req.headers["user-agent"] === "string" ? req.headers["user-agent"] : undefined,
: undefined,
}); });
const baseUrl = this.configService.get<string>("auth.url") ?? ""; const baseUrl = this.configService.get<string>("auth.url") ?? "";
@@ -149,8 +148,7 @@ export class ImpersonationController {
private consumeSecret(): string { private consumeSecret(): string {
const secret = const secret =
process.env.INTERNAL_IMPERSONATION_SECRET ?? process.env.INTERNAL_IMPERSONATION_SECRET ?? this.configService.get<string>("auth.secret");
this.configService.get<string>("auth.secret");
if (!secret) throw new Error("INTERNAL_IMPERSONATION_SECRET or auth.secret required"); if (!secret) throw new Error("INTERNAL_IMPERSONATION_SECRET or auth.secret required");
return secret; return secret;
} }

View File

@@ -32,10 +32,7 @@ export class ImpersonationService {
expiresAt: Date; expiresAt: Date;
sessionId: string; sessionId: string;
}> { }> {
const ttl = Math.min( const ttl = Math.min(Math.max(params.ttlMinutes || DEFAULT_TTL_MINUTES, 1), MAX_TTL_MINUTES);
Math.max(params.ttlMinutes || DEFAULT_TTL_MINUTES, 1),
MAX_TTL_MINUTES,
);
// Verify target exists (and isn't an admin — defense in depth) // Verify target exists (and isn't an admin — defense in depth)
const [target] = await this.db const [target] = await this.db
@@ -78,9 +75,7 @@ export class ImpersonationService {
const cookieValue = encodeURIComponent(signedValue); const cookieValue = encodeURIComponent(signedValue);
const isHttps = (this.configService.get<string>("auth.url") ?? "").startsWith("https://"); const isHttps = (this.configService.get<string>("auth.url") ?? "").startsWith("https://");
const cookieName = isHttps const cookieName = isHttps ? SESSION_COOKIE_NAME_SECURE : SESSION_COOKIE_NAME_INSECURE;
? SESSION_COOKIE_NAME_SECURE
: SESSION_COOKIE_NAME_INSECURE;
this.logger.log( this.logger.log(
`Impersonation session created: founder=${params.founderId} target=${params.targetUserId} ttl=${ttl}m reason="${params.reason.slice(0, 80)}" sessionId=${sessionId.slice(0, 8)}`, `Impersonation session created: founder=${params.founderId} target=${params.targetUserId} ttl=${ttl}m reason="${params.reason.slice(0, 80)}" sessionId=${sessionId.slice(0, 8)}`,

View File

@@ -1,6 +1,8 @@
import { Module } from "@nestjs/common"; import { Module } from "@nestjs/common";
import { StripeModule } from "../payments/stripe/stripe.module"; import { StripeModule } from "../payments/stripe/stripe.module";
import { PublicApiQuotaService } from "../public-api/public-api-quota.service";
import { SubscriptionsModule } from "../subscriptions/subscriptions.module"; import { SubscriptionsModule } from "../subscriptions/subscriptions.module";
import { ApiKeysAdminController } from "./api-keys.controller";
import { BillingController } from "./billing.controller"; import { BillingController } from "./billing.controller";
import { BillingService } from "./billing.service"; import { BillingService } from "./billing.service";
import { ImpersonationController } from "./impersonation.controller"; import { ImpersonationController } from "./impersonation.controller";
@@ -14,6 +16,7 @@ import { InternalVehiclesService } from "./vehicles.service";
@Module({ @Module({
imports: [SubscriptionsModule, StripeModule], imports: [SubscriptionsModule, StripeModule],
controllers: [ controllers: [
ApiKeysAdminController,
ImpersonationController, ImpersonationController,
LifecycleController, LifecycleController,
BillingController, BillingController,
@@ -25,6 +28,9 @@ import { InternalVehiclesService } from "./vehicles.service";
LifecycleService, LifecycleService,
BillingService, BillingService,
InternalVehiclesService, InternalVehiclesService,
// Redis kota sayaçlarını /internal/admin/api-keys/usage için okur
// (RedisModule @Global olduğundan bağımlılığı hazır).
PublicApiQuotaService,
], ],
}) })
export class InternalAdminModule {} export class InternalAdminModule {}

View File

@@ -0,0 +1,276 @@
import { describe, expect, it } from "vitest";
import { PRIORITY, classifyNode, mapToCanonical } from "../canonical-lexicon";
// Inputs are already NORMALIZE_SQL-folded (ASCII, lowercase, collapsed spaces).
describe("classifyNode", () => {
it("flags Opel model-code selector nodes as structural", () => {
expect(classifyNode("j82 ascona-c 1982 - 1988")).toBe("structural");
expect(classifyNode("c06 bls 2006 - 2009")).toBe("structural");
expect(classifyNode("b16 astra-k 2016 - 2021")).toBe("structural");
});
it("flags bare year-range nodes as structural", () => {
expect(classifyNode("astra-h 2004 - 2014")).toBe("structural");
});
it("flags MAN truck model descriptors as structural", () => {
expect(classifyNode("12.155 star")).toBe("structural");
expect(classifyNode("10/11.150 hocl/r")).toBe("structural");
expect(classifyNode("18t hocl-sd")).toBe("structural");
// a real category starting with digits+space (not a truck code) stays part
expect(classifyNode("4 silindirli motor")).toBe("part");
});
it("flags MAN bus/coachbuilder model names as structural", () => {
expect(classifyNode("lion's city 12m")).toBe("structural");
expect(classifyNode("nl 202 muenchen")).toBe("structural");
expect(classifyNode("ng 242/272")).toBe("structural");
expect(classifyNode("man 292/332/362 h")).toBe("structural");
expect(classifyNode("n 1216 hd cityliner")).toBe("structural");
expect(classifyNode("aufbau caetano")).toBe("structural");
// "LPG" bus models (NL 205 M11 LPG) had wrongly mapped to Yakıt — now caught
expect(classifyNode("nl 205 m11 lpg")).toBe("structural");
// guard: real electrical part still maps, not swallowed by "n ?\d" etc.
expect(mapToCanonical("elektr. sistemler, gost. cih.").slug).toBe("elektrik");
});
it("flags bare Mercedes catalog codes structural and index pages junk", () => {
expect(classifyNode("c442")).toBe("structural");
expect(classifyNode("c105011")).toBe("structural");
expect(classifyNode("sa listesi")).toBe("junk");
expect(classifyNode("ilk on resim aciklama sayfasi")).toBe("junk");
// descriptor codes that carry a part word must stay part (map via keyword)
expect(classifyNode("d765300(22h), ls 7 f, mb servo direksiyon")).toBe("part");
expect(classifyNode("conta takimi")).toBe("part");
expect(classifyNode("debriyaj balatasi")).toBe("part");
});
it("flags empty / doc nodes as junk", () => {
expect(classifyNode("")).toBe("junk");
expect(classifyNode("ab")).toBe("junk");
expect(classifyNode("teknik literatur talimatlar")).toBe("junk");
});
it("flags VIN markers and bare engine/order codes as structural", () => {
expect(classifyNode("wvwzzz9nz5-180000")).toBe("structural");
expect(classifyNode("z16dt[lfu]")).toBe("structural");
});
it("keeps a real part that merely carries a date-validity marker", () => {
// ">>" alone must not reclassify a genuine part name
expect(classifyNode("pencere camlari d >> - 14.07.2019")).toBe("part");
expect(classifyNode("gosterge tablosu d - 30.05.2011>> - 27.05.2012")).toBe("part");
});
it("keeps real part-category names as part", () => {
expect(classifyNode("fren balata on")).toBe("part");
expect(classifyNode("eksantrik mili, supaplar (1,4/1,6ltr.)")).toBe("part");
expect(classifyNode("yag pompasi yag filtresi")).toBe("part");
});
});
describe("mapToCanonical", () => {
it("maps clear names to the expected bucket", () => {
expect(mapToCanonical("fren balata on").slug).toBe("fren");
expect(mapToCanonical("eksantrik mili supaplar").slug).toBe("motor");
expect(mapToCanonical("far temizleme sistemi").slug).toBe("aydinlatma");
expect(mapToCanonical("5-vitesli duz sanziman").slug).toBe("sanziman");
expect(mapToCanonical("koltuk sirtlik dolgu").slug).toBe("ic-doseme");
});
it("maps expanded-lexicon terms (egzoz, karoseri, bcm)", () => {
expect(mapToCanonical("kat konvertorlu egz borusu").slug).toBe("yakit-sistemi");
expect(mapToCanonical("pikap karoserisi").slug).toBe("panjur-panel");
expect(mapToCanonical("merkezi elektrik kontrol unitesi bcm").slug).toBe("elektrik");
});
it("maps short-stem inflections (yağı, mili, kardan)", () => {
expect(mapToCanonical("hps yagi").slug).toBe("yaglar-sivilar");
expect(mapToCanonical("kardan mili").slug).toBe("sanziman");
expect(mapToCanonical("araba akusu").slug).toBe("elektrik");
// regression: a strong term still wins over the newly-added weak inflection
expect(mapToCanonical("eksantrik mili supaplar").slug).toBe("motor");
expect(mapToCanonical("yag filtresi").slug).toBe("filtreler");
});
it("maps a root named exactly like its canonical bucket", () => {
expect(mapToCanonical("suspansiyon, sasi").slug).toBe("suspansiyon");
expect(mapToCanonical("elektrik").slug).toBe("elektrik");
expect(mapToCanonical("aydinlatma").slug).toBe("aydinlatma");
expect(mapToCanonical("direksiyon").slug).toBe("direksiyon");
expect(mapToCanonical("motor").slug).toBe("motor");
});
it("maps pcat shared-taxonomy roots", () => {
expect(mapToCanonical("jenerator").slug).toBe("elektrik");
expect(mapToCanonical("yaglama sistemi").slug).toBe("yaglar-sivilar");
expect(mapToCanonical("karo, arac cami").slug).toBe("panjur-panel");
expect(mapToCanonical("guc aktarim organlari").slug).toBe("sanziman");
expect(mapToCanonical("cerceve").slug).toBe("panjur-panel");
// regression: şasi stays under süspansiyon when paired with it
expect(mapToCanonical("suspansiyon, sasi").slug).toBe("suspansiyon");
expect(mapToCanonical("sasi kabugu ve plakalar").slug).toBe("panjur-panel");
});
it("maps the new wheel + accessory buckets and the resolved ambiguous roots", () => {
expect(mapToCanonical("jantlar, lastikler").slug).toBe("jant-lastik");
expect(mapToCanonical("gobek ve tekerlekler").slug).toBe("jant-lastik");
expect(mapToCanonical("aksesuarlar ve cesitli").slug).toBe("aksesuar");
expect(mapToCanonical("hava sistemi").slug).toBe("sogutma");
expect(mapToCanonical("bakim parcalari").slug).toBe("filtreler");
// "hava yastığı" (airbag) must beat sogutma's weak "hava"
expect(mapToCanonical("hava yastigi surucu tarafi").slug).toBe("direksiyon");
// "hava filtresi" still filtreler; "…Yağlar Araç Bakım Ürünleri" still yakıt
expect(mapToCanonical("hava filtresi").slug).toBe("filtreler");
expect(mapToCanonical("yakitlar ve yaglar arac bakim urunleri").slug).toBe("yakit-sistemi");
// a real part with an "aksesuar" mention still wins over the catch-all
expect(mapToCanonical("fren aksesuarlari").slug).toBe("fren");
});
it("maps English category names (pl24 Asian brands / untranslated leaves)", () => {
expect(mapToCanonical("engine").slug).toBe("motor");
expect(mapToCanonical("transmission").slug).toBe("sanziman");
expect(mapToCanonical("brake").slug).toBe("fren");
expect(mapToCanonical("chassis").slug).toBe("panjur-panel");
expect(mapToCanonical("axle & suspension").slug).toBe("suspansiyon");
expect(mapToCanonical("floor covering").slug).toBe("ic-doseme");
expect(mapToCanonical("body seats").slug).toBe("ic-doseme");
// system word beats the weak location word in a phrase
expect(mapToCanonical("body electrical").slug).toBe("elektrik");
expect(mapToCanonical("engine mechanical").slug).toBe("motor");
});
it("maps Opel spelling variants", () => {
expect(mapToCanonical("otomatik sanzuman").slug).toBe("sanziman");
expect(mapToCanonical("on dingil ve suspensiyon").slug).toBe("suspansiyon");
expect(mapToCanonical("disli secici kolu").slug).toBe("sanziman");
});
it("maps the Mercedes long-tail (English + Turkish part names)", () => {
expect(mapToCanonical("propeller shaft").slug).toBe("sanziman");
expect(mapToCanonical("radiator").slug).toBe("sogutma");
expect(mapToCanonical("sliding roof").slug).toBe("panjur-panel");
expect(mapToCanonical("driver's door").slug).toBe("dis-kaporta");
expect(mapToCanonical("windows").slug).toBe("dis-kaporta");
expect(mapToCanonical("timing").slug).toBe("triger-zincir-rulman");
expect(mapToCanonical("carburetor").slug).toBe("yakit-sistemi");
expect(mapToCanonical("pedal sistemi").slug).toBe("fren");
expect(mapToCanonical("arka kapak").slug).toBe("dis-kaporta");
expect(mapToCanonical("ortu").slug).toBe("ic-doseme");
});
it("maps BMW electronics + lighting and Ford trans/engine variants", () => {
expect(mapToCanonical("audio, navigasyon, elektronik sistem").slug).toBe("elektrik");
expect(mapToCanonical("iletisim sistemi").slug).toBe("elektrik");
expect(mapToCanonical("distance systems, cruise control").slug).toBe("elektrik");
expect(mapToCanonical("isik verme").slug).toBe("aydinlatma");
expect(mapToCanonical("tvmt6 - 6 speed manual trans").slug).toBe("sanziman");
expect(mapToCanonical("t20dd0j - 2.0l diesel panther").slug).toBe("motor");
expect(mapToCanonical("govde ve boya").slug).toBe("panjur-panel");
});
it("flags BMW service/doc pseudo-pages as junk", () => {
expect(classifyNode("teknik dokuman")).toBe("junk");
expect(classifyNode("servis ve onarim kapsamlari")).toBe("junk");
expect(classifyNode("value parts&packages servisi ve onarim")).toBe("junk");
});
it("maps small-brand terms (English + Turkish) added for max coverage", () => {
expect(mapToCanonical("diagnostic devices").slug).toBe("elektrik");
expect(mapToCanonical("in car entertainment").slug).toBe("elektrik");
expect(mapToCanonical("genel arac elektrigi").slug).toBe("elektrik");
expect(mapToCanonical("heater,a/c & ventilation").slug).toBe("sogutma");
expect(mapToCanonical("braking system").slug).toBe("fren");
expect(mapToCanonical("exterior fittings and sunroof").slug).toBe("dis-kaporta");
expect(mapToCanonical("occupant restraints").slug).toBe("ic-doseme");
expect(mapToCanonical("aracta konfor ve yasam").slug).toBe("ic-doseme");
expect(mapToCanonical("yapistiricilar / onarim kitleri").slug).toBe("yaglar-sivilar");
});
it("maps Kollar / Ön Takım to süspansiyon without breaking stronger matches", () => {
expect(mapToCanonical("kollar").slug).toBe("suspansiyon");
expect(mapToCanonical("on takim").slug).toBe("suspansiyon");
expect(mapToCanonical("arka takim").slug).toBe("suspansiyon");
// strong words still win over the weak axle terms
expect(mapToCanonical("conta takimi").slug).toBe("motor");
expect(mapToCanonical("vites kollari").slug).toBe("sanziman");
// "kol demiri" (armrest) must NOT become süspansiyon (we didn't add bare "kol")
expect(mapToCanonical("kol demirleri").slug).not.toBe("suspansiyon");
});
it("maps emex high-frequency terms", () => {
expect(mapToCanonical("stabilizer / baglanti elemanlari").slug).toBe("suspansiyon");
expect(mapToCanonical("sok absorber").slug).toBe("suspansiyon");
expect(mapToCanonical("isi esanjoru, ic isitma").slug).toBe("sogutma");
expect(mapToCanonical("kondansator").slug).toBe("sogutma");
expect(mapToCanonical("ventilator").slug).toBe("sogutma");
expect(mapToCanonical("uflec / parcalari").slug).toBe("sogutma");
expect(mapToCanonical("yer halilari ve halilar").slug).toBe("ic-doseme");
expect(mapToCanonical("dis kilitler").slug).toBe("dis-kaporta");
expect(mapToCanonical("stepne donanimlari").slug).toBe("jant-lastik");
expect(mapToCanonical("yagmurluklar").slug).toBe("dis-kaporta");
});
it("maps emex low-brand terms (deep-dive additions)", () => {
expect(mapToCanonical("buharlastirici").slug).toBe("sogutma");
expect(mapToCanonical("kurutucu").slug).toBe("sogutma");
expect(mapToCanonical("aktuatorler").slug).toBe("elektrik");
expect(mapToCanonical("selenoid anahtar").slug).toBe("elektrik");
expect(mapToCanonical("anten").slug).toBe("elektrik");
expect(mapToCanonical("lambda kontrol").slug).toBe("yakit-sistemi");
expect(mapToCanonical("rolanti kontrolu").slug).toBe("yakit-sistemi");
expect(mapToCanonical("kanat parcalari").slug).toBe("panjur-panel");
expect(mapToCanonical("yan capraz kiris").slug).toBe("panjur-panel");
expect(mapToCanonical("burkma cubugu").slug).toBe("suspansiyon");
});
it("phrase overrides fix adjective+head mis-buckets", () => {
// "<system> yağı" = fluid, not the system
expect(mapToCanonical("motor yagi").slug).toBe("yaglar-sivilar");
expect(mapToCanonical("sanziman yagi").slug).toBe("yaglar-sivilar");
// but the PARTS keep their bucket (end-anchor excludes them)
expect(mapToCanonical("motor yagi filtresi").slug).toBe("filtreler");
expect(mapToCanonical("motor yagi pompasi").slug).toBe("motor");
// body electric motors → elektrik, not engine
expect(mapToCanonical("cam silecek motoru").slug).toBe("elektrik");
expect(mapToCanonical("cam motoru").slug).toBe("elektrik");
// timing gear → triger, not şanzıman
expect(mapToCanonical("zamanlama dislileri").slug).toBe("triger-zincir-rulman");
// engine cooling → soğutma; turbocharger → motor; drive belt → triger
expect(mapToCanonical("ic motor sogutma sistemi").slug).toBe("sogutma");
expect(mapToCanonical("emme manifoldu ve turbo sarjli").slug).toBe("motor");
expect(mapToCanonical("ic motor tahrik kayisi").slug).toBe("triger-zincir-rulman");
});
it("returns null when no keyword matches", () => {
expect(mapToCanonical("romork duzenegi cikarilabilir").slug).toBeNull();
// genuinely generic Mercedes names stay unmapped (better than wrong bucket)
expect(mapToCanonical("ayar").slug).toBeNull();
expect(mapToCanonical("genel parcalar").slug).toBeNull();
});
it("a strong head-noun beats a coincidental weak term (not ambiguous)", () => {
// 'filtre' (strong, filtreler) outweighs 'yag' (weak, yaglar-sivilar)
const m = mapToCanonical("yag filtresi");
expect(m.slug).toBe("filtreler");
expect(m.ambiguous).toBe(false);
// 'fren' (strong) outweighs 'hidrolik' (weak) — the old logic flagged this ambiguous
const f = mapToCanonical("fren hidrolik deposu");
expect(f.slug).toBe("fren");
expect(f.ambiguous).toBe(false);
});
it("flags a genuine two-strong tie as ambiguous", () => {
// fren(fren+balata) vs debriyaj(debriyaj+baski) — both score 6
const m = mapToCanonical("fren balata debriyaj baski");
expect(m.ambiguous).toBe(true);
expect(m.confidence).toBeLessThan(0.6);
});
it("only returns slugs present in the priority list", () => {
const m = mapToCanonical("motor blok");
expect(m.slug).not.toBeNull();
expect(PRIORITY).toContain(m.slug);
});
});

View File

@@ -0,0 +1,62 @@
import { describe, expect, it } from "vitest";
import { SUB_LEXICON, allSubLeaves, mapToSubcategory } from "../canonical-sublexicon";
describe("mapToSubcategory", () => {
it("maps Fren part names to the right sub-leaf (incl. Turkish variants)", () => {
expect(mapToSubcategory("on fren balatasi", "fren")).toBe("fren-balata");
expect(mapToSubcategory("fren diski on", "fren")).toBe("fren-disk");
expect(mapToSubcategory("fren kaliyeri", "fren")).toBe("fren-kaliper"); // kaliyer variant
expect(mapToSubcategory("fren kampanasi", "fren")).toBe("fren-kampana");
expect(mapToSubcategory("abs sensoru", "fren")).toBe("fren-abs");
expect(mapToSubcategory("fren hidroligi", "fren")).toBe("fren-hidrolik"); // hidroliği variant
expect(mapToSubcategory("el fren teli", "fren")).toBe("fren-elfren");
});
it("catches assembly hardware in the donanım leaf, not a part leaf", () => {
expect(mapToSubcategory("baglanti civatasi", "fren")).toBe("fren-donanim");
expect(mapToSubcategory("klips", "fren")).toBe("fren-donanim");
// a real part keyword still wins over hardware words in the same name
expect(mapToSubcategory("fren balatasi klipsi", "fren")).toBe("fren-balata");
});
it("maps across other high-value buckets", () => {
expect(mapToSubcategory("yag filtresi", "filtreler")).toBe("filtre-yag");
expect(mapToSubcategory("hava filtresi", "filtreler")).toBe("filtre-hava");
expect(mapToSubcategory("on far", "aydinlatma")).toBe("ayd-far");
expect(mapToSubcategory("stop lambasi", "aydinlatma")).toBe("ayd-stop");
expect(mapToSubcategory("on amortisor", "suspansiyon")).toBe("susp-amortisor");
expect(mapToSubcategory("silindir kapak contasi", "motor")).toBe("motor-silindirkapak");
expect(mapToSubcategory("debriyaj baski balata seti", "debriyaj")).toBe("debriyaj-balata");
});
it("maps queue-shrink additions (2026-07-06)", () => {
expect(mapToSubcategory("sok absorber", "suspansiyon")).toBe("susp-amortisor");
expect(mapToSubcategory("guneslik", "ic-doseme")).toBe("ic-tavan");
expect(mapToSubcategory("gosterge paneli", "elektrik")).toBe("elektrik-modul");
expect(mapToSubcategory("anten", "elektrik")).toBe("elektrik-modul");
expect(mapToSubcategory("korna", "elektrik")).toBe("elektrik-korna");
expect(mapToSubcategory("silecek lastigi", "elektrik")).toBe("elektrik-silecek");
expect(mapToSubcategory("silecek motoru", "elektrik")).toBe("elektrik-motor"); // motor wins
expect(mapToSubcategory("havalandirma ve isitma", "sogutma")).toBe("sogutma-kalorifer");
expect(mapToSubcategory("isi esanjoru", "sogutma")).toBe("sogutma-kalorifer");
expect(mapToSubcategory("yag tavasi", "yaglar-sivilar")).toBe("yag-sistemi");
expect(mapToSubcategory("kisa dingil", "suspansiyon")).toBe("susp-aks");
});
it("returns null for unknown bucket or unmatched name (→ SP manual)", () => {
expect(mapToSubcategory("balata", "aksesuar")).toBeNull(); // bucket not sub-split
expect(mapToSubcategory("belirtilmemis", "fren")).toBeNull();
expect(mapToSubcategory("", "fren")).toBeNull();
});
it("every sub-leaf slug is globally unique and bucket-prefixed", () => {
const leaves = allSubLeaves();
const slugs = leaves.map((l) => l.slug);
expect(new Set(slugs).size).toBe(slugs.length); // no dupes
for (const l of leaves) {
// slug starts with a stem of its bucket (fren-*, motor-*, ayd-*, susp-*, …)
expect(l.slug).toMatch(/^[a-z]+-/);
}
expect(Object.keys(SUB_LEXICON).length).toBeGreaterThanOrEqual(15);
});
});

View File

@@ -25,6 +25,16 @@ export const QUEUE_NAMES = {
SUBSCRIPTION_EXPIRY: "subscription-expiry", SUBSCRIPTION_EXPIRY: "subscription-expiry",
QUERY_CLEANUP: "query-cleanup", QUERY_CLEANUP: "query-cleanup",
CATALOG_PREFETCH: "catalog-prefetch", CATALOG_PREFETCH: "catalog-prefetch",
// Dedicated lane for freshly-decoded vehicles: the main queue's deep-drill
// backlog runs in a production>consumption regime for hours at a time, where
// lifo tail-insertion starves ANY job pushed a moment earlier — including new
// vehicles' init chains (BMW 320i sat 58min at 0 parts). A separate queue +
// worker gives fresh decodes their own consumption budget.
CATALOG_PREFETCH_FAST: "catalog-prefetch-fast",
TRANSLATION: "translation", TRANSLATION: "translation",
LIFECYCLE_EMAIL: "lifecycle-email", LIFECYCLE_EMAIL: "lifecycle-email",
EXPERT_REWARDS: "expert-rewards",
PART_PRICE_REFRESH: "part-price-refresh",
VINPIN_DECODE: "vinpin-decode",
CANONICAL_BACKFILL: "canonical-backfill",
} as const; } as const;

View File

@@ -0,0 +1,367 @@
// Canonical taxonomy mapping — shared logic for the canonical-backfill job.
//
// Two-stage mapping (proven on the VW+Opel dev pilot 2026-07-04):
// 1. classifyNode — is this raw OEM node an actual part-category, or a
// vehicle-selector (model/year/VIN/engine-code) node, or junk? Opel's pl24
// tree is ~87% model-code nodes ("j82 ascona-c 1982-1988"); those must NOT
// be mapped.
// 2. mapToCanonical — WEIGHTED keyword-match the surviving part-categories to
// one of the 20 canonical top buckets (migration 0024 seed). Strong
// head-nouns weigh 3, supporting terms 1; the score margin decides the
// winner and flags near-ties as ambiguous (rather than the old equal-count
// tie-break, which over-flagged).
//
// Names arrive already folded by NORMALIZE_SQL (Turkish → ASCII, lowercased,
// whitespace collapsed) so the same expression can drive the SELECT that builds
// the map and the UPDATE that materializes categories.canonical_category_id.
// Keep in exact sync with the UPDATE join in canonical-backfill.processor.ts —
// both sides must fold identically or the join misses.
export const NORMALIZE_SQL =
"btrim(regexp_replace(translate(lower(name),'çşğıöü','csgiou'),'\\s+',' ','g'))";
const STRONG_WEIGHT = 3;
const WEAK_WEIGHT = 1;
// JS equivalent of NORMALIZE_SQL, for on-the-fly serving-side mapping of
// categories whose canonical_category_id isn't materialized yet (freshly
// decoded vehicles between backfills). Must fold like the SQL side.
const TR_FOLD: Record<string, string> = {
ç: "c",
ş: "s",
ğ: "g",
ı: "i",
ö: "o",
ü: "u",
};
export function foldName(name: string): string {
return name
.toLowerCase()
.replace(/[çşğıöü]/g, (c) => TR_FOLD[c] ?? c)
.replace(/\s+/g, " ")
.trim();
}
interface Bucket {
strong: string[];
weak: string[];
}
// slug → { strong head-nouns, weak supporting terms }. slug MUST match a
// canonical_categories.slug seeded in migration 0024. Weak terms are the ones
// that recur across buckets (yag, hidrolik, cam, panel, rot, kayis…) so they
// only tip the balance, never carry a match alone.
export const LEXICON: Record<string, Bucket> = {
// English keywords (engine/body/brake…) are mixed in per bucket: many pl24
// Asian-brand catalogs (Hyundai/Kia/Nissan/Toyota) and untranslated emex/pcat
// leaves ship English category names. System words are strong, location words
// (body/floor/axle) weak so "BODY ELECTRICAL" → elektrik, not panjur.
motor: {
// "conta" (gasket) strong: mostly engine, and beats the weak "takim" so
// "conta takımı" → motor (not süspansiyon).
strong:
"motor silindir krank piston segman biyel supap subap eksantrik karter kulbutor conta engine".split(
" ",
),
// "diesel"/"dohc" Ford engine variants; "mekanik" (Turkish mechanical);
// "aggregate" (MAN engine/genset unit).
weak: "blok manifold yatak kompresyon bolmesi kam turbo mechanical mekanik diesel dohc aggregate".split(
" ",
),
},
"yakit-sistemi": {
// 'depo' is weak, not strong: "deposu" (reservoir) recurs in fren-hidrolik-
// deposu / cam-suyu-deposu etc.; "yakit" carries the fuel-tank signal.
strong:
"yakit enjektor enjeksiyon karburator carburetor egzoz egz konvertor katalitik susturucu fuel exhaust".split(
" ",
),
weak: "benzin mazot lpg rail dagitici depo lambda rolanti".split(" "),
},
"atesleme-sistemi": {
strong: "buji bobin atesleme distributor kizdirma ignition".split(" "),
weak: [],
},
"triger-zincir-rulman": {
strong: "triger timing".split(" "),
weak: "zincir gergi kayis rulman".split(" "),
},
sogutma: {
// emex adds: esanjor (ısı eşanjörü), kondansator, ventilator, kompresor
// (AC), sogutucu; "uflec" (blower) weak.
strong:
"radyator termostat klima kalorifer intercooler evaporator buharlastirici kurutucu devirdaim fan cooling heater ventilation ventilator esanjor kondansator kompresor sogutucu".split(
" ",
),
// "hava" weak so "Hava Sistemi" → soğutma (user decision) but "hava filtresi"
// (filtre strong) / "hava yastığı" (yastigi strong, direksiyon) still win.
weak: "sogutma isitici hortum hava heating radiator uflec".split(" "),
},
filtreler: {
strong: "filtre filter".split(" "),
// "bakım parçaları / rutin bakım" → filtreler (user decision); weak so a
// stronger term (yakıt/yağ) still wins on "…Araç Bakım Ürünleri" roots.
weak: "bakim".split(" "),
},
fren: {
strong: "fren balata kaliper kampana disk abs brake braking".split(" "),
// "pedal" weak → Mercedes "PEDAL SİSTEMİ" (pedal cluster) lands in fren.
weak: "mustur diskli pedal".split(" "),
},
debriyaj: {
strong: "debriyaj volan baski clutch".split(" "),
weak: "kavrama".split(" "),
},
sanziman: {
// "sanzuman"/"disli" are Opel spelling variants (şanzıman / dişli).
strong: "sanziman sanzuman vites diferansiyel kardan aktarim transmission gearbox".split(" "),
// 3-char stems (mil…) don't prefix-match their inflections, so list them.
// "speed"/"spd" for Ford trans variants ("6 Speed Manual Trans", "8 Spd Auto").
weak: "flans kavrama saft mil mili miller gear disli propeller tahrik speed spd".split(" "),
},
suspansiyon: {
// "suspensiyon" Opel variant; "dingil"=axle; "stabilizator/stabilizer"=anti-
// roll bar; "absorber"=(şok) absorber (emex).
strong:
"suspansiyon suspensiyon amortisor salincak rotil helezon viraj mafsal suspension stabilizator stabilizer absorber".split(
" ",
),
// "takoz/takozu"=strut/engine mount; "kollar"=(salıncak) control arms;
// "takim"=(ön/arka) axle assembly. Weak so strong words win: "conta takımı"
// → motor, "vites kolları" → şanzıman, but standalone "Kollar"/"Ön Takım" →
// süspansiyon. NOT "kol" (singular): "kol demiri" (armrest) is ic-doseme.
weak: "rot aks dingil burc yay bilya rulman tasiyici zemberek denge axle takoz takozu kollar takim burkma".split(
" ",
),
},
direksiyon: {
// "yastigi" strong so "hava yastığı" (airbag) beats the sogutma "hava" weak.
strong: "direksiyon kremayer airbag yastigi steering".split(" "),
weak: "rot hidrolik".split(" "),
},
elektrik: {
strong:
"elektrik elektr elektrigi alternator jenerator mars sarj role sigorta sensor korna bcm ecu beyin modul aku akusu enstruman gosterge electric electrical electricity audio ses navigasyon elektronik iletisim multimedya diagnostic entertainment bilgisayar aktuator selenoid anten".split(
" ",
),
weak: "kablo sinyal silecek tesisat salter salteri verici vericisi cruise".split(" "),
},
aydinlatma: {
// far/farlar/fari — 3-char "far" only matches exactly; add inflections.
// "isik" = BMW "Işık verme" (lighting).
strong: "aydinlatma isik far farlar fari stop lamba ampul sis reflektor lighting lamp".split(
" ",
),
weak: "sinyal".split(" "),
},
"dis-kaporta": {
strong: "ayna".split(" "),
weak: "cam camlar camlari fitil krikosu kapi kapak kapaklar mentese cikma bagaj camlik mirror glass door window sunroof exterior locking kilit yagmurluk".split(
" ",
),
},
tampon: {
strong: "tampon izgara".split(" "),
weak: "grill cekme".split(" "),
},
"panjur-panel": {
strong: "panjur kaput kaporta davlumbaz marspiyel camurluk karoser karo cerceve".split(" "),
// "sasi"/"body"/"chassis" weak so system words (electrical…) win in phrases
// like "BODY ELECTRICAL"; "süspansiyon, şasi" also stays a clean süspansiyon.
weak: "panel tavan sac canak direk sasi govde kanat kiris body chassis roof wall underpan".split(
" ",
),
},
"ic-doseme": {
// "koltugu" = seat with possessive suffix (koltuk→koltuğu, k→ğ mutation
// breaks the prefix match, so list it explicitly).
strong:
"koltuk koltugu doseme sirtlik arkalik guneslik minder torpido konsol konfor hali seat seats trim restraint occupant".split(
" ",
),
weak: "kaplama zemin baslik kemer emniyet panel bardaklik dayama dolgu floor covering interior ortu halilar".split(
" ",
),
},
"yaglar-sivilar": {
strong: "antifriz gres adblue".split(" "),
// "yag" (3 chars) misses "yagi/yaglar/yagli" under the >=4 prefix rule.
weak: "yag yagi yaglar yagli yaglama hidrolik sprey katki madeni sivi oil yapistirici".split(
" ",
),
},
"jant-lastik": {
strong: "jant lastik tekerlek gobek stepne wheel tyre tire".split(" "),
weak: "hub".split(" "),
},
aksesuar: {
strong: "aksesuar accessory accessories".split(" "),
weak: [],
},
};
// Deterministic tie-break for exactly-equal scores: earlier = more specific.
export const PRIORITY: string[] = [
"atesleme-sistemi",
"triger-zincir-rulman",
"filtreler",
"fren",
"debriyaj",
"sanziman",
"aydinlatma",
"ic-doseme",
"sogutma",
"yakit-sistemi",
"yaglar-sivilar",
"direksiyon",
"suspansiyon",
"jant-lastik",
"tampon",
"panjur-panel",
"dis-kaporta",
"elektrik",
"motor",
// Catch-all last: a name with any real part keyword wins over "aksesuar".
"aksesuar",
];
// Model-code prefix: Opel j82/c06/tf0/b16. 1-3 letters + 2 digits at start.
const MODEL_CODE = /^[a-z]{1,3}\d{2}\b/;
// VW/OEM VIN prefixes (wvwzzz…, wvgzzz…) — decode markers, not categories.
const VIN_LIKE = /\b[a-z]{2,3}zzz|w[a-z]{2}\d{3,}/;
// Pure "YYYY - YYYY" range with little else = a year/model selector node.
const YEAR_RANGE_ONLY = /^\D*(19|20)\d{2}\s*-\s*(19|20)\d{2}\D*$/;
const YEAR_RANGE_ANY = /\b(19|20)\d{2}\s*-\s*(19|20)\d{2}\b/;
// Bare engine/order code like "z16dt", "z16dt[lfu]" — a single token with digits.
const CODE_TOKEN = /^[a-z]{1,3}\d{1,3}[a-z]{0,4}(\[[a-z]+\])?$/;
// Mercedes BARE catalog codes only ("C442", "C105011"). Anchored so descriptor
// nodes that also carry a part name ("D765300(22H), LS 7 F, MB SERVO DİREKSİYON"
// → direksiyon) still map via keyword instead of being wrongly dropped.
const MB_CATALOG_CODE = /^[cd]\d{3,}$/;
// MAN truck model descriptors: "12.155 STAR", "10/11.150 HOCL/R", "18T HOCL-SD".
// (Bare "14250"-style codes are NOT matched — ^\d{4,}/^\d{4,5} would hit 362 pl24
// + 2800 emex mapped rows.)
const TRUCK_MODEL_CODE = /^\d{1,2}[./]\d|^\d{2}t[ -]/i;
// MAN bus/truck model + coachbuilder names (LION'S CITY, SKYLINER, CITYLINER,
// NL 202, ND 313, NG 242, MAN 292, AUFBAU CAETANO). Vehicle selectors, not part
// categories — verified 0 emex/pcat mapped rows collide; the few pl24 hits are
// LION'S/LPG models that had wrongly matched a part keyword.
const MAN_MODEL_CODE =
/^(nd|ng|nl|ngt|man|el|gs|le|sg|sl|sr|sue|uel|rh|nm|nmt|nue|sd) \d|^(maz|mftpl)\b|^266m|lion|skyliner|starliner|cityliner|tourliner|trendliner|jetliner|centroliner|intercity|neoplan|sinotruk|doromat|aufbau|goeppel|gorica|joskin|neoman|caetano|newcar|new flyer|gelenk|maribor|slovenien/i;
const DOC_JUNK = new Set([
"teknik literatur talimatlar",
"aletler",
"kargo tahditi",
"kriko alet",
"preliminary remarks",
]);
// Catalog index / service / doc pseudo-pages — not part categories. Mercedes
// "SA LİSTESİ"/"…resim açıklama sayfası"; BMW "Teknik doküman", "Servis ve
// onarım kapsamları", "Value Parts&Packages servisi", "Parça Onarım Servisi".
const DOC_PAGE =
/resim aciklama|^sa listesi$|genel bakis tablosu|teknik dokuman|servis ve onarim|onarim servisi|onarim kapsam|value parts|servis aralik|ek onarim/;
export type NodeKind = "part" | "structural" | "junk";
function wordTokens(folded: string): string[] {
return folded.split(" ").filter((w) => w.length > 1);
}
/** folded = output of NORMALIZE_SQL. */
export function classifyNode(folded: string): NodeKind {
const f = folded.trim();
if (!f || f.length < 3) return "junk";
if (DOC_JUNK.has(f) || DOC_PAGE.test(f)) return "junk";
if (MB_CATALOG_CODE.test(f) || TRUCK_MODEL_CODE.test(f) || MAN_MODEL_CODE.test(f))
return "structural";
// NOTE: don't treat a bare ">>" marker as structural — real VW part names
// carry validity-date markers ("pencere camlari d >> - 14.07.2019"); VIN_LIKE
// below catches the actual VIN nodes ("wvwzzz…") without dropping real parts.
if (VIN_LIKE.test(f)) return "structural";
if (MODEL_CODE.test(f)) return "structural";
if (YEAR_RANGE_ONLY.test(f)) return "structural";
const words = wordTokens(f);
// Single bare code token (engine/order code) → selector, not a category.
if (words.length <= 1 && CODE_TOKEN.test(f)) return "structural";
// year range + very few real words → still a selector ("astra-h 2004 - 2014")
if (YEAR_RANGE_ANY.test(f) && words.length <= 4) return "structural";
return "part";
}
function tokens(folded: string): string[] {
return folded.split(/[^a-z0-9]+/).filter((t) => t.length > 1);
}
function hitCount(toks: string[], keywords: string[]): number {
let c = 0;
for (const t of toks) {
for (const k of keywords) {
if (t === k || (k.length >= 4 && t.startsWith(k))) {
c++;
break;
}
}
}
return c;
}
export interface CanonicalMatch {
slug: string | null;
ambiguous: boolean;
confidence: number;
}
/**
* Weighted keyword-map a part-category folded name to a canonical top slug.
* Score = 3·(strong hits) + 1·(weak hits) per bucket. The winner is the top
* score; a runner-up within 1 point (no strong keyword decisively separated the
* two) is flagged ambiguous. Confidence scales with the winning margin.
*/
// High-confidence PHRASE overrides, checked before the bag-of-words score.
// They fix "adjective + head-noun" cases where a strong keyword wins on the
// modifier instead of the head: "motor yağı" scores motor(3) but the head noun
// is the oil. Order doesn't matter (first match wins); keep each tight.
const OVERRIDES: [RegExp, string][] = [
// "<system> yağı/yağları" (head = oil) → fluid. END-ANCHORED so the PARTS
// "motor yağı filtresi / pompası / soğutucusu" are NOT captured (they keep
// scoring to filtre / motor / soğutma).
[/(motor|sanziman|diferansiyel|direksiyon|hidrolik|fren|torpido) yag(i|lari)$/, "yaglar-sivilar"],
// body/electric motors (window/wiper/blower), not the engine.
[/(cam|silecek|kalorifer|kaput) motor/, "elektrik"],
// timing gear/chain/belt → triger, not şanzıman via the weak "dişli".
[/zamanlama (disli|zincir|kayis|seti|gergi|kasnak)/, "triger-zincir-rulman"],
// "(IC) Motor Soğutma …" = engine COOLING; head is soğutma, "motor" is the
// subsystem qualifier (pcat's "IC Motor <system>" prefix inflates motor).
[/motor sogutma/, "sogutma"],
// "türbo şarj(lı)" = turbocharger → engine, not elektrik via "şarj" (charge).
[/turbo sarj/, "motor"],
// "(tahrik) kayışı" = drive/accessory belt → triger (belt bucket), not motor.
[/tahrik kayis/, "triger-zincir-rulman"],
];
export function mapToCanonical(folded: string): CanonicalMatch {
for (const [re, slug] of OVERRIDES) {
if (re.test(folded)) return { slug, ambiguous: false, confidence: 0.95 };
}
const toks = tokens(folded);
let bestSlug: string | null = null;
let best = 0;
let second = 0;
for (const slug of PRIORITY) {
const b = LEXICON[slug];
const score = STRONG_WEIGHT * hitCount(toks, b.strong) + WEAK_WEIGHT * hitCount(toks, b.weak);
if (score > best) {
second = best;
best = score;
bestSlug = slug;
} else if (score > second) {
second = score;
}
}
if (best === 0) return { slug: null, ambiguous: false, confidence: 0 };
const margin = best - second;
if (second === 0) return { slug: bestSlug, ambiguous: false, confidence: 0.95 };
const ambiguous = margin < 2;
const confidence = margin >= 3 ? 0.9 : margin >= 2 ? 0.8 : margin >= 1 ? 0.65 : 0.5;
return { slug: bestSlug, ambiguous, confidence };
}

View File

@@ -0,0 +1,399 @@
// Second-level (sub-category) mapping — one step finer than the 20 top buckets.
// Target leaves are curated from the otoparcasan.com retail tree (564 leaves)
// down to the high-signal, sellable part types a B2C API consumer browses by.
//
// Design (proven by the 5-bucket pilot 2026-07-05):
// • A source category name is first mapped to a TOP bucket (canonical-lexicon).
// • Within that bucket we run mapToSubcategory: ordered substring match, first
// hit wins (most-specific sub-leaf listed first). Turkish spelling variants
// matter — "kaliper" ships as "kaliyer", "hidrolik" as "hidroliği" — so both
// forms are listed.
// • A shared "donanım" (fasteners: klips/cıvata/braket…) sub catches assembly
// hardware that has no retail leaf; genuinely unresolved names stay null and
// are completed manually in the super-panel (SP).
//
// slug is globally unique (canonical_categories.slug unique index) → every sub
// slug is prefixed with its bucket ("fren-balata"). Names arrive folded by
// foldName (Turkish→ASCII, lowercased) exactly like the top lexicon.
import { foldName } from "./canonical-lexicon";
export interface SubLeaf {
slug: string; // globally-unique, bucket-prefixed
name: string; // display
kw: string[]; // folded substrings; first matching leaf (in order) wins
}
// Shared hardware leaf appended to every mechanical bucket. Kept LAST so a real
// part keyword always wins ("fren balatasi klipsi" → balata, not donanım).
const HW_KW =
"klips civata vida somun saplama perno rondela kelepce baglanti civatasi montaj kiti tespit civatasi".split(
" ",
);
function hw(bucket: string): SubLeaf {
return { slug: `${bucket}-donanim`, name: "Bağlantı ve Donanım", kw: HW_KW };
}
// bucket slug → ordered sub-leaves (most specific first). Only buckets we can
// meaningfully sub-split are listed; others serve at top level only.
export const SUB_LEXICON: Record<string, SubLeaf[]> = {
fren: [
{ slug: "fren-abs", name: "ABS Sensörü / Ünitesi", kw: ["abs", "esp"] },
{ slug: "fren-balata", name: "Balata", kw: ["balata", "pabuc", "disk balata", "pabuclu"] },
{ slug: "fren-disk", name: "Fren Diski / Ayna", kw: ["disk", "ayna", "rotor"] },
{
slug: "fren-kaliper",
name: "Kaliper",
kw: ["kaliper", "kaliyer", "kundak", "kaliper kundagi"],
},
{ slug: "fren-kampana", name: "Kampana", kw: ["kampana"] },
{
slug: "fren-merkez",
name: "Ana Merkez / Silindir",
kw: ["ana merkez", "merkez", "silindir", "master"],
},
{ slug: "fren-guclendirici", name: "Fren Güçlendirici (Servo)", kw: ["guclendirici", "servo"] },
{
slug: "fren-hidrolik",
name: "Hortum / Boru / Hidrolik",
kw: ["hortum", "hidrol", "boru", "borusu"],
},
{ slug: "fren-elfren", name: "El Freni", kw: ["el fren", "el-fren", "park fren"] },
{
slug: "fren-musur",
name: "Fren Müşürü / İkaz",
kw: ["musur", "ikaz", "limitor", "fren lambasi anahtari"],
},
{ slug: "fren-vakum", name: "Vakum Pompası", kw: ["vakum"] },
{ slug: "fren-pedal", name: "Fren Pedalı", kw: ["pedal"] },
hw("fren"),
],
debriyaj: [
{ slug: "debriyaj-balata", name: "Debriyaj Balatası", kw: ["balata"] },
{ slug: "debriyaj-baski", name: "Baskı", kw: ["baski"] },
{ slug: "debriyaj-set", name: "Debriyaj Seti", kw: ["set", "seti", "kit"] },
{ slug: "debriyaj-rulman", name: "Debriyaj Rulmanı", kw: ["rulman", "bilya"] },
{
slug: "debriyaj-merkez",
name: "Debriyaj Merkezi / Hidrolik",
kw: ["merkez", "silindir", "serbest birakma", "hidrol"],
},
{ slug: "debriyaj-volan", name: "Volan", kw: ["volan"] },
{ slug: "debriyaj-catal", name: "Çatal / Bilya", kw: ["catal", "catali"] },
{ slug: "debriyaj-tel", name: "Debriyaj Teli / Pedalı", kw: ["tel", "teli", "pedal", "musur"] },
hw("debriyaj"),
],
motor: [
{
slug: "motor-silindirkapak",
name: "Silindir Kapağı",
kw: ["silindir kapak", "kapak contasi", "silindir kapagi"],
},
{ slug: "motor-piston", name: "Piston / Segman / Gömlek", kw: ["piston", "segman", "gomlek"] },
{
slug: "motor-krank",
name: "Krank / Biyel",
kw: ["krank", "biyel", "kol yatak", "ana yatak"],
},
{
slug: "motor-supap",
name: "Supap / Külbütör",
kw: ["supap", "subap", "kulbutor", "eksantrik", "tapet", "gayd"],
},
{ slug: "motor-conta", name: "Conta", kw: ["conta"] },
{ slug: "motor-kece", name: "Keçe", kw: ["kece"] },
{ slug: "motor-karter", name: "Karter", kw: ["karter"] },
{ slug: "motor-yagpompa", name: "Yağ Pompası", kw: ["yag pompa", "yaglama", "yag musur"] },
{ slug: "motor-takoz", name: "Motor Takozu", kw: ["takoz"] },
{ slug: "motor-turbo", name: "Turbo", kw: ["turbo"] },
{ slug: "motor-manifold", name: "Manifold", kw: ["manifold"] },
{
slug: "motor-komple",
name: "Motor Komple / Blok / Takım",
kw: ["komple", "blok", "motor bloku", "motor takimi"],
},
hw("motor"),
],
"yakit-sistemi": [
{ slug: "yakit-enjektor", name: "Enjektör", kw: ["enjektor", "enjeksiyon"] },
{ slug: "yakit-pompa", name: "Yakıt Pompası", kw: ["pompa", "pompasi", "otomatik"] },
{ slug: "yakit-kelebek", name: "Gaz Kelebeği", kw: ["gaz kelebegi", "kelebek"] },
{ slug: "yakit-depo", name: "Yakıt Deposu / Şamandıra", kw: ["depo", "deposu", "samandira"] },
{ slug: "yakit-boru", name: "Yakıt Borusu", kw: ["boru", "borusu", "rail"] },
{ slug: "yakit-egr", name: "EGR", kw: ["egr"] },
{ slug: "yakit-karburator", name: "Karbüratör", kw: ["karburator", "karbur"] },
{
slug: "yakit-egzoz",
name: "Egzoz / Lambda",
kw: ["egzoz", "susturucu", "katalitik", "konvertor", "lambda"],
},
hw("yakit-sistemi"),
],
"atesleme-sistemi": [
{ slug: "atesleme-buji", name: "Buji", kw: ["buji", "isitma buji", "kizdirma"] },
{ slug: "atesleme-bobin", name: "Ateşleme Bobini", kw: ["bobin"] },
{ slug: "atesleme-kablo", name: "Buji Kablosu", kw: ["kablo", "kablosu"] },
{ slug: "atesleme-distributor", name: "Distribütör", kw: ["distributor", "distribu"] },
],
"triger-zincir-rulman": [
{ slug: "triger-kayis", name: "Triger Kayışı / Seti", kw: ["kayis", "kayisi", "seti"] },
{ slug: "triger-zincir", name: "Triger Zinciri", kw: ["zincir", "zinciri"] },
{ slug: "triger-gergi", name: "Gergi / Rulman", kw: ["gergi", "rulman", "bilya"] },
],
sogutma: [
{
slug: "sogutma-radyator",
name: "Radyatör / Soğutucu",
kw: ["su radyator", "radyator", "radyatoru", "sogutucu"],
},
{ slug: "sogutma-termostat", name: "Termostat", kw: ["termostat"] },
{
slug: "sogutma-supompa",
name: "Devirdaim / Su Pompası",
kw: ["su pompa", "devirdaim", "devir daim"],
},
{
slug: "sogutma-klima",
name: "Klima (Kompresör/Radyatör)",
kw: ["klima", "kompresor", "kondansator", "evaporator", "buharlastirici", "kurutucu"],
},
{
slug: "sogutma-kalorifer",
name: "Kalorifer / Havalandırma / Isıtma",
kw: ["kalorifer", "isitici", "heater", "havalandirma", "isitma", "hava kanal", "esanjor"],
},
{ slug: "sogutma-fan", name: "Fan / Pervane", kw: ["fan", "pervane", "vantilator", "uflec"] },
{ slug: "sogutma-hortum", name: "Su Hortumu", kw: ["hortum", "hortumu"] },
{ slug: "sogutma-musur", name: "Hararet / Fan Müşürü", kw: ["musur", "hararet", "sicaklik"] },
{ slug: "sogutma-intercooler", name: "Intercooler / Turbo Radyatör", kw: ["intercooler"] },
hw("sogutma"),
],
filtreler: [
{ slug: "filtre-yag", name: "Yağ Filtresi", kw: ["yag filtre", "yag filtresi"] },
{ slug: "filtre-hava", name: "Hava Filtresi", kw: ["hava filtre", "hava filtresi"] },
{
slug: "filtre-yakit",
name: "Yakıt Filtresi",
kw: ["yakit filtre", "mazot filtre", "yakit filtresi"],
},
{ slug: "filtre-polen", name: "Polen / Kabin Filtresi", kw: ["polen", "kabin", "partikul"] },
{ slug: "filtre-diger", name: "Diğer Filtre", kw: ["filtre", "filter"] },
],
suspansiyon: [
{
slug: "susp-amortisor",
name: "Amortisör",
kw: ["amortisor", "amortisor takoz", "amortisor rulman", "koruk", "absorber"],
},
{ slug: "susp-salincak", name: "Salıncak", kw: ["salincak", "kontrol kol"] },
{ slug: "susp-rotil", name: "Rotil", kw: ["rotil"] },
{ slug: "susp-rot", name: "Rot / Rot Başı", kw: ["rot basi", "rot mili", "rot"] },
{
slug: "susp-viraj",
name: "Viraj Demiri / Z Rot / Burkma",
kw: ["viraj", "stabiliz", "z rot", "denge", "burkma"],
},
{ slug: "susp-helezon", name: "Helezon Yay", kw: ["helezon", "yay"] },
{ slug: "susp-rulman", name: "Aks / Porya Rulmanı", kw: ["rulman", "porya", "bilya"] },
{ slug: "susp-aks", name: "Aks / Aktarma", kw: ["aks", "koru", "kafasi", "mafsal", "dingil"] },
{ slug: "susp-burc", name: "Burç / Takoz / Pim", kw: ["burc", "takoz", "pim"] },
{ slug: "susp-bijon", name: "Bijon", kw: ["bijon"] },
hw("suspansiyon"),
],
direksiyon: [
{ slug: "direksiyon-simit", name: "Direksiyon Simidi", kw: ["simit", "simidi"] },
{
slug: "direksiyon-pompa",
name: "Direksiyon Pompası / Hidrolik",
kw: ["pompa", "pompasi", "hidrol"],
},
{
slug: "direksiyon-kutu",
name: "Direksiyon Kutusu / Dişli / Kremayer",
kw: ["kutu", "kremayer", "disli", "dislisi"],
},
{ slug: "direksiyon-airbag", name: "Airbag", kw: ["airbag", "hava yastigi", "yastigi"] },
{ slug: "direksiyon-koruk", name: "Direksiyon Körüğü", kw: ["koruk", "korugu"] },
{
slug: "direksiyon-elektronik",
name: "Kumanda / Sensör",
kw: ["kumanda", "sensor", "sensorleri", "unite"],
},
{
slug: "direksiyon-mil",
name: "Direksiyon Mili / Boğaz / Göbek",
kw: ["mil", "kol", "rot", "bogaz", "gobek", "dingil"],
},
],
sanziman: [
{
slug: "sanziman-vites",
name: "Vites / İç Kontroller",
kw: ["vites", "kol korugu", "topuz", "ic kontrol"],
},
{ slug: "sanziman-takoz", name: "Şanzıman Takozu", kw: ["takoz"] },
{ slug: "sanziman-saft", name: "Şaft / Mafsal", kw: ["saft", "mafsal", "istavroz", "kaplin"] },
{ slug: "sanziman-diferansiyel", name: "Diferansiyel", kw: ["diferansiyel", "difransiyel"] },
{
slug: "sanziman-sensor",
name: "Hız / Km Sensörü",
kw: ["hiz sensor", "kilometre", "km", "geri vites musur"],
},
{ slug: "sanziman-karter", name: "Şanzıman Karteri / Yağ", kw: ["karter", "yag"] },
hw("sanziman"),
],
elektrik: [
{ slug: "elektrik-aku", name: "Akü", kw: ["aku", "akusu"] },
{ slug: "elektrik-alternator", name: "Alternatör", kw: ["alternator", "sarj dinamo"] },
{ slug: "elektrik-mars", name: "Marş Motoru", kw: ["mars", "mars dinamo", "mars otomat"] },
{ slug: "elektrik-sensor", name: "Sensör", kw: ["sensor", "verici", "sender", "pulse"] },
{ slug: "elektrik-sigorta", name: "Sigorta / Röle", kw: ["sigorta", "role"] },
{
slug: "elektrik-motor",
name: "Cam / Silecek Motoru",
kw: ["cam motor", "silecek motor", "kalorifer motor", "cam kolu", "cam kaldirma"],
},
{ slug: "elektrik-silecek", name: "Silecek", kw: ["silecek"] },
{ slug: "elektrik-korna", name: "Korna", kw: ["korna"] },
{ slug: "elektrik-kontak", name: "Kontak / Marş Anahtarı", kw: ["kontak", "anahtar", "dugme"] },
{
slug: "elektrik-modul",
name: "Modül / Gösterge / Elektronik",
kw: ["modul", "beyin", "ecu", "kontrol unite", "kumanda", "gosterge", "anten"],
},
{
slug: "elektrik-kablo",
name: "Kablo / Tesisat",
kw: ["kablo", "tesisat", "soket", "demeti"],
},
hw("elektrik"),
],
aydinlatma: [
{ slug: "ayd-far", name: "Far", kw: ["far", "gunduz far", "xenon", "led far"] },
{ slug: "ayd-stop", name: "Stop Lambası", kw: ["stop", "arka lamba", "geri vites lamba"] },
{ slug: "ayd-sinyal", name: "Sinyal", kw: ["sinyal", "flasor"] },
{ slug: "ayd-sis", name: "Sis Farı", kw: ["sis"] },
{ slug: "ayd-plaka", name: "Plaka Lambası", kw: ["plaka"] },
{ slug: "ayd-ic", name: "İç / Tavan Lambası", kw: ["tavan", "ic aydinlatma", "harita"] },
{ slug: "ayd-ampul", name: "Ampul", kw: ["ampul"] },
],
"dis-kaporta": [
{ slug: "disk-kapi", name: "Kapı / Menteşe / Kilit", kw: ["kapi", "mentese", "kilit"] },
{ slug: "disk-ayna", name: "Dikiz Aynası", kw: ["ayna", "dikiz"] },
{ slug: "disk-cam", name: "Cam / Kriko / Fitil", kw: ["cam", "kriko", "fitil"] },
{ slug: "disk-bagaj", name: "Bagaj Kapağı", kw: ["bagaj"] },
{ slug: "disk-kaput", name: "Motor Kaputu", kw: ["kaput", "kaputu"] },
{ slug: "disk-kol", name: "Kapı Kolu", kw: ["kol", "kolu"] },
],
"panjur-panel": [
{ slug: "panjur-camurluk", name: "Çamurluk / Kanat", kw: ["camurluk", "kanat"] },
{ slug: "panjur-kaput", name: "Kaput / Kaporta", kw: ["kaput", "kaporta"] },
{ slug: "panjur-tavan", name: "Tavan", kw: ["tavan"] },
{ slug: "panjur-sasi", name: "Şasi / Kiriş / Alt Panel", kw: ["sasi", "kiris", "alt panel"] },
{ slug: "panjur-cam", name: "Cam / Karo", kw: ["cam", "karo"] },
{ slug: "panjur-panjur", name: "Panjur / Ön Panel", kw: ["panjur", "panel", "izgara"] },
{ slug: "panjur-marspiyel", name: "Marşpiyel", kw: ["marspiyel"] },
{
slug: "panjur-farcerceve",
name: "Far Çerçevesi / Sacı",
kw: ["far cerceve", "far kasi", "far saci", "far yuva"],
},
{ slug: "panjur-travers", name: "Travers / Takviye", kw: ["travers", "takviye"] },
{
slug: "panjur-etek",
name: "Etek Sacı / Muhafaza",
kw: ["etek", "muhafaza", "pacalik", "davlumbaz"],
},
],
tampon: [
{ slug: "tampon-tampon", name: "Tampon", kw: ["tampon"] },
{ slug: "tampon-demir", name: "Tampon Demiri / Travers", kw: ["demir", "demiri", "travers"] },
{ slug: "tampon-izgara", name: "Izgara / Panjur", kw: ["izgara", "panjur"] },
{ slug: "tampon-braket", name: "Braket / Sac", kw: ["braket", "sac", "muhafaza"] },
{ slug: "tampon-cita", name: ıta / Kaplama", kw: ["cita", "kaplama", "nikelaj"] },
],
"ic-doseme": [
{ slug: "ic-koltuk", name: "Koltuk / Kılıf", kw: ["koltuk", "kilif", "minder", "sirtlik"] },
{ slug: "ic-torpido", name: "Torpido / Konsol", kw: ["torpido", "konsol"] },
{ slug: "ic-tavan", name: "Tavan Döşeme / Güneşlik", kw: ["tavan", "guneslik"] },
{ slug: "ic-kemer", name: "Emniyet Kemeri", kw: ["kemer", "emniyet"] },
{ slug: "ic-kolcak", name: "Kolçak / Bardaklık", kw: ["kolcak", "bardaklik"] },
{ slug: "ic-paspas", name: "Paspas / Halı", kw: ["paspas", "hali"] },
],
"yaglar-sivilar": [
{ slug: "yag-motor", name: "Motor Yağı", kw: ["motor yag"] },
{
slug: "yag-sanziman",
name: "Şanzıman / Dif. Yağı",
kw: ["sanziman yag", "diferansiyel yag", "torpido yag"],
},
{ slug: "yag-fren", name: "Fren Hidroliği", kw: ["fren hidrolik", "hidrolik yag"] },
{ slug: "yag-direksiyon", name: "Direksiyon Yağı", kw: ["direksiyon yag"] },
{ slug: "yag-antifriz", name: "Antifriz", kw: ["antifriz"] },
{ slug: "yag-gres", name: "Gres", kw: ["gres"] },
{ slug: "yag-adblue", name: "AdBlue", kw: ["adblue", "ad blue"] },
{ slug: "yag-katki", name: "Katkı / Sprey", kw: ["katki", "sprey", "temizleyici", "cila"] },
// oil-SYSTEM hardware that lands in this bucket via "yağ" (pump/dipstick/
// line/lubrication) — no fluid leaf fits, so its own leaf.
{
slug: "yag-sistemi",
name: "Yağlama Sistemi (Pompa/Tava/Keçe)",
kw: [
"yaglama",
"yag pompa",
"yag cubugu",
"yag boru",
"yag karter",
"yag tava",
"yag kece",
"yag basinc",
"gonderici",
"pnomatik",
"hidrolik",
],
},
],
"jant-lastik": [
{ slug: "jant-jant", name: "Jant", kw: ["jant"] },
// slug "jant-tire" (not "jant-lastik") — the latter collides with the top
// bucket slug and the seed's ON CONFLICT would drop it.
{ slug: "jant-tire", name: "Lastik", kw: ["lastik", "tyre", "tire"] },
{ slug: "jant-kapak", name: "Jant Kapağı / Bijon", kw: ["kapak", "bijon", "logo"] },
{ slug: "jant-sibop", name: "Sibop", kw: ["sibop"] },
{ slug: "jant-rulman", name: "Teker Rulmanı / Göbek", kw: ["rulman", "gobek", "porya", "hub"] },
{ slug: "jant-zincir", name: "Kar Zinciri", kw: ["zincir", "kar corabi"] },
],
};
const NORMALIZE = (s: string) => foldName(s);
// Map a folded category name to a sub-leaf within its top bucket. Returns the
// sub-leaf slug or null (→ served at top level, completed manually in SP).
export function mapToSubcategory(rawName: string, bucketSlug: string): string | null {
const leaves = SUB_LEXICON[bucketSlug];
if (!leaves) return null;
const n = NORMALIZE(rawName);
if (!n) return null;
for (const leaf of leaves) {
for (const k of leaf.kw) {
if (n.includes(k)) return leaf.slug;
}
}
return null;
}
// All sub-leaves flattened, with their parent bucket slug — used by the seed
// migration builder and the backfill materializer.
export function allSubLeaves(): Array<{
bucket: string;
slug: string;
name: string;
sort: number;
}> {
const out: Array<{ bucket: string; slug: string; name: string; sort: number }> = [];
for (const [bucket, leaves] of Object.entries(SUB_LEXICON)) {
leaves.forEach((l, i) => out.push({ bucket, slug: l.slug, name: l.name, sort: i + 1 }));
}
return out;
}

View File

@@ -1,19 +1,31 @@
import { Inject, Module, type OnModuleDestroy, type OnModuleInit } from "@nestjs/common"; import { Inject, Module, type OnModuleDestroy, type OnModuleInit } from "@nestjs/common";
import { Queue } from "bullmq"; import { Queue } from "bullmq";
import { CategoriesModule } from "../categories/categories.module"; import { CategoriesModule } from "../categories/categories.module";
import { isCatalogBackfillEnabled, isLifecycleEmailEnabled } from "./prefetch-utils"; import { isLifecycleEmailEnabled } from "./prefetch-utils";
import { PrefetchWorkerService } from "./prefetch-worker.service"; import { PrefetchWorkerService } from "./prefetch-worker.service";
import { import {
CANONICAL_BACKFILL_QUEUE,
CanonicalBackfillQueueProvider,
} from "./queues/canonical-backfill.queue";
import {
CATALOG_PREFETCH_FAST_QUEUE,
CATALOG_PREFETCH_QUEUE, CATALOG_PREFETCH_QUEUE,
CatalogPrefetchFastQueueProvider,
CatalogPrefetchQueueProvider, CatalogPrefetchQueueProvider,
} from "./queues/catalog-prefetch.queue"; } from "./queues/catalog-prefetch.queue";
import { EMEX_SCRAPE_QUEUE, EmexScrapeQueueProvider } from "./queues/emex-scrape.queue"; import { EMEX_SCRAPE_QUEUE, EmexScrapeQueueProvider } from "./queues/emex-scrape.queue";
import { EXPERT_REWARDS_QUEUE, ExpertRewardsQueueProvider } from "./queues/expert-rewards.queue";
import { LIFECYCLE_EMAIL_QUEUE, LifecycleEmailQueueProvider } from "./queues/lifecycle-email.queue"; import { LIFECYCLE_EMAIL_QUEUE, LifecycleEmailQueueProvider } from "./queues/lifecycle-email.queue";
import {
PART_PRICE_REFRESH_QUEUE,
PartPriceRefreshQueueProvider,
} from "./queues/part-price-refresh.queue";
import { QUERY_CLEANUP_QUEUE, QueryCleanupQueueProvider } from "./queues/query-cleanup.queue"; import { QUERY_CLEANUP_QUEUE, QueryCleanupQueueProvider } from "./queues/query-cleanup.queue";
import { import {
SUBSCRIPTION_EXPIRY_QUEUE, SUBSCRIPTION_EXPIRY_QUEUE,
SubscriptionExpiryQueueProvider, SubscriptionExpiryQueueProvider,
} from "./queues/subscription-expiry.queue"; } from "./queues/subscription-expiry.queue";
import { VINPIN_DECODE_QUEUE, VinpinDecodeQueueProvider } from "./queues/vinpin-decode.queue";
@Module({ @Module({
imports: [CategoriesModule], imports: [CategoriesModule],
@@ -22,7 +34,12 @@ import {
SubscriptionExpiryQueueProvider, SubscriptionExpiryQueueProvider,
QueryCleanupQueueProvider, QueryCleanupQueueProvider,
CatalogPrefetchQueueProvider, CatalogPrefetchQueueProvider,
CatalogPrefetchFastQueueProvider,
LifecycleEmailQueueProvider, LifecycleEmailQueueProvider,
ExpertRewardsQueueProvider,
PartPriceRefreshQueueProvider,
VinpinDecodeQueueProvider,
CanonicalBackfillQueueProvider,
PrefetchWorkerService, PrefetchWorkerService,
], ],
exports: [ exports: [
@@ -30,7 +47,12 @@ import {
SUBSCRIPTION_EXPIRY_QUEUE, SUBSCRIPTION_EXPIRY_QUEUE,
QUERY_CLEANUP_QUEUE, QUERY_CLEANUP_QUEUE,
CATALOG_PREFETCH_QUEUE, CATALOG_PREFETCH_QUEUE,
CATALOG_PREFETCH_FAST_QUEUE,
LIFECYCLE_EMAIL_QUEUE, LIFECYCLE_EMAIL_QUEUE,
EXPERT_REWARDS_QUEUE,
PART_PRICE_REFRESH_QUEUE,
VINPIN_DECODE_QUEUE,
CANONICAL_BACKFILL_QUEUE,
], ],
}) })
export class JobsModule implements OnModuleInit, OnModuleDestroy { export class JobsModule implements OnModuleInit, OnModuleDestroy {
@@ -39,6 +61,9 @@ export class JobsModule implements OnModuleInit, OnModuleDestroy {
@Inject(QUERY_CLEANUP_QUEUE) private queryCleanupQueue: Queue, @Inject(QUERY_CLEANUP_QUEUE) private queryCleanupQueue: Queue,
@Inject(CATALOG_PREFETCH_QUEUE) private catalogPrefetchQueue: Queue, @Inject(CATALOG_PREFETCH_QUEUE) private catalogPrefetchQueue: Queue,
@Inject(LIFECYCLE_EMAIL_QUEUE) private lifecycleEmailQueue: Queue, @Inject(LIFECYCLE_EMAIL_QUEUE) private lifecycleEmailQueue: Queue,
@Inject(EXPERT_REWARDS_QUEUE) private expertRewardsQueue: Queue,
@Inject(PART_PRICE_REFRESH_QUEUE) private partPriceRefreshQueue: Queue,
@Inject(CANONICAL_BACKFILL_QUEUE) private canonicalBackfillQueue: Queue,
) {} ) {}
async onModuleInit() { async onModuleInit() {
@@ -100,30 +125,79 @@ export class JobsModule implements OnModuleInit, OnModuleDestroy {
console.log("[jobs] Skipped lifecycle-email cron (not prod host)"); console.log("[jobs] Skipped lifecycle-email cron (not prod host)");
} }
// Catalog backfill: every hour at :00. Scans for decoded vehicles whose catalog // Catalog backfill now runs on an IN-PROCESS hourly timer inside
// isn't fully prefetched and queues them. Self-throttled (queue-depth guard), // PrefetchWorkerService — NOT a BullMQ cron. A cron marker is a delayed job that
// cooldown- and business-hours-aware — handled by PrefetchWorkerService. // BullMQ promotes to the wait-list HEAD with LPUSH (lifo ignored), and the worker
// PRODUCTION (sase.tr) ONLY: dev.sase.tr is a separate DB and must not // pops from the tail, so behind a deep prefetch backlog the scan was buried for
// sweep/scrape. NODE_ENV is "production" on BOTH, so gate on the prod host. // days and never fired. Remove any previously-registered cron scheduler here so
if (isCatalogBackfillEnabled()) { // stale markers stop being produced (the in-process timer self-gates on the prod
await this.catalogPrefetchQueue.upsertJobScheduler( // host). The "backfill-scan" job handler is kept so any already-queued legacy
"catalog-backfill-hourly", // marker still runs harmlessly when reached.
{ pattern: "0 * * * *" }, await this.catalogPrefetchQueue.removeJobScheduler("catalog-backfill-hourly").catch(() => {});
console.log("[jobs] catalog-backfill cron removed (scan runs in-process)");
// Parça Uzmanları sezon kapanışı: her ayın 1'i 00:00 Türkiye saati —
// biten ayın ilk 3'ünü admin@sase.tr'ye mailler (üyelik günlerini admin
// elle tanımlar) + denetim satırı yazar. Dev'de de çalışır; dev maili
// "[dev]" konu önekiyle ayrışır, (period, rank) unique index'i aynı
// dönem için ikinci maili/satırı engeller.
await this.expertRewardsQueue.upsertJobScheduler(
"expert-rewards-monthly",
{ pattern: "0 0 1 * *", tz: "Europe/Istanbul" },
{ {
name: "backfill-scan", name: "expert-rewards-grant",
data: {}, data: {},
opts: { opts: {
removeOnComplete: { count: 48 }, removeOnComplete: { count: 24 },
removeOnFail: { count: 50 },
},
},
);
console.log("[jobs] Registered expert-rewards cron: 0 0 1 * * (Europe/Istanbul)");
// Tedarikçi fiyat tazeleme: her gün 19:30 İstanbul — takip MySQL'inin
// 18:00 CEST (19:00 İstanbul) sync'i ~5 dk'da bitiyor; sonrasında izlenen
// parça kodlarına bugünün p50/p95/p99 satırı yazılır. Dev'de de çalışır:
// pg yazımları kendi DB'sine, MySQL tarafı idempotent INSERT IGNORE.
// SUPPLIER_PRICE_DB_* env'i yoksa processor sessiz no-op.
await this.partPriceRefreshQueue.upsertJobScheduler(
"part-price-refresh-daily",
{ pattern: "30 19 * * *", tz: "Europe/Istanbul" },
{
name: "part-price-refresh-run",
data: {},
opts: {
removeOnComplete: { count: 30 },
removeOnFail: { count: 100 }, removeOnFail: { count: 100 },
}, },
}, },
); );
console.log("[jobs] Registered catalog-backfill cron: 0 * * * *"); console.log("[jobs] Registered part-price-refresh cron: 30 19 * * * (Europe/Istanbul)");
} else {
// Clean up any stale scheduler (e.g. if NODE_ENV changed) and stay idle. // Kanonik taksonomi tazeleme: her gün 05:00 İstanbul (gecelik trafik
await this.catalogPrefetchQueue.removeJobScheduler("catalog-backfill-hourly").catch(() => {}); // sonrası, decode edilen yeni araçların kategorileri materyalize edilsin).
console.log("[jobs] Skipped catalog-backfill cron (not prod host)"); // incremental=true → yalnız o günün YENİ kategori adları sınıflandırılır
} // (tüm ~113k değil); materialize adımı differential, inheritance NULL-only,
// is_manual satırları korunur. Serving zaten anlık lexicon fallback yapıyor,
// bu cron materyalizasyonu güncel tutar (istek-başı hesap yükünü ve DB
// doğruluğunu iyileştirir). Dev + prod'da güvenle çalışır (harici yan-etki
// yok, kendi DB'sine yazar). Lexicon iyileştirmesi sonrası tam yeniden
// eşleme hâlâ ELLE `{ reset: true }` global run'la yapılır.
await this.canonicalBackfillQueue.upsertJobScheduler(
"canonical-backfill-daily",
{ pattern: "0 5 * * *", tz: "Europe/Istanbul" },
{
name: "canonical-backfill-run",
data: { incremental: true },
opts: {
removeOnComplete: { count: 14 },
removeOnFail: { count: 50 },
},
},
);
console.log(
"[jobs] Registered canonical-backfill cron: 0 5 * * * (Europe/Istanbul, incremental)",
);
} }
async onModuleDestroy() { async onModuleDestroy() {
@@ -132,6 +206,9 @@ export class JobsModule implements OnModuleInit, OnModuleDestroy {
this.queryCleanupQueue.close(), this.queryCleanupQueue.close(),
this.catalogPrefetchQueue.close(), this.catalogPrefetchQueue.close(),
this.lifecycleEmailQueue.close(), this.lifecycleEmailQueue.close(),
this.expertRewardsQueue.close(),
this.partPriceRefreshQueue.close(),
this.canonicalBackfillQueue.close(),
]); ]);
} }
} }

View File

@@ -50,7 +50,7 @@ export function isLifecycleEmailEnabled(): boolean {
export class RateLimitError extends Error { export class RateLimitError extends Error {
constructor( constructor(
public readonly retryAfterMs: number, public readonly retryAfterMs: number,
public readonly cause: "cooldown" | "time-window", public readonly cause: "cooldown" | "time-window" | "source-rate",
) { ) {
super(`Rate limited (${cause}) — retry after ${retryAfterMs}ms`); super(`Rate limited (${cause}) — retry after ${retryAfterMs}ms`);
this.name = "RateLimitError"; this.name = "RateLimitError";
@@ -86,21 +86,21 @@ function currentIstanbulHour(): number {
/** /**
* Whether `source` may be scraped right now. * Whether `source` may be scraped right now.
* PL24 → 09:0018:00 (Europe/Istanbul); upstream rate-limit is still *
* tighter outside that window so user-facing requests benefit from the * Time windows are DISABLED by default — PL24 (and everything else) may be
* guard. EMEX, parts-catalogs, and everything else have no window * scraped 24/7. The window is still env-tunable: set PREFETCH_PL24_START /
* (always true). parts-catalogs used to be 09:0019:00 too, but the * PREFETCH_PL24_END (Europe/Istanbul hours) to re-narrow it (e.g.
* Redis-persisted warm JWT pool (commits aa4d055 + dcf7e06) now keeps * PREFETCH_PL24_START=9 PREFETCH_PL24_END=18). EMEX and parts-catalogs never
* captures alive 24/7 so backfill can sweep through the night when the * had a window — their Redis-persisted warm JWT pool keeps captures alive
* upstream is most idle. * through the night.
*/ */
/** PL24 scrape window (Europe/Istanbul hours), env-tunable so it can be widened const PL24_WINDOW_START = Number(process.env.PREFETCH_PL24_START) || 0;
* without a redeploy (e.g. PREFETCH_PL24_START=8 PREFETCH_PL24_END=22). */ const PL24_WINDOW_END = Number(process.env.PREFETCH_PL24_END) || 24;
const PL24_WINDOW_START = Number(process.env.PREFETCH_PL24_START) || 9;
const PL24_WINDOW_END = Number(process.env.PREFETCH_PL24_END) || 18;
export function isWithinTimeWindow(source: string): boolean { export function isWithinTimeWindow(source: string): boolean {
if (source !== "pl24") return true; if (source !== "pl24") return true;
// Default (024) means the window is disabled → PL24 flows 24/7.
if (PL24_WINDOW_START <= 0 && PL24_WINDOW_END >= 24) return true;
const h = currentIstanbulHour(); const h = currentIstanbulHour();
return h >= PL24_WINDOW_START && h < PL24_WINDOW_END; return h >= PL24_WINDOW_START && h < PL24_WINDOW_END;
} }

View File

@@ -0,0 +1,304 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { PrefetchWorkerService } from "./prefetch-worker.service";
/**
* Chainable drizzle mock: select/from/where/orderBy return the chain; the
* terminal limit() yields the next queued result array. Mirrors the trick the
* other api specs use (await on a plain array resolves to the array itself).
*/
function makeDb(limitResults: unknown[][]) {
const queued = [...limitResults];
const chain: Record<string, unknown> = {};
for (const m of ["select", "from", "where", "orderBy"]) {
chain[m] = vi.fn(() => chain);
}
chain.limit = vi.fn(() => queued.shift() ?? []);
return chain;
}
function makeDeps(opts: { waiting: number; limitResults: unknown[][] }) {
const queue = {
// typed args so `.mock.calls[i]` is `unknown[]` (not a 0-length tuple) — the
// production `nest build` compiles spec files and rejects tuple-index access.
add: vi.fn((..._args: unknown[]) => Promise.resolve(undefined)),
getJobCounts: vi.fn(async () => ({ waiting: opts.waiting, delayed: 0, active: 0 })),
};
const redis = {
// typed args (like queue.add) so mockImplementation((k)=>…) type-checks under
// the production nest build, which compiles spec files.
exists: vi.fn(async (..._args: unknown[]) => false), // no cooldown / guard / complete marker
get: vi.fn(async () => null), // no no-result residue
set: vi.fn(async (..._args: unknown[]) => undefined),
del: vi.fn(async (..._args: unknown[]) => undefined),
incr: vi.fn(async (..._args: unknown[]) => 1), // per-source rate window counter
expire: vi.fn(async () => undefined),
ttl: vi.fn(async () => -2), // checkCooldown: no cooldown key
setNx: vi.fn(async () => true), // scan lock acquired by default
getJson: vi.fn(async (..._args: unknown[]): Promise<unknown> => null), // cursor / progress empty
setJson: vi.fn(async () => undefined),
};
const posthog = { payload: vi.fn(async () => ({})) }; // compiled-in defaults
const db = makeDb(opts.limitResults);
const fastQueue = { add: vi.fn(async (..._args: unknown[]) => ({ id: "fastjob" })) };
const service = new PrefetchWorkerService(
queue as never,
fastQueue as never,
{} as never, // categoriesService — unused by the scan
redis as never,
posthog as never,
db as never,
);
return { service, queue, fastQueue, redis, posthog, db };
}
describe("PrefetchWorkerService — fast lane (lifo) + backlog gating", () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.CATALOG_BACKFILL_ENABLED = "true"; // force the prod-host gate on
});
afterEach(() => {
process.env.CATALOG_BACKFILL_ENABLED = undefined;
});
describe("enqueueInit", () => {
it("sets lifo + data.fast when fast (fast queue)", async () => {
const { service, fastQueue } = makeDeps({ waiting: 0, limitResults: [] });
await (
service as never as { enqueueInit: (a: string, b: string, c: boolean) => Promise<void> }
).enqueueInit("v1", "emex", true);
const [name, data, jobOpts] = fastQueue.add.mock.calls[0];
expect(name).toBe("prefetch-init");
expect(data).toMatchObject({ vehicleId: "v1", source: "emex", fast: true });
expect(jobOpts).toMatchObject({ lifo: true });
});
it("omits lifo when not fast", async () => {
const { service, queue } = makeDeps({ waiting: 0, limitResults: [] });
await (
service as never as { enqueueInit: (a: string, b: string, c: boolean) => Promise<void> }
).enqueueInit("v1", "emex", false);
const [, data, jobOpts] = queue.add.mock.calls[0];
expect(data).toMatchObject({ fast: false });
expect((jobOpts as { lifo?: boolean }).lifo).toBeUndefined();
});
});
describe("addJob (child jobs)", () => {
it("sets lifo when data.fast is true (fast queue)", async () => {
const { service, fastQueue: queue } = makeDeps({ waiting: 0, limitResults: [] });
await (service as never as { addJob: (n: string, d: unknown) => Promise<void> }).addJob(
"prefetch-children",
{
vehicleId: "v1",
categoryId: "c1",
source: "emex",
action: "children",
depth: 1,
fast: true,
},
);
const [, , jobOpts] = queue.add.mock.calls[0];
expect(jobOpts).toMatchObject({ lifo: true });
});
it("omits lifo when data.fast is falsy", async () => {
const { service, queue } = makeDeps({ waiting: 0, limitResults: [] });
await (service as never as { addJob: (n: string, d: unknown) => Promise<void> }).addJob(
"prefetch-children",
{ vehicleId: "v1", categoryId: "c1", source: "emex", action: "children", depth: 1 },
);
const [, , jobOpts] = queue.add.mock.calls[0];
expect((jobOpts as { lifo?: boolean }).lifo).toBeUndefined();
});
});
describe("processBackfillScan", () => {
it("runs Phase-1 in the fast lane even when the backlog is far over the ceiling", async () => {
// waiting 50k ≫ default maxBacklog 1000 → Phase-2 must be suspended, but a
// zero-parts vehicle must still be onboarded with lifo.
const {
service,
fastQueue: queue,
redis,
} = makeDeps({
waiting: 50_000,
limitResults: [[{ id: "empty1", source: "emex" }]], // Phase-1 query result
});
await (
service as never as { processBackfillScan: () => Promise<void> }
).processBackfillScan();
expect(queue.add).toHaveBeenCalledTimes(1);
const [name, data, jobOpts] = queue.add.mock.calls[0];
expect(name).toBe("prefetch-init");
expect(data).toMatchObject({ vehicleId: "empty1", fast: true });
expect(jobOpts).toMatchObject({ lifo: true });
// Phase-2 suspended → the rolling cursor is never read or advanced.
expect(redis.getJson).not.toHaveBeenCalled();
expect(redis.setJson).not.toHaveBeenCalled();
});
it("runs Phase-2 (normal lane, no lifo) when the backlog is under the ceiling", async () => {
// waiting 10 < 1000 → Phase-1 empty, Phase-2 picks a vehicle with fast=false.
const { service, queue, redis } = makeDeps({
waiting: 10,
limitResults: [
[], // Phase-1: no zero-parts vehicles
[{ id: "stale1", source: "emex", createdAt: new Date("2026-01-01T00:00:00Z") }], // Phase-2
],
});
await (
service as never as { processBackfillScan: () => Promise<void> }
).processBackfillScan();
expect(queue.add).toHaveBeenCalledTimes(1);
const [, data, jobOpts] = queue.add.mock.calls[0];
expect(data).toMatchObject({ vehicleId: "stale1", fast: false });
expect((jobOpts as { lifo?: boolean }).lifo).toBeUndefined();
// Phase-2 ran → cursor advanced.
expect(redis.setJson).toHaveBeenCalled();
});
it("skips a vehicle already marked complete (Phase-2 does not re-walk it)", async () => {
const { service, queue, redis } = makeDeps({
waiting: 10, // under ceiling → Phase-2 active
limitResults: [
[], // Phase-1: no zero-parts vehicles
[{ id: "done1", source: "emex", createdAt: new Date("2026-01-01T00:00:00Z") }], // Phase-2
],
});
// scheduled guard absent (false), but the complete marker is present.
redis.exists.mockImplementation(async (...a: unknown[]) =>
String(a[0]).includes("prefetch:complete:"),
);
await (
service as never as { processBackfillScan: () => Promise<void> }
).processBackfillScan();
expect(queue.add).not.toHaveBeenCalled();
});
it("skips entirely when the backfill gate is off (not prod host)", async () => {
process.env.CATALOG_BACKFILL_ENABLED = "false";
const { service, queue } = makeDeps({ waiting: 0, limitResults: [] });
await (
service as never as { processBackfillScan: () => Promise<void> }
).processBackfillScan();
expect(queue.add).not.toHaveBeenCalled();
});
});
describe("runBackfillScan (in-process timer entry, NX-locked)", () => {
it("runs the scan when the lock is acquired", async () => {
const {
service,
fastQueue: queue,
redis,
} = makeDeps({
waiting: 50_000,
limitResults: [[{ id: "empty1", source: "emex" }]],
});
await (service as never as { runBackfillScan: () => Promise<void> }).runBackfillScan();
expect(redis.setNx).toHaveBeenCalledWith("prefetch:backfill:lock", "1", expect.any(Number));
expect(queue.add).toHaveBeenCalledTimes(1); // Phase-1 init enqueued (fast lane)
});
it("skips the scan when the lock is already held", async () => {
const { service, queue, redis } = makeDeps({ waiting: 50_000, limitResults: [] });
redis.setNx.mockResolvedValueOnce(false);
await (service as never as { runBackfillScan: () => Promise<void> }).runBackfillScan();
expect(queue.add).not.toHaveBeenCalled();
});
});
describe("checkSourceRate — per-source rate limit", () => {
type CSR = { checkSourceRate: (s: string) => Promise<void> };
it("passes when under the source ceiling", async () => {
const { service, redis } = makeDeps({ waiting: 0, limitResults: [] });
redis.incr.mockResolvedValueOnce(1);
await expect((service as never as CSR).checkSourceRate("pl24")).resolves.toBeUndefined();
});
it("throws a source-rate RateLimitError once over the ceiling", async () => {
const { service, redis } = makeDeps({ waiting: 0, limitResults: [] });
redis.incr.mockResolvedValueOnce(21); // pl24 default ceiling is 20
await expect((service as never as CSR).checkSourceRate("pl24")).rejects.toMatchObject({
cause: "source-rate",
});
});
it("is unlimited (no counter) for a source without a configured ceiling", async () => {
const { service, redis } = makeDeps({ waiting: 0, limitResults: [] });
await (service as never as CSR).checkSourceRate("unknown-source");
expect(redis.incr).not.toHaveBeenCalled();
});
});
describe("poison guard (Faz 6: category cap — brand-agnostic anti-explosion)", () => {
type PC = { processChildren: (j: unknown) => Promise<void> };
it("processChildren marks poison once progress.total exceeds CATEGORY_CAP", async () => {
const { service, queue, redis } = makeDeps({ waiting: 0, limitResults: [] });
redis.getJson.mockImplementation(async (...a: unknown[]) =>
String(a[0]).includes("progress") ? { total: 5000 } : null,
);
await (service as never as PC).processChildren({
data: { vehicleId: "op1", categoryId: "c1", source: "pl24", depth: 1 },
} as never);
const poisonSet = redis.set.mock.calls.some((c) =>
String(c[0]).includes("prefetch:poison:op1"),
);
expect(poisonSet).toBe(true);
expect(queue.add).not.toHaveBeenCalled(); // stopped drilling
});
it("scan skips a poison-marked vehicle", async () => {
const { service, queue, redis } = makeDeps({
waiting: 10,
limitResults: [
[],
[{ id: "op1", source: "pl24", createdAt: new Date("2026-01-01T00:00:00Z") }],
],
});
redis.exists.mockImplementation(async (...a: unknown[]) =>
String(a[0]).includes("prefetch:poison:"),
);
await (
service as never as { processBackfillScan: () => Promise<void> }
).processBackfillScan();
expect(queue.add).not.toHaveBeenCalled();
});
});
describe("queueCategoryJob — depth cap (no-op guard)", () => {
// pl24 linkPath with no leaf marker (/bom/, /partinfo/ …) → treated as a
// non-leaf folder that would normally queue a prefetch-children job.
const nonLeaf = {
id: "c1",
linkPath: "/catalog/groups/x",
source: "pl24",
unavailable: false,
hasSubgroups: false,
};
type QCJ = {
queueCategoryJob: (c: unknown, v: string, s: string, d: number, f: boolean) => Promise<void>;
};
it("does NOT enqueue a children job at depth >= MAX_DEPTH (avoids a no-op)", async () => {
const { service, queue } = makeDeps({ waiting: 0, limitResults: [] });
// depth 12 == default MAX_DEPTH → processChildren would early-return, so the
// job must never be created.
await (service as never as QCJ).queueCategoryJob(nonLeaf, "v1", "pl24", 12, false);
expect(queue.add).not.toHaveBeenCalled();
});
it("enqueues a children job for a non-leaf within the cap", async () => {
// childCheck query (.limit(1)) returns no rows → enqueue a children fetch.
const { service, queue } = makeDeps({ waiting: 0, limitResults: [[]] });
await (service as never as QCJ).queueCategoryJob(nonLeaf, "v1", "pl24", 1, false);
expect(queue.add).toHaveBeenCalledTimes(1);
expect(queue.add.mock.calls[0][0]).toBe("prefetch-children");
});
});
});

View File

@@ -24,9 +24,24 @@ import {
updateProgress, updateProgress,
} from "./prefetch-utils"; } from "./prefetch-utils";
import { PrefetchCategoryJobData, PrefetchInitJobData } from "./prefetch.types"; import { PrefetchCategoryJobData, PrefetchInitJobData } from "./prefetch.types";
import { CATALOG_PREFETCH_QUEUE } from "./queues/catalog-prefetch.queue"; import {
CATALOG_PREFETCH_FAST_QUEUE,
CATALOG_PREFETCH_QUEUE,
} from "./queues/catalog-prefetch.queue";
const MAX_DEPTH = 5; /**
* Depth cap for the prefetch BFS — a safety ceiling, NOT the target depth.
*
* Goal is COMPLETE coverage: every decoded vehicle's full category tree + all
* parts in the DB so viewing is instant (DB read). The BFS terminates naturally
* at real leaves (isLeafLinkPath); this cap only guards against pathological /
* cyclic trees running away. Real PL24/EMEX/pcat trees are ~5-7 deep, so 12 is
* comfortably beyond every genuine leaf while still bounding a runaway.
* Efficiency is handled separately: queueCategoryJob never enqueues a children
* job past the cap (no early-return no-ops), and completed vehicles are skipped
* by the scan. Env-tunable (PREFETCH_MAX_DEPTH).
*/
const MAX_DEPTH = Number(process.env.PREFETCH_MAX_DEPTH) || 12;
// ── Backfill scan (hourly cron) tuning ── // ── Backfill scan (hourly cron) tuning ──
/** /**
@@ -45,20 +60,49 @@ const BACKFILL_SOURCES = ["pl24", "emex", "parts-catalogs"];
/** Redis key holding the rolling rescan cursor (last createdAt seen). */ /** Redis key holding the rolling rescan cursor (last createdAt seen). */
const BACKFILL_CURSOR_KEY = "prefetch:backfill:cursor"; const BACKFILL_CURSOR_KEY = "prefetch:backfill:cursor";
// ── In-process scan timer (replaces the BullMQ cron, which gets buried) ──
/** How often the in-process backfill scan runs. */
const BACKFILL_SCAN_INTERVAL_MS = 60 * 60 * 1000;
/** Delay before the first scan after boot, so startup settles first. */
const BACKFILL_SCAN_KICKOFF_MS = 60 * 1000;
/** NX-lock TTL (seconds) — just under the interval so each cycle re-acquires. */
const BACKFILL_SCAN_LOCK_TTL_S = 55 * 60;
// ── Throughput knobs (env-tunable so prod can ramp without a redeploy) ── // ── Throughput knobs (env-tunable so prod can ramp without a redeploy) ──
/** /**
* Concurrent jobs the worker runs. emex/pl24 fetches parallelise well, so >1 * Concurrent jobs the worker runs. emex/pl24 fetches parallelise well, so >1
* lets a slow parts-catalogs job stop head-of-line-blocking emex. A user-active * lets a slow parts-catalogs job stop head-of-line-blocking emex. A user-active
* cooldown still pauses the whole worker, so this only speeds up idle periods. * cooldown still pauses the whole worker, so this only speeds up idle periods.
*/ */
const WORKER_CONCURRENCY = Number(process.env.PREFETCH_CONCURRENCY) || 3; const WORKER_CONCURRENCY = Number(process.env.PREFETCH_CONCURRENCY) || 8;
/** Global queue rate ceiling (jobs per minute). Was a hard 5/min. */ /**
const WORKER_RATE_MAX = Number(process.env.PREFETCH_RATE_MAX) || 20; * Global queue rate ceiling (jobs/min) across ALL sources — now a SAFETY CAP,
* not the real throttle. Per-source limits (below) do the actual pacing so a
* slow/limited source can't starve the others. Defaults to the sum of the
* per-source ceilings + headroom. Was a hard 5/min, then a shared 20/min.
*/
const WORKER_RATE_MAX = Number(process.env.PREFETCH_RATE_MAX) || 180;
/**
* Per-source rate ceilings (jobs/min). Each source is throttled INDEPENDENTLY
* (Redis fixed-window) so PL24, EMEX and parts-catalogs run concurrently at
* their own safe rates instead of fighting over one global budget. 0 = unlimited
* for that source. PL24 stays conservative (upstream ban risk); raise it only
* with rotating residential proxies. pcat is already paced by PCAT_PACE_MS.
*/
const SOURCE_RATE_MAX: Record<string, number> = {
pl24: Number(process.env.PREFETCH_RATE_PL24) || 20,
emex: Number(process.env.PREFETCH_RATE_EMEX) || 20,
// pcat runs through a DataImpulse rotating gateway (74.81.81.81:10000-10999,
// ~1000 port-rotated exit IPs — verified 2026-06-25), so per-IP ban load is
// spread; safe to run well above the old shared cap. Ramp step 2 = 90/min
// (2026-07-08: 93k backlog %89 pcat idi — 20/min + 5s pace drenaja yetmiyordu).
"parts-catalogs": Number(process.env.PREFETCH_RATE_PCAT) || 90,
};
/** /**
* Per-job pacing for parts-catalogs only (its browser/JWT capture is heavy). * Per-job pacing for parts-catalogs only (its browser/JWT capture is heavy).
* Set 0 to disable. Other sources are paced by the limiter + cooldown alone. * Set 0 to disable. Other sources are paced by the limiter + cooldown alone.
*/ */
const PCAT_PACE_MS = Number(process.env.PREFETCH_PCAT_DELAY_MS) || 15_000; const PCAT_PACE_MS = Number(process.env.PREFETCH_PCAT_DELAY_MS) || 1_500;
// ── Phase-1 residue exclusion ── // ── Phase-1 residue exclusion ──
/** /**
@@ -70,14 +114,36 @@ const NORESULT_MAX_ATTEMPTS = Number(process.env.PREFETCH_NORESULT_MAX) || 2;
/** TTL for the no-result counter — excluded vehicles retry after this, so a later /** TTL for the no-result counter — excluded vehicles retry after this, so a later
* catalog fix eventually re-fills them (default 7 days). */ * catalog fix eventually re-fills them (default 7 days). */
const NORESULT_TTL_S = (Number(process.env.PREFETCH_NORESULT_TTL_DAYS) || 7) * 86_400; const NORESULT_TTL_S = (Number(process.env.PREFETCH_NORESULT_TTL_DAYS) || 7) * 86_400;
/** TTL for the "fully fetched" marker. A vehicle whose whole prefetch chain
* finished WITH parts is marked complete and skipped by the Phase-2 rescan, so
* the scan stops re-walking finished trees (the old behaviour churned the queue
* forever). The TTL re-validates periodically so a later upstream catalog change
* is eventually picked up (default 21 days). Env-tunable. */
const COMPLETE_TTL_S = (Number(process.env.PREFETCH_COMPLETE_TTL_DAYS) || 21) * 86_400;
/**
* Per-vehicle category ceiling — anti-poison guard. A correctly-decoded PL24
* vehicle has ~100-800 categories (p95 744). A vehicle whose tree blows past
* this is almost always a decode that failed to resolve the model and landed on
* the generic ROOT catalog (the whole brand universe — e.g. 5 Opels with
* model="Opel" produced 420k/102k/21k categories and ZERO parts). Once a
* vehicle's discovered tree exceeds this, stop drilling it and mark it poison so
* it never gets re-picked. Env-tunable. */
const CATEGORY_CAP = Number(process.env.PREFETCH_CATEGORY_CAP) || 3000;
/** TTL for the poison marker (generic-model / over-cap vehicles). Long, because
* the underlying cause is a decode bug — re-validate monthly. */
const POISON_TTL_S = (Number(process.env.PREFETCH_POISON_TTL_DAYS) || 30) * 86_400;
@Injectable() @Injectable()
export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy { export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(PrefetchWorkerService.name); private readonly logger = new Logger(PrefetchWorkerService.name);
private worker: Worker | null = null; private worker: Worker | null = null;
private fastWorker?: Worker;
private backfillKickoff?: ReturnType<typeof setTimeout>;
private backfillInterval?: ReturnType<typeof setInterval>;
constructor( constructor(
@Inject(CATALOG_PREFETCH_QUEUE) private queue: Queue, @Inject(CATALOG_PREFETCH_QUEUE) private queue: Queue,
@Inject(CATALOG_PREFETCH_FAST_QUEUE) private fastQueue: Queue,
private categoriesService: CategoriesService, private categoriesService: CategoriesService,
private redis: RedisService, private redis: RedisService,
private posthog: PostHogService, private posthog: PostHogService,
@@ -107,21 +173,90 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
this.logger.error(`[prefetch] Worker error: ${err.message}`); this.logger.error(`[prefetch] Worker error: ${err.message}`);
}); });
this.logger.log( // FAST lane: own queue + worker so fresh decodes never compete with the
`[prefetch] Worker started (concurrency=${WORKER_CONCURRENCY}, ${WORKER_RATE_MAX} jobs/min, pcatPace=${PCAT_PACE_MS}ms)`, // deep-drill backlog (whose production>consumption regime starves lifo tails).
// Same process fn; the shared per-source redis rate gate still protects
// upstream across BOTH workers.
this.fastWorker = new Worker(
QUEUE_NAMES.CATALOG_PREFETCH_FAST,
(job, token) => backfillContext.run(true, () => this.process(job, token)),
{
connection: getBullConnection(),
concurrency: 4,
limiter: { max: 120, duration: 60_000 },
},
); );
this.fastWorker.on("failed", (job, err) => {
this.logger.warn(`[prefetch:fast] Job ${job?.name} failed: ${err.message}`);
});
this.fastWorker.on("error", (err) => {
this.logger.error(`[prefetch:fast] Worker error: ${err.message}`);
});
this.logger.log("[prefetch] Fast worker started (concurrency=4, 120/min)");
this.logger.log(
`[prefetch] Worker started (concurrency=${WORKER_CONCURRENCY}, global ${WORKER_RATE_MAX}/min, ` +
`per-source ${JSON.stringify(SOURCE_RATE_MAX)}, pcatPace=${PCAT_PACE_MS}ms)`,
);
// Hourly backfill scan — run IN-PROCESS, not as a BullMQ cron job. A cron
// scheduler enqueues a delayed marker; BullMQ promotes delayed jobs to the
// wait-list HEAD with LPUSH (lifo is ignored on delayed promotion) while the
// worker pops from the tail, so behind a deep prefetch backlog the scan marker
// is buried for days and never fires. An in-process timer sidesteps the queue
// entirely; the scan's Phase-1 then enqueues `lifo` init jobs that DO jump the
// wait list. Prod-host gated (same as the scan body); single-fired via a Redis
// NX lock so restarts / multiple instances don't double-scan.
if (isCatalogBackfillEnabled()) {
const run = () =>
backfillContext.run(true, () =>
this.runBackfillScan().catch((err) =>
this.logger.error(`[backfill] scan crashed: ${(err as Error).message}`),
),
);
this.backfillKickoff = setTimeout(run, BACKFILL_SCAN_KICKOFF_MS);
this.backfillInterval = setInterval(run, BACKFILL_SCAN_INTERVAL_MS);
this.logger.log("[backfill] in-process hourly scan enabled");
}
} }
async onModuleDestroy() { async onModuleDestroy() {
if (this.backfillKickoff) clearTimeout(this.backfillKickoff);
if (this.backfillInterval) clearInterval(this.backfillInterval);
if (this.worker) { if (this.worker) {
await this.fastWorker?.close();
await this.worker.close(); await this.worker.close();
this.worker = null; this.worker = null;
} }
} }
/**
* Hourly scan entry point used by the in-process timer. A Redis NX lock keeps it
* single-fire across restarts and (future) multiple instances; processBackfillScan
* is idempotent, so a missed cycle is harmless.
*/
private async runBackfillScan(): Promise<void> {
const got = await this.redis.setNx("prefetch:backfill:lock", "1", BACKFILL_SCAN_LOCK_TTL_S);
if (!got) return;
await this.processBackfillScan();
}
private async process(job: Job, token?: string): Promise<void> { private async process(job: Job, token?: string): Promise<void> {
try { try {
const data = job.data as { source?: string }; const data = job.data as { source?: string };
// Per-source rate gate FIRST (before the pcat pace) so we don't burn the 15s
// sleep on a job we're about to defer. Scan jobs are exempt.
if (
data.source &&
(job.name === "prefetch-init" ||
job.name === "prefetch-children" ||
job.name === "prefetch-parts")
) {
await this.checkSourceRate(
data.source,
(job.data as { fast?: boolean }).fast ? "fast" : "main",
);
}
if (data.source === "parts-catalogs" && PCAT_PACE_MS > 0) { if (data.source === "parts-catalogs" && PCAT_PACE_MS > 0) {
await new Promise((r) => setTimeout(r, PCAT_PACE_MS)); await new Promise((r) => setTimeout(r, PCAT_PACE_MS));
} }
@@ -149,14 +284,16 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
// global wait is correct: when it ends and cooldown is still active, // global wait is correct: when it ends and cooldown is still active,
// we'll pause again — at most ~1 cycle per retryAfterMs instead of // we'll pause again — at most ~1 cycle per retryAfterMs instead of
// cycling every queued job through. // cycling every queued job through.
await this.worker?.rateLimit(delayMs); const lane = (job.data as { fast?: boolean }).fast ? this.fastWorker : this.worker;
await lane?.rateLimit(delayMs);
this.logger.debug(`[prefetch] Worker paused ${delayMs}ms (cooldown ${job.data.source})`); this.logger.debug(`[prefetch] Worker paused ${delayMs}ms (cooldown ${job.data.source})`);
throw Worker.RateLimitError(); throw Worker.RateLimitError();
} }
// Time-window deferral can be hours (msUntilNext9AM). Per-job defer so // time-window (off-hours, up to hours) and source-rate (this source hit
// EMEX (no window) keeps flowing while PL24/pcat jobs sleep till 09:00. // its per-minute ceiling, ~sub-minute) both defer PER-JOB so other sources
// keep flowing — only cooldown pauses the whole worker.
await job.moveToDelayed(Date.now() + delayMs, token); await job.moveToDelayed(Date.now() + delayMs, token);
this.logger.debug(`[prefetch] Job ${job.name} deferred ${delayMs}ms (off-hours)`); this.logger.debug(`[prefetch] Job ${job.name} deferred ${delayMs}ms (${err.cause})`);
throw new DelayedError(); throw new DelayedError();
} }
throw err; throw err;
@@ -167,12 +304,18 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
* Init job: walk the category tree for a vehicle and queue sub-jobs. * Init job: walk the category tree for a vehicle and queue sub-jobs.
*/ */
private async processInit(job: Job<PrefetchInitJobData>): Promise<void> { private async processInit(job: Job<PrefetchInitJobData>): Promise<void> {
const { vehicleId, source } = job.data; const { vehicleId, source, fast = false } = job.data;
this.logger.log(`[prefetch] Init for vehicle=${vehicleId}, source=${source}`); this.logger.log(`[prefetch] Init for vehicle=${vehicleId}, source=${source}`);
await checkCooldown(this.redis, source); await checkCooldown(this.redis, source);
checkTimeWindow(source); checkTimeWindow(source);
// Already flagged as poison (tree exceeded CATEGORY_CAP on a prior run) — skip.
if (await this.redis.exists(this.poisonKey(vehicleId))) {
this.logger.debug(`[prefetch] Skip poison vehicle=${vehicleId}`);
return;
}
// Verify vehicle still exists // Verify vehicle still exists
const [vehicle] = await this.db const [vehicle] = await this.db
.select({ id: vehicles.id }) .select({ id: vehicles.id })
@@ -185,7 +328,7 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
return; return;
} }
await initProgress(this.redis, vehicleId); await initProgress(this.redis, vehicleId); // resets progress.total — the cap's tree-size proxy
// Get all top-level categories for this vehicle // Get all top-level categories for this vehicle
let topCategories = await this.db let topCategories = await this.db
@@ -244,7 +387,7 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
for (const child of children) { for (const child of children) {
if (child.unavailable) continue; if (child.unavailable) continue;
await this.queueCategoryJob(child, vehicleId, source, 1); await this.queueCategoryJob(child, vehicleId, source, 1, fast);
queued++; queued++;
} }
} else if (this.isLeafLinkPath(cat.linkPath, cat.source, cat.hasSubgroups)) { } else if (this.isLeafLinkPath(cat.linkPath, cat.source, cat.hasSubgroups)) {
@@ -262,6 +405,7 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
source, source,
action: "parts" as const, action: "parts" as const,
depth: 0, depth: 0,
fast,
}); });
queued++; queued++;
} }
@@ -273,6 +417,7 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
source, source,
action: "children" as const, action: "children" as const,
depth: 0, depth: 0,
fast,
}); });
queued++; queued++;
} }
@@ -286,7 +431,7 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
* Fetch children (sub-categories) for a category. * Fetch children (sub-categories) for a category.
*/ */
private async processChildren(job: Job<PrefetchCategoryJobData>): Promise<void> { private async processChildren(job: Job<PrefetchCategoryJobData>): Promise<void> {
const { vehicleId, categoryId, source, depth } = job.data; const { vehicleId, categoryId, source, depth, fast = false } = job.data;
this.logger.log(`[prefetch] Children for category=${categoryId}, depth=${depth}`); this.logger.log(`[prefetch] Children for category=${categoryId}, depth=${depth}`);
await checkCooldown(this.redis, source); await checkCooldown(this.redis, source);
@@ -297,13 +442,26 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
return; return;
} }
// Anti-poison cap: progress.total tracks every sub-job queued for this vehicle
// (≈ its discovered tree size). Once it blows past the ceiling the vehicle is a
// generic-catalog explosion — stop drilling and mark it poison so the rest of
// its (part-less) tree is never fetched and it's never re-picked.
const prog = await this.redis.getJson<{ total: number }>(`prefetch:progress:${vehicleId}`);
if ((prog?.total ?? 0) >= CATEGORY_CAP) {
this.logger.warn(
`[prefetch] Category cap ${CATEGORY_CAP} hit for vehicle=${vehicleId} (tree≈${prog?.total}) — marking poison, stop drilling`,
);
await this.markPoison(vehicleId);
return;
}
try { try {
const children = await this.categoriesService.getChildren(categoryId); const children = await this.categoriesService.getChildren(categoryId);
let queued = 0; let queued = 0;
for (const child of children) { for (const child of children) {
if (child.unavailable) continue; if (child.unavailable) continue;
await this.queueCategoryJob(child, vehicleId, source, depth + 1); await this.queueCategoryJob(child, vehicleId, source, depth + 1, fast);
queued++; queued++;
} }
@@ -378,12 +536,16 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
? cfg.maxBacklog ? cfg.maxBacklog
: BACKFILL_MAX_BACKLOG; : BACKFILL_MAX_BACKLOG;
// Self-throttle: don't pile on if the queue is already deep — let it drain. // Self-throttle: when the queue is already deep, suspend only the Phase-2
// rolling rescan (the part that piles on). Phase-1 still runs every wave so
// genuinely-empty vehicles keep getting onboarded through the fast lane even
// while a large deep-drill backlog is still draining — otherwise a single
// backlog spike freezes new-vehicle coverage until the whole queue clears.
const counts = await this.queue.getJobCounts("waiting", "delayed", "active"); const counts = await this.queue.getJobCounts("waiting", "delayed", "active");
const backlog = (counts.waiting ?? 0) + (counts.delayed ?? 0) + (counts.active ?? 0); const backlog = (counts.waiting ?? 0) + (counts.delayed ?? 0) + (counts.active ?? 0);
if (backlog > maxBacklog) { const phase2Allowed = backlog <= maxBacklog;
this.logger.log(`[backfill] Skip — queue backlog ${backlog} > ${maxBacklog}`); if (!phase2Allowed) {
return; this.logger.log(`[backfill] Backlog ${backlog} > ${maxBacklog} — Phase-1 (fast lane) only`);
} }
// Only target sources eligible right now: not in cooldown (user active) and // Only target sources eligible right now: not in cooldown (user active) and
@@ -399,19 +561,29 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
return; return;
} }
const picked: Array<{ id: string; source: string }> = []; const picked: Array<{ id: string; source: string; fast: boolean }> = [];
const seen = new Set<string>(); const seen = new Set<string>();
const overfetch = batchSize * 4; // headroom for in-flight skips const overfetch = batchSize * 4; // headroom for in-flight skips
const tryPick = async (v: { id: string; source: string | null }): Promise<void> => { const tryPick = async (
v: { id: string; source: string | null },
fast: boolean,
): Promise<void> => {
if (picked.length >= batchSize || seen.has(v.id) || !v.source) return; if (picked.length >= batchSize || seen.has(v.id) || !v.source) return;
if (await this.redis.exists(`prefetch:scheduled:${v.id}`)) return; // already in flight if (await this.redis.exists(`prefetch:scheduled:${v.id}`)) return; // already in flight
// Skip exhausted residue: vehicles whose prefetch keeps finishing with zero // Skip exhausted residue: vehicles whose prefetch keeps finishing with zero
// parts (no catalog data). They'd otherwise be re-picked every wave forever. // parts (no catalog data). They'd otherwise be re-picked every wave forever.
const noResult = await this.redis.get(this.noResultKey(v.id)); const noResult = await this.redis.get(this.noResultKey(v.id));
if (noResult && Number(noResult) >= NORESULT_MAX_ATTEMPTS) return; if (noResult && Number(noResult) >= NORESULT_MAX_ATTEMPTS) return;
// Skip vehicles whose full tree is already fetched (marker set on chain
// completion with parts). Stops Phase-2 from re-walking finished vehicles
// every wave; the marker's TTL re-validates them periodically. Phase-1
// (zero-parts) vehicles never carry this marker, so this is a no-op there.
if (await this.redis.exists(this.completeKey(v.id))) return;
// Skip poison vehicles (generic-model / over-cap catalog explosions).
if (await this.redis.exists(this.poisonKey(v.id))) return;
seen.add(v.id); seen.add(v.id);
picked.push({ id: v.id, source: v.source }); picked.push({ id: v.id, source: v.source, fast });
}; };
// Phase 1 — clear the obvious backlog first: decoded vehicles with zero parts. // Phase 1 — clear the obvious backlog first: decoded vehicles with zero parts.
@@ -429,11 +601,12 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
.orderBy(asc(vehicles.createdAt)) .orderBy(asc(vehicles.createdAt))
.limit(overfetch); .limit(overfetch);
for (const v of noParts) await tryPick(v); for (const v of noParts) await tryPick(v, true);
// Phase 2 — rolling rescan of ALL decoded vehicles to gap-fill partially-fetched // Phase 2 — rolling rescan of ALL decoded vehicles to gap-fill partially-fetched
// ones. A createdAt cursor walks forward and wraps around at the end. // ones. A createdAt cursor walks forward and wraps around at the end. Gated by
if (picked.length < batchSize) { // the backlog ceiling (above) so it doesn't pile on while the queue is deep.
if (phase2Allowed && picked.length < batchSize) {
const cursorObj = await this.redis.getJson<{ ts: string }>(BACKFILL_CURSOR_KEY); const cursorObj = await this.redis.getJson<{ ts: string }>(BACKFILL_CURSOR_KEY);
const cursor = cursorObj?.ts ? new Date(cursorObj.ts) : new Date(0); const cursor = cursorObj?.ts ? new Date(cursorObj.ts) : new Date(0);
@@ -456,7 +629,7 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
let lastTs: Date | null = null; let lastTs: Date | null = null;
for (const v of rolling) { for (const v of rolling) {
lastTs = v.createdAt; lastTs = v.createdAt;
await tryPick(v); await tryPick(v, false);
} }
if (lastTs) { if (lastTs) {
await this.redis.setJson(BACKFILL_CURSOR_KEY, { ts: lastTs.toISOString() }, 30 * 86400); await this.redis.setJson(BACKFILL_CURSOR_KEY, { ts: lastTs.toISOString() }, 30 * 86400);
@@ -468,18 +641,27 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
return; return;
} }
for (const v of picked) await this.enqueueInit(v.id, v.source); for (const v of picked) await this.enqueueInit(v.id, v.source, v.fast);
const fastCount = picked.filter((v) => v.fast).length;
this.logger.log( this.logger.log(
`[backfill] Queued ${picked.length} vehicle(s) (sources=${eligible.join(",")}, backlog=${backlog})`, `[backfill] Queued ${picked.length} vehicle(s) (${fastCount} fast-lane, ` +
`sources=${eligible.join(",")}, backlog=${backlog})`,
); );
} }
/** Queue a prefetch-init for a vehicle and set the in-flight guard. */ /** Queue a prefetch-init for a vehicle and set the in-flight guard. */
private async enqueueInit(vehicleId: string, source: string): Promise<void> { private async enqueueInit(vehicleId: string, source: string, fast = false): Promise<void> {
await this.queue.add( const q = fast ? this.fastQueue : this.queue;
await q.add(
"prefetch-init", "prefetch-init",
{ vehicleId, source: source as PrefetchInitJobData["source"] }, { vehicleId, source: source as PrefetchInitJobData["source"], fast },
{ removeOnComplete: { count: 1000 }, removeOnFail: { count: 5000 } }, {
removeOnComplete: { count: 1000 },
removeOnFail: { count: 5000 },
// Fast lane (Phase-1): lifo so a zero-parts vehicle's whole chain jumps
// the deep-drill backlog instead of queueing behind it (see addJob).
...(fast ? { lifo: true } : {}),
},
); );
// Guard cleared on completion (incrementCompleted) or by TTL if the run dies. // Guard cleared on completion (incrementCompleted) or by TTL if the run dies.
await this.redis.set(`prefetch:scheduled:${vehicleId}`, "1", BACKFILL_SCHEDULED_TTL); await this.redis.set(`prefetch:scheduled:${vehicleId}`, "1", BACKFILL_SCHEDULED_TTL);
@@ -498,6 +680,7 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
vehicleId: string, vehicleId: string,
source: string, source: string,
depth: number, depth: number,
fast = false,
): Promise<void> { ): Promise<void> {
if (cat.unavailable) return; if (cat.unavailable) return;
@@ -516,10 +699,14 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
source: source as "pl24" | "emex", source: source as "pl24" | "emex",
action: "parts" as const, action: "parts" as const,
depth, depth,
fast,
}); });
} }
} else if (cat.linkPath) { } else if (cat.linkPath && depth < MAX_DEPTH) {
// Check if children already exist // Non-leaf within the depth cap — explore children. The `depth < MAX_DEPTH`
// gate mirrors processChildren's early-return: without it we'd enqueue a
// prefetch-children job that processChildren just drops, burning a rate-limit
// slot on a no-op (this was ~85% of the queue at MAX_DEPTH=2).
const [childCheck] = await this.db const [childCheck] = await this.db
.select({ id: categories.id }) .select({ id: categories.id })
.from(categories) .from(categories)
@@ -535,7 +722,7 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
for (const child of children) { for (const child of children) {
if (child.unavailable) continue; if (child.unavailable) continue;
await this.queueCategoryJob(child, vehicleId, source, depth + 1); await this.queueCategoryJob(child, vehicleId, source, depth + 1, fast);
} }
} else { } else {
await this.addJob("prefetch-children", { await this.addJob("prefetch-children", {
@@ -544,6 +731,7 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
source: source as "pl24" | "emex", source: source as "pl24" | "emex",
action: "children" as const, action: "children" as const,
depth, depth,
fast,
}); });
} }
} }
@@ -583,7 +771,17 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
// "-" instead. The values are UUIDs — the ID only needs to be deterministic // "-" instead. The values are UUIDs — the ID only needs to be deterministic
// (for dedup), not parseable. // (for dedup), not parseable.
jobId: `prefetch-${data.vehicleId}-${data.categoryId}-${data.action}`, jobId: `prefetch-${data.vehicleId}-${data.categoryId}-${data.action}`,
// Fast lane (Phase-1 / reactive): add with `lifo` so the job RPUSHes to the
// TAIL of the wait list, where BullMQ's RPOPLPUSH picks it next — i.e. ahead
// of the deep deep-drill backlog already sitting in wait. (BullMQ 5 drains
// the wait list before the prioritized ZSET, so `priority` would do the
// OPPOSITE here and starve the job behind the backlog; lifo is correct.)
...(data.fast ? { lifo: true } : {}),
}; };
if (data.fast) {
await this.fastQueue.add(name, data, opts);
return;
}
// parts-catalogs pacing is handled per-job in process() (PCAT_PACE_MS) + the // parts-catalogs pacing is handled per-job in process() (PCAT_PACE_MS) + the
// limiter. The old cumulative `index * 20s` delay was pathological (the Nth // limiter. The old cumulative `index * 20s` delay was pathological (the Nth
@@ -617,7 +815,15 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
.from(parts) .from(parts)
.where(eq(parts.vehicleId, vehicleId)) .where(eq(parts.vehicleId, vehicleId))
.limit(1); .limit(1);
if (!hasPart) await this.markNoResult(vehicleId); if (hasPart) {
// Fully fetched with parts → mark complete so the Phase-2 rescan skips it
// (re-validates after the TTL). A chain with any failed job never reaches
// isFinished, so partially-fetched vehicles are never marked — they keep
// getting gap-filled.
await this.redis.set(this.completeKey(vehicleId), "1", COMPLETE_TTL_S);
} else {
await this.markNoResult(vehicleId);
}
// Clean up Redis keys — data is in PostgreSQL now // Clean up Redis keys — data is in PostgreSQL now
await this.redis.del(`prefetch:scheduled:${vehicleId}`); await this.redis.del(`prefetch:scheduled:${vehicleId}`);
await this.redis.del(`prefetch:progress:${vehicleId}`); await this.redis.del(`prefetch:progress:${vehicleId}`);
@@ -636,6 +842,50 @@ export class PrefetchWorkerService implements OnModuleInit, OnModuleDestroy {
return `prefetch:noresult:${vehicleId}`; return `prefetch:noresult:${vehicleId}`;
} }
private completeKey(vehicleId: string): string {
return `prefetch:complete:${vehicleId}`;
}
private poisonKey(vehicleId: string): string {
return `prefetch:poison:${vehicleId}`;
}
/** Flag a vehicle as poison (its tree blew past CATEGORY_CAP — a generic
* root-catalog explosion from a decode that didn't resolve the model) so
* init/scan skip it, and clear its in-flight + progress state. Brand-agnostic:
* only an actual explosion trips it, so small legit generic-model PSA vehicles
* (which stay ~200 categories and DO yield parts) are never affected. */
private async markPoison(vehicleId: string): Promise<void> {
await this.redis.set(this.poisonKey(vehicleId), "1", POISON_TTL_S);
await this.redis.del(`prefetch:scheduled:${vehicleId}`);
await this.redis.del(`prefetch:progress:${vehicleId}`);
}
/**
* Per-source fixed-window (60s) rate limit. Increments the source's window
* counter; once it exceeds the source's ceiling, throws RateLimitError
* ("source-rate") so process() defers THIS job to the window's end while other
* sources keep flowing. 0 / unknown source ceiling = unlimited.
*/
private async checkSourceRate(source: string, lane: "main" | "fast" = "main"): Promise<void> {
// Per-LANE budgets: the main worker's deep-drill backlog would otherwise eat
// the whole per-source window and starve the fast lane with 18s defers
// (observed: fresh BMW init couldn't get a single pcat slot). Totals per
// source stay the same as before, so upstream load is unchanged.
const total = SOURCE_RATE_MAX[source] ?? 0;
if (total <= 0) return;
const fastShare = Math.max(1, Math.floor(total / 3));
const max = lane === "fast" ? fastShare : total - fastShare;
const windowMs = 60_000;
const now = Date.now();
const key = `prefetch:rate:${lane}:${source}:${Math.floor(now / windowMs)}`;
const n = await this.redis.incr(key);
if (n === 1) await this.redis.expire(key, 61);
if (n > max) {
throw new RateLimitError(windowMs - (now % windowMs) + 50, "source-rate");
}
}
/** Record that a backfill attempt finished with the vehicle still at zero parts. */ /** Record that a backfill attempt finished with the vehicle still at zero parts. */
private async markNoResult(vehicleId: string): Promise<void> { private async markNoResult(vehicleId: string): Promise<void> {
const key = this.noResultKey(vehicleId); const key = this.noResultKey(vehicleId);

View File

@@ -4,6 +4,13 @@ export type PrefetchSource = "pl24" | "emex" | "parts-catalogs";
export interface PrefetchInitJobData { export interface PrefetchInitJobData {
vehicleId: string; vehicleId: string;
source: PrefetchSource; source: PrefetchSource;
/**
* Fast lane: enqueue this job (and its whole sub-job chain) with BullMQ `lifo`
* so it lands at the tail of the wait list and is picked before the deep
* deep-drill backlog. Set for Phase-1 (zero-parts) backfill so newly-decoded
* vehicles aren't starved behind the rolling rescan. See PrefetchWorkerService.
*/
fast?: boolean;
} }
/** Per-category job: fetches children OR parts */ /** Per-category job: fetches children OR parts */
@@ -13,4 +20,6 @@ export interface PrefetchCategoryJobData {
source: PrefetchSource; source: PrefetchSource;
action: "children" | "parts"; action: "children" | "parts";
depth: number; depth: number;
/** Inherited from the init job — keeps the whole chain in the fast lane. */
fast?: boolean;
} }

View File

@@ -0,0 +1,346 @@
import { Job } from "bullmq";
import { inArray, sql } from "drizzle-orm";
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
import { NORMALIZE_SQL, classifyNode, mapToCanonical } from "../canonical-lexicon";
import { mapToSubcategory } from "../canonical-sublexicon";
type Database = PostgresJsDatabase<Record<string, unknown>>;
export interface CanonicalBackfillJobData {
// Limit input to these brands (matched via catalog_vehicles/vehicles). Empty
// = all brands. The map rows written are brand-agnostic (keyed by name), so
// scoping only limits WHICH names are considered this run — safe to widen later.
brands?: string[];
// Limit to these sources (pl24 | emex | parts-catalogs). Empty = all.
sources?: string[];
// Compute + log coverage but write nothing.
dryRun?: boolean;
// Full rebuild of the non-manual keyword layer: before re-mapping, NULL the
// materialized ids and DELETE stale non-manual map rows in scope, so names
// that no longer map (lexicon/classifier changed) don't leave dangling rows.
// Manual rows survive. ONLY honored for a GLOBAL run (no brands): the map is
// name-level/brand-agnostic, so a brand-scoped delete would wipe mappings
// shared with other brands. Use this for the periodic full re-run.
reset?: boolean;
// Keep-fresh mode for the nightly cron: only classify names that have NO map
// row yet (the day's newly-decoded categories), instead of re-classifying all
// ~113k. The materialize step is differential anyway, so a brand-new category
// whose name already maps still gets its id set. Does NOT re-apply lexicon
// tuning to already-mapped names — that's what a manual `reset` run is for.
// Ignored when `reset` is set (a reset intends a full re-map).
incremental?: boolean;
}
export interface CanonicalBackfillStats {
distinctNames: number;
partCategories: number;
structural: number;
junk: number;
mapped: number;
subMapped: number;
ambiguous: number;
categoriesUpdated: number;
subcategoriesUpdated: number;
inherited: number;
mapRowsPruned: number;
dryRun: boolean;
}
// The bare `name` in NORMALIZE_SQL resolves to categories.name in every query
// below (catalog_vehicles/vehicles have no `name` column), so folding is
// identical on both the SELECT that builds the map and the UPDATE join.
const INSERT_CHUNK = 500;
export async function processCanonicalBackfill(
job: Job,
db: Database,
): Promise<CanonicalBackfillStats> {
const data = (job.data ?? {}) as CanonicalBackfillJobData;
const brands = data.brands?.length ? data.brands : null;
const sources = data.sources?.length ? data.sources : null;
const dryRun = data.dryRun === true;
const reset = data.reset === true && !brands;
const incremental = data.incremental === true && !reset;
if (data.reset === true && brands) {
console.warn(
"[canonical-backfill] reset ignored: only honored for a global run (map is name-level/brand-agnostic)",
);
}
console.log(
`[canonical-backfill] job ${job.id} start (brands=${brands ?? "ALL"}, sources=${sources ?? "ALL"}, dryRun=${dryRun}, reset=${reset})`,
);
// 1. canonical slug → id (seeded by migration 0024)
const canon = await db.execute<{ id: string; slug: string }>(
sql`SELECT id, slug FROM canonical_categories`,
);
const slugToId = new Map<string, string>();
for (const r of canon) slugToId.set(r.slug, r.id);
if (slugToId.size === 0) {
throw new Error(
"[canonical-backfill] canonical_categories is empty — migration 0024 seed missing",
);
}
// inArray expands to `expr IN ($1,$2,…)` — driver-safe across postgres-js,
// unlike `= ANY($1)` which needs the param bound as a Postgres array.
const sourceCond = sources ? sql`AND ${inArray(sql`c.source`, sources)}` : sql``;
// Brand scope via a correlated EXISTS on c.id — pl24 nodes hang off
// catalog_vehicles, emex/pcat off vehicles, so coalesce covers both. EXISTS
// (not a FROM-join) is used in the UPDATE because Postgres forbids referencing
// the UPDATE target inside its FROM-list join conditions.
const brandExists = brands
? sql`AND EXISTS (
SELECT 1 FROM categories c2
LEFT JOIN catalog_vehicles cv ON c2.catalog_vehicle_id = cv.id
LEFT JOIN vehicles v ON c2.vehicle_id = v.id
WHERE c2.id = c.id AND ${inArray(sql`coalesce(cv.brand_name, v.brand_name)`, brands)}
)`
: sql``;
// 2. distinct (folded name, source) in scope
const brandJoinFilter = brands
? sql`AND ${inArray(sql`coalesce(cv.brand_name, v.brand_name)`, brands)}`
: sql``;
// Incremental keep-fresh: skip names already in the map — only the day's new
// names are (re)classified. Materialize (step 5) still fixes any category
// whose name maps but whose own id is stale/null, so nothing new is missed.
const incrementalCond = incremental
? sql`AND NOT EXISTS (
SELECT 1 FROM category_canonical_map m
WHERE m.source = c.source AND m.normalized_name = ${sql.raw(NORMALIZE_SQL)}
)`
: sql``;
const names = await db.execute<{ folded: string; source: string }>(sql`
SELECT DISTINCT ${sql.raw(NORMALIZE_SQL)} AS folded, c.source AS source
FROM categories c
LEFT JOIN catalog_vehicles cv ON c.catalog_vehicle_id = cv.id
LEFT JOIN vehicles v ON c.vehicle_id = v.id
WHERE true ${brandJoinFilter} ${sourceCond} ${incrementalCond}
`);
// 3. classify + keyword-map
const rows: {
source: string;
folded: string;
canonicalId: string;
subcategoryId: string | null;
ambiguous: boolean;
confidence: number;
}[] = [];
const stats: CanonicalBackfillStats = {
distinctNames: names.length,
partCategories: 0,
structural: 0,
junk: 0,
mapped: 0,
subMapped: 0,
ambiguous: 0,
categoriesUpdated: 0,
subcategoriesUpdated: 0,
inherited: 0,
mapRowsPruned: 0,
dryRun,
};
for (const { folded, source } of names) {
const kind = classifyNode(folded);
if (kind === "junk") {
stats.junk++;
continue;
}
if (kind === "structural") {
stats.structural++;
continue;
}
stats.partCategories++;
const { slug, ambiguous, confidence } = mapToCanonical(folded);
if (!slug) continue;
const canonicalId = slugToId.get(slug);
if (!canonicalId) continue; // slug not seeded — skip defensively
stats.mapped++;
if (ambiguous) stats.ambiguous++;
// second level: name→bucket→sub is deterministic, so resolve it here.
const subSlug = mapToSubcategory(folded, slug);
const subcategoryId = subSlug ? (slugToId.get(subSlug) ?? null) : null;
if (subcategoryId) stats.subMapped++;
rows.push({ source, folded, canonicalId, subcategoryId, ambiguous, confidence });
}
console.log(
`[canonical-backfill] ${stats.distinctNames} names → part=${stats.partCategories} structural=${stats.structural} junk=${stats.junk} | mapped=${stats.mapped} (ambiguous=${stats.ambiguous})`,
);
if (dryRun) {
console.log("[canonical-backfill] dryRun — no writes");
return stats;
}
// 3b. reset (global only): clear the materialized ids + prune non-manual map
// rows in scope so stale mappings from an older lexicon/classifier disappear.
if (reset) {
const mapSourceCond = sources ? sql`AND ${inArray(sql`source`, sources)}` : sql``;
await db.execute(sql`
UPDATE categories c SET canonical_category_id = NULL, canonical_subcategory_id = NULL
WHERE (canonical_category_id IS NOT NULL OR canonical_subcategory_id IS NOT NULL) ${sourceCond}
`);
const pruned = await db.execute<{ n: number }>(sql`
WITH d AS (
DELETE FROM category_canonical_map
WHERE is_manual = false ${mapSourceCond}
RETURNING 1
)
SELECT count(*)::int AS n FROM d
`);
stats.mapRowsPruned = pruned[0]?.n ?? 0;
console.log(
`[canonical-backfill] reset — cleared materialized ids in scope, pruned ${stats.mapRowsPruned} non-manual map rows`,
);
}
// 4. upsert name-level map rows (chunked). is_manual rows are protected: the
// conflict target updates only when the existing row was not human-approved.
for (let i = 0; i < rows.length; i += INSERT_CHUNK) {
const chunk = rows.slice(i, i + INSERT_CHUNK);
const values = chunk.map(
(r) =>
sql`(${r.source}, ${r.folded}, ${r.canonicalId}, ${r.subcategoryId}, 'keyword', ${r.confidence}, ${r.ambiguous})`,
);
await db.execute(sql`
INSERT INTO category_canonical_map
(source, normalized_name, canonical_category_id, canonical_subcategory_id, method, confidence, ambiguous)
VALUES ${sql.join(values, sql`, `)}
ON CONFLICT (source, normalized_name) DO UPDATE SET
canonical_category_id = EXCLUDED.canonical_category_id,
canonical_subcategory_id = EXCLUDED.canonical_subcategory_id,
method = EXCLUDED.method,
confidence = EXCLUDED.confidence,
ambiguous = EXCLUDED.ambiguous,
updated_at = now()
WHERE category_canonical_map.is_manual = false
`);
}
// 5. materialize categories.canonical_category_id + _subcategory_id from the
// map (scoped join). Both levels come from the same name-keyed row.
const updated = await db.execute<{ n: number }>(sql`
WITH upd AS (
UPDATE categories c
SET canonical_category_id = m.canonical_category_id,
canonical_subcategory_id = m.canonical_subcategory_id
FROM category_canonical_map m
WHERE m.source = c.source
AND m.normalized_name = ${sql.raw(NORMALIZE_SQL)}
AND (c.canonical_category_id IS DISTINCT FROM m.canonical_category_id
OR c.canonical_subcategory_id IS DISTINCT FROM m.canonical_subcategory_id)
${sourceCond} ${brandExists}
RETURNING 1
)
SELECT count(*)::int AS n FROM upd
`);
stats.categoriesUpdated = updated[0]?.n ?? 0;
// 5b. parent-inheritance: a category whose own name didn't map inherits its
// parent's bucket. Handles code/plate-named illustration leaves under a
// descriptive parent (Renault P5 "Alternatör" → X1318_… ; any figure leaf).
// Cascade a few levels (deepest OEM trees are ~5).
//
// EXCEPT vehicle-selector nodes (year-range "… 2004 - 2014" / model-code
// "A04 ASTRA-H"): these are NOT parts, so inheriting a part bucket into them
// is wrong AND cascades that wrong bucket onto their generic children
// (Opel "BOYALAR"/"MÜTEFERRİK" under a model node were landing in Debriyaj).
// Blocking the selector node from receiving keeps it null, which breaks the
// whole downstream chain. Code/plate leaves (X1318_, 20-201A) don't match
// these patterns, so Renault/Hyundai inheritance is untouched.
for (let depth = 0; depth < 5; depth++) {
const inh = await db.execute<{ n: number }>(sql`
WITH upd AS (
UPDATE categories c
SET canonical_category_id = p.canonical_category_id,
canonical_subcategory_id = p.canonical_subcategory_id
FROM categories p
WHERE c.parent_id = p.id
AND c.canonical_category_id IS NULL
AND p.canonical_category_id IS NOT NULL
AND c.name !~ '(19|20)[0-9]{2} - (19|20)[0-9]{2}'
AND c.name !~* '^[a-z]{1,3}[0-9]{2} '
${sourceCond} ${brandExists}
RETURNING 1
)
SELECT count(*)::int AS n FROM upd
`);
const n = inh[0]?.n ?? 0;
stats.inherited += n;
if (n === 0) break;
}
// subcategory coverage: how many mapped categories in scope now carry a sub.
const subCount = await db.execute<{ n: number }>(sql`
SELECT count(*)::int AS n FROM categories c
WHERE c.canonical_subcategory_id IS NOT NULL ${sourceCond} ${brandExists}
`);
stats.subcategoriesUpdated = subCount[0]?.n ?? 0;
// 6. rebuild the per (source, brand) structure template — the shared canonical
// skeleton the serving overlays so a freshly-decoded vehicle shows the FULL
// structure instantly (before its own deep-drill). Derived from all mapped
// categories, so only on a GLOBAL run (brand-scoped runs see a partial world).
if (!brands && !dryRun) {
await db.execute(sql`DELETE FROM canonical_structure_template`);
const tpl = await db.execute<{ n: number }>(sql`
WITH ins AS (
INSERT INTO canonical_structure_template
(source, brand, canonical_category_id, canonical_subcategory_id, vehicle_count)
SELECT c.source,
coalesce(cv.brand_name, v.brand_name) AS brand,
c.canonical_category_id,
c.canonical_subcategory_id,
count(DISTINCT coalesce(c.catalog_vehicle_id, c.vehicle_id))::int
FROM categories c
LEFT JOIN catalog_vehicles cv ON c.catalog_vehicle_id = cv.id
LEFT JOIN vehicles v ON c.vehicle_id = v.id
WHERE c.canonical_category_id IS NOT NULL
AND coalesce(cv.brand_name, v.brand_name) IS NOT NULL
GROUP BY c.source, coalesce(cv.brand_name, v.brand_name),
c.canonical_category_id, c.canonical_subcategory_id
RETURNING 1
)
SELECT count(*)::int AS n FROM ins
`);
console.log(`[canonical-backfill] structure template rebuilt — ${tpl[0]?.n ?? 0} rows`);
// Entry-level template: the ENTRY names (bucket entry-point children — same
// level buildCanonicalInput serves) shared across the brand's vehicles.
// min 2 vehicles so one-off vehicle quirks don't become "the brand".
await db.execute(sql`DELETE FROM canonical_template_entries`);
const tent = await db.execute<{ n: number }>(sql`
WITH ins AS (
INSERT INTO canonical_template_entries
(source, brand, canonical_category_id, canonical_subcategory_id, name, vehicle_count)
SELECT c.source, coalesce(cv.brand_name, v.brand_name),
c.canonical_category_id, c.canonical_subcategory_id,
min(c.name),
count(DISTINCT coalesce(c.catalog_vehicle_id, c.vehicle_id))::int
FROM categories c
LEFT JOIN catalog_vehicles cv ON c.catalog_vehicle_id = cv.id
LEFT JOIN vehicles v ON c.vehicle_id = v.id
LEFT JOIN categories p ON p.id = c.parent_id
WHERE c.canonical_category_id IS NOT NULL
AND coalesce(cv.brand_name, v.brand_name) IS NOT NULL
AND (c.parent_id IS NULL OR p.canonical_category_id IS DISTINCT FROM c.canonical_category_id
OR (p.parent_id IS NULL AND p.canonical_category_id = c.canonical_category_id))
GROUP BY c.source, coalesce(cv.brand_name, v.brand_name),
c.canonical_category_id, c.canonical_subcategory_id,
btrim(regexp_replace(translate(lower(c.name),'çşğıöü','csgiou'),'\\s+',' ','g'))
HAVING count(DISTINCT coalesce(c.catalog_vehicle_id, c.vehicle_id)) >= 2
RETURNING 1
)
SELECT count(*)::int AS n FROM ins
`);
console.log(`[canonical-backfill] template entries rebuilt — ${tent[0]?.n ?? 0} rows`);
}
console.log(
`[canonical-backfill] done — ${rows.length} map rows (${stats.subMapped} with sub, ${stats.mapRowsPruned} pruned), ${stats.categoriesUpdated} materialized, ${stats.inherited} inherited, ${stats.subcategoriesUpdated} sub-categorized`,
);
return stats;
}

View File

@@ -0,0 +1,158 @@
import { Job } from "bullmq";
import { and, asc, desc, eq, gte, lt, sql } from "drizzle-orm";
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
import { oemExpertRewards, oemVotePoints, users } from "../../database/schema/core";
import { EXPERT_REWARD_LADDER, previousTrMonthWindow } from "../../oem-votes/expert-period";
type Database = PostgresJsDatabase<Record<string, unknown>>;
const ADMIN_EMAIL = "admin@sase.tr";
interface SeasonWinner {
rank: number;
days: number;
userId: string;
name: string;
email: string;
points: number;
}
// Parça Uzmanları sezon kapanışı: her ayın 1'i 00:00 TR'de biten ayın ilk 3
// oylayıcısını hesaplar, kazananları admin@sase.tr'ye MAİLLER ve
// oem_expert_rewards'a denetim satırı yazar. Üyelik uzatması OTOMATİK
// YAPILMAZ — günleri (30/15/7) admin maildeki listeye göre elle tanımlar.
// (period, rank) unique index run-once garantisidir. Sıra bilinçli: önce
// mail, sonra satırlar — satırlar önce yazılsaydı mail hatasındaki retry
// alreadyGranted'a takılır, mail hiç gitmezdi (nadir çift mail, kayıp
// mailden iyidir).
export async function processExpertRewards(
job: Job,
db: Database,
): Promise<{ period: string; winners: number; skipped: boolean }> {
const { start, end } = previousTrMonthWindow(new Date());
const periodLabel = start.toISOString();
console.log(`[expert-rewards] Processing job ${job.id} for period ${periodLabel}`);
const [alreadyGranted] = await db
.select({ id: oemExpertRewards.id })
.from(oemExpertRewards)
.where(eq(oemExpertRewards.periodStart, start))
.limit(1);
if (alreadyGranted) {
console.log(`[expert-rewards] Period ${periodLabel} already granted — skipping`);
return { period: periodLabel, winners: 0, skipped: true };
}
// Biten sezonun ilk 3'ü — liderlik tablosuyla aynı sıralama: puan desc,
// eşitlikte puana daha erken ulaşan önde.
const totalPoints = sql<number>`sum(${oemVotePoints.points})::int`;
const top = await db
.select({
userId: oemVotePoints.userId,
name: users.name,
email: users.email,
points: totalPoints,
})
.from(oemVotePoints)
.innerJoin(users, eq(users.id, oemVotePoints.userId))
.where(and(gte(oemVotePoints.createdAt, start), lt(oemVotePoints.createdAt, end)))
.groupBy(oemVotePoints.userId, users.name, users.email)
.orderBy(desc(totalPoints), asc(sql`min(${oemVotePoints.createdAt})`))
.limit(EXPERT_REWARD_LADDER.length);
if (top.length === 0) {
console.log(`[expert-rewards] No votes in period ${periodLabel} — nothing to report`);
return { period: periodLabel, winners: 0, skipped: false };
}
const winners: SeasonWinner[] = top.map((row, i) => ({
rank: EXPERT_REWARD_LADDER[i].rank,
days: EXPERT_REWARD_LADDER[i].days,
userId: row.userId,
name: row.name,
email: row.email,
points: row.points,
}));
await sendWinnersMail(start, winners);
for (const winner of winners) {
await db
.insert(oemExpertRewards)
.values({
periodStart: start,
userId: winner.userId,
rank: winner.rank,
points: winner.points,
rewardDays: winner.days,
})
.onConflictDoNothing();
}
console.log(
`[expert-rewards] Period ${periodLabel}: mailed ${winners.length} winner(s) to ${ADMIN_EMAIL}`,
);
return { period: periodLabel, winners: winners.length, skipped: false };
}
const escapeHtml = (value: string) =>
value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
// Worker Nest DI'sız çalıştığı için EmailService kullanılamıyor — aynı Postal
// HTTP çağrısı (email.service.ts ile aynı payload) env üzerinden yapılır.
async function sendWinnersMail(periodStart: Date, winners: SeasonWinner[]): Promise<void> {
const apiUrl = process.env.POSTAL_API_URL;
const apiKey = process.env.POSTAL_API_KEY;
if (!apiUrl || !apiKey) {
throw new Error("POSTAL_API_URL/POSTAL_API_KEY yok — kazanan maili gönderilemiyor");
}
const fromAddress = process.env.POSTAL_FROM_ADDRESS ?? "noreply@sase.tr";
const fromName = process.env.POSTAL_FROM_NAME ?? "Sase.tr";
const monthLabel = new Intl.DateTimeFormat("tr-TR", {
month: "long",
year: "numeric",
timeZone: "Europe/Istanbul",
}).format(periodStart);
// dev DB'den atılan deneme mailleri prod kapanışlarıyla karışmasın.
const isDev = (process.env.DATABASE_URL ?? "").includes("sase_dev");
const subject = `${isDev ? "[dev] " : ""}Parça Uzmanları ${monthLabel} kazananları — üyelik günlerini tanımlayın`;
const rows = winners
.map(
(w) =>
`<tr><td style="padding:6px 10px">${w.rank}.</td><td style="padding:6px 10px">${escapeHtml(w.name)}</td><td style="padding:6px 10px">${escapeHtml(w.email)}</td><td style="padding:6px 10px;text-align:right">${w.points}</td><td style="padding:6px 10px"><b>${w.days} gün</b></td><td style="padding:6px 10px;font-family:monospace;font-size:12px">${w.userId}</td></tr>`,
)
.join("");
const html = `
<h2>Parça Uzmanları${monthLabel} sezonu kapandı</h2>
<p>Kazananlara üyelik uzatmasını panelden tanımlayın:</p>
<table border="1" cellspacing="0" style="border-collapse:collapse;border-color:#ddd">
<tr><th style="padding:6px 10px">Sıra</th><th style="padding:6px 10px">Ad</th><th style="padding:6px 10px">E-posta</th><th style="padding:6px 10px">Puan</th><th style="padding:6px 10px">Ödül</th><th style="padding:6px 10px">Kullanıcı ID</th></tr>
${rows}
</table>
<p style="color:#888;font-size:12px">Bu mail expert-rewards cron'undan otomatik gönderildi; üyelik günleri otomatik tanımlanmaz.</p>`;
const text = [
`Parça Uzmanları ${monthLabel} kazananları:`,
...winners.map(
(w) => `${w.rank}. ${w.name} <${w.email}> — ${w.points} puan → ${w.days} gün (${w.userId})`,
),
].join("\n");
const response = await fetch(`${apiUrl}/api/v1/send/message`, {
method: "POST",
headers: { "Content-Type": "application/json", "X-Server-API-Key": apiKey },
body: JSON.stringify({
to: [ADMIN_EMAIL],
from: `${fromName} <${fromAddress}>`,
subject,
html_body: html,
plain_body: text,
tag: "expert-rewards",
}),
});
const result = (await response.json()) as { status?: string };
if (result.status !== "success") {
throw new Error(`Postal kazanan maili gönderemedi: ${JSON.stringify(result)}`);
}
}

View File

@@ -67,17 +67,11 @@ async function sendTrialEnding(db: Database, now: Date): Promise<number> {
.innerJoin(users, eq(userSubscriptions.userId, users.id)) .innerJoin(users, eq(userSubscriptions.userId, users.id))
.leftJoin( .leftJoin(
emailPreferences, emailPreferences,
and( and(eq(emailPreferences.userId, users.id), eq(emailPreferences.workflow, "trial-ending")),
eq(emailPreferences.userId, users.id),
eq(emailPreferences.workflow, "trial-ending"),
),
) )
.leftJoin( .leftJoin(
lifecycleEmailSent, lifecycleEmailSent,
and( and(eq(lifecycleEmailSent.userId, users.id), eq(lifecycleEmailSent.workflow, "trial-ending")),
eq(lifecycleEmailSent.userId, users.id),
eq(lifecycleEmailSent.workflow, "trial-ending"),
),
) )
.where( .where(
and( and(
@@ -96,11 +90,7 @@ async function sendTrialEnding(db: Database, now: Date): Promise<number> {
{ subscriberId: r.userId, email: r.email, firstName: firstNameOf(r.name) }, { subscriberId: r.userId, email: r.email, firstName: firstNameOf(r.name) },
{ {
daysLeft: 3, daysLeft: 3,
ctaUrl: buildTrackedUrl( ctaUrl: buildTrackedUrl("trial-ending", r.email, webUrl("/dashboard/subscription")),
"trial-ending",
r.email,
webUrl("/dashboard/subscription"),
),
...(trackPixel ? { trackPixel } : {}), ...(trackPixel ? { trackPixel } : {}),
}, },
); );
@@ -134,17 +124,11 @@ async function sendWinBack(db: Database, now: Date): Promise<number> {
.innerJoin(users, eq(userSubscriptions.userId, users.id)) .innerJoin(users, eq(userSubscriptions.userId, users.id))
.leftJoin( .leftJoin(
emailPreferences, emailPreferences,
and( and(eq(emailPreferences.userId, users.id), eq(emailPreferences.workflow, "win-back")),
eq(emailPreferences.userId, users.id),
eq(emailPreferences.workflow, "win-back"),
),
) )
.leftJoin( .leftJoin(
lifecycleEmailSent, lifecycleEmailSent,
and( and(eq(lifecycleEmailSent.userId, users.id), eq(lifecycleEmailSent.workflow, "win-back")),
eq(lifecycleEmailSent.userId, users.id),
eq(lifecycleEmailSent.workflow, "win-back"),
),
) )
.where( .where(
and( and(

View File

@@ -0,0 +1,131 @@
import { Job } from "bullmq";
import { asc, inArray, sql } from "drizzle-orm";
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
import Redis from "ioredis";
import { partPriceDaily, partPriceTracks } from "../../database/schema/core";
import {
computeStats,
filterOffersForBrand,
istanbulToday,
} from "../../part-prices/part-prices.logic";
import { createSupplierPriceSource } from "../../part-prices/supplier-price-source";
type Database = PostgresJsDatabase<Record<string, unknown>>;
const TRACKED_CAP = 50_000;
const BATCH = 300;
/**
* Günlük tedarikçi fiyat tazeleme (19:30 Europe/Istanbul — takip sync'i
* ~19:05'te biter):
* 1. sku_map artımlı bakım (son 3 günde güncellenen ürünler, INSERT IGNORE).
* 2. İzlenen her (kod, marka) için bugünün p50/p95/p99'unu takip.products'tan
* hesapla (teklifler markaya süzülür — kısa kodlar markalar arası çakışır),
* part_price_daily'ye upsert et (teklif kalmadıysa null-fiyatlı satır —
* "stok yok" da seridir).
* 3. Tazelenen parçaların Redis cache'ini düşür.
* SUPPLIER_PRICE_DB_* yoksa sessiz no-op (dev/prod'da env'le açılır).
*/
export async function processPartPriceRefresh(
job: Job,
db: Database,
): Promise<{ tracked: number; refreshed: number; skipped: boolean }> {
const enabled = process.env.SUPPLIER_PRICE_DB_ENABLED === "true";
const url = process.env.SUPPLIER_PRICE_DB_URL;
if (!enabled || !url) {
console.log(`[part-price-refresh] disabled (enabled=${enabled}, urlSet=${Boolean(url)})`);
return { tracked: 0, refreshed: 0, skipped: true };
}
const source = createSupplierPriceSource(url);
const redis = new Redis({
host: process.env.REDIS_HOST || "localhost",
port: Number(process.env.REDIS_PORT) || 6379,
password: process.env.REDIS_PASSWORD || undefined,
maxRetriesPerRequest: null,
});
try {
console.log(`[part-price-refresh] job ${job.id} starting`);
await source.refreshSkuMap();
const tracked = await db
.select({ codeNorm: partPriceTracks.codeNorm, brandNorm: partPriceTracks.brandNorm })
.from(partPriceTracks)
.where(sql`${partPriceTracks.backfilledAt} IS NOT NULL`)
.orderBy(asc(partPriceTracks.codeNorm), asc(partPriceTracks.brandNorm))
.limit(TRACKED_CAP);
const today = istanbulToday();
let refreshed = 0;
for (let i = 0; i < tracked.length; i += BATCH) {
const chunk = tracked.slice(i, i + BATCH);
const codes = [...new Set(chunk.map((t) => t.codeNorm))];
const rows = await source.fetchCurrentOfferRows(codes);
const byCode = new Map<string, { brandNorm: string; price: number; stock: number }[]>();
for (const r of rows) {
let list = byCode.get(r.codeNorm);
if (!list) {
list = [];
byCode.set(r.codeNorm, list);
}
list.push({ brandNorm: r.brandNorm, price: r.price, stock: 1 });
}
const values = chunk.map((t) => {
const offers = filterOffersForBrand(byCode.get(t.codeNorm) ?? [], t.brandNorm, t.codeNorm);
const stats = computeStats(offers);
return {
codeNorm: t.codeNorm,
brandNorm: t.brandNorm,
source: "supplier" as const,
date: today,
p50: stats.p50 === null ? null : String(stats.p50),
p95: stats.p95 === null ? null : String(stats.p95),
p99: stats.p99 === null ? null : String(stats.p99),
offerCount: stats.offerCount,
};
});
await db
.insert(partPriceDaily)
.values(values)
.onConflictDoUpdate({
target: [
partPriceDaily.codeNorm,
partPriceDaily.brandNorm,
partPriceDaily.source,
partPriceDaily.date,
],
set: {
p50: sql`excluded.p50`,
p95: sql`excluded.p95`,
p99: sql`excluded.p99`,
offerCount: sql`excluded.offer_count`,
},
});
// Bileşik anahtarla tek tek değil çift-kolon inArray ile güncellenemez;
// chunk küçük (≤300) — kod listesi yeterli (marka varyantları birlikte
// tazelenir, fazladan timestamp güncellemesi zararsız).
await db
.update(partPriceTracks)
.set({ lastRefreshedAt: new Date() })
.where(inArray(partPriceTracks.codeNorm, codes));
// Cache düşür — bir sonraki sayfa görüntülemesi taze pg satırını okur.
const keys = chunk.flatMap((t) => [
`partprice:series:v2:${t.codeNorm}::${t.brandNorm}`,
`partprice:cur:v2:${t.codeNorm}::${t.brandNorm}`,
]);
if (keys.length > 0) await redis.del(...keys);
refreshed += chunk.length;
console.log(`[part-price-refresh] ${refreshed}/${tracked.length} refreshed`);
}
return { tracked: tracked.length, refreshed, skipped: false };
} finally {
await source.end().catch(() => {});
redis.disconnect();
}
}

View File

@@ -1,11 +1,14 @@
import { Job } from "bullmq"; import { Job } from "bullmq";
import { lt } from "drizzle-orm"; import { lt } from "drizzle-orm";
import { PostgresJsDatabase } from "drizzle-orm/postgres-js"; import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
import { queryLogs } from "../../database/schema/core"; import { proxyLogs, queryLogs } from "../../database/schema/core";
type Database = PostgresJsDatabase<Record<string, unknown>>; type Database = PostgresJsDatabase<Record<string, unknown>>;
const RETENTION_DAYS = 90; const RETENTION_DAYS = 90;
// proxy_logs is high-volume per-attempt telemetry; 30 days is plenty for
// grading providers and spotting ban waves.
const PROXY_LOG_RETENTION_DAYS = 30;
export async function processQueryCleanup( export async function processQueryCleanup(
job: Job, job: Job,
@@ -27,5 +30,15 @@ export async function processQueryCleanup(
`[query-cleanup] Deleted ${deletedCount} query log(s) older than ${RETENTION_DAYS} days (before ${cutoffDate.toISOString()})`, `[query-cleanup] Deleted ${deletedCount} query log(s) older than ${RETENTION_DAYS} days (before ${cutoffDate.toISOString()})`,
); );
const proxyCutoff = new Date();
proxyCutoff.setDate(proxyCutoff.getDate() - PROXY_LOG_RETENTION_DAYS);
const proxyDeleted = await db
.delete(proxyLogs)
.where(lt(proxyLogs.createdAt, proxyCutoff))
.returning({ id: proxyLogs.id });
console.log(
`[query-cleanup] Deleted ${proxyDeleted.length} proxy log(s) older than ${PROXY_LOG_RETENTION_DAYS} days`,
);
return { deletedCount }; return { deletedCount };
} }

View File

@@ -0,0 +1,109 @@
import { Job } from "bullmq";
import { and, eq, sql } from "drizzle-orm";
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
import { catalogVehicles, vinpinDecodes } from "../../database/schema/core";
import { getVinpinDriver } from "../../integrations/vinpin/vinpin-driver.service";
import {
type CatalogCandidate,
pickBestCatalogMatch,
} from "../../integrations/vinpin/vinpin.matcher";
type Database = PostgresJsDatabase<Record<string, unknown>>;
export interface VinpinDecodeJobData {
vin: string;
}
/**
* Vinpin ePER decode-oracle processor (worker, concurrency 1). Drives Vinpin to
* identify a Fiat VIN the normal chain couldn't, matches it to an EXISTING PL24
* catalog_vehicle, and records the outcome in vinpin_decodes.
*
* Strict no-op when VINPIN_ENABLED is false (the feature flag) so it can sit in
* prod fully wired but inert until the paid seat is ready.
*/
export async function processVinpinDecode(
job: Job<VinpinDecodeJobData>,
db: Database,
): Promise<{ status: string; catalogVehicleId: string | null; skipped?: boolean }> {
const { vin } = job.data;
if (process.env.VINPIN_ENABLED !== "true") {
console.log(`[vinpin-decode] disabled (VINPIN_ENABLED!=true) — job ${job.id} no-op`);
return { status: "pending", catalogVehicleId: null, skipped: true };
}
console.log(`[vinpin-decode] job ${job.id} decoding VIN ${vin}`);
// Bump attempts up-front so a hard crash still records the effort.
await db
.update(vinpinDecodes)
.set({ attempts: sql`${vinpinDecodes.attempts} + 1`, updatedAt: new Date() })
.where(eq(vinpinDecodes.vin, vin));
try {
const decoded = await getVinpinDriver().decode(vin);
if (!decoded) {
await db
.update(vinpinDecodes)
.set({ status: "not_found", updatedAt: new Date() })
.where(eq(vinpinDecodes.vin, vin));
console.log(`[vinpin-decode] ${vin} → not_found`);
return { status: "not_found", catalogVehicleId: null };
}
// Match the decoded model to an existing PL24 Fiat catalog_vehicle.
const rows = await db
.select({
id: catalogVehicles.id,
model: catalogVehicles.model,
year: catalogVehicles.year,
})
.from(catalogVehicles)
.where(
and(sql`lower(${catalogVehicles.brandName}) = 'fiat'`, eq(catalogVehicles.source, "pl24")),
);
const catalogVehicleId = pickBestCatalogMatch(
{ model: decoded.model, modelYear: decoded.modelYear },
rows as CatalogCandidate[],
);
await db
.update(vinpinDecodes)
.set({
status: "decoded",
brandName: "Fiat",
model: decoded.model,
sincom: decoded.sincom,
trim: decoded.trim,
modelYear: decoded.modelYear,
engine: decoded.engine,
catalogVehicleId,
raw: decoded.raw,
decodedAt: new Date(),
updatedAt: new Date(),
})
.where(eq(vinpinDecodes.vin, vin));
console.log(
`[vinpin-decode] ${vin} → decoded model="${decoded.model}" catalog_vehicle=${catalogVehicleId ?? "null"}`,
);
return { status: "decoded", catalogVehicleId };
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`[vinpin-decode] job ${job.id} failed for ${vin}: ${message}`);
// Mark failed only once the BullMQ attempt budget is exhausted; otherwise let
// it retry (status stays 'pending').
const isLastAttempt = job.attemptsMade + 1 >= (job.opts.attempts ?? 1);
if (isLastAttempt) {
await db
.update(vinpinDecodes)
.set({ status: "failed", updatedAt: new Date() })
.where(eq(vinpinDecodes.vin, vin));
}
throw error;
}
}

View File

@@ -0,0 +1,24 @@
import { Provider } from "@nestjs/common";
import { Queue } from "bullmq";
import { QUEUE_NAMES, getBullConnection, getBullTelemetry } from "../bull.config";
export const CANONICAL_BACKFILL_QUEUE = "CANONICAL_BACKFILL_QUEUE";
// On-demand backfill (no cron): enqueue a job to (re)map raw category names to
// the canonical taxonomy. Trigger manually with data { brands?, sources?, dryRun? }.
export const CanonicalBackfillQueueProvider: Provider = {
provide: CANONICAL_BACKFILL_QUEUE,
useFactory: () => {
const telemetry = getBullTelemetry();
return new Queue(QUEUE_NAMES.CANONICAL_BACKFILL, {
connection: getBullConnection(),
...(telemetry ? { telemetry } : {}),
defaultJobOptions: {
attempts: 2,
backoff: { type: "fixed", delay: 30000 },
removeOnComplete: { count: 50 },
removeOnFail: { count: 100 },
},
});
},
};

View File

@@ -3,6 +3,7 @@ import { Queue } from "bullmq";
import { QUEUE_NAMES, getBullConnection, getBullTelemetry } from "../bull.config"; import { QUEUE_NAMES, getBullConnection, getBullTelemetry } from "../bull.config";
export const CATALOG_PREFETCH_QUEUE = "CATALOG_PREFETCH_QUEUE"; export const CATALOG_PREFETCH_QUEUE = "CATALOG_PREFETCH_QUEUE";
export const CATALOG_PREFETCH_FAST_QUEUE = "CATALOG_PREFETCH_FAST_QUEUE";
export const CatalogPrefetchQueueProvider: Provider = { export const CatalogPrefetchQueueProvider: Provider = {
provide: CATALOG_PREFETCH_QUEUE, provide: CATALOG_PREFETCH_QUEUE,
@@ -23,3 +24,20 @@ export const CatalogPrefetchQueueProvider: Provider = {
}); });
}, },
}; };
export const CatalogPrefetchFastQueueProvider: Provider = {
provide: CATALOG_PREFETCH_FAST_QUEUE,
useFactory: () => {
const telemetry = getBullTelemetry();
return new Queue(QUEUE_NAMES.CATALOG_PREFETCH_FAST, {
connection: getBullConnection(),
...(telemetry ? { telemetry } : {}),
defaultJobOptions: {
attempts: 3,
backoff: { type: "exponential", delay: 30000 },
removeOnComplete: { count: 1000 },
removeOnFail: { count: 5000 },
},
});
},
};

View File

@@ -0,0 +1,25 @@
import { Provider } from "@nestjs/common";
import { Queue } from "bullmq";
import { QUEUE_NAMES, getBullConnection, getBullTelemetry } from "../bull.config";
export const EXPERT_REWARDS_QUEUE = "EXPERT_REWARDS_QUEUE";
export const ExpertRewardsQueueProvider: Provider = {
provide: EXPERT_REWARDS_QUEUE,
useFactory: () => {
const telemetry = getBullTelemetry();
return new Queue(QUEUE_NAMES.EXPERT_REWARDS, {
connection: getBullConnection(),
...(telemetry ? { telemetry } : {}),
defaultJobOptions: {
attempts: 3,
backoff: {
type: "exponential",
delay: 10000,
},
removeOnComplete: { count: 100 },
removeOnFail: { count: 200 },
},
});
},
};

View File

@@ -0,0 +1,25 @@
import { Provider } from "@nestjs/common";
import { Queue } from "bullmq";
import { QUEUE_NAMES, getBullConnection, getBullTelemetry } from "../bull.config";
export const PART_PRICE_REFRESH_QUEUE = "PART_PRICE_REFRESH_QUEUE";
export const PartPriceRefreshQueueProvider: Provider = {
provide: PART_PRICE_REFRESH_QUEUE,
useFactory: () => {
const telemetry = getBullTelemetry();
return new Queue(QUEUE_NAMES.PART_PRICE_REFRESH, {
connection: getBullConnection(),
...(telemetry ? { telemetry } : {}),
defaultJobOptions: {
attempts: 3,
backoff: {
type: "exponential",
delay: 60000,
},
removeOnComplete: { count: 30 },
removeOnFail: { count: 100 },
},
});
},
};

View File

@@ -0,0 +1,26 @@
import { Provider } from "@nestjs/common";
import { Queue } from "bullmq";
import { QUEUE_NAMES, getBullConnection, getBullTelemetry } from "../bull.config";
export const VINPIN_DECODE_QUEUE = "VINPIN_DECODE_QUEUE";
/** Vinpin ePER decode-oracle queue. Single Vinpin seat → the worker consumes
* this with concurrency 1. Jobs carry just `{ vin }`. */
export const VinpinDecodeQueueProvider: Provider = {
provide: VINPIN_DECODE_QUEUE,
useFactory: () => {
const telemetry = getBullTelemetry();
return new Queue(QUEUE_NAMES.VINPIN_DECODE, {
connection: getBullConnection(),
...(telemetry ? { telemetry } : {}),
defaultJobOptions: {
// The driver itself retries (VINPIN_DECODE_MAX_ATTEMPTS) before reporting
// not_found, so the queue keeps a small attempt budget for hard crashes.
attempts: 2,
backoff: { type: "exponential", delay: 30_000 },
removeOnComplete: { count: 50 },
removeOnFail: { count: 100 },
},
});
},
};

View File

@@ -4,10 +4,12 @@ import "./telemetry/tracing"; // MUST be early — instruments modules before th
import { ConfigService } from "@nestjs/config"; import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core"; import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { NextFunction, Request, Response } from "express"; import { NextFunction, Request, Response } from "express";
import helmet from "helmet"; import helmet from "helmet";
import { AppModule } from "./app.module"; import { AppModule } from "./app.module";
import { fileUploadValidation } from "./common/middleware/file-upload-validation.middleware"; import { fileUploadValidation } from "./common/middleware/file-upload-validation.middleware";
import { PublicApiModule } from "./public-api/public-api.module";
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule, { rawBody: true }); const app = await NestFactory.create(AppModule, { rawBody: true });
@@ -18,6 +20,32 @@ async function bootstrap() {
app.setGlobalPrefix("api", { exclude: ["/collect/(.*)"] }); app.setGlobalPrefix("api", { exclude: ["/collect/(.*)"] });
// api.sase.tr kısa yolları: müşteriye dokümante edilen taban
// https://api.sase.tr/v1/... — global prefix'i host bazında yeniden yazarak
// karşılarız (/v1/* → /api/v1/*, /docs* → /api/v1/docs*). Uzun form
// (/api/v1/...) her host'ta çalışmaya devam eder.
app.use((req: Request, _res: Response, next: NextFunction) => {
const host = (req.headers.host ?? "").toLowerCase();
if (host === "api.sase.tr" || host.startsWith("api.")) {
if (req.url === "/v1" || req.url.startsWith("/v1/")) {
req.url = `/api${req.url}`;
} else if (
req.url === "/widget" ||
req.url.startsWith("/widget?") ||
req.url.startsWith("/widget/")
) {
req.url = `/api${req.url}`;
} else if (
req.url === "/docs" ||
req.url.startsWith("/docs/") ||
req.url.startsWith("/docs-json")
) {
req.url = `/api/v1${req.url}`;
}
}
next();
});
// Security headers // Security headers
app.use( app.use(
helmet({ helmet({
@@ -101,6 +129,43 @@ async function bootstrap() {
next(); next();
}); });
// Public API dokümantasyonu — SADECE PublicApiModule controller'larını
// kapsar; dashboard'ın iç API yüzeyi spec'e asla girmez. parts-catalogs
// modeli: spec herkese açık (https://api.sase.tr/docs + /docs-json).
const swaggerConfig = new DocumentBuilder()
.setTitle("sase.tr Public API")
.setDescription(
[
"Şase (VIN) çözümleme ve OEM çapraz-referans API'si.",
"",
"**Kimlik doğrulama:** Anahtarınızı `Authorization: Bearer sase_live_...` header'ında gönderin.",
"**Kota:** Faturalama birimi başarılı araç tanımlamasıdır (decode). Aynı VIN'in aynı gün",
"tekrar çözülmesi, aday listesi dönmesi ve OEM sorguları kotadan düşmez. Güncel durum her",
"decode cevabındaki `X-Decode-Quota-Limit/Used/Remaining` header'larında raporlanır;",
"kota dolduğunda `429` döner ve Türkiye saatiyle gece yarısı yenilenir.",
"",
"Cevaplar `{ success, data }` zarfıyla sarılıdır.",
].join("\n"),
)
.setVersion("1.0")
.addBearerAuth(
{
type: "http",
scheme: "bearer",
description: "API anahtarınız (sase_live_... / sase_test_...)",
},
"apiKey",
)
.addServer("https://api.sase.tr", "Canlı")
.build();
const swaggerDoc = SwaggerModule.createDocument(app, swaggerConfig, {
include: [PublicApiModule],
});
SwaggerModule.setup("api/v1/docs", app, swaggerDoc, {
customSiteTitle: "sase.tr Public API",
swaggerOptions: { persistAuthorization: true },
});
await app.listen(port); await app.listen(port);
console.log(`API running on http://localhost:${port}`); console.log(`API running on http://localhost:${port}`);
} }

View File

@@ -159,9 +159,7 @@ export class EmailPreferencesService {
async getCategoryState(userId: string, key: NotificationCategoryKey): Promise<boolean> { async getCategoryState(userId: string, key: NotificationCategoryKey): Promise<boolean> {
const cat = NOTIFICATION_CATEGORIES.find((c) => c.key === key); const cat = NOTIFICATION_CATEGORIES.find((c) => c.key === key);
if (!cat) return false; if (!cat) return false;
const states = await Promise.all( const states = await Promise.all(cat.workflows.map((w) => this.isOptedOut(userId, w)));
cat.workflows.map((w) => this.isOptedOut(userId, w)),
);
return states.length > 0 && states.every(Boolean); return states.length > 0 && states.every(Boolean);
} }

View File

@@ -155,11 +155,7 @@ export class NovuService {
amount: formatTryAmount(opts.amountKurus), amount: formatTryAmount(opts.amountKurus),
...(opts.plan ? { plan: opts.plan } : {}), ...(opts.plan ? { plan: opts.plan } : {}),
...(opts.nextBillingDate ? { nextBillingDate: formatTrDate(opts.nextBillingDate) } : {}), ...(opts.nextBillingDate ? { nextBillingDate: formatTrDate(opts.nextBillingDate) } : {}),
ctaUrl: buildTrackedUrl( ctaUrl: buildTrackedUrl("payment-success", user.email, webUrl("/dashboard/subscription")),
"payment-success",
user.email,
webUrl("/dashboard/subscription"),
),
}); });
} }
@@ -171,11 +167,7 @@ export class NovuService {
await this.trigger("payment-failed", user, { await this.trigger("payment-failed", user, {
...(opts.amountKurus !== undefined ? { amount: formatTryAmount(opts.amountKurus) } : {}), ...(opts.amountKurus !== undefined ? { amount: formatTryAmount(opts.amountKurus) } : {}),
...(opts.retryDate ? { retryDate: formatTrDate(opts.retryDate) } : {}), ...(opts.retryDate ? { retryDate: formatTrDate(opts.retryDate) } : {}),
ctaUrl: buildTrackedUrl( ctaUrl: buildTrackedUrl("payment-failed", user.email, webUrl("/dashboard/subscription")),
"payment-failed",
user.email,
webUrl("/dashboard/subscription"),
),
}); });
} }
} }

Some files were not shown because too many files have changed in this diff Show More