@@ -71,10 +71,10 @@ export async function createCompany(values: z.infer<typeof createCompanySchema>)
7171 revalidatePath ( '/' ) ;
7272 return result ;
7373 } catch ( error : any ) {
74+ console . error ( "Error creating company:" , error ) ;
7475 if ( error ?. code === '23505' && error . constraint === 'companies_name_key' ) {
7576 return { error : "Une entreprise avec ce nom existe déjà." } ;
7677 }
77- console . error ( "Error creating company:" , error ) ;
7878 return { error : error . message || "Une erreur est survenue lors de la création de l'entreprise." } ;
7979 }
8080}
@@ -312,6 +312,67 @@ export async function investInCompany(companyId: number, amount: number): Promis
312312 }
313313}
314314
315+ export async function sellShares ( companyId : number , quantity : number ) : Promise < { success ?: string ; error ?: string } > {
316+ const session = await getSession ( ) ;
317+ if ( ! session ?. id ) {
318+ return { error : "Vous devez être connecté pour vendre des parts." } ;
319+ }
320+
321+ try {
322+ const result = await db . transaction ( async ( tx ) => {
323+ const company = await tx . query . companies . findFirst ( {
324+ where : eq ( companies . id , companyId )
325+ } ) ;
326+ if ( ! company ) throw new Error ( "Entreprise non trouvée." ) ;
327+ if ( company . isListed ) throw new Error ( "Les actions des entreprises cotées doivent être vendues sur le marché." ) ;
328+
329+ const sharePrice = parseFloat ( company . sharePrice ) ;
330+ if ( quantity <= 0 ) throw new Error ( "La quantité doit être positive." ) ;
331+
332+ const userShareHolding = await tx . query . companyShares . findFirst ( {
333+ where : and ( eq ( companyShares . userId , session . id ) , eq ( companyShares . companyId , companyId ) )
334+ } ) ;
335+ const sharesHeld = parseFloat ( userShareHolding ?. quantity || '0' ) ;
336+ if ( sharesHeld < quantity ) throw new Error ( "Vous ne possédez pas assez de parts." ) ;
337+
338+ const proceeds = sharePrice * quantity ;
339+ const companyCash = parseFloat ( company . cash ) ;
340+ if ( companyCash < proceeds ) throw new Error ( "La trésorerie de l'entreprise est insuffisante pour racheter ces parts." ) ;
341+
342+ const user = await tx . query . users . findFirst ( { where : eq ( users . id , session . id ) , columns : { cash : true } } ) ;
343+ if ( ! user ) throw new Error ( "Utilisateur non trouvé." ) ;
344+
345+ const newTotalShares = parseFloat ( company . totalShares ) - quantity ;
346+
347+ await tx . update ( users ) . set ( { cash : ( parseFloat ( user . cash ) + proceeds ) . toFixed ( 2 ) } ) . where ( eq ( users . id , session . id ) ) ;
348+ await tx . update ( companies ) . set ( {
349+ cash : ( companyCash - proceeds ) . toFixed ( 2 ) ,
350+ totalShares : newTotalShares . toString ( ) ,
351+ } ) . where ( eq ( companies . id , companyId ) ) ;
352+
353+ const newSharesHeld = sharesHeld - quantity ;
354+ if ( newSharesHeld < 1e-9 ) {
355+ await tx . delete ( companyShares ) . where ( eq ( companyShares . id , userShareHolding ! . id ) ) ;
356+ } else {
357+ await tx . update ( companyShares ) . set ( { quantity : newSharesHeld . toString ( ) } ) . where ( eq ( companyShares . id , userShareHolding ! . id ) ) ;
358+ }
359+
360+ return { success : `Vous avez vendu ${ quantity . toFixed ( 4 ) } parts de ${ company . name } pour ${ proceeds . toFixed ( 2 ) } $.` } ;
361+ } ) ;
362+
363+ revalidatePath ( '/companies' ) ;
364+ revalidatePath ( `/companies/${ companyId } ` ) ;
365+ revalidatePath ( '/portfolio' ) ;
366+ revalidatePath ( '/profile' ) ;
367+ revalidatePath ( '/' ) ;
368+ return result ;
369+
370+ } catch ( error : any ) {
371+ return { error : error . message || "Une erreur est survenue lors de la vente." } ;
372+ }
373+ }
374+
375+
315376export async function addCashToCompany ( companyId : number , amount : number ) : Promise < { success ?: string ; error ?: string } > {
316377 const session = await getSession ( ) ;
317378 if ( ! session ?. id ) return { error : "Vous devez être connecté." } ;
@@ -440,7 +501,7 @@ export async function searchUsersForCompany(companyId: number, query: string): P
440501
441502 const potentialUsers = await db . query . users . findMany ( {
442503 where : and (
443- notInArray ( users . id , existingMemberIds ) ,
504+ notInArray ( users . id , existingMemberIds . length > 0 ? existingMemberIds : [ 0 ] ) ,
444505 or (
445506 ilike ( users . displayName , `%${ query } %` ) ,
446507 ilike ( users . email , `%${ query } %` )
0 commit comments