@@ -21,16 +21,34 @@ async function updateSaleAdministrator(
2121 . select ( '*' ) ;
2222
2323 if ( ! sales ?. length || salesError ) {
24- console . error ( 'Error inviting user:' , salesError ) ;
24+ console . error ( 'Error updating user:' , salesError ) ;
2525 throw salesError ?? new Error ( 'Failed to update sale' ) ;
2626 }
2727 return sales . at ( 0 ) ;
2828}
2929
30- async function inviteUser ( req : Request ) {
30+ async function updateSaleAvatar ( user_id : string , avatar : string ) {
31+ const { data : sales , error : salesError } = await supabaseAdmin
32+ . from ( 'sales' )
33+ . update ( { avatar } )
34+ . eq ( 'user_id' , user_id )
35+ . select ( '*' ) ;
36+
37+ if ( ! sales ?. length || salesError ) {
38+ console . error ( 'Error updating user:' , salesError ) ;
39+ throw salesError ?? new Error ( 'Failed to update sale' ) ;
40+ }
41+ return sales . at ( 0 ) ;
42+ }
43+
44+ async function inviteUser ( req : Request , currentUserSale : any ) {
3145 const { email, password, first_name, last_name, disabled, administrator } =
3246 await req . json ( ) ;
3347
48+ if ( ! currentUserSale . administrator ) {
49+ return createErrorResponse ( 401 , 'Not Authorized' ) ;
50+ }
51+
3452 const { data, error : userError } =
3553 await supabaseAdmin . auth . admin . createUser ( {
3654 email,
@@ -69,9 +87,16 @@ async function inviteUser(req: Request) {
6987 }
7088}
7189
72- async function patchUser ( req : Request ) {
73- const { sales_id, email, first_name, last_name, administrator, disabled } =
74- await req . json ( ) ;
90+ async function patchUser ( req : Request , currentUserSale : any ) {
91+ const {
92+ sales_id,
93+ email,
94+ first_name,
95+ last_name,
96+ avatar,
97+ administrator,
98+ disabled,
99+ } = await req . json ( ) ;
75100 const { data : sale } = await supabaseAdmin
76101 . from ( 'sales' )
77102 . select ( '*' )
@@ -82,6 +107,11 @@ async function patchUser(req: Request) {
82107 return createErrorResponse ( 404 , 'Not Found' ) ;
83108 }
84109
110+ // Users can only update their own profile unless they are an administrator
111+ if ( ! currentUserSale . administrator && currentUserSale . id !== sale . id ) {
112+ return createErrorResponse ( 401 , 'Not Authorized' ) ;
113+ }
114+
85115 const { data, error : userError } =
86116 await supabaseAdmin . auth . admin . updateUserById ( sale . user_id , {
87117 email,
@@ -94,16 +124,42 @@ async function patchUser(req: Request) {
94124 return createErrorResponse ( 500 , 'Internal Server Error' ) ;
95125 }
96126
127+ if ( avatar ) {
128+ await updateSaleAvatar ( data . user . id , avatar ) ;
129+ }
130+
131+ // Only administrators can update the administrator and disabled status
132+ if ( ! currentUserSale . administrator ) {
133+ const { data : new_sale } = await supabaseAdmin
134+ . from ( 'sales' )
135+ . select ( '*' )
136+ . eq ( 'id' , sales_id )
137+ . single ( ) ;
138+ return new Response (
139+ JSON . stringify ( {
140+ data : new_sale ,
141+ } ) ,
142+ {
143+ headers : {
144+ 'Content-Type' : 'application/json' ,
145+ ...corsHeaders ,
146+ } ,
147+ }
148+ ) ;
149+ }
150+
97151 try {
98152 await updateSaleDisabled ( data . user . id , disabled ) ;
99153 const sale = await updateSaleAdministrator ( data . user . id , administrator ) ;
100-
101154 return new Response (
102155 JSON . stringify ( {
103156 data : sale ,
104157 } ) ,
105158 {
106- headers : { 'Content-Type' : 'application/json' , ...corsHeaders } ,
159+ headers : {
160+ 'Content-Type' : 'application/json' ,
161+ ...corsHeaders ,
162+ } ,
107163 }
108164 ) ;
109165 } catch ( e ) {
@@ -126,18 +182,25 @@ Deno.serve(async (req: Request) => {
126182 Deno . env . get ( 'SUPABASE_ANON_KEY' ) ?? '' ,
127183 { global : { headers : { Authorization : authHeader } } }
128184 ) ;
129-
130185 const { data } = await localClient . auth . getUser ( ) ;
131186 if ( ! data ?. user ) {
132187 return createErrorResponse ( 401 , 'Unauthorized' ) ;
133188 }
189+ const currentUserSale = await supabaseAdmin
190+ . from ( 'sales' )
191+ . select ( '*' )
192+ . eq ( 'user_id' , data . user . id )
193+ . single ( ) ;
134194
195+ if ( ! currentUserSale ?. data ) {
196+ return createErrorResponse ( 401 , 'Unauthorized' ) ;
197+ }
135198 if ( req . method === 'POST' ) {
136- return inviteUser ( req ) ;
199+ return inviteUser ( req , currentUserSale . data ) ;
137200 }
138201
139202 if ( req . method === 'PATCH' ) {
140- return patchUser ( req ) ;
203+ return patchUser ( req , currentUserSale . data ) ;
141204 }
142205
143206 return createErrorResponse ( 405 , 'Method Not Allowed' ) ;
0 commit comments