Each collection row is a unique card (own condition/notes), so quantity was noise: dropped the field in Directus and removed it from the add dialog, collection/grid/table views, dashboard stats and sort options. Cards without Cardmarket data now consistently show 0,00 € (base price row is always displayed on the card page).
44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
/**
|
|
* Shared helpers for the `mycards` collection:
|
|
* record typing and Cardmarket price resolution via the cached TCGdex client.
|
|
*/
|
|
|
|
import { getCard } from "./tcgdex-cache";
|
|
import type { MyCard } from "./directus";
|
|
|
|
export type MyCardRecord = MyCard;
|
|
|
|
export type PricedCard = MyCard & { 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 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. Cards without a
|
|
* Cardmarket price get 0 for consistent display (0,00 €).
|
|
*/
|
|
export async function priceCollectionCards(
|
|
cards: MyCard[],
|
|
): 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 };
|
|
}),
|
|
);
|
|
}
|