refactor getCollectionTotalValue to use pre-calculated value from user profile
This commit is contained in:
@@ -82,26 +82,20 @@ export interface PaginatedCollectionResult {
|
|||||||
hasMore: boolean;
|
hasMore: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get total collection value from database (lightweight query)
|
// Get total collection value from user profile (pre-calculated by triggers)
|
||||||
export const getCollectionTotalValue = async (userId: string): Promise<number> => {
|
export const getCollectionTotalValue = async (userId: string): Promise<number> => {
|
||||||
const { data, error } = await supabase
|
const { data, error } = await supabase
|
||||||
.from('collections')
|
.from('profiles')
|
||||||
.select('price_usd, quantity')
|
.select('collection_total_value')
|
||||||
.eq('user_id', userId);
|
.eq('id', userId)
|
||||||
|
.single();
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error('Error fetching collection total value:', error);
|
console.error('Error fetching collection total value:', error);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate total: sum of (price * quantity) for each card
|
return data?.collection_total_value || 0;
|
||||||
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 (
|
||||||
|
|||||||
Reference in New Issue
Block a user