42 lines
1.6 KiB
SQL
42 lines
1.6 KiB
SQL
-- Multi-Standort: Stadtwerke / Konzerne haben oft mehrere Standorte
|
|
-- (Werk, Verwaltung, Außenstelle). Mitarbeiter werden einem Standort
|
|
-- zugeordnet, im Admin-Verzeichnis und Organigramm filterbar.
|
|
-- ====================================================================
|
|
-- Optional: ein Standort kann eine eigene Anschrift haben (Adresse,
|
|
-- PLZ, Stadt). Die Mitarbeiter-Adresse hat aber Vorrang, wenn sie
|
|
-- gepflegt ist.
|
|
|
|
create table if not exists public.locations (
|
|
id uuid primary key default gen_random_uuid(),
|
|
slug text not null unique,
|
|
name text not null,
|
|
short_name text,
|
|
street text,
|
|
postal_code text,
|
|
city text,
|
|
sort_order integer not null default 0,
|
|
created_at timestamptz not null default now(),
|
|
updated_at timestamptz not null default now()
|
|
);
|
|
|
|
create index if not exists locations_sort_order_idx
|
|
on public.locations (sort_order, name);
|
|
|
|
drop trigger if exists trg_locations_updated_at on public.locations;
|
|
create trigger trg_locations_updated_at
|
|
before update on public.locations
|
|
for each row execute function public.set_updated_at();
|
|
|
|
alter table public.employees
|
|
add column if not exists location_id uuid
|
|
references public.locations(id) on delete set null;
|
|
|
|
create index if not exists employees_location_id_idx
|
|
on public.employees (location_id);
|
|
|
|
grant select (location_id) on public.employees to anon;
|
|
|
|
alter table public.locations enable row level security;
|
|
-- Service-Role-only — anon braucht den Standort nicht direkt; wenn die
|
|
-- Karte ihn anzeigt, hilft App-Code die Auflösung zu machen.
|