@@ -4,6 +4,7 @@ import { redirect } from "next/navigation";
44import { useUserStore } from "@/store/codev-store" ;
55import { Codev } from "@/types/home/codev" ;
66import { createClientServerComponent } from "@/utils/supabase/server" ;
7+ import { uploadNdaToStorage , updateCodevNdaUrls } from "@/utils/ndaStorageService" ;
78
89const uploadProfileImage = async (
910 file : File ,
@@ -69,6 +70,10 @@ export const signupUser = async (formData: FormData) => {
6970 parseInt ( formData . get ( "years_of_experience" ) as string ) || 0 ;
7071 const profileImage = formData . get ( "profileImage" ) as File ;
7172
73+ // Extract user names for NDA processing
74+ const firstName = formData . get ( "first_name" ) as string ;
75+ const lastName = formData . get ( "last_name" ) as string ;
76+
7277 // Check for an existing user (comparing lowercase email)
7378 const { data : existingUser } = await supabase
7479 . from ( "codev" )
@@ -100,32 +105,74 @@ export const signupUser = async (formData: FormData) => {
100105 if ( authError ) throw authError ;
101106 if ( ! user ) throw new Error ( "Failed to create user" ) ;
102107
103- // Extract NDA data from formData (passed from client-side localStorage)
108+ // **ENHANCED NDA PROCESSING WITH STORAGE SYSTEM**
104109 let nda_status = false ;
105110 let nda_signature : string | undefined = undefined ;
106111 let nda_document : string | undefined = undefined ;
107112 let nda_signed_at : string | undefined = undefined ;
113+ let ndaProcessedWithStorage = false ;
108114
109115 // Check if NDA data was included in the form submission
110116 const ndaSigned = formData . get ( "ndaSigned" ) as string ;
111117 const signature = formData . get ( "ndaSignature" ) as string ;
112118 const document = formData . get ( "ndaDocument" ) as string ;
113119 const signedAt = formData . get ( "ndaSignedAt" ) as string ;
114120
115- if ( ndaSigned === "true" && signature && document && signedAt ) {
116- nda_status = true ;
117- nda_signature = signature ;
118- nda_document = document ;
119- nda_signed_at = signedAt ;
121+ if ( ndaSigned === "true" && signature && document ) {
122+ console . log ( "NDA data found in form submission, processing with storage system..." ) ;
120123
121- console . log ( "NDA data found in form submission, integrating into signup..." ) ;
124+ try {
125+ // **NEW: Upload NDA files to Supabase Storage instead of storing base64**
126+ const ndaUploadResult = await uploadNdaToStorage (
127+ signature ,
128+ document ,
129+ {
130+ first_name : firstName ,
131+ last_name : lastName ,
132+ codev_id : user . id , // Use the newly created user ID
133+ }
134+ ) ;
135+
136+ if ( ndaUploadResult . success ) {
137+ console . log ( "NDA files uploaded to storage successfully" ) ;
138+
139+ // Set the storage URLs instead of base64 data
140+ nda_status = true ;
141+ nda_signature = ndaUploadResult . signatureUrl ;
142+ nda_document = ndaUploadResult . documentUrl ;
143+ nda_signed_at = new Date ( ) . toISOString ( ) ;
144+ ndaProcessedWithStorage = true ;
145+
146+ console . log ( `NDA stored in Supabase Storage:
147+ - Signature: ${ ndaUploadResult . signatureUrl }
148+ - Document: ${ ndaUploadResult . documentUrl } ` ) ;
149+ } else {
150+ console . error ( "Failed to upload NDA to storage:" , ndaUploadResult . error ) ;
151+
152+ // **FALLBACK: Use base64 storage if storage upload fails**
153+ console . log ( "Falling back to base64 storage for NDA data" ) ;
154+ nda_status = true ;
155+ nda_signature = signature ;
156+ nda_document = document ;
157+ nda_signed_at = signedAt || new Date ( ) . toISOString ( ) ;
158+ }
159+ } catch ( ndaError ) {
160+ console . error ( "Error processing NDA with storage system:" , ndaError ) ;
161+
162+ // **FALLBACK: Use base64 storage if there's an error**
163+ console . log ( "Falling back to base64 storage due to error" ) ;
164+ nda_status = true ;
165+ nda_signature = signature ;
166+ nda_document = document ;
167+ nda_signed_at = signedAt || new Date ( ) . toISOString ( ) ;
168+ }
122169 }
123170
124171 // Prepare user data for insertion into the "codev" table
125172 const userData : Codev = {
126173 id : user . id ,
127- first_name : formData . get ( "first_name" ) as string ,
128- last_name : formData . get ( "last_name" ) as string ,
174+ first_name : firstName ,
175+ last_name : lastName ,
129176 email_address,
130177 phone_number : formData . get ( "phone_number" ) as string ,
131178 address : null ,
@@ -175,12 +222,23 @@ export const signupUser = async (formData: FormData) => {
175222
176223 setUser ( userData ) ;
177224
178- // Return success with all necessary data
225+ // **ENHANCED: Return additional information about NDA processing**
226+ const responseMessage = ndaProcessedWithStorage
227+ ? "Account created successfully! NDA files securely stored. Redirecting to sign-in page..."
228+ : "Account created successfully! Redirecting to sign-in page..." ;
229+
230+ console . log ( `Signup completed for ${ email_address } :
231+ - User ID: ${ user . id }
232+ - NDA Status: ${ nda_status }
233+ - NDA Storage Method: ${ ndaProcessedWithStorage ? 'Supabase Storage' : 'Database' }
234+ - Profile Image: ${ image_url ? 'Uploaded' : 'None' } ` ) ;
235+
179236 return {
180237 success : true ,
181238 data : user ,
182239 ndaProcessed : nda_status ,
183- message : "Account created successfully! Redirecting to sign-in page..."
240+ ndaStorageMethod : ndaProcessedWithStorage ? 'storage' : 'database' ,
241+ message : responseMessage
184242 } ;
185243 } catch ( error : any ) {
186244 console . error ( "Signup error:" , error ) ;
0 commit comments