@@ -69,17 +69,6 @@ export const updateCards = async (email: string, rowsToUpdate: CardAmountUpdate[
6969 const now = new Date ( )
7070 const nowString = now . toISOString ( )
7171
72- const { data : account , error : accountError } = await supabase
73- . from ( 'accounts' )
74- . update ( { collection_last_updated : now } )
75- . eq ( 'email' , email )
76- . select ( '*, trade_rarity_settings:trade_rarity_settings!email(*)' )
77- . single ( )
78-
79- if ( accountError ) {
80- throw new Error ( `Error fetching account: ${ accountError . message } ` )
81- }
82-
8372 // Update collection records
8473 const collectionRows : CollectionRowUpdate [ ] = rowsToUpdate . map ( ( row ) => ( {
8574 email,
@@ -97,15 +86,36 @@ export const updateCards = async (email: string, rowsToUpdate: CardAmountUpdate[
9786 //deduplicate amountRows on internal_id, needed for card csv import feature
9887 . filter ( ( row , index , self ) => index === self . findIndex ( ( r ) => r . internal_id === row . internal_id ) )
9988
100- //then update the card amounts
101- const { error : cardAmountsError } = await supabase . from ( 'card_amounts' ) . upsert ( amountRows )
102- if ( cardAmountsError ) {
103- throw new Error ( `Error bulk updating collection: ${ cardAmountsError ?. message } ` )
104- }
89+ // Execute all three database calls in parallel
90+ let account : AccountRow
91+ try {
92+ const [ accountResult , cardAmountsResult , collectionResult ] = await Promise . all ( [
93+ supabase
94+ . from ( 'accounts' )
95+ . update ( { collection_last_updated : now } )
96+ . eq ( 'email' , email )
97+ . select ( '*, trade_rarity_settings:trade_rarity_settings!email(*)' )
98+ . single ( ) ,
99+ supabase . from ( 'card_amounts' ) . upsert ( amountRows ) ,
100+ supabase . from ( 'collection' ) . upsert ( collectionRows ) ,
101+ ] )
102+
103+ if ( accountResult . error ) {
104+ throw new Error ( `Error fetching account: ${ accountResult . error . message } ` )
105+ }
106+ if ( cardAmountsResult . error ) {
107+ throw new Error ( `Error bulk updating card amounts: ${ cardAmountsResult . error . message } ` )
108+ }
109+ if ( collectionResult . error ) {
110+ throw new Error ( `Error bulk updating collection: ${ collectionResult . error . message } ` )
111+ }
105112
106- const { error : collectionError } = await supabase . from ( 'collection' ) . upsert ( collectionRows )
107- if ( collectionError ) {
108- throw new Error ( `Error bulk updating collection: ${ collectionError ?. message } ` )
113+ account = accountResult . data as AccountRow
114+ } catch ( error ) {
115+ // Invalidate local cache by removing records from localStorage
116+ localStorage . removeItem ( `${ COLLECTION_CACHE_KEY } _${ email } ` )
117+ localStorage . removeItem ( `${ COLLECTION_TIMESTAMP_KEY } _${ email } ` )
118+ throw error
109119 }
110120
111121 // Update cache with the changes
0 commit comments