[feat] dashboard shows inventory
This commit is contained in:
@@ -85,7 +85,7 @@ const getInventory = async (userId: string, cardId: number) => {
|
||||
}
|
||||
|
||||
|
||||
const addToInventory = async (userId: string, skuId: number, purchasePrice: number, quantity: number, note: string, catalogName: string) => {
|
||||
const addToInventory = async (userId: string, cardId: number, skuId: number, purchasePrice: number, quantity: number, note: string, catalogName: string) => {
|
||||
// First add to database
|
||||
const inv = await db.insert(inventory).values({
|
||||
userId: userId,
|
||||
@@ -95,13 +95,37 @@ const addToInventory = async (userId: string, skuId: number, purchasePrice: numb
|
||||
quantity: quantity,
|
||||
note: note,
|
||||
}).returning();
|
||||
// Get card details from the database to add to Typesense
|
||||
const card = await db.query.cards.findFirst({
|
||||
where: { cardId: cardId },
|
||||
with: { set: true },
|
||||
});
|
||||
|
||||
try {
|
||||
// And then add to Typesense for searching
|
||||
await client.collections('inventories').documents().import(inv.map(i => ({
|
||||
id: i.inventoryId,
|
||||
userId: i.userId,
|
||||
catalogName: i.catalogName,
|
||||
sku_id: i.skuId.toString(),
|
||||
productLineName: card?.productLineName,
|
||||
rarityName: card?.rarityName,
|
||||
setName: card?.set?.setName || "",
|
||||
cardType: card?.cardType || "",
|
||||
energyType: card?.energyType || "",
|
||||
card_id: card?.cardId.toString() || "",
|
||||
content: [
|
||||
card?.productName,
|
||||
card?.productLineName,
|
||||
card?.set?.setName || "",
|
||||
card?.number,
|
||||
card?.rarityName,
|
||||
card?.artist || ""
|
||||
].join(' '),
|
||||
})));
|
||||
} catch (error) {
|
||||
console.error('Error adding inventory to Typesense:', error);
|
||||
}
|
||||
}
|
||||
|
||||
const removeFromInventory = async (inventoryId: string) => {
|
||||
@@ -141,7 +165,7 @@ export const POST: APIRoute = async ({ request, locals }) => {
|
||||
if (!skuId) {
|
||||
return new Response('SKU not found for card', { status: 404 });
|
||||
}
|
||||
await addToInventory(userId!, skuId, purchasePrice, quantity, note, catalogName);
|
||||
await addToInventory(userId!, cardId, skuId, purchasePrice, quantity, note, catalogName);
|
||||
break;
|
||||
|
||||
case 'remove':
|
||||
|
||||
Reference in New Issue
Block a user