add price tracking to collections and implement total value calculation
This commit is contained in:
@@ -82,6 +82,28 @@ export interface PaginatedCollectionResult {
|
|||||||
hasMore: boolean;
|
hasMore: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get total collection value from database (lightweight query)
|
||||||
|
export const getCollectionTotalValue = async (userId: string): Promise<number> => {
|
||||||
|
const { data, error } = await supabase
|
||||||
|
.from('collections')
|
||||||
|
.select('price_usd, quantity')
|
||||||
|
.eq('user_id', userId);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
console.error('Error fetching collection total value:', error);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate total: sum of (price * quantity) for each card
|
||||||
|
const totalValue = data?.reduce((total, item) => {
|
||||||
|
const price = item.price_usd || 0;
|
||||||
|
const quantity = item.quantity || 0;
|
||||||
|
return total + (price * quantity);
|
||||||
|
}, 0) || 0;
|
||||||
|
|
||||||
|
return totalValue;
|
||||||
|
};
|
||||||
|
|
||||||
export const getUserCollectionPaginated = async (
|
export const getUserCollectionPaginated = async (
|
||||||
userId: string,
|
userId: string,
|
||||||
pageSize: number = 50,
|
pageSize: number = 50,
|
||||||
@@ -127,7 +149,8 @@ export const getUserCollectionPaginated = async (
|
|||||||
export const addCardToCollection = async (
|
export const addCardToCollection = async (
|
||||||
userId: string,
|
userId: string,
|
||||||
cardId: string,
|
cardId: string,
|
||||||
quantity: number = 1
|
quantity: number = 1,
|
||||||
|
priceUsd: number = 0
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
// Check if card already exists in collection
|
// Check if card already exists in collection
|
||||||
const { data: existing, error: fetchError } = await supabase
|
const { data: existing, error: fetchError } = await supabase
|
||||||
@@ -143,11 +166,12 @@ export const addCardToCollection = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (existing) {
|
if (existing) {
|
||||||
// Update existing card quantity
|
// Update existing card quantity and price
|
||||||
const { error: updateError } = await supabase
|
const { error: updateError } = await supabase
|
||||||
.from('collections')
|
.from('collections')
|
||||||
.update({
|
.update({
|
||||||
quantity: existing.quantity + quantity,
|
quantity: existing.quantity + quantity,
|
||||||
|
price_usd: priceUsd,
|
||||||
updated_at: new Date().toISOString()
|
updated_at: new Date().toISOString()
|
||||||
})
|
})
|
||||||
.eq('id', existing.id);
|
.eq('id', existing.id);
|
||||||
@@ -161,6 +185,7 @@ export const addCardToCollection = async (
|
|||||||
user_id: userId,
|
user_id: userId,
|
||||||
card_id: cardId,
|
card_id: cardId,
|
||||||
quantity: quantity,
|
quantity: quantity,
|
||||||
|
price_usd: priceUsd,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (insertError) throw insertError;
|
if (insertError) throw insertError;
|
||||||
@@ -169,7 +194,7 @@ export const addCardToCollection = async (
|
|||||||
|
|
||||||
export const addMultipleCardsToCollection = async (
|
export const addMultipleCardsToCollection = async (
|
||||||
userId: string,
|
userId: string,
|
||||||
cards: { cardId: string; quantity: number }[]
|
cards: { cardId: string; quantity: number; priceUsd?: number }[]
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
// Fetch existing cards in collection
|
// Fetch existing cards in collection
|
||||||
const cardIds = cards.map(c => c.cardId);
|
const cardIds = cards.map(c => c.cardId);
|
||||||
@@ -195,6 +220,7 @@ export const addMultipleCardsToCollection = async (
|
|||||||
toUpdate.push({
|
toUpdate.push({
|
||||||
id: existing.id,
|
id: existing.id,
|
||||||
quantity: existing.quantity + card.quantity,
|
quantity: existing.quantity + card.quantity,
|
||||||
|
price_usd: card.priceUsd || 0,
|
||||||
updated_at: new Date().toISOString(),
|
updated_at: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -202,6 +228,7 @@ export const addMultipleCardsToCollection = async (
|
|||||||
user_id: userId,
|
user_id: userId,
|
||||||
card_id: card.cardId,
|
card_id: card.cardId,
|
||||||
quantity: card.quantity,
|
quantity: card.quantity,
|
||||||
|
price_usd: card.priceUsd || 0,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -219,7 +246,11 @@ export const addMultipleCardsToCollection = async (
|
|||||||
for (const update of toUpdate) {
|
for (const update of toUpdate) {
|
||||||
const { error: updateError } = await supabase
|
const { error: updateError } = await supabase
|
||||||
.from('collections')
|
.from('collections')
|
||||||
.update({ quantity: update.quantity, updated_at: update.updated_at })
|
.update({
|
||||||
|
quantity: update.quantity,
|
||||||
|
price_usd: update.price_usd,
|
||||||
|
updated_at: update.updated_at
|
||||||
|
})
|
||||||
.eq('id', update.id);
|
.eq('id', update.id);
|
||||||
|
|
||||||
if (updateError) throw updateError;
|
if (updateError) throw updateError;
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
-- Add price_usd column to collections table
|
||||||
|
ALTER TABLE collections
|
||||||
|
ADD COLUMN IF NOT EXISTS price_usd DECIMAL(10, 2) DEFAULT 0;
|
||||||
|
|
||||||
|
-- Create index for faster price calculations
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_collections_price ON collections(price_usd);
|
||||||
|
|
||||||
|
-- Add comment
|
||||||
|
COMMENT ON COLUMN collections.price_usd IS 'USD price of the card at time of addition/update';
|
||||||
Reference in New Issue
Block a user