Initial commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Shared helpers for the `mycards` PocketBase collection:
|
||||
* record typing and Cardmarket price resolution via the cached TCGdex client.
|
||||
*/
|
||||
|
||||
import { getCard } from "./tcgdex-cache";
|
||||
|
||||
export interface MyCardRecord {
|
||||
id: string;
|
||||
user: string;
|
||||
card_id: string;
|
||||
card_name: string;
|
||||
card_image: string;
|
||||
variant: string;
|
||||
quantity: number;
|
||||
condition: string;
|
||||
notes: string;
|
||||
set_name: string;
|
||||
created: string;
|
||||
updated: string;
|
||||
}
|
||||
|
||||
export type PricedCard = MyCardRecord & { price: number; totalPrice: number };
|
||||
|
||||
/** Collection variant label → Cardmarket avg price field. */
|
||||
export const variantPriceMap: Record<string, string> = {
|
||||
Base: "avg",
|
||||
Holo: "avg-holo",
|
||||
Reverse: "avg-reverse",
|
||||
"First Edition": "avg-firstEdition",
|
||||
Promo: "avg-wPromo",
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve the Cardmarket unit price and line total for each collection card.
|
||||
* Card details come from the 24h in-memory cache, so repeated cards and
|
||||
* repeated page loads don't hit the TCGdex API again.
|
||||
*/
|
||||
export async function priceCollectionCards(
|
||||
cards: MyCardRecord[],
|
||||
): Promise<PricedCard[]> {
|
||||
return Promise.all(
|
||||
cards.map(async (card) => {
|
||||
let price = 0;
|
||||
const detail = await getCard(card.card_id);
|
||||
const cardmarket = detail?.pricing?.cardmarket;
|
||||
if (cardmarket) {
|
||||
const priceKey = variantPriceMap[card.variant] ?? "avg";
|
||||
price = cardmarket[priceKey] ?? cardmarket.avg ?? 0;
|
||||
}
|
||||
return { ...card, price, totalPrice: price * card.quantity };
|
||||
}),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user