diff --git a/scripts/pokemon-helper.ts b/scripts/pokemon-helper.ts index a3c05b8..9a9a9c9 100644 --- a/scripts/pokemon-helper.ts +++ b/scripts/pokemon-helper.ts @@ -88,6 +88,25 @@ export const createSkuCollection = async () => { console.log(chalk.green('Collection "skus" created successfully.')); } +// Delete and recreate the 'inventory' index +export const createInventoryCollection = async () => { + try { + await client.collections('inventories').delete(); + } catch (error) { + // Ignore error, just means collection doesn't exist + } + await client.collections().create({ + name: 'inventories', + fields: [ + { name: 'id', type: 'string' }, + { name: 'userId', type: 'string' }, + { name: 'catalogName', type: 'string' }, + { name: 'card_id', type: 'string', reference: 'cards.id' }, + ] + }); + console.log(chalk.green('Collection "inventories" created successfully.')); +} + export const upsertCardCollection = async (db:DBInstance) => { const pokemon = await db.query.cards.findMany({ @@ -131,6 +150,17 @@ export const upsertSkuCollection = async (db:DBInstance) => { console.log(chalk.green('Collection "skus" indexed successfully.')); } +export const upsertInventoryCollection = async (db:DBInstance) => { + const inv = await db.query.inventory.findMany(); + await client.collections('inventories').documents().import(inv.map(i => ({ + id: i.inventoryId, + userId: i.userId, + catalogName: i.catalogName, + card_id: i.cardId.toString(), + })), { action: 'upsert' }); + console.log(chalk.green('Collection "inventories" indexed successfully.')); +} + diff --git a/scripts/reindex.ts b/scripts/reindex.ts index 7a93288..71e4eed 100644 --- a/scripts/reindex.ts +++ b/scripts/reindex.ts @@ -5,7 +5,10 @@ import * as Indexing from './pokemon-helper.ts'; //await Indexing.createCardCollection(); //await Indexing.createSkuCollection(); -await Indexing.upsertCardCollection(db); -await Indexing.upsertSkuCollection(db); +await Indexing.createInventoryCollection(); + +//await Indexing.upsertCardCollection(db); +//await Indexing.upsertSkuCollection(db); +await Indexing.upsertInventoryCollection(db); await ClosePool(); console.log(chalk.green('Pokémon reindex complete.')); diff --git a/src/db/relations.ts b/src/db/relations.ts index 218d423..0ecc14d 100644 --- a/src/db/relations.ts +++ b/src/db/relations.ts @@ -21,9 +21,21 @@ export const relations = defineRelations(schema, (r) => ({ }), history: r.many.priceHistory(), latestSales: r.many.salesHistory(), + inventories: r.many.inventory(), + }, + inventory: { + card: r.one.cards({ + from: r.inventory.cardId, + to: r.cards.cardId, + }), + sku: r.one.skus({ + from: [r.inventory.cardId, r.inventory.condition], + to: [r.skus.cardId, r.skus.condition], + }), }, cards: { prices: r.many.skus(), + inventories: r.many.inventory(), set: r.one.sets({ from: r.cards.setId, to: r.sets.setId, diff --git a/src/db/schema.ts b/src/db/schema.ts index 5c16cc7..f61fdcc 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -1,5 +1,5 @@ //import { mysqlTable, int, varchar, boolean, decimal, datetime, index } from "drizzle-orm/mysql-core" -import { integer, varchar, boolean, decimal, timestamp, index, pgSchema, uniqueIndex, primaryKey } from "drizzle-orm/pg-core"; +import { integer, varchar, boolean, decimal, timestamp, index, pgSchema, uuid, primaryKey } from "drizzle-orm/pg-core"; export const pokeSchema = pgSchema("pokemon"); @@ -98,6 +98,7 @@ export const skus = pokeSchema.table('skus', { }, (table) => [ index('idx_product_id_condition').on(table.productId, table.variant, table.condition), + index('idx_card_id_condition').on(table.cardId, table.condition), ]); export const priceHistory = pokeSchema.table('price_history', { @@ -124,6 +125,20 @@ export const salesHistory = pokeSchema.table('sales_history',{ primaryKey({ name: 'pk_sales_history', columns: [table.skuId, table.orderDate] }) ]); +export const inventory = pokeSchema.table('inventory',{ + inventoryId: uuid().primaryKey().notNull().defaultRandom(), + userId: varchar({ length: 100 }).notNull(), + catalogName: varchar({ length: 100 }), + cardId: integer().notNull(), + condition: varchar({ length: 255 }).notNull(), + quantity: integer(), + purchasePrice: integer(), + note: varchar({ length:255 }) +}, +(table) => [ + index('idx_userid_cardid').on(table.userId, table.cardId) +]); + export const processingSkus = pokeSchema.table('processing_skus', { skuId: integer().primaryKey(), }); diff --git a/src/pages/api/inventory.ts b/src/pages/api/inventory.ts new file mode 100644 index 0000000..1f661e6 --- /dev/null +++ b/src/pages/api/inventory.ts @@ -0,0 +1,161 @@ +import type { APIRoute } from 'astro'; +import { db } from '../../db/index'; +import { inventory } from '../../db/schema'; +import { client } from '../../db/typesense'; +import { eq } from 'drizzle-orm'; + + +const GainLoss = (purchasePrice:any, marketPrice:any) => { + if (!purchasePrice || !marketPrice) return '