[chore] move inventory relationship to sku
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import { db } from '../../db/index';
|
||||
import { inventory } from '../../db/schema';
|
||||
import { inventory, priceHistory } from '../../db/schema';
|
||||
import { client } from '../../db/typesense';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
@@ -15,27 +15,29 @@ const GainLoss = (purchasePrice: any, marketPrice: any) => {
|
||||
|
||||
const getInventory = async (userId: string, cardId: number) => {
|
||||
|
||||
const inventories = await db.query.inventory.findMany({
|
||||
where: { userId:userId, cardId:cardId, },
|
||||
with: { card: true, sku: true, }
|
||||
const card = await db.query.cards.findFirst({
|
||||
where: { cardId: cardId, },
|
||||
with : { prices: {
|
||||
with: { inventories: { where: { userId: userId } }, }
|
||||
}, },
|
||||
});
|
||||
|
||||
const invHtml = inventories.map(inv => {
|
||||
const marketPrice = inv.sku?.marketPrice;
|
||||
const invHtml = card?.prices?.flatMap(price => price.inventories.map(inv => {
|
||||
const marketPrice = price.marketPrice;
|
||||
const marketPriceDisplay = marketPrice ? `$${marketPrice}` : '—';
|
||||
const purchasePriceDisplay = inv.purchasePrice ? `$${Number(inv.purchasePrice).toFixed(2)}` : '—';
|
||||
|
||||
return `
|
||||
<article class="border rounded-4 p-2 inventory-entry-card"
|
||||
data-inventory-id="${inv.inventoryId}"
|
||||
data-card-id="${inv.cardId}"
|
||||
data-card-id="${price.cardId}"
|
||||
data-purchase-price="${inv.purchasePrice}"
|
||||
data-note="${(inv.note || '').replace(/"/g, '"')}">
|
||||
<div class="d-flex flex-column">
|
||||
<!-- Top row -->
|
||||
<div class="d-flex justify-content-between gap-3">
|
||||
<div class="min-w-0 flex-grow-1">
|
||||
<div class="fw-semibold fs-6 text-body mb-1">${inv.condition}</div>
|
||||
<div class="fw-semibold fs-6 text-body mb-1">${price.condition}</div>
|
||||
</div>
|
||||
<div class="fs-7 text-secondary">Added: ${inv.createdAt ? new Date(inv.createdAt).toLocaleDateString() : '—'}</div>
|
||||
</div>
|
||||
@@ -71,7 +73,7 @@ const getInventory = async (userId: string, cardId: number) => {
|
||||
</div>
|
||||
</div>
|
||||
</article>`;
|
||||
});
|
||||
})) || [];
|
||||
|
||||
return new Response(
|
||||
invHtml.join(''),
|
||||
@@ -83,13 +85,12 @@ const getInventory = async (userId: string, cardId: number) => {
|
||||
}
|
||||
|
||||
|
||||
const addToInventory = async (userId: string, cardId: number, condition: string, variant: string, purchasePrice: number, quantity: number, note: string, catalogName: string) => {
|
||||
const addToInventory = async (userId: string, skuId: number, purchasePrice: number, quantity: number, note: string, catalogName: string) => {
|
||||
// First add to database
|
||||
const inv = await db.insert(inventory).values({
|
||||
userId: userId,
|
||||
cardId: cardId,
|
||||
skuId: skuId,
|
||||
catalogName: catalogName,
|
||||
condition: condition,
|
||||
purchasePrice: purchasePrice.toFixed(2),
|
||||
quantity: quantity,
|
||||
note: note,
|
||||
@@ -99,7 +100,7 @@ const addToInventory = async (userId: string, cardId: number, condition: string,
|
||||
id: i.inventoryId,
|
||||
userId: i.userId,
|
||||
catalogName: i.catalogName,
|
||||
card_id: i.cardId.toString(),
|
||||
sku_id: i.skuId.toString(),
|
||||
})));
|
||||
}
|
||||
|
||||
@@ -128,13 +129,19 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
||||
switch (action) {
|
||||
|
||||
case 'add':
|
||||
const condition = formData.get('condition')?.toString() || 'Unknown';
|
||||
const variant = formData.get('variant')?.toString() || 'Normal';
|
||||
const purchasePrice = Number(formData.get('purchasePrice')) || 0;
|
||||
const quantity = Number(formData.get('quantity')) || 1;
|
||||
const note = formData.get('note')?.toString() || '';
|
||||
const catalogName = formData.get('catalogName')?.toString() || 'Default';
|
||||
await addToInventory(userId!, cardId, condition, variant, purchasePrice, quantity, note, catalogName);
|
||||
const condition = formData.get('condition')?.toString() || 'Near Mint';
|
||||
const skuId = await db.query.skus.findFirst({
|
||||
where: { cardId: cardId, condition: condition },
|
||||
columns: { skuId: true },
|
||||
}).then(sku => sku?.skuId);
|
||||
if (!skuId) {
|
||||
return new Response('SKU not found for card', { status: 404 });
|
||||
}
|
||||
await addToInventory(userId!, skuId, purchasePrice, quantity, note, catalogName);
|
||||
break;
|
||||
|
||||
case 'remove':
|
||||
|
||||
Reference in New Issue
Block a user