From 5dc7ce2de77f8c8bdb9f73c71629f3eea755068c Mon Sep 17 00:00:00 2001 From: Thad Miller Date: Mon, 6 Apr 2026 14:31:39 -0400 Subject: [PATCH] [bugfix] correcting issues introduced by Claude --- src/db/schema.ts | 1 - src/pages/api/inventory.ts | 54 +++++++++++--------------------------- 2 files changed, 16 insertions(+), 39 deletions(-) diff --git a/src/db/schema.ts b/src/db/schema.ts index 7407e14..c19a5fd 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -131,7 +131,6 @@ export const inventory = pokeSchema.table('inventory',{ catalogName: varchar({ length: 100 }), cardId: integer().notNull(), condition: varchar({ length: 255 }).notNull(), - variant: varchar({ length: 100 }).default('Normal'), quantity: integer(), purchasePrice: decimal({ precision: 10, scale: 2 }), note: varchar({ length:255 }), diff --git a/src/pages/api/inventory.ts b/src/pages/api/inventory.ts index 4ce4901..3ccd6cc 100644 --- a/src/pages/api/inventory.ts +++ b/src/pages/api/inventory.ts @@ -1,8 +1,8 @@ import type { APIRoute } from 'astro'; import { db } from '../../db/index'; -import { inventory, skus, cards } from '../../db/schema'; +import { inventory } from '../../db/schema'; import { client } from '../../db/typesense'; -import { eq, and, sql } from 'drizzle-orm'; +import { eq } from 'drizzle-orm'; const GainLoss = (purchasePrice: any, marketPrice: any) => { if (!purchasePrice || !marketPrice) return '
N/A
'; @@ -15,41 +15,13 @@ const GainLoss = (purchasePrice: any, marketPrice: any) => { const getInventory = async (userId: string, cardId: number) => { -const inventories = await db - .select({ - inventoryId: inventory.inventoryId, - cardId: inventory.cardId, - condition: inventory.condition, - variant: inventory.variant, - quantity: inventory.quantity, - purchasePrice: inventory.purchasePrice, - note: inventory.note, - marketPrice: skus.marketPrice, - createdAt: inventory.createdAt, - }) - .from(inventory) - .leftJoin( - cards, - eq(inventory.cardId, cards.cardId) - ) - .leftJoin( - skus, - and( - eq(cards.productId, skus.productId), - eq(inventory.condition, skus.condition), - eq( - sql`COALESCE(${inventory.variant}, 'Normal')`, - skus.variant - ) - ) - ) - .where(and( - eq(inventory.userId, userId), - eq(inventory.cardId, cardId) - )); + const inventories = await db.query.inventory.findMany({ + where: { userId:userId, cardId:cardId, }, + with: { card: true, sku: true, } + }); const invHtml = inventories.map(inv => { - const marketPrice = inv.marketPrice ? Number(inv.marketPrice).toFixed(2) : null; + const marketPrice = inv.sku?.marketPrice; const marketPriceDisplay = marketPrice ? `$${marketPrice}` : '—'; const purchasePriceDisplay = inv.purchasePrice ? `$${Number(inv.purchasePrice).toFixed(2)}` : '—'; @@ -112,16 +84,17 @@ const inventories = await db const addToInventory = async (userId: string, cardId: number, condition: string, variant: string, purchasePrice: number, quantity: number, note: string, catalogName: string) => { + // First add to database const inv = await db.insert(inventory).values({ userId: userId, cardId: cardId, catalogName: catalogName, condition: condition, - variant: variant, - purchasePrice: purchasePrice, + purchasePrice: purchasePrice.toFixed(2), quantity: quantity, note: note, }).returning(); + // And then add to Typesense for searching await client.collections('inventories').documents().import(inv.map(i => ({ id: i.inventoryId, userId: i.userId, @@ -136,14 +109,17 @@ const removeFromInventory = async (inventoryId: string) => { } const updateInventory = async (inventoryId: string, quantity: number, purchasePrice: number, note: string) => { + // Update the database await db.update(inventory).set({ quantity: quantity, - purchasePrice: purchasePrice, + purchasePrice: purchasePrice.toFixed(2), note: note, }).where(eq(inventory.inventoryId, inventoryId)); + // No need to update Typesense since we don't search by quantity or price } export const POST: APIRoute = async ({ request, locals }) => { + // Access form data from the request body const formData = await request.formData(); const action = formData.get('action'); const cardId = Number(formData.get('cardId')) || 0; @@ -175,8 +151,10 @@ export const POST: APIRoute = async ({ request, locals }) => { break; default: + // No action = list inventory for this card return getInventory(userId!, cardId); } + // Always return current inventory after a mutation return getInventory(userId!, cardId); }; \ No newline at end of file