From 29deb19b897cefe71ea895e2f788ed888427b486 Mon Sep 17 00:00:00 2001 From: Thad Miller Date: Fri, 10 Apr 2026 15:15:00 -0400 Subject: [PATCH] [feat] move inventory data to db call --- src/pages/partials/inventory-cards.astro | 38 ++++++++++++++++-------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/pages/partials/inventory-cards.astro b/src/pages/partials/inventory-cards.astro index d12be3f..2e50131 100644 --- a/src/pages/partials/inventory-cards.astro +++ b/src/pages/partials/inventory-cards.astro @@ -5,9 +5,7 @@ import FirstEditionIcon from "../../components/FirstEditionIcon.astro"; export const prerender = false; import * as util from 'util'; - -const schema = await client.collections('inventories').retrieve(); -console.log('inventories schema:', JSON.stringify(schema.fields, null, 2)); +import { db } from '../../db'; // get the query from post request using form data const formData = await Astro.request.formData(); @@ -16,15 +14,25 @@ const start = Number(formData.get('start')?.toString() || '0'); const { userId } = Astro.locals.auth(); +const InventoryDetails = async (inventoryId: string) => { + //console.log('inventoryid', inventoryId); + const details = await db.query.inventory.findFirst({ + where: { inventoryId: inventoryId }, + with: { sku: { with: { card: true } } } + }) + return details; +} + + // primary search values (for cards) let searchArray = [{ collection: 'inventories', - filter_by: `userId:=${userId} && $skus($cards(id:*))`, + filter_by: `userId:=${userId}`, per_page: 20, facet_by: '', max_facet_values: 0, page: Math.floor(start / 20) + 1, - include_fields: '$skus(*),$cards(*)', + include_fields: 'id', }]; // on first load (start === 0) we want to get the facets for the filters @@ -56,20 +64,26 @@ const commonSearchParams = { // use typesense to search for cards matching the query and return the productIds of the results const searchResults = await client.multiSearch.perform(searchRequests, commonSearchParams); const inventoryResults = searchResults.results[0] as any; -console.log('inventoryResults', util.inspect(inventoryResults, { depth: null })); +// console.log('inventoryResults', util.inspect(inventoryResults, { depth: null })); -const pokemon = inventoryResults.hits?.map((hit: any) => hit.document) ?? []; +const pokemon = inventoryResults.hits ? + await Promise.all( + inventoryResults.hits.map( + async (hit: any) => { return (await InventoryDetails(hit.document.id)); } + ) + ) : []; const totalHits = inventoryResults?.found; +// console.log('pokemon', util.inspect(pokemon, { depth: null })); console.log(`totalHits: ${totalHits}`); --- {pokemon.map((inventory:any) => { - const sku = inventory.skus; - const card = inventory.cards; - const market = sku.marketPrice/100 || 0; - const purchase = (inventory.purchasePrice ?? 0) / 100; + const sku = inventory.sku; + const card = sku.card; + const market = Number(sku.marketPrice) || 0; + const purchase = Number(inventory.purchasePrice) || 0; const diff = market - purchase; const pct = purchase > 0 ? (diff / purchase) * 100 : 0; const isGain = diff >= 0; @@ -85,7 +99,7 @@ console.log(`totalHits: ${totalHits}`);
- +