11import { error , redirect } from '@sveltejs/kit' ;
2- import { PrismaClient } from '@prisma/client' ;
2+ import { PrismaClient , LeadStatus , LeadSource } from '@prisma/client' ;
33
44const prisma = new PrismaClient ( ) ;
55
@@ -20,7 +20,10 @@ export async function load({ params, locals }) {
2020 }
2121
2222 const users = await prisma . userOrganization . findMany ( {
23- where : { organizationId : org . id }
23+ where : { organizationId : org . id } ,
24+ include : {
25+ user : true
26+ }
2427 } ) ;
2528
2629 return {
@@ -37,6 +40,49 @@ export const actions = {
3740 const org = locals . org ;
3841
3942 const leadEmail = formData . get ( 'email' ) ;
43+ const ownerId = formData . get ( 'ownerId' ) ;
44+ const firstName = formData . get ( 'firstName' ) ;
45+ const lastName = formData . get ( 'lastName' ) ;
46+
47+ // Validate required fields
48+ if ( ! firstName || typeof firstName !== 'string' || firstName . trim ( ) === '' ) {
49+ return {
50+ success : false ,
51+ error : 'First name is required.'
52+ } ;
53+ }
54+
55+ if ( ! lastName || typeof lastName !== 'string' || lastName . trim ( ) === '' ) {
56+ return {
57+ success : false ,
58+ error : 'Last name is required.'
59+ } ;
60+ }
61+
62+ if ( ! ownerId || typeof ownerId !== 'string' ) {
63+ return {
64+ success : false ,
65+ error : 'Owner ID is required.'
66+ } ;
67+ }
68+
69+ // Validate owner ID - ensure the user belongs to the organization
70+ const ownerValidation = await prisma . userOrganization . findUnique ( {
71+ where : {
72+ userId_organizationId : {
73+ userId : ownerId ,
74+ organizationId : org . id ,
75+ } ,
76+ } ,
77+ select : { id : true }
78+ } ) ;
79+
80+ if ( ! ownerValidation ) {
81+ return {
82+ success : false ,
83+ error : 'Invalid owner selected. User is not part of this organization.'
84+ } ;
85+ }
4086
4187 // Check if leadEmail is a non-empty string before proceeding
4288 if ( typeof leadEmail === 'string' && leadEmail . trim ( ) !== '' ) {
@@ -73,26 +119,49 @@ export const actions = {
73119 }
74120 }
75121
76- const updatedLead = {
77- firstName : formData . get ( 'firstName' ) ,
78- lastName : formData . get ( 'lastName' ) ,
79- email : formData . get ( 'email' ) ,
80- phone : formData . get ( 'phone' ) ,
81- company : formData . get ( 'company' ) ,
82- title : formData . get ( 'title' ) ,
83- status : formData . get ( 'status' ) ,
84- leadSource : formData . get ( 'leadSource' ) || null ,
85- industry : formData . get ( 'industry' ) || null ,
86- rating : formData . get ( 'rating' ) || null ,
87- description : formData . get ( 'description' ) || null ,
88- ownerId : formData . get ( 'ownerId' ) ,
89- organizationId : org . id // Always set from session
90- } ;
122+ // Get and validate form data
123+ const statusValue = formData . get ( 'status' ) ?. toString ( ) || 'NEW' ;
124+ const leadSourceValue = formData . get ( 'leadSource' ) ?. toString ( ) ;
125+
126+ // Simple string validation - Prisma will validate the enum at runtime
127+ const validStatuses = [ 'NEW' , 'PENDING' , 'CONTACTED' , 'QUALIFIED' , 'UNQUALIFIED' , 'CONVERTED' ] ;
128+ const validSources = [ 'WEB' , 'PHONE_INQUIRY' , 'PARTNER_REFERRAL' , 'COLD_CALL' , 'TRADE_SHOW' , 'EMPLOYEE_REFERRAL' , 'ADVERTISEMENT' , 'OTHER' ] ;
91129
130+ if ( ! validStatuses . includes ( statusValue ) ) {
131+ return {
132+ success : false ,
133+ error : 'Invalid lead status provided.'
134+ } ;
135+ }
136+
137+ if ( leadSourceValue && ! validSources . includes ( leadSourceValue ) ) {
138+ return {
139+ success : false ,
140+ error : 'Invalid lead source provided.'
141+ } ;
142+ }
143+
92144 try {
145+ // Use the correct Prisma update method with proper typing
93146 await prisma . lead . update ( {
94147 where : { id : lead_id } ,
95- data : updatedLead
148+ data : {
149+ firstName : firstName . trim ( ) ,
150+ lastName : lastName . trim ( ) ,
151+ email : formData . get ( 'email' ) ?. toString ( ) || null ,
152+ phone : formData . get ( 'phone' ) ?. toString ( ) || null ,
153+ company : formData . get ( 'company' ) ?. toString ( ) || null ,
154+ title : formData . get ( 'title' ) ?. toString ( ) || null ,
155+ industry : formData . get ( 'industry' ) ?. toString ( ) || null ,
156+ rating : formData . get ( 'rating' ) ?. toString ( ) || null ,
157+ description : formData . get ( 'description' ) ?. toString ( ) || null ,
158+ ownerId : ownerId ,
159+ organizationId : org . id ,
160+ // @ts -ignore - Bypassing TypeScript enum checking for validated enum values
161+ status : statusValue ,
162+ // @ts -ignore - Bypassing TypeScript enum checking for validated enum values
163+ leadSource : leadSourceValue || null
164+ }
96165 } ) ;
97166
98167 return { success : true } ;
0 commit comments