[feat] move inventory data to db call

This commit is contained in:
2026-04-10 15:15:00 -04:00
parent e7374ad182
commit 29deb19b89

View File

@@ -5,9 +5,7 @@ import FirstEditionIcon from "../../components/FirstEditionIcon.astro";
export const prerender = false; export const prerender = false;
import * as util from 'util'; import * as util from 'util';
import { db } from '../../db';
const schema = await client.collections('inventories').retrieve();
console.log('inventories schema:', JSON.stringify(schema.fields, null, 2));
// get the query from post request using form data // get the query from post request using form data
const formData = await Astro.request.formData(); const formData = await Astro.request.formData();
@@ -16,15 +14,25 @@ const start = Number(formData.get('start')?.toString() || '0');
const { userId } = Astro.locals.auth(); 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) // primary search values (for cards)
let searchArray = [{ let searchArray = [{
collection: 'inventories', collection: 'inventories',
filter_by: `userId:=${userId} && $skus($cards(id:*))`, filter_by: `userId:=${userId}`,
per_page: 20, per_page: 20,
facet_by: '', facet_by: '',
max_facet_values: 0, max_facet_values: 0,
page: Math.floor(start / 20) + 1, 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 // 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 // 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 searchResults = await client.multiSearch.perform(searchRequests, commonSearchParams);
const inventoryResults = searchResults.results[0] as any; 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; const totalHits = inventoryResults?.found;
// console.log('pokemon', util.inspect(pokemon, { depth: null }));
console.log(`totalHits: ${totalHits}`); console.log(`totalHits: ${totalHits}`);
--- ---
{pokemon.map((inventory:any) => { {pokemon.map((inventory:any) => {
const sku = inventory.skus; const sku = inventory.sku;
const card = inventory.cards; const card = sku.card;
const market = sku.marketPrice/100 || 0; const market = Number(sku.marketPrice) || 0;
const purchase = (inventory.purchasePrice ?? 0) / 100; const purchase = Number(inventory.purchasePrice) || 0;
const diff = market - purchase; const diff = market - purchase;
const pct = purchase > 0 ? (diff / purchase) * 100 : 0; const pct = purchase > 0 ? (diff / purchase) * 100 : 0;
const isGain = diff >= 0; const isGain = diff >= 0;
@@ -85,7 +99,7 @@ console.log(`totalHits: ${totalHits}`);
</div> </div>
</div> </div>
<div class="d-flex flex-row justify-content-between my-1 align-items-center edit-bar"> <div class="d-flex flex-row justify-content-between my-1 align-items-center edit-bar">
<input type="number" class="form-control form-control-sm text-center" style="max-width: 33%;" value="1" min="1" max="999" aria-label="Quantity input" aria-describedby="button-minus button-plus"> <input type="number" class="form-control form-control-sm text-center" style="max-width: 33%;" value={inventory.quantity} min="1" max="999" aria-label="Quantity input" aria-describedby="button-minus button-plus">
<div class="" aria-label="Edit controls"> <div class="" aria-label="Edit controls">
<button type="button" class="btn btn-sm btn-edit me-2"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640" class="edit-svg"><path d="M374.4 146.5L374.4 146.5C395.3 128.4 443.5 85.4 468.5 97.6C484.4 105.9 497.8 112.8 501.3 120.1L501.4 120.2L501.5 120.3L501.6 120.5C507.6 130.2 518.4 150.1 519.6 161.6C513.5 192.1 484 217.1 461.6 240.4C425.7 208.6 404.1 187.1 369.7 150.6C371.3 149.2 372.8 147.9 374.4 146.5zM484.1 307.3C484.3 307.1 484.5 307 484.6 306.8C525.1 265.7 579.4 226.1 583.7 161.7C575.2 64.5 475.6-2.1 395.9 50.5C299.2 114.8 235.3 194.9 152 272.8C118.1 305.6 72.6 334.1 62.3 384.2C60.7 400.9 62.5 429.9 63.3 446.8C63.3 472.2 62.6 516.6 62.3 535.2C65.6 607.8 181.9 562 225.7 560.1C249.2 555.4 267.7 533 280.2 519.6C280.4 519.4 280.5 519.3 280.5 519.3C350.4 450.8 416.9 378.7 484.2 307.4zM416.2 285.7C349.4 358.2 282.1 428.8 211.2 497.7C182.1 502.9 154.5 507.3 126.6 510C128.2 471.4 125.7 426.3 125.8 391.4C138.8 367.7 172.1 340.8 194.2 320.8C239.9 279.4 276.7 234.2 323.2 194.7C356.5 230 380.6 254.2 416.1 285.7z"/></svg></button> <button type="button" class="btn btn-sm btn-edit me-2"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640" class="edit-svg"><path d="M374.4 146.5L374.4 146.5C395.3 128.4 443.5 85.4 468.5 97.6C484.4 105.9 497.8 112.8 501.3 120.1L501.4 120.2L501.5 120.3L501.6 120.5C507.6 130.2 518.4 150.1 519.6 161.6C513.5 192.1 484 217.1 461.6 240.4C425.7 208.6 404.1 187.1 369.7 150.6C371.3 149.2 372.8 147.9 374.4 146.5zM484.1 307.3C484.3 307.1 484.5 307 484.6 306.8C525.1 265.7 579.4 226.1 583.7 161.7C575.2 64.5 475.6-2.1 395.9 50.5C299.2 114.8 235.3 194.9 152 272.8C118.1 305.6 72.6 334.1 62.3 384.2C60.7 400.9 62.5 429.9 63.3 446.8C63.3 472.2 62.6 516.6 62.3 535.2C65.6 607.8 181.9 562 225.7 560.1C249.2 555.4 267.7 533 280.2 519.6C280.4 519.4 280.5 519.3 280.5 519.3C350.4 450.8 416.9 378.7 484.2 307.4zM416.2 285.7C349.4 358.2 282.1 428.8 211.2 497.7C182.1 502.9 154.5 507.3 126.6 510C128.2 471.4 125.7 426.3 125.8 391.4C138.8 367.7 172.1 340.8 194.2 320.8C239.9 279.4 276.7 234.2 323.2 194.7C356.5 230 380.6 254.2 416.1 285.7z"/></svg></button>