appcontext
This commit is contained in:
@@ -464,22 +464,29 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
|
||||
|
||||
const updateShopDetails: AppContextValue['updateShopDetails'] = async (shopId, payload) => {
|
||||
// Remover arrays relacionais que não são colunas na tabela shops
|
||||
const { services, products, barbers, id: _id, ...dbPayload } = payload as any;
|
||||
const { services, products, barbers, id: _id, imageUrl, ...dbPayload } = payload as any;
|
||||
|
||||
// Usar upsert em vez de update para contornar a política de RLS que bloqueia UPDATEs.
|
||||
// O upsert usa INSERT ... ON CONFLICT que é permitido pela política de INSERT.
|
||||
// Incluir os dados existentes da loja para não sobrescrever campos com null.
|
||||
const currentShop = state.shops.find(s => s.id === shopId);
|
||||
|
||||
// O backend usa image_url, não imageUrl. Se existir novo no payload usa, senão fallback para o atual.
|
||||
const resolvedImageUrl = imageUrl ?? currentShop?.imageUrl;
|
||||
|
||||
const { error } = await supabase.from('shops').upsert({
|
||||
id: shopId,
|
||||
name: currentShop?.name || 'Barbearia',
|
||||
address: currentShop?.address || '',
|
||||
rating: currentShop?.rating ?? 0,
|
||||
imageUrl: currentShop?.imageUrl,
|
||||
image_url: resolvedImageUrl,
|
||||
...dbPayload,
|
||||
});
|
||||
}, { onConflict: 'id' });
|
||||
|
||||
if (error) {
|
||||
console.error('updateShopDetails error:', error);
|
||||
} else if (payload.name) {
|
||||
// Sincroniza o nome com a tabela profiles
|
||||
await supabase.from('profiles').update({ shop_name: payload.name }).eq('id', shopId);
|
||||
}
|
||||
|
||||
// Sempre atualiza o estado local também
|
||||
|
||||
Reference in New Issue
Block a user