@@ -10,9 +10,9 @@ import { updatePriceFromTrade } from './assets';
1010import { getRigById } from '@/lib/mining' ;
1111
1212const createCompanySchema = z . object ( {
13- name : z . string ( ) . min ( 3 , "Le nom doit faire au moins 3 caractères." ) . max ( 50 ) ,
14- industry : z . string ( ) . min ( 3 , "L'industrie doit faire au moins 3 caractères." ) . max ( 50 ) ,
15- description : z . string ( ) . min ( 10 , "La description doit faire au moins 10 caractères." ) . max ( 200 ) ,
13+ name : z . string ( ) . min ( 3 , "Le nom doit faire au moins 3 caractères." ) . max ( 50 , "Le nom ne doit pas dépasser 50 caractères." ) ,
14+ industry : z . string ( ) . min ( 3 , "L'industrie doit faire au moins 3 caractères." ) . max ( 50 , "L'industrie ne doit pas dépasser 50 caractères." ) ,
15+ description : z . string ( ) . min ( 10 , "La description doit faire au moins 10 caractères." ) . max ( 200 , "La description ne doit pas dépasser 200 caractères." ) ,
1616} ) ;
1717
1818export async function createCompany ( values : z . infer < typeof createCompanySchema > ) : Promise < { success ?: string ; error ?: string } > {
@@ -28,6 +28,7 @@ export async function createCompany(values: z.infer<typeof createCompanySchema>)
2828 return { error : "Données invalides." } ;
2929 }
3030 const { name, industry, description } = validatedFields . data ;
31+ const ticker = name . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) . substring ( 0 , 4 ) . toUpperCase ( ) ;
3132
3233 try {
3334 const result = await db . transaction ( async ( tx ) => {
@@ -37,6 +38,11 @@ export async function createCompany(values: z.infer<typeof createCompanySchema>)
3738 const userCash = parseFloat ( user . cash ) ;
3839 if ( userCash < creationCost ) throw new Error ( `Fonds insuffisants. La création d'une entreprise coûte ${ creationCost . toLocaleString ( ) } $.` ) ;
3940
41+ const existingTicker = await tx . query . companies . findFirst ( { where : eq ( companies . ticker , ticker ) } ) ;
42+ if ( existingTicker ) {
43+ throw new Error ( "Une entreprise avec un ticker similaire existe déjà. Veuillez choisir un nom légèrement différent." ) ;
44+ }
45+
4046 // Deduct cost from user
4147 const newUserCash = userCash - creationCost ;
4248 await tx . update ( users ) . set ( { cash : newUserCash . toFixed ( 2 ) } ) . where ( eq ( users . id , session . id ) ) ;
@@ -49,7 +55,8 @@ export async function createCompany(values: z.infer<typeof createCompanySchema>)
4955 creatorId : session . id ,
5056 cash : creationCost . toFixed ( 2 ) ,
5157 totalShares : '1000.00000000' ,
52- sharePrice : '1.00'
58+ sharePrice : '1.00' ,
59+ ticker : ticker ,
5360 } ) . returning ( ) ;
5461
5562 // Add the creator as the CEO
@@ -68,8 +75,8 @@ export async function createCompany(values: z.infer<typeof createCompanySchema>)
6875 revalidatePath ( '/' ) ;
6976 return result ;
7077 } catch ( error : any ) {
71- // Check for unique constraint violation
72- if ( error ?. code === '23505' ) {
78+ // Check for unique constraint violation on name
79+ if ( error ?. code === '23505' && error . constraint === 'companies_name_key' ) {
7380 return { error : "Une entreprise avec ce nom existe déjà." } ;
7481 }
7582 console . error ( "Error creating company:" , error ) ;
@@ -237,6 +244,14 @@ export async function getCompanyById(companyId: number) {
237244 const totalShares = parseFloat ( company . totalShares ) ;
238245 const sharePrice = totalShares > 0 ? companyValue / totalShares : parseFloat ( company . sharePrice ) ;
239246
247+ // Update the stored share price to reflect the latest calculation
248+ if ( Math . abs ( sharePrice - parseFloat ( company . sharePrice ) ) > 0.00001 ) { // Only update if there's a meaningful change
249+ await db . update ( companies )
250+ . set ( { sharePrice : sharePrice . toString ( ) } )
251+ . where ( eq ( companies . id , company . id ) ) ;
252+ }
253+
254+
240255 return {
241256 ...company ,
242257 cash : companyCash ,
@@ -701,3 +716,30 @@ export async function removeMemberFromCompany(companyId: number, memberIdToRemov
701716 return { error : error . message || "Une erreur est survenue." } ;
702717 }
703718}
719+
720+
721+ export async function listCompanyOnMarket ( companyId : number ) : Promise < { success ?: string ; error ?: string } > {
722+ const session = await getSession ( ) ;
723+ if ( ! session ?. id ) {
724+ return { error : 'Non autorisé.' } ;
725+ }
726+
727+ try {
728+ const member = await db . query . companyMembers . findFirst ( {
729+ where : and ( eq ( companyMembers . companyId , companyId ) , eq ( companyMembers . userId , session . id ) ) ,
730+ } ) ;
731+
732+ if ( ! member || member . role !== 'ceo' ) {
733+ return { error : 'Seul le PDG peut mettre une entreprise en bourse.' } ;
734+ }
735+
736+ await db . update ( companies ) . set ( { isListed : true } ) . where ( eq ( companies . id , companyId ) ) ;
737+
738+ revalidatePath ( '/trading' ) ;
739+ revalidatePath ( `/companies/${ companyId } ` ) ;
740+
741+ return { success : 'Entreprise mise en bourse avec succès !' } ;
742+ } catch ( error : any ) {
743+ return { error : error . message || "Une erreur est survenue." } ;
744+ }
745+ }
0 commit comments