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>
This commit is contained in:
@@ -15,6 +15,13 @@ export class PartsController {
|
||||
return this.partsService.searchByOem(oem);
|
||||
}
|
||||
|
||||
// Reverse catalog for the OEM detail page: which of the user's vehicles use
|
||||
// this OEM code. Declared before ":id" so the literal path wins the match.
|
||||
@Get("oem-vehicles")
|
||||
async vehiclesByOem(@Query("code") code: string) {
|
||||
return this.partsService.vehiclesByOem(code ?? "");
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
async getById(@Param("id") id: string) {
|
||||
return this.partsService.getById(id);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Inject, Injectable, Logger, NotFoundException } from "@nestjs/common";
|
||||
import { eq, like } from "drizzle-orm";
|
||||
import { eq, like, sql } from "drizzle-orm";
|
||||
import { DATABASE, type Database } from "../database/database.provider";
|
||||
import { categories, parts, schemaPics, vehicles } from "../database/schema/core";
|
||||
import { PL24Service } from "../integrations/pl24/pl24.service";
|
||||
@@ -142,4 +142,32 @@ export class PartsService {
|
||||
if (!part) throw new NotFoundException("Parça bulunamadı");
|
||||
return part;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse catalog: the user's own decoded vehicles whose parts list contains
|
||||
* this exact OEM code. Powers the "bu kod kataloğunuzda şu araçlarda var"
|
||||
* section of the OEM detail page — pure sase data, no TecDoc/vehicle-structure
|
||||
* dependency. Exact match on the indexed `oem_code` (the code came from a real
|
||||
* part row, so the spelling matches). One representative `categoryId` per
|
||||
* vehicle lets the UI deep-link straight to a schema page showing the part.
|
||||
*/
|
||||
async vehiclesByOem(oemCode: string) {
|
||||
const code = (oemCode ?? "").trim();
|
||||
if (!code) return [];
|
||||
return this.db
|
||||
.select({
|
||||
vehicleId: vehicles.id,
|
||||
brandName: vehicles.brandName,
|
||||
model: vehicles.model,
|
||||
year: vehicles.year,
|
||||
categoryId: sql<string>`(array_agg(${parts.categoryId} ORDER BY ${parts.categoryId}))[1]`,
|
||||
occurrences: sql<number>`count(*)::int`,
|
||||
})
|
||||
.from(parts)
|
||||
.innerJoin(vehicles, eq(parts.vehicleId, vehicles.id))
|
||||
.where(eq(parts.oemCode, code))
|
||||
.groupBy(vehicles.id, vehicles.brandName, vehicles.model, vehicles.year)
|
||||
.orderBy(vehicles.brandName, vehicles.model)
|
||||
.limit(50);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user