feat: item service joins manufacturers for brand display

This commit is contained in:
2026-04-18 16:22:10 +02:00
parent 12b3f8e380
commit 2924c2269c

View File

@@ -1,6 +1,6 @@
import { and, eq, sql } from "drizzle-orm";
import type { db as prodDb } from "../../db/index.ts";
import { categories, globalItems, items } from "../../db/schema.ts";
import { categories, globalItems, items, manufacturers } from "../../db/schema.ts";
import type { CreateItem } from "../../shared/types.ts";
type Db = typeof prodDb;
@@ -11,7 +11,7 @@ export async function getAllItems(db: Db, userId: number) {
id: items.id,
name: sql<string>`COALESCE(
CASE WHEN ${items.globalItemId} IS NOT NULL
THEN ${globalItems.brand} || ' ' || ${globalItems.model}
THEN ${manufacturers.name} || ' ' || ${globalItems.model}
ELSE ${items.name}
END,
${items.name}
@@ -38,7 +38,7 @@ export async function getAllItems(db: Db, userId: number) {
globalItemId: items.globalItemId,
brand: sql<
string | null
>`COALESCE(${globalItems.brand}, ${items.brand})`.as("brand"),
>`COALESCE(${manufacturers.name}, ${items.brand})`.as("brand"),
dominantColor: items.dominantColor,
cropZoom: items.cropZoom,
cropX: items.cropX,
@@ -51,6 +51,7 @@ export async function getAllItems(db: Db, userId: number) {
.from(items)
.innerJoin(categories, eq(items.categoryId, categories.id))
.leftJoin(globalItems, eq(items.globalItemId, globalItems.id))
.leftJoin(manufacturers, eq(globalItems.manufacturerId, manufacturers.id))
.where(eq(items.userId, userId));
}
@@ -60,7 +61,7 @@ export async function getItemById(db: Db, userId: number, id: number) {
id: items.id,
name: sql<string>`COALESCE(
CASE WHEN ${items.globalItemId} IS NOT NULL
THEN ${globalItems.brand} || ' ' || ${globalItems.model}
THEN ${manufacturers.name} || ' ' || ${globalItems.model}
ELSE ${items.name}
END,
${items.name}
@@ -87,7 +88,7 @@ export async function getItemById(db: Db, userId: number, id: number) {
globalItemId: items.globalItemId,
brand: sql<
string | null
>`COALESCE(${globalItems.brand}, ${items.brand})`.as("brand"),
>`COALESCE(${manufacturers.name}, ${items.brand})`.as("brand"),
dominantColor: items.dominantColor,
cropZoom: items.cropZoom,
cropX: items.cropX,
@@ -100,6 +101,7 @@ export async function getItemById(db: Db, userId: number, id: number) {
.from(items)
.innerJoin(categories, eq(items.categoryId, categories.id))
.leftJoin(globalItems, eq(items.globalItemId, globalItems.id))
.leftJoin(manufacturers, eq(globalItems.manufacturerId, manufacturers.id))
.where(and(eq(items.id, id), eq(items.userId, userId)));
return row ?? null;
@@ -118,11 +120,12 @@ export async function createItem(
let name = data.name;
if (data.globalItemId) {
const [gi] = await db
.select({ brand: globalItems.brand, model: globalItems.model })
.select({ name: manufacturers.name, model: globalItems.model })
.from(globalItems)
.innerJoin(manufacturers, eq(globalItems.manufacturerId, manufacturers.id))
.where(eq(globalItems.id, data.globalItemId));
if (gi) {
name = `${gi.brand} ${gi.model}`;
name = `${gi.name} ${gi.model}`;
}
}