[feat] pull dashboard summary from db
This commit is contained in:
@@ -4,15 +4,29 @@ import NavBar from "../components/NavBar.astro";
|
|||||||
import NavItems from "../components/NavItems.astro";
|
import NavItems from "../components/NavItems.astro";
|
||||||
import Footer from "../components/Footer.astro";
|
import Footer from "../components/Footer.astro";
|
||||||
import FirstEditionIcon from "../components/FirstEditionIcon.astro";
|
import FirstEditionIcon from "../components/FirstEditionIcon.astro";
|
||||||
|
import { db } from '../db/index';
|
||||||
|
import { inventory, skus } from '../db/schema';
|
||||||
|
import { sql, sum, eq } from "drizzle-orm";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// const totalQty = inventory.reduce((s, c) => s + c.qty, 0);
|
const { userId } = Astro.locals.auth();
|
||||||
// const totalValue = inventory.reduce((s, c) => s + nmPrice(c) * c.qty, 0);
|
|
||||||
// const totalGain = inventory.reduce((s, c) => s + gain(c) * c.qty, 0);
|
const summary = await db
|
||||||
const totalQty = 1234;
|
.select({
|
||||||
const totalValue = 5678.90;
|
totalQty: sum(inventory.quantity).mapWith(Number),
|
||||||
const totalGain = 1234.56;
|
totalValue: sum(sql`(${inventory.quantity} * ${skus.marketPrice})`).mapWith(Number),
|
||||||
|
totalGain: sum(sql`(${inventory.quantity} * (${skus.marketPrice} - ${inventory.purchasePrice}))`).mapWith(Number),
|
||||||
|
})
|
||||||
|
.from(inventory)
|
||||||
|
.innerJoin(skus, eq(inventory.skuId, skus.skuId))
|
||||||
|
.where(eq(inventory.userId, userId!))
|
||||||
|
.execute()
|
||||||
|
.then(res => res[0]);
|
||||||
|
const totalQty = summary.totalQty || 0;
|
||||||
|
const totalValue = summary.totalValue || 0;
|
||||||
|
const totalGain = summary.totalGain || 0;
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title="Inventory Dashboard">
|
<Layout title="Inventory Dashboard">
|
||||||
|
|||||||
Reference in New Issue
Block a user