[bugfix] correcting issues introduced by Claude

This commit is contained in:
2026-04-06 14:31:39 -04:00
parent 29ec850eef
commit 5dc7ce2de7
2 changed files with 16 additions and 39 deletions

View File

@@ -131,7 +131,6 @@ export const inventory = pokeSchema.table('inventory',{
catalogName: varchar({ length: 100 }), catalogName: varchar({ length: 100 }),
cardId: integer().notNull(), cardId: integer().notNull(),
condition: varchar({ length: 255 }).notNull(), condition: varchar({ length: 255 }).notNull(),
variant: varchar({ length: 100 }).default('Normal'),
quantity: integer(), quantity: integer(),
purchasePrice: decimal({ precision: 10, scale: 2 }), purchasePrice: decimal({ precision: 10, scale: 2 }),
note: varchar({ length:255 }), note: varchar({ length:255 }),

View File

@@ -1,8 +1,8 @@
import type { APIRoute } from 'astro'; import type { APIRoute } from 'astro';
import { db } from '../../db/index'; import { db } from '../../db/index';
import { inventory, skus, cards } from '../../db/schema'; import { inventory } from '../../db/schema';
import { client } from '../../db/typesense'; import { client } from '../../db/typesense';
import { eq, and, sql } from 'drizzle-orm'; import { eq } from 'drizzle-orm';
const GainLoss = (purchasePrice: any, marketPrice: any) => { const GainLoss = (purchasePrice: any, marketPrice: any) => {
if (!purchasePrice || !marketPrice) return '<div class="fs-5 fw-semibold">N/A</div>'; if (!purchasePrice || !marketPrice) return '<div class="fs-5 fw-semibold">N/A</div>';
@@ -15,41 +15,13 @@ const GainLoss = (purchasePrice: any, marketPrice: any) => {
const getInventory = async (userId: string, cardId: number) => { const getInventory = async (userId: string, cardId: number) => {
const inventories = await db const inventories = await db.query.inventory.findMany({
.select({ where: { userId:userId, cardId:cardId, },
inventoryId: inventory.inventoryId, with: { card: true, sku: true, }
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 invHtml = inventories.map(inv => { 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 marketPriceDisplay = marketPrice ? `$${marketPrice}` : '—';
const purchasePriceDisplay = inv.purchasePrice ? `$${Number(inv.purchasePrice).toFixed(2)}` : '—'; 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) => { 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({ const inv = await db.insert(inventory).values({
userId: userId, userId: userId,
cardId: cardId, cardId: cardId,
catalogName: catalogName, catalogName: catalogName,
condition: condition, condition: condition,
variant: variant, purchasePrice: purchasePrice.toFixed(2),
purchasePrice: purchasePrice,
quantity: quantity, quantity: quantity,
note: note, note: note,
}).returning(); }).returning();
// And then add to Typesense for searching
await client.collections('inventories').documents().import(inv.map(i => ({ await client.collections('inventories').documents().import(inv.map(i => ({
id: i.inventoryId, id: i.inventoryId,
userId: i.userId, userId: i.userId,
@@ -136,14 +109,17 @@ const removeFromInventory = async (inventoryId: string) => {
} }
const updateInventory = async (inventoryId: string, quantity: number, purchasePrice: number, note: string) => { const updateInventory = async (inventoryId: string, quantity: number, purchasePrice: number, note: string) => {
// Update the database
await db.update(inventory).set({ await db.update(inventory).set({
quantity: quantity, quantity: quantity,
purchasePrice: purchasePrice, purchasePrice: purchasePrice.toFixed(2),
note: note, note: note,
}).where(eq(inventory.inventoryId, inventoryId)); }).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 }) => { export const POST: APIRoute = async ({ request, locals }) => {
// Access form data from the request body
const formData = await request.formData(); const formData = await request.formData();
const action = formData.get('action'); const action = formData.get('action');
const cardId = Number(formData.get('cardId')) || 0; const cardId = Number(formData.get('cardId')) || 0;
@@ -175,8 +151,10 @@ export const POST: APIRoute = async ({ request, locals }) => {
break; break;
default: default:
// No action = list inventory for this card
return getInventory(userId!, cardId); return getInventory(userId!, cardId);
} }
// Always return current inventory after a mutation
return getInventory(userId!, cardId); return getInventory(userId!, cardId);
}; };