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:
@@ -1,6 +1,6 @@
|
||||
import { and, eq, sql } from "drizzle-orm";
|
||||
import type { db as prodDb } from "../../db/index.ts";
|
||||
import { categories, items } from "../../db/schema.ts";
|
||||
import { categories, globalItems, items } from "../../db/schema.ts";
|
||||
|
||||
type Db = typeof prodDb;
|
||||
|
||||
@@ -10,12 +10,23 @@ export async function getCategoryTotals(db: Db, userId: number) {
|
||||
categoryId: items.categoryId,
|
||||
categoryName: categories.name,
|
||||
categoryIcon: categories.icon,
|
||||
totalWeight: sql<number>`COALESCE(SUM(${items.weightGrams} * ${items.quantity}), 0)`,
|
||||
totalCost: sql<number>`COALESCE(SUM(${items.priceCents} * ${items.quantity}), 0)`,
|
||||
totalWeight: sql<number>`COALESCE(SUM(
|
||||
COALESCE(
|
||||
CASE WHEN ${items.globalItemId} IS NOT NULL THEN ${globalItems.weightGrams} ELSE NULL END,
|
||||
${items.weightGrams}
|
||||
) * ${items.quantity}
|
||||
), 0)`,
|
||||
totalCost: sql<number>`COALESCE(SUM(
|
||||
COALESCE(
|
||||
CASE WHEN ${items.globalItemId} IS NOT NULL THEN ${globalItems.priceCents} ELSE NULL END,
|
||||
${items.priceCents}
|
||||
) * ${items.quantity}
|
||||
), 0)`,
|
||||
itemCount: sql<number>`COUNT(*)`,
|
||||
})
|
||||
.from(items)
|
||||
.innerJoin(categories, eq(items.categoryId, categories.id))
|
||||
.leftJoin(globalItems, eq(items.globalItemId, globalItems.id))
|
||||
.where(eq(items.userId, userId))
|
||||
.groupBy(items.categoryId, categories.name, categories.icon);
|
||||
}
|
||||
@@ -23,11 +34,22 @@ export async function getCategoryTotals(db: Db, userId: number) {
|
||||
export async function getGlobalTotals(db: Db, userId: number) {
|
||||
const [row] = await db
|
||||
.select({
|
||||
totalWeight: sql<number>`COALESCE(SUM(${items.weightGrams} * ${items.quantity}), 0)`,
|
||||
totalCost: sql<number>`COALESCE(SUM(${items.priceCents} * ${items.quantity}), 0)`,
|
||||
totalWeight: sql<number>`COALESCE(SUM(
|
||||
COALESCE(
|
||||
CASE WHEN ${items.globalItemId} IS NOT NULL THEN ${globalItems.weightGrams} ELSE NULL END,
|
||||
${items.weightGrams}
|
||||
) * ${items.quantity}
|
||||
), 0)`,
|
||||
totalCost: sql<number>`COALESCE(SUM(
|
||||
COALESCE(
|
||||
CASE WHEN ${items.globalItemId} IS NOT NULL THEN ${globalItems.priceCents} ELSE NULL END,
|
||||
${items.priceCents}
|
||||
) * ${items.quantity}
|
||||
), 0)`,
|
||||
itemCount: sql<number>`COUNT(*)`,
|
||||
})
|
||||
.from(items)
|
||||
.leftJoin(globalItems, eq(items.globalItemId, globalItems.id))
|
||||
.where(eq(items.userId, userId));
|
||||
|
||||
return row;
|
||||
|
||||
Reference in New Issue
Block a user