caterium-app/supabase/migrations/20260920104500_workspace_sidebar_brand.sql
pavlov346346-source 1e644e6dfb
Fix employee sync and expose mobile profile/logout (#37)
Keep notification-read state personal to user/company, preserve server read-only sections during staff synchronization, and respect separate order-create/edit/delete rights. Add visible mobile header session actions and a sticky logout that survives profile RPC failure; scope asynchronous profile/branding to user and workspace. Targeted browser suites and isolated SQL recovery tests passed in run 35506145405, iPhone screenshots reviewed. Full main QA remains required before production promotion. Workspace branding RPC migration is included but has NOT been applied to production Supabase; older servers retain safe owner-only fallback. No live membership/business-data repair is claimed without identifying the reported employee.
2026-09-20 13:55:56 +03:00

35 lines
1.8 KiB
PL/PgSQL

begin;
-- Extend the exclusive identity to the selected company's active employees.
-- Keep the original zero-argument RPC for old installed clients.
alter table public.caterium_sidebar_brand_assignment
add column if not exists workspace_id uuid references public.sun_workspaces(id) on delete set null;
-- Never guess between several companies owned by the assigned account.
update public.caterium_sidebar_brand_assignment a
set workspace_id=w.id
from public.sun_workspaces w
where a.workspace_id is null and w.created_by=a.user_id
and (select count(*) from public.sun_workspaces x where x.created_by=a.user_id)=1;
create or replace function public.caterium_workspace_sidebar_brand(p_workspace uuid)
returns jsonb language plpgsql stable security definer
set search_path=public,pg_temp as $$
declare variant text:='caterium';
begin
if auth.uid() is null or public.sun_member_role(p_workspace) is null
or not exists(select 1 from auth.users where id=auth.uid() and email_confirmed_at is not null)
then raise exception 'Нет доступа к компании'; end if;
if exists(
select 1 from public.caterium_sidebar_brand_assignment a
join auth.users owner_account on owner_account.id=a.user_id and owner_account.email_confirmed_at is not null
join public.sun_workspaces w on w.id=p_workspace
where a.workspace_id=w.id or (a.workspace_id is null and w.created_by=a.user_id
and (select count(*) from public.sun_workspaces x where x.created_by=a.user_id)=1)
) then variant:='solnce'; end if;
return jsonb_build_object('variant',variant,'workspace_id',p_workspace);
end $$;
revoke all on function public.caterium_workspace_sidebar_brand(uuid) from public,anon;
grant execute on function public.caterium_workspace_sidebar_brand(uuid) to authenticated;
notify pgrst,'reload schema';
commit;