@@ -179,24 +179,21 @@ export async function getCompaniesForUserDashboard() {
179179 const sharesByCompanyId = new Map ( userShares . map ( s => [ s . companyId , s ] ) ) ;
180180 const membershipsByCompanyId = new Map ( userMemberships . map ( m => [ m . companyId , m ] ) ) ;
181181
182- const userRelatedPrivateCompanyIds = new Set < number > ( ) ;
183- const managedCompanyIds = new Set < number > ( ) ;
184- userMemberships . forEach ( m => {
185- userRelatedPrivateCompanyIds . add ( m . companyId ) ;
186- managedCompanyIds . add ( m . companyId ) ;
187- } ) ;
188- userShares . forEach ( s => userRelatedPrivateCompanyIds . add ( s . companyId ) ) ;
189-
190182 const managedCompanies : any [ ] = [ ] ;
191183 const investedCompanies : any [ ] = [ ] ;
192184
193185 const privateCompanies = companiesWithMarketData . filter ( c => ! c . isListed ) ;
186+ const managedAndInvestedIds = new Set < number > ( ) ;
187+
188+ // Process all companies user has a relation to (private and listed)
189+ const allRelatedCompanyIds = new Set ( [ ...sharesByCompanyId . keys ( ) , ...membershipsByCompanyId . keys ( ) ] ) ;
190+
191+ for ( const companyId of allRelatedCompanyIds ) {
192+ const company = companiesWithMarketData . find ( c => c . id === companyId ) ;
193+ if ( ! company ) continue ;
194194
195- // Process private companies the user is related to
196- for ( const company of privateCompanies ) {
197- if ( userRelatedPrivateCompanyIds . has ( company . id ) ) {
198- const membership = membershipsByCompanyId . get ( company . id ) ;
199- const shareData = sharesByCompanyId . get ( company . id ) ;
195+ const membership = membershipsByCompanyId . get ( companyId ) ;
196+ const shareData = sharesByCompanyId . get ( companyId ) ;
200197 const sharesHeld = parseFloat ( shareData ?. quantity || '0' ) ;
201198
202199 const companyData = {
@@ -208,16 +205,17 @@ export async function getCompaniesForUserDashboard() {
208205
209206 if ( membership ) {
210207 managedCompanies . push ( companyData ) ;
211- } else if ( sharesHeld > 0 ) {
208+ managedAndInvestedIds . add ( company . id ) ;
209+ } else if ( sharesHeld > 0 && ! company . isListed ) {
210+ // Only add to invested if not managed and it's a private company
212211 investedCompanies . push ( companyData ) ;
212+ managedAndInvestedIds . add ( company . id ) ;
213213 }
214- }
215214 }
216215
217- // Filter out private companies the user is related to from the "other" list
218- const otherPrivateCompanies = privateCompanies . filter ( c => ! userRelatedPrivateCompanyIds . has ( c . id ) ) ;
216+ const otherPrivateCompanies = privateCompanies . filter ( c => ! allRelatedCompanyIds . has ( c . id ) ) ;
219217
220- // Add user's share data to listed companies
218+ // Add user's share data to all listed companies
221219 for ( const company of allListedCompanies ) {
222220 const shareData = sharesByCompanyId . get ( company . id ) ;
223221 if ( shareData ) {
@@ -227,9 +225,6 @@ export async function getCompaniesForUserDashboard() {
227225 }
228226 }
229227
230- const listedCompaniesTheUserIsRelatedTo = new Set ( allListedCompanies . filter ( c => sharesByCompanyId . has ( c . id ) || managedCompanyIds . has ( c . id ) ) . map ( c => c . id ) ) ;
231- const finalListedCompanies = allListedCompanies . filter ( c => ! listedCompaniesTheUserIsRelatedTo . has ( c . id ) ) ;
232-
233228 return { managedCompanies, investedCompanies, otherPrivateCompanies, listedCompanies : allListedCompanies } ;
234229}
235230
@@ -474,12 +469,16 @@ export async function sellShares(companyId: number, quantity: number): Promise<{
474469 const sharePrice = currentTotalShares > 0 ? currentNav / currentTotalShares : 0 ;
475470 const proceeds = sharePrice * quantity ;
476471
472+ // New 35/65 rule
473+ const companyLiability = proceeds * 0.35 ;
477474 const companyCash = parseFloat ( company . cash ) ;
478- if ( companyCash < proceeds ) throw new Error ( "La trésorerie de l'entreprise est insuffisante pour racheter ces parts." ) ;
475+
476+ if ( companyCash < companyLiability ) throw new Error ( "La trésorerie de l'entreprise est insuffisante pour racheter ces parts." ) ;
479477
480478 const user = await tx . query . users . findFirst ( { where : eq ( users . id , session . id ) , columns : { cash : true } } ) ;
481479 if ( ! user ) throw new Error ( "Utilisateur non trouvé." ) ;
482480
481+ // User gets full proceeds
483482 await tx . update ( users ) . set ( { cash : ( parseFloat ( user . cash ) + proceeds ) . toString ( ) } ) . where ( eq ( users . id , session . id ) ) ;
484483
485484 const newSharesHeld = sharesHeld - quantity ;
@@ -489,7 +488,8 @@ export async function sellShares(companyId: number, quantity: number): Promise<{
489488 await tx . update ( companyShares ) . set ( { quantity : newSharesHeld . toString ( ) } ) . where ( eq ( companyShares . id , userShareHolding ! . id ) ) ;
490489 }
491490
492- const newCompanyCash = companyCash - proceeds ;
491+ // Company cash decreases by its liability, total shares decrease by amount sold
492+ const newCompanyCash = companyCash - companyLiability ;
493493 const newTotalShares = parseFloat ( company . totalShares ) - quantity ;
494494 await tx . update ( companies ) . set ( {
495495 cash : newCompanyCash . toString ( ) ,
0 commit comments