feat(19-03): add COALESCE merge for reference items in secondary services

- Setup service: LEFT JOIN globalItems in getAllSetups totals and getSetupWithItems
- Totals service: LEFT JOIN globalItems in getCategoryTotals and getGlobalTotals
- Profile service: LEFT JOIN globalItems in getPublicProfile totals and getPublicSetupWithItems
- CSV service: LEFT JOIN globalItems in exportItemsCsv for merged name/weight/price
This commit is contained in:
2026-04-06 00:26:13 +02:00
parent ecc6ac689a
commit 0a233c754d
4 changed files with 124 additions and 23 deletions

View File

@@ -2,6 +2,7 @@ import { and, eq, sql } from "drizzle-orm";
import type { db as prodDb } from "../../db/index.ts";
import {
categories,
globalItems,
items,
setupItems,
setups,
@@ -55,13 +56,25 @@ export async function getPublicProfile(db: Db, userId: number) {
WHERE setup_items.setup_id = setups.id
), 0)`.as("item_count"),
totalWeight: sql<number>`COALESCE((
SELECT SUM(items.weight_grams * items.quantity) FROM setup_items
SELECT SUM(
COALESCE(
CASE WHEN items.global_item_id IS NOT NULL THEN global_items.weight_grams ELSE NULL END,
items.weight_grams
) * items.quantity
) FROM setup_items
JOIN items ON items.id = setup_items.item_id
LEFT JOIN global_items ON global_items.id = items.global_item_id
WHERE setup_items.setup_id = setups.id
), 0)`.as("total_weight"),
totalCost: sql<number>`COALESCE((
SELECT SUM(items.price_cents * items.quantity) FROM setup_items
SELECT SUM(
COALESCE(
CASE WHEN items.global_item_id IS NOT NULL THEN global_items.price_cents ELSE NULL END,
items.price_cents
) * items.quantity
) FROM setup_items
JOIN items ON items.id = setup_items.item_id
LEFT JOIN global_items ON global_items.id = items.global_item_id
WHERE setup_items.setup_id = setups.id
), 0)`.as("total_cost"),
})
@@ -82,14 +95,30 @@ export async function getPublicSetupWithItems(db: Db, setupId: number) {
const itemList = await db
.select({
id: items.id,
name: items.name,
weightGrams: items.weightGrams,
priceCents: items.priceCents,
name: sql<string>`COALESCE(
CASE WHEN ${items.globalItemId} IS NOT NULL
THEN ${globalItems.brand} || ' ' || ${globalItems.model}
ELSE ${items.name}
END,
${items.name}
)`.as("name"),
weightGrams: sql<number | null>`COALESCE(
CASE WHEN ${items.globalItemId} IS NOT NULL THEN ${globalItems.weightGrams} ELSE NULL END,
${items.weightGrams}
)`.as("weight_grams"),
priceCents: sql<number | null>`COALESCE(
CASE WHEN ${items.globalItemId} IS NOT NULL THEN ${globalItems.priceCents} ELSE NULL END,
${items.priceCents}
)`.as("price_cents"),
quantity: items.quantity,
categoryId: items.categoryId,
notes: items.notes,
productUrl: items.productUrl,
imageFilename: items.imageFilename,
imageFilename: sql<string | null>`COALESCE(
${items.imageFilename},
CASE WHEN ${items.globalItemId} IS NOT NULL THEN ${globalItems.imageUrl} ELSE NULL END
)`.as("image_filename"),
globalItemId: items.globalItemId,
createdAt: items.createdAt,
updatedAt: items.updatedAt,
categoryName: categories.name,
@@ -99,6 +128,7 @@ export async function getPublicSetupWithItems(db: Db, setupId: number) {
.from(setupItems)
.innerJoin(items, eq(setupItems.itemId, items.id))
.innerJoin(categories, eq(items.categoryId, categories.id))
.leftJoin(globalItems, eq(items.globalItemId, globalItems.id))
.where(eq(setupItems.setupId, setupId));
return { ...setup, items: itemList };