@@ -168,6 +168,103 @@ export function ChatProvider({ children }: { children: ReactNode }) {
168168 if ( isActive ) setUnreadCount ( 0 ) ;
169169 } ;
170170
171+ /* ------------------------------------------------------------------------ */
172+ /* Helper function to get UUID from user ID */
173+ /* ------------------------------------------------------------------------ */
174+
175+ const getUserIdAsUUID = async ( profileId : string ) : Promise < string > => {
176+ if ( ! profile ) {
177+ throw new Error ( 'No user profile available' ) ;
178+ }
179+
180+ // If it's already a UUID, return it
181+ if ( profileId . match ( / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 12 } $ / i) ) {
182+ return profileId ;
183+ }
184+
185+ // Otherwise, look it up in the database
186+ const cleanPhone = profile . phone ? profile . phone . replace ( / \D / g, '' ) : '' ;
187+
188+ // Try to find user by phone, email, or username
189+ let dbProfile = null ;
190+
191+ if ( cleanPhone ) {
192+ const { data, error } = await supabase
193+ . from ( 'profiles' )
194+ . select ( 'id' )
195+ . eq ( 'phone' , cleanPhone )
196+ . maybeSingle ( ) ;
197+ if ( ! error && data ) {
198+ dbProfile = data ;
199+ }
200+ }
201+
202+ if ( ! dbProfile && profile . email ) {
203+ const { data, error } = await supabase
204+ . from ( 'profiles' )
205+ . select ( 'id' )
206+ . eq ( 'email' , profile . email )
207+ . maybeSingle ( ) ;
208+ if ( ! error && data ) {
209+ dbProfile = data ;
210+ }
211+ }
212+
213+ if ( ! dbProfile && profile . username ) {
214+ const { data, error } = await supabase
215+ . from ( 'profiles' )
216+ . select ( 'id' )
217+ . eq ( 'username' , profile . username )
218+ . maybeSingle ( ) ;
219+ if ( ! error && data ) {
220+ dbProfile = data ;
221+ }
222+ }
223+
224+ // If found, return the UUID
225+ if ( dbProfile ) {
226+ return dbProfile . id ;
227+ }
228+
229+ // If not found, try to create the user profile in the database
230+ try {
231+ const { data : newProfile , error : createError } = await supabase
232+ . from ( 'profiles' )
233+ . insert ( {
234+ name : profile . name ,
235+ username : profile . username ,
236+ phone : cleanPhone || null ,
237+ email : profile . email || null ,
238+ password : profile . password || '' ,
239+ role : profile . role || 'user' ,
240+ profile_pic_url : profile . profile_pic_url || 'https://api.dicebear.com/7.x/thumbs/svg?seed=default' ,
241+ location : profile . location || 'Broville' ,
242+ is_verified : profile . isVerified || true ,
243+ } )
244+ . select ( 'id' )
245+ . single ( ) ;
246+
247+ if ( createError ) {
248+ // If creation fails (e.g., username already exists), try one more lookup
249+ const { data : finalLookup } = await supabase
250+ . from ( 'profiles' )
251+ . select ( 'id' )
252+ . or ( `phone.eq.${ cleanPhone } ,email.eq.${ profile . email || '' } ,username.eq.${ profile . username } ` )
253+ . maybeSingle ( ) ;
254+
255+ if ( finalLookup ) {
256+ return finalLookup . id ;
257+ }
258+
259+ throw new Error ( `Unable to create or find user profile: ${ createError . message } ` ) ;
260+ }
261+
262+ return newProfile . id ;
263+ } catch ( createErr : any ) {
264+ throw new Error ( `User profile not found in database and could not be created. Please ensure you are logged in with a valid account. Error: ${ createErr . message } ` ) ;
265+ }
266+ } ;
267+
171268 /* ------------------------------------------------------------------------ */
172269 /* Send message */
173270 /* ------------------------------------------------------------------------ */
@@ -176,13 +273,16 @@ export function ChatProvider({ children }: { children: ReactNode }) {
176273 content_text ?: string | null ;
177274 content_image_urls ?: string [ ] ;
178275 } ) => {
179- if ( ! user ) throw new Error ( "User not found" ) ;
276+ if ( ! user || ! profile ) throw new Error ( "User not found" ) ;
180277
181278 try {
279+ // Get UUID for user ID
280+ const userId = await getUserIdAsUUID ( user . id ) ;
281+
182282 const { data, error } = await supabase
183283 . from ( 'chat_messages' )
184284 . insert ( {
185- user_id : user . id ,
285+ user_id : userId ,
186286 content_text : messageData . content_text || null ,
187287 content_image_urls : messageData . content_image_urls || null ,
188288 reactions : { } ,
@@ -230,14 +330,17 @@ export function ChatProvider({ children }: { children: ReactNode }) {
230330 /* ------------------------------------------------------------------------ */
231331
232332 const deleteMessage = async ( messageId : string ) => {
233- if ( ! user ) return ;
333+ if ( ! user || ! profile ) return ;
234334
235335 try {
336+ // Get UUID for user ID
337+ const userId = await getUserIdAsUUID ( user . id ) ;
338+
236339 const { error } = await supabase
237340 . from ( 'chat_messages' )
238341 . delete ( )
239342 . eq ( 'id' , messageId )
240- . eq ( 'user_id' , user . id ) ; // Only allow deleting own messages
343+ . eq ( 'user_id' , userId ) ; // Only allow deleting own messages
241344
242345 if ( error ) throw error ;
243346
@@ -258,25 +361,42 @@ export function ChatProvider({ children }: { children: ReactNode }) {
258361 /* ------------------------------------------------------------------------ */
259362
260363 const addReaction = async ( messageId : string , emoji : string ) => {
261- if ( ! user ) return ;
364+ if ( ! user || ! profile ) return ;
262365
263366 try {
264- setMessages ( ( prev ) =>
265- prev . map ( ( msg ) => {
266- if ( msg . id !== messageId ) return msg ;
367+ // Get UUID for user ID
368+ const userId = await getUserIdAsUUID ( user . id ) ;
267369
268- const reactions = { ...( msg . reactions || { } ) } ;
269- const users = reactions [ emoji ] ? [ ...reactions [ emoji ] ] : [ ] ;
370+ // Get current message to update reactions
371+ const currentMessage = messages . find ( m => m . id === messageId ) ;
372+ if ( ! currentMessage ) return ;
270373
271- const index = users . indexOf ( user . id ) ;
374+ const reactions = { ...( currentMessage . reactions || { } ) } ;
375+ const users = reactions [ emoji ] ? [ ...reactions [ emoji ] ] : [ ] ;
272376
273- if ( index > - 1 ) {
274- users . splice ( index , 1 ) ;
275- users . length ? ( reactions [ emoji ] = users ) : delete reactions [ emoji ] ;
276- } else {
277- reactions [ emoji ] = [ ...users , user . id ] ;
278- }
377+ const index = users . indexOf ( userId ) ;
378+
379+ if ( index > - 1 ) {
380+ // Remove reaction
381+ users . splice ( index , 1 ) ;
382+ users . length ? ( reactions [ emoji ] = users ) : delete reactions [ emoji ] ;
383+ } else {
384+ // Add reaction
385+ reactions [ emoji ] = [ ...users , userId ] ;
386+ }
387+
388+ // Update in database
389+ const { error } = await supabase
390+ . from ( 'chat_messages' )
391+ . update ( { reactions } )
392+ . eq ( 'id' , messageId ) ;
279393
394+ if ( error ) throw error ;
395+
396+ // Update local state
397+ setMessages ( ( prev ) =>
398+ prev . map ( ( msg ) => {
399+ if ( msg . id !== messageId ) return msg ;
280400 return { ...msg , reactions } ;
281401 } )
282402 ) ;
0 commit comments