@@ -49,6 +49,8 @@ export const actions = {
4949 const org = locals . org ;
5050
5151 try {
52+ console . log ( 'Starting lead conversion for lead:' , lead_id ) ;
53+
5254 const lead = await prisma . lead . findUnique ( {
5355 where : { id : lead_id , organizationId : org . id } ,
5456 include : {
@@ -65,6 +67,7 @@ export const actions = {
6567 return { status : 'success' , message : 'Lead already converted' } ;
6668 }
6769
70+ console . log ( 'Creating contact...' ) ;
6871 const contact = await prisma . contact . create ( {
6972 data : {
7073 firstName : lead . firstName ,
@@ -77,10 +80,12 @@ export const actions = {
7780 organization : { connect : { id : lead . organizationId } }
7881 }
7982 } ) ;
83+ console . log ( 'Contact created with ID:' , contact . id ) ;
8084
8185 let accountId = null ;
8286 let account = null ;
8387 if ( lead . company ) {
88+ console . log ( 'Creating account for company:' , lead . company ) ;
8489 account = await prisma . account . create ( {
8590 data : {
8691 name : lead . company ,
@@ -90,7 +95,9 @@ export const actions = {
9095 }
9196 } ) ;
9297 accountId = account . id ;
98+ console . log ( 'Account created with ID:' , accountId ) ;
9399
100+ console . log ( 'Creating account-contact relationship...' ) ;
94101 await prisma . accountContactRelationship . create ( {
95102 data : {
96103 account : { connect : { id : account . id } } ,
@@ -99,46 +106,48 @@ export const actions = {
99106 role : 'Primary Contact'
100107 }
101108 } ) ;
102- }
103-
104- const opportunityData = {
105- name : `${ lead . company || lead . firstName + ' ' + lead . lastName } Opportunity` ,
106- stage : 'PROSPECTING' ,
107- amount : 0 ,
108- closeDate : new Date ( Date . now ( ) + 30 * 24 * 60 * 60 * 1000 ) ,
109- contacts : { connect : { id : contact . id } } ,
110- owner : { connect : { id : lead . ownerId } } ,
111- organization : { connect : { id : lead . organizationId } }
112- } ;
113-
114- if ( ! accountId ) {
115- const placeholderAccount = await prisma . account . create ( {
109+ console . log ( 'Account-contact relationship created' ) ;
110+ } else {
111+ console . log ( 'Creating placeholder account...' ) ;
112+ // Create a placeholder account if no company
113+ account = await prisma . account . create ( {
116114 data : {
117115 name : `${ lead . firstName } ${ lead . lastName } Account` ,
118116 owner : { connect : { id : lead . ownerId } } ,
119117 organization : { connect : { id : lead . organizationId } }
120118 }
121119 } ) ;
120+ accountId = account . id ;
121+ console . log ( 'Placeholder account created with ID:' , accountId ) ;
122122
123- accountId = placeholderAccount . id ;
124- account = placeholderAccount ;
125-
123+ console . log ( 'Creating account-contact relationship...' ) ;
126124 await prisma . accountContactRelationship . create ( {
127125 data : {
128- account : { connect : { id : placeholderAccount . id } } ,
126+ account : { connect : { id : account . id } } ,
129127 contact : { connect : { id : contact . id } } ,
130128 isPrimary : true ,
131129 role : 'Primary Contact'
132130 }
133131 } ) ;
132+ console . log ( 'Account-contact relationship created' ) ;
134133 }
135134
136- opportunityData . account = { connect : { id : accountId } } ;
137-
135+ console . log ( 'Creating opportunity...' ) ;
138136 const opportunity = await prisma . opportunity . create ( {
139- data : opportunityData
137+ data : {
138+ name : `${ lead . company || lead . firstName + ' ' + lead . lastName } Opportunity` ,
139+ stage : 'PROSPECTING' ,
140+ amount : 0 ,
141+ closeDate : new Date ( Date . now ( ) + 30 * 24 * 60 * 60 * 1000 ) ,
142+ contacts : { connect : { id : contact . id } } ,
143+ owner : { connect : { id : lead . ownerId } } ,
144+ organization : { connect : { id : lead . organizationId } } ,
145+ account : { connect : { id : accountId } }
146+ }
140147 } ) ;
148+ console . log ( 'Opportunity created with ID:' , opportunity . id ) ;
141149
150+ console . log ( 'Updating lead status...' ) ;
142151 await prisma . lead . update ( {
143152 where : { id : lead_id } ,
144153 data : {
@@ -151,17 +160,35 @@ export const actions = {
151160 contact : { connect : { id : contact . id } }
152161 }
153162 } ) ;
163+ console . log ( 'Lead status updated successfully' ) ;
154164
165+ console . log ( 'Lead conversion completed, account created:' , accountId ) ;
166+
155167 return {
156168 status : 'success' ,
157169 message : 'Lead successfully converted' ,
170+ redirectTo : `/app/accounts/${ accountId } ` ,
158171 contact,
159172 account,
160173 opportunity
161174 } ;
162175 } catch ( err ) {
163- console . error ( 'Error converting lead:' , err . message ) ;
164- return fail ( 500 , { status : 'error' , message : 'Failed to convert lead' } ) ;
176+ console . error ( 'Error converting lead:' , err ) ;
177+
178+ // Extract meaningful error message
179+ let errorMessage = 'Failed to convert lead' ;
180+ if ( err instanceof Error ) {
181+ errorMessage = err . message ;
182+ } else if ( typeof err === 'string' ) {
183+ errorMessage = err ;
184+ } else if ( err && typeof err === 'object' && 'message' in err && typeof err . message === 'string' ) {
185+ errorMessage = err . message ;
186+ }
187+
188+ return fail ( 500 , {
189+ status : 'error' ,
190+ message : `Error converting lead: ${ errorMessage } `
191+ } ) ;
165192 }
166193 } ,
167194
@@ -206,10 +233,11 @@ export const actions = {
206233 return {
207234 status : 'success' ,
208235 message : 'Comment added successfully' ,
236+ commentAdded : true ,
209237 comments : updatedLead ?. comments || [ ]
210238 } ;
211239 } catch ( err ) {
212- console . error ( 'Error adding comment:' , err . message ) ;
240+ console . error ( 'Error adding comment:' , err instanceof Error ? err . message : String ( err ) ) ;
213241 if ( err instanceof z . ZodError ) {
214242 return fail ( 400 , { status : 'error' , message : err . errors [ 0 ] . message } ) ;
215243 }
0 commit comments