Admin panel
Ovde odobravaš članstvo i dodaješ tikete. Sve izmene se odmah vide na sajtu.
-- =====================================================================
-- SESTRE TIPS – dopuna baze v30 (pokreni JEDNOM; bezbedno je pokrenuti i više puta)
-- 1) Obaveštenja: pretplata uređaja se uvek prebacuje na nalog koji je sada prijavljen
-- (ranije uključivanje nije uspevalo ako je isti telefon/računar bio prijavljen na drugi nalog)
-- 2) Zaštita sadržaja: beleženje pokušaja deljenja (snimak ekrana, kopiranje, štampa,
-- čuvanje, desni klik, izbacivanje sa uređaja) i pregled „Ko deli sadržaj“ za admina
-- Ništa postojeće se ne briše.
-- =====================================================================
-- 1) OBAVEŠTENJA ----------------------------------------------------------
-- jedinstven endpoint (ako već postoji – ništa se ne menja)
do $$
begin
if not exists (
select 1 from pg_index i join pg_attribute a on a.attrelid = i.indrelid and a.attnum = i.indkey[0]
where i.indrelid = 'public.push_subs'::regclass and i.indisunique and i.indnatts = 1 and a.attname = 'endpoint'
) then
create unique index push_subs_endpoint_uq on public.push_subs (endpoint);
end if;
end $$;
create index if not exists push_subs_user_idx on public.push_subs (user_id);
create or replace function public.push_claim(p_endpoint text, p_p256dh text, p_auth text, p_lang text, p_platform text)
returns void
language plpgsql
security definer
set search_path = public
as $$
begin
if auth.uid() is null then raise exception 'Prijavi se'; end if;
if coalesce(p_endpoint, '') !~ '^https://' or length(p_endpoint) > 1000
or coalesce(p_p256dh, '') = '' or coalesce(p_auth, '') = '' then
raise exception 'Neispravna pretplata';
end if;
-- isti uređaj je ranije bio prijavljen na drugi nalog: pretplata prelazi na ovaj nalog
delete from public.push_subs where endpoint = p_endpoint and user_id <> auth.uid();
insert into public.push_subs (user_id, endpoint, p256dh, auth, lang, platform)
values (auth.uid(), p_endpoint, left(p_p256dh, 200), left(p_auth, 100), left(coalesce(p_lang, 'sr'), 5), left(coalesce(p_platform, ''), 10))
on conflict (endpoint) do update
set user_id = excluded.user_id, p256dh = excluded.p256dh, auth = excluded.auth,
lang = excluded.lang, platform = excluded.platform;
end;
$$;
revoke all on function public.push_claim(text, text, text, text, text) from public, anon;
grant execute on function public.push_claim(text, text, text, text, text) to authenticated;
-- 2) ZAŠTITA SADRŽAJA -----------------------------------------------------
create table if not exists public.share_events (
id bigserial primary key,
user_id uuid not null references auth.users(id) on delete cascade,
at timestamptz not null default now(),
kind text not null check (kind in ('shot', 'copy', 'print', 'save', 'ctx', 'kick', 'ack')),
ctx text,
device text,
ua text
);
create index if not exists share_events_at_idx on public.share_events (at desc);
create index if not exists share_events_user_idx on public.share_events (user_id, at desc);
alter table public.share_events enable row level security;
drop policy if exists "deljenje: admin cita" on public.share_events;
create policy "deljenje: admin cita" on public.share_events for select to authenticated using (public.is_admin());
drop policy if exists "admin 2FA" on public.share_events;
create policy "admin 2FA" on public.share_events as restrictive for select to authenticated using (public.st_mfa_ok());
revoke insert, update, delete on public.share_events from anon, authenticated;
grant select on public.share_events to authenticated;
-- upis ide samo preko ove funkcije: samo za sebe, najviše 60 zapisa na sat po nalogu
create or replace function public.log_share_event(p_kind text, p_ctx text, p_device text, p_ua text)
returns void
language plpgsql
security definer
set search_path = public
as $$
declare n int;
begin
if auth.uid() is null or public.is_admin() then return; end if;
if p_kind not in ('shot', 'copy', 'print', 'save', 'ctx', 'kick', 'ack') then return; end if;
if p_kind = 'ack' and exists (select 1 from public.share_events where user_id = auth.uid() and kind = 'ack') then return; end if;
select count(*) into n from public.share_events where user_id = auth.uid() and at > now() - interval '1 hour';
if n >= 60 then return; end if;
insert into public.share_events (user_id, kind, ctx, device, ua)
values (auth.uid(), p_kind, left(p_ctx, 40), left(p_device, 60), left(p_ua, 200));
end;
$$;
revoke all on function public.log_share_event(text, text, text, text) from public, anon;
grant execute on function public.log_share_event(text, text, text, text) to authenticated;
-- pregled za admina: ko je koliko puta pokušao, za izabrani broj dana
-- (zapisi stariji od 180 dana se brišu, osim potvrde pravila – Zakon o zaštiti podataka o ličnosti)
create or replace function public.admin_share_report(p_days int default 30)
returns table (user_id uuid, total bigint, shots bigint, copies bigint, prints bigint, saves bigint, ctx bigint, kicks bigint,
first_at timestamptz, last_at timestamptz, ack_at timestamptz, devices bigint, uas text[])
language plpgsql
security definer
set search_path = public
as $$
begin
if not public.is_admin() or not public.st_mfa_ok() then raise exception 'Samo admin'; end if;
delete from public.share_events where at < now() - interval '180 days' and kind <> 'ack';
return query
with e as (
select * from public.share_events
where at > now() - make_interval(days => greatest(1, least(coalesce(p_days, 30), 365)))
), a as (
select s.user_id, min(s.at) as ack_at from public.share_events s where s.kind = 'ack' group by s.user_id
)
select e.user_id,
count(*) filter (where e.kind <> 'ack'),
count(*) filter (where e.kind = 'shot'),
count(*) filter (where e.kind = 'copy'),
count(*) filter (where e.kind = 'print'),
count(*) filter (where e.kind = 'save'),
count(*) filter (where e.kind = 'ctx'),
count(*) filter (where e.kind = 'kick'),
min(e.at) filter (where e.kind <> 'ack'),
max(e.at) filter (where e.kind <> 'ack'),
(select a.ack_at from a where a.user_id = e.user_id),
count(distinct e.device) filter (where e.kind <> 'ack'),
(array_agg(distinct left(e.ua, 160)) filter (where e.kind <> 'ack' and e.ua is not null))[1:5]
from e
group by e.user_id
having count(*) filter (where e.kind <> 'ack') > 0
order by max(e.at) desc
limit 2000;
end;
$$;
revoke all on function public.admin_share_report(int) from public, anon;
grant execute on function public.admin_share_report(int) to authenticated;
-- potvrde pravila (ko je pročitao i prihvatio zabranu deljenja)
create or replace function public.admin_share_acks()
returns table (user_id uuid, ack_at timestamptz)
language plpgsql
stable
security definer
set search_path = public
as $$
begin
if not public.is_admin() or not public.st_mfa_ok() then raise exception 'Samo admin'; end if;
return query select s.user_id, min(s.at) from public.share_events s where s.kind = 'ack' group by s.user_id;
end;
$$;
revoke all on function public.admin_share_acks() from public, anon;
grant execute on function public.admin_share_acks() to authenticated;
-- detalji za jednog člana (poslednjih 200 zapisa)
create or replace function public.admin_share_events(p_user uuid)
returns table (at timestamptz, kind text, ctx text, device text, ua text)
language plpgsql
stable
security definer
set search_path = public
as $$
begin
if not public.is_admin() or not public.st_mfa_ok() then raise exception 'Samo admin'; end if;
return query select s.at, s.kind, s.ctx, s.device, s.ua from public.share_events s where s.user_id = p_user order by s.at desc limit 200;
end;
$$;
revoke all on function public.admin_share_events(uuid) from public, anon;
grant execute on function public.admin_share_events(uuid) to authenticated;
-- =====================================================================
-- SESTRE TIPS – dopuna baze v25: jedan nalog = jedan uređaj u isto vreme
-- (na tom uređaju mogu zajedno aplikacija i pregledač)
-- Bezbedno je pokrenuti i više puta. Ništa postojeće se ne briše.
-- • Kad se član prijavi na drugom uređaju, odjavljuje se sa prethodnog.
-- • Standardne tikete vidi samo aktivna prijava.
-- • Admin nalog je izuzet.
-- =====================================================================
create table if not exists public.active_devices (
user_id uuid not null references auth.users(id) on delete cascade,
slot text not null check (slot in ('web', 'app')),
session_id text not null,
device text not null default '',
ua text,
updated_at timestamptz not null default now(),
primary key (user_id, slot)
);
alter table public.active_devices enable row level security;
drop policy if exists "uredjaji: admin cita" on public.active_devices;
create policy "uredjaji: admin cita" on public.active_devices for select to authenticated using (public.is_admin());
-- stare verzije funkcija (ako postoje)
drop function if exists public.claim_session(text);
drop function if exists public.session_check();
-- oznaka trenutne prijave (Supabase je upisuje u svaki token)
create or replace function public.st_sid() returns text
language sql stable set search_path = public as $$
select coalesce(auth.jwt() ->> 'session_id', '')
$$;
-- da li je ova prijava aktivna (koristi se u pravilima pristupa)
create or replace function public.st_session_ok() returns boolean
language sql stable security definer set search_path = public as $$
select auth.uid() is null
or public.st_sid() = ''
or public.is_admin()
or not exists (select 1 from public.active_devices d where d.user_id = auth.uid())
or exists (select 1 from public.active_devices d where d.user_id = auth.uid() and d.session_id = public.st_sid())
$$;
-- prijava: ovo mesto (app ili web) postaje aktivno; drugo mesto ostaje samo ako je na ISTOM uređaju
create or replace function public.claim_session(p_slot text, p_device text, p_ua text default null) returns boolean
language plpgsql security definer set search_path = public as $$
declare v_slot text := case when p_slot = 'app' then 'app' else 'web' end;
begin
if auth.uid() is null or public.st_sid() = '' then return true; end if;
insert into public.active_devices (user_id, slot, session_id, device, ua, updated_at)
values (auth.uid(), v_slot, public.st_sid(), coalesce(p_device, ''), left(p_ua, 200), now())
on conflict (user_id, slot) do update set session_id = excluded.session_id, device = excluded.device, ua = excluded.ua, updated_at = now();
delete from public.active_devices where user_id = auth.uid() and slot <> v_slot and device <> coalesce(p_device, '');
return true;
end $$;
-- provera sa sajta/aplikacije: false = nalog je u međuvremenu prijavljen na drugom uređaju
create or replace function public.session_check(p_slot text, p_device text) returns boolean
language plpgsql security definer set search_path = public as $$
declare v_slot text := case when p_slot = 'app' then 'app' else 'web' end; r record; o record;
begin
if auth.uid() is null or public.st_sid() = '' or public.is_admin() then return true; end if;
select * into r from public.active_devices where user_id = auth.uid() and slot = v_slot;
if found then
if r.session_id <> public.st_sid() then return false; end if;
update public.active_devices set updated_at = now() where user_id = auth.uid() and slot = v_slot and updated_at < now() - interval '15 minutes';
return true;
end if;
select * into o from public.active_devices where user_id = auth.uid() and slot <> v_slot;
if found and o.device <> coalesce(p_device, '') then return false; end if;
insert into public.active_devices (user_id, slot, session_id, device, updated_at)
values (auth.uid(), v_slot, public.st_sid(), coalesce(p_device, ''), now())
on conflict (user_id, slot) do nothing;
return true;
end $$;
revoke all on function public.claim_session(text, text, text) from public, anon;
revoke all on function public.session_check(text, text) from public, anon;
grant execute on function public.claim_session(text, text, text) to authenticated;
grant execute on function public.session_check(text, text) to authenticated;
grant execute on function public.st_session_ok() to anon, authenticated;
grant execute on function public.st_sid() to anon, authenticated;
-- standardni tiketi: samo za aktivnu prijavu (besplatni tiketi ostaju dostupni)
drop policy if exists "tiketi: jedan uredjaj" on public.tickets;
create policy "tiketi: jedan uredjaj" on public.tickets as restrictive for select to authenticated
using (category = 'free' or (select public.st_session_ok()));
create index if not exists active_devices_upd on public.active_devices (updated_at);
-- =====================================================================
-- SESTRE TIPS – dopuna baze v17 (pokreni JEDNOM, bezbedno i više puta)
-- 1) Uplate i prihod
-- 2) Dnevnik aktivnosti (ko je šta uradio u admin panelu)
-- 3) Podešavanja (dani za automatske podsetnike)
-- 4) Zakazana objava tiketa
-- 5) Dvostepena prijava za admin nalog
-- 6) Pregled automatskih podsetnika
-- Ništa postojeće se ne briše.
-- =====================================================================
-- 1) UPLATE -------------------------------------------------------------
create table if not exists public.payments (
id bigserial primary key,
user_id uuid references public.profiles(id) on delete set null,
email text,
name text,
amount numeric(12,2) not null check (amount >= 0),
currency text not null default 'EUR' check (currency in ('EUR', 'RSD')),
method text not null default 'uplatnica',
paid_at date not null,
member_until date,
kind text not null default 'new' check (kind in ('new', 'renew')),
note text,
created_at timestamptz not null default now(),
created_by uuid default auth.uid()
);
create index if not exists payments_paid_idx on public.payments (paid_at desc);
create index if not exists payments_user_idx on public.payments (user_id);
alter table public.payments enable row level security;
drop policy if exists "uplate: samo admin" on public.payments;
create policy "uplate: samo admin" on public.payments for all to authenticated
using (public.is_admin()) with check (public.is_admin());
grant select, insert, update, delete on public.payments to authenticated;
grant usage, select on sequence public.payments_id_seq to authenticated;
-- 2) DNEVNIK AKTIVNOSTI (zapisi se ne mogu menjati ni brisati) ----------------
create table if not exists public.admin_log (
id bigserial primary key,
at timestamptz not null default now(),
actor uuid default auth.uid(),
action text not null,
target uuid,
ticket_id bigint,
info jsonb not null default '{}'::jsonb
);
create index if not exists admin_log_at_idx on public.admin_log (at desc);
alter table public.admin_log enable row level security;
drop policy if exists "aktivnosti: admin cita" on public.admin_log;
create policy "aktivnosti: admin cita" on public.admin_log for select to authenticated using (public.is_admin());
drop policy if exists "aktivnosti: admin upisuje" on public.admin_log;
create policy "aktivnosti: admin upisuje" on public.admin_log for insert to authenticated with check (public.is_admin());
grant select, insert on public.admin_log to authenticated;
grant usage, select on sequence public.admin_log_id_seq to authenticated;
-- 3) PODEŠAVANJA --------------------------------------------------------
create table if not exists public.app_settings (
key text primary key,
value jsonb not null,
updated_at timestamptz not null default now()
);
alter table public.app_settings enable row level security;
drop policy if exists "podesavanja: samo admin" on public.app_settings;
create policy "podesavanja: samo admin" on public.app_settings for all to authenticated
using (public.is_admin()) with check (public.is_admin());
grant select, insert, update, delete on public.app_settings to authenticated;
-- podsetnik: 3 dana pre isteka i na dan isteka
insert into public.app_settings (key, value) values ('reminder_days', '[3, 0]'::jsonb)
on conflict (key) do nothing;
-- 4) ZAKAZANA OBJAVA ------------------------------------------------------
-- tiket sa "vremenom objave" u budućnosti vidi samo admin, dok to vreme ne dođe
alter table public.tickets add column if not exists push_pending boolean not null default false;
drop policy if exists "tiketi: zakazani se ne vide pre vremena" on public.tickets;
create policy "tiketi: zakazani se ne vide pre vremena" on public.tickets as restrictive for select
using (posted_at is null or posted_at <= now() or public.is_admin());
create index if not exists tickets_push_pending_idx on public.tickets (push_pending) where push_pending;
-- pregled meseci za zaključanu standardnu grupu ne broji zakazane tikete
create or replace function public.std_summary()
returns table(month text, tickets bigint, won bigint, profit bigint)
language sql
stable
security definer
set search_path = public
as $$
select to_char(date, 'YYYY-MM') as month,
count(*) as tickets,
count(*) filter (where won) as won,
sum(case when won then round(stake * total_odds) - stake
when won = false then -stake else 0 end)::bigint as profit
from public.tickets
where category = 'std' and (posted_at is null or posted_at <= now())
group by 1 order by 1 desc
$$;
-- 5) DVOSTEPENA PRIJAVA -----------------------------------------------------
-- true ako je prijava potvrđena kodom sa telefona (aal2) ili korisnik nema uključenu dvostepenu prijavu
create or replace function public.st_mfa_ok()
returns boolean
language sql
stable
security definer
set search_path = public, auth
as $$
select coalesce(auth.jwt() ->> 'aal', 'aal1') = 'aal2'
or not exists (select 1 from auth.mfa_factors f where f.user_id = auth.uid() and f.status = 'verified');
$$;
revoke all on function public.st_mfa_ok() from public;
grant execute on function public.st_mfa_ok() to anon, authenticated;
-- dodatna ("restrictive") pravila: admin bez koda sa telefona ne može ništa da menja ni da vidi tuđe podatke
-- (na obične korisnike ova pravila ne utiču)
do $$
declare t text; c text;
begin
-- izmene
foreach t in array array['profiles', 'tickets', 'diary_entries', 'notifications', 'std_months'] loop
foreach c in array array['insert', 'update', 'delete'] loop
execute format('drop policy if exists %I on public.%I', 'admin 2FA ' || c, t);
if c = 'insert' then
execute format('create policy %I on public.%I as restrictive for insert to authenticated with check (not public.is_admin() or public.st_mfa_ok())', 'admin 2FA ' || c, t);
elsif c = 'update' then
execute format('create policy %I on public.%I as restrictive for update to authenticated using (not public.is_admin() or public.st_mfa_ok()) with check (not public.is_admin() or public.st_mfa_ok())', 'admin 2FA ' || c, t);
else
execute format('create policy %I on public.%I as restrictive for delete to authenticated using (not public.is_admin() or public.st_mfa_ok())', 'admin 2FA ' || c, t);
end if;
end loop;
end loop;
-- čitanje tuđih podataka
drop policy if exists "admin 2FA select" on public.profiles;
create policy "admin 2FA select" on public.profiles as restrictive for select to authenticated
using (id = auth.uid() or not public.is_admin() or public.st_mfa_ok());
drop policy if exists "admin 2FA select" on public.notifications;
create policy "admin 2FA select" on public.notifications as restrictive for select to authenticated
using (user_id = auth.uid() or not public.is_admin() or public.st_mfa_ok());
drop policy if exists "admin 2FA select" on public.push_subs;
create policy "admin 2FA select" on public.push_subs as restrictive for select to authenticated
using (user_id = auth.uid() or not public.is_admin() or public.st_mfa_ok());
-- tabele koje koristi samo admin
foreach t in array array['payments', 'admin_log', 'app_settings'] loop
execute format('drop policy if exists %I on public.%I', 'admin 2FA', t);
execute format('create policy %I on public.%I as restrictive for all to authenticated using (public.st_mfa_ok()) with check (public.st_mfa_ok())', 'admin 2FA', t);
end loop;
end $$;
-- evidencija korisnika traži i potvrdu kodom
create or replace function public.admin_users()
returns table (id uuid, last_sign_in_at timestamptz, email_confirmed_at timestamptz, created_at timestamptz)
language plpgsql
stable
security definer
set search_path = public, auth
as $$
begin
if not public.is_admin() or not public.st_mfa_ok() then
raise exception 'Samo admin (sa dvostepenom prijavom, ako je uključena) moze da vidi evidenciju korisnika';
end if;
return query
select u.id, u.last_sign_in_at, u.email_confirmed_at, u.created_at
from auth.users u
order by u.created_at desc, u.id;
end;
$$;
revoke all on function public.admin_users() from public;
revoke all on function public.admin_users() from anon;
grant execute on function public.admin_users() to authenticated;
-- 6) PREGLED AUTOMATSKIH PODSETNIKA -------------------------------------------
create or replace function public.admin_jobs()
returns jsonb
language plpgsql
stable
security definer
set search_path = public, cron
as $$
declare r jsonb;
begin
if not public.is_admin() or not public.st_mfa_ok() then raise exception 'Samo admin'; end if;
select coalesce(jsonb_agg(x order by x->>'start' desc), '[]'::jsonb) into r from (
select jsonb_build_object('job', j.jobname, 'status', d.status, 'start', d.start_time, 'msg', left(d.return_message, 120)) x
from cron.job_run_details d join cron.job j on j.jobid = d.jobid
where j.jobname like 'sestre-%'
order by d.start_time desc limit 30
) s;
return r;
end;
$$;
revoke all on function public.admin_jobs() from public;
revoke all on function public.admin_jobs() from anon;
grant execute on function public.admin_jobs() to authenticated;
-- 7) OBAVEŠTENJE NA TELEFON ZA ZAKAZANE TIKETE (provera svakih 5 minuta)
-- koristi isti poziv kao jutarnji podsetnik, samo sa akcijom "scheduled"
do $$
declare cmd text;
begin
select command into cmd from cron.job where jobname = 'sestre-push-daily';
if cmd is not null then
perform cron.schedule('sestre-push-scheduled', '*/5 * * * *', replace(cmd, '"daily"', '"scheduled"'));
end if;
end $$;
Statistika
Pregledi sajta i aplikacije
Prihodi
Novi članovi
Produženja
Nove registracije
Prolaznost tiketa i parova sport, liga, vrsta i tip igre, kvota, broj parova, dan i mesec
📲 Instalirane aplikacije
Nove instalacije po danima – ovaj mesec
Broji se svaki telefon ili računar na kome je aplikacija instalirana i bar jednom otvorena. iPhone ne javlja samu instalaciju, pa se broji pri prvom otvaranju sa početnog ekrana. Brisanje aplikacije sa telefona ne može da se vidi.
Poslednje aktivnosti
-- =====================================================================
-- SESTRE TIPS – obaveštenja na sajtu (zvonce)
-- Svaki korisnik vidi samo svoja obaveštenja. Šalje ih samo admin.
-- =====================================================================
create table if not exists public.notifications (
id bigserial primary key,
user_id uuid not null references auth.users(id) on delete cascade,
kind text not null default 'msg', -- msg | approved | extended
data jsonb not null default '{}'::jsonb, -- tekst poruke, prevodi, datum članarine
read_at timestamptz,
created_at timestamptz not null default now()
);
create index if not exists notifications_user_idx on public.notifications (user_id, created_at desc);
alter table public.notifications enable row level security;
drop policy if exists "nt svoja" on public.notifications;
create policy "nt svoja" on public.notifications for select to authenticated
using (user_id = auth.uid());
drop policy if exists "nt admin" on public.notifications;
create policy "nt admin" on public.notifications for all to authenticated
using (exists (select 1 from public.profiles p where p.id = auth.uid() and p.is_admin))
with check (exists (select 1 from public.profiles p where p.id = auth.uid() and p.is_admin));
-- korisnik označava svoja obaveštenja kao pročitana (i ništa drugo ne može da menja)
create or replace function public.notifications_mark_read()
returns void
language sql
security definer
set search_path = public
as $$
update public.notifications set read_at = now()
where user_id = auth.uid() and read_at is null;
$$;
revoke all on function public.notifications_mark_read() from public;
grant execute on function public.notifications_mark_read() to authenticated;
-- =====================================================================
-- SESTRE TIPS – dopuna baze v15 (pokreni JEDNOM)
-- Evidencija korisnika u admin panelu: poslednja prijava i potvrda imejla.
-- Samo admin može da pročita ove podatke. Ništa se ne briše.
-- =====================================================================
create or replace function public.admin_users()
returns table (id uuid, last_sign_in_at timestamptz, email_confirmed_at timestamptz, created_at timestamptz)
language plpgsql
stable
security definer
set search_path = public, auth
as $$
begin
if not exists (select 1 from public.profiles p where p.id = auth.uid() and p.is_admin) then
raise exception 'Samo admin moze da vidi evidenciju korisnika';
end if;
return query
select u.id, u.last_sign_in_at, u.email_confirmed_at, u.created_at
from auth.users u
order by u.created_at desc, u.id;
end;
$$;
revoke all on function public.admin_users() from public;
revoke all on function public.admin_users() from anon;
grant execute on function public.admin_users() to authenticated;
-- brže sortiranje evidencije po datumu registracije
create index if not exists profiles_created_at_idx on public.profiles (created_at desc);
Ovde je tabela „Dnevnik tiketa“ sa sajta. Redovi stižu sami iz završenih tiketa. Možeš da ih ispraviš ili sakriješ (tiket se ne menja) i da ručno dodaš utakmice.
-- =====================================================================
-- SESTRE TIPS – Dnevnik tiketa: ručne ispravke i dodate utakmice
-- Nova tabela "diary_entries". Tiketi se NE menjaju.
-- * red sa ticket_id = ispravka (ili sakrivanje) reda koji dolazi iz tiketa
-- * red bez ticket_id = utakmica koju si ručno dodao
-- =====================================================================
create table if not exists public.diary_entries (
id bigserial primary key,
ticket_id bigint,
hidden boolean not null default false,
date date not null,
category text not null default 'std',
stake numeric not null default 0,
odds numeric not null default 1,
won boolean,
legs jsonb not null default '[]'::jsonb,
created_at timestamptz not null default now()
);
create unique index if not exists diary_entries_ticket_uq on public.diary_entries (ticket_id) where ticket_id is not null;
alter table public.diary_entries enable row level security;
drop policy if exists "diary citanje" on public.diary_entries;
create policy "diary citanje" on public.diary_entries for select using (true);
drop policy if exists "diary admin" on public.diary_entries;
create policy "diary admin" on public.diary_entries for all to authenticated
using (exists (select 1 from public.profiles p where p.id = auth.uid() and p.is_admin))
with check (exists (select 1 from public.profiles p where p.id = auth.uid() and p.is_admin));
-- tabela za sajt: završeni tiketi (osim ispravljenih/sakrivenih) + ispravke + ručno dodate utakmice
create or replace function public.ticket_diary()
returns jsonb
language sql
stable
security definer
set search_path = public
as $$
with legs_of as (
select t.id,
coalesce((
select jsonb_agg(jsonb_build_object(
'sport', l->'sport', 'won', l->'won', 'home', l->'home', 'away', l->'away', 'tr', l->'tr',
'pick', coalesce(l->'pick', '{}'::jsonb) || jsonb_build_object('side',
case when l->'pick'->>'team' is null then null
when l->'pick'->>'team' = l->>'home' then 'home'
when l->'pick'->>'team' = l->>'away' then 'away' end)
) order by e.ord)
from jsonb_array_elements(coalesce(t.legs::jsonb, '[]'::jsonb)) with ordinality as e(l, ord)
), '[]'::jsonb) as legs
from public.tickets t
),
rows as (
select jsonb_build_object(
'src', 'ticket', 'tid', t.id, 'date', t.date, 'category', t.category,
'stake', t.stake, 'odds', t.total_odds, 'won', t.won,
'at', coalesce(t.posted_at, t.created_at), 'legs', lo.legs) as r
from public.tickets t join legs_of lo on lo.id = t.id
where t.won is not null
and not exists (select 1 from public.diary_entries d where d.ticket_id = t.id)
union all
select jsonb_build_object(
'src', 'entry', 'eid', d.id, 'tid', d.ticket_id, 'date', d.date, 'category', d.category,
'stake', d.stake, 'odds', d.odds, 'won', d.won, 'at', d.created_at, 'legs', d.legs)
from public.diary_entries d
where not d.hidden and d.won is not null
)
select coalesce(jsonb_agg(r order by r->>'date', r->>'at'), '[]'::jsonb) from rows;
$$;
revoke all on function public.ticket_diary() from public;
grant execute on function public.ticket_diary() to anon, authenticated;
🔒 Dvostepena prijava za admin nalog
Učitavam…
⏰ Automatski podsetnici za članarinu
Svako jutro u 9:00 članovi kojima ističe članarina dobijaju obaveštenje na telefon (ako su uključili obaveštenja), na svom jeziku. Izaberi kada:
Ko dobija podsetnik narednih 7 dana
Poslednja automatska slanja
💾 Rezervna kopija
Preuzmi sve podatke kod sebe: tikete sa parovima, dnevnik tiketa, sve korisnike i članove, uplate i dnevnik aktivnosti. Preporuka: jednom nedeljno.
📜 Dnevnik aktivnosti
Svako odobrenje, produženje, ukidanje, obaveštenje, uplata i izmena tiketa se beleži ovde. Zapisi ne mogu da se menjaju ni brišu.
-- =====================================================================
-- SESTRE TIPS – dopuna baze (pokreni JEDNOM)
-- Supabase → SQL Editor → New query → nalepi sve ovo → Run
-- Bezbedno je pokrenuti i više puta, ništa se ne briše.
-- =====================================================================
-- 1) nove kolone u tabeli tiketa
alter table public.tickets add column if not exists posted_at timestamptz; -- vreme objave (ručno iz admin panela)
alter table public.tickets add column if not exists fb_post text; -- link Facebook objave
alter table public.tickets add column if not exists slip_img text; -- slika tiketa
-- 2) skladište za slike tiketa (javno vidljive slike)
insert into storage.buckets (id, name, public)
values ('slips', 'slips', true)
on conflict (id) do update set public = true;
-- 3) pravila: svi vide slike, samo admin ih otprema i briše
drop policy if exists "slips citanje" on storage.objects;
create policy "slips citanje" on storage.objects
for select using (bucket_id = 'slips');
drop policy if exists "slips admin upis" on storage.objects;
create policy "slips admin upis" on storage.objects
for insert to authenticated
with check (bucket_id = 'slips' and exists (
select 1 from public.profiles p where p.id = auth.uid() and p.is_admin));
drop policy if exists "slips admin izmena" on storage.objects;
create policy "slips admin izmena" on storage.objects
for update to authenticated
using (bucket_id = 'slips' and exists (
select 1 from public.profiles p where p.id = auth.uid() and p.is_admin));
drop policy if exists "slips admin brisanje" on storage.objects;
create policy "slips admin brisanje" on storage.objects
for delete to authenticated
using (bucket_id = 'slips' and exists (
select 1 from public.profiles p where p.id = auth.uid() and p.is_admin));
-- =====================================================================
-- SESTRE TIPS – dopuna baze v14 (pokreni JEDNOM)
-- Supabase → SQL Editor → New query → nalepi sve ovo → Run
-- Bezbedno je pokrenuti i više puta, ništa se ne briše.
--
-- 1) Registrovani (besplatni) članovi vide tikete i tabele standardne
-- grupe iz PROŠLIH meseci. Tekući mesec vide samo članovi.
-- 2) Admin za svaki mesec bira da li je javan (tabela std_months).
-- 3) Sajt učitava tikete i dnevnik po mesecima, pa ostaje brz i kad
-- tiketa bude na hiljade, a članova na stotine hiljada.
-- =====================================================================
-- 1) meseci standardne grupe: javan ili ne (podrazumevano: javan)
create table if not exists public.std_months (
month text primary key check (month ~ '^[0-9]{4}-[0-9]{2}$'),
is_public boolean not null default true,
updated_at timestamptz not null default now()
);
alter table public.std_months enable row level security;
drop policy if exists "std_months citanje" on public.std_months;
create policy "std_months citanje" on public.std_months for select using (true);
drop policy if exists "std_months admin" on public.std_months;
create policy "std_months admin" on public.std_months for all to authenticated
using (exists (select 1 from public.profiles p where p.id = auth.uid() and p.is_admin))
with check (exists (select 1 from public.profiles p where p.id = auth.uid() and p.is_admin));
-- 2) pomoćne funkcije
-- mesec je otvoren za registrovane: prošao je i admin ga nije sakrio
create or replace function public.std_month_open(d date)
returns boolean
language sql
stable
security definer
set search_path = public
as $$
select to_char(d, 'YYYY-MM') < to_char((now() at time zone 'Europe/Belgrade')::date, 'YYYY-MM')
and coalesce((select m.is_public from public.std_months m where m.month = to_char(d, 'YYYY-MM')), true);
$$;
revoke all on function public.std_month_open(date) from public;
grant execute on function public.std_month_open(date) to anon, authenticated;
-- ulogovani korisnik je aktivan član standardne grupe (ili admin)
create or replace function public.is_std_member()
returns boolean
language sql
stable
security definer
set search_path = public
as $$
select exists (
select 1 from public.profiles p
where p.id = auth.uid()
and (p.is_admin or (p.status = 'active' and p.member_until is not null
and p.member_until::date >= (now() at time zone 'Europe/Belgrade')::date)));
$$;
revoke all on function public.is_std_member() from public;
grant execute on function public.is_std_member() to anon, authenticated;
-- 3) registrovani korisnici smeju da čitaju tikete standardne grupe iz otvorenih (prošlih, javnih) meseci
-- (postojeća pravila za članove i besplatne tikete ostaju ista; ovo samo dodaje arhivu)
drop policy if exists "std arhiva registrovani" on public.tickets;
create policy "std arhiva registrovani" on public.tickets for select to authenticated
using (category <> 'free' and public.std_month_open(date::date));
-- brže pretrage po datumu
create index if not exists tickets_date_idx on public.tickets (date);
-- 4) broj tiketa po mesecima (poštuje ko šta sme da vidi)
create or replace function public.ticket_months()
returns jsonb
language sql
stable
security invoker
set search_path = public
as $$
select coalesce(jsonb_agg(jsonb_build_object('m', x.m, 'c', x.c, 'n', x.n) order by x.m desc), '[]'::jsonb)
from (
select to_char(t.date::date, 'YYYY-MM') as m,
case when t.category = 'free' then 'free' else 'std' end as c,
count(*) as n
from public.tickets t
group by 1, 2
) x;
$$;
revoke all on function public.ticket_months() from public;
grant execute on function public.ticket_months() to anon, authenticated;
-- 5) dnevnik tiketa za jedan period (prazno = sve). Standardna grupa:
-- članovi vide sve, ostali samo prošle javne mesece.
create or replace function public.diary_rows(p_from date default null, p_to date default null)
returns jsonb
language sql
stable
security definer
set search_path = public
as $$
with mem as (select public.is_std_member() as ok),
tk as (
select t.* from public.tickets t
where t.won is not null
and (p_from is null or t.date::date >= p_from)
and (p_to is null or t.date::date < p_to)
and (t.category = 'free' or (select ok from mem) or public.std_month_open(t.date::date))
),
legs_of as (
select t.id,
coalesce((
select jsonb_agg(jsonb_build_object(
'sport', l->'sport', 'won', l->'won', 'home', l->'home', 'away', l->'away', 'tr', l->'tr', 'code', l->'code',
'pick', coalesce(l->'pick', '{}'::jsonb) || jsonb_build_object('side',
case when l->'pick'->>'team' is null then null
when l->'pick'->>'team' = l->>'home' then 'home'
when l->'pick'->>'team' = l->>'away' then 'away' end)
) order by e.ord)
from jsonb_array_elements(coalesce(t.legs::jsonb, '[]'::jsonb)) with ordinality as e(l, ord)
), '[]'::jsonb) as legs
from tk t
),
rows as (
select jsonb_build_object(
'src', 'ticket', 'tid', t.id, 'date', t.date, 'category', t.category,
'stake', t.stake, 'odds', t.total_odds, 'won', t.won,
'at', coalesce(t.posted_at, t.created_at), 'legs', lo.legs) as r
from tk t join legs_of lo on lo.id = t.id
where not exists (select 1 from public.diary_entries d where d.ticket_id = t.id)
union all
select jsonb_build_object(
'src', 'entry', 'eid', d.id, 'tid', d.ticket_id, 'date', d.date, 'category', d.category,
'stake', d.stake, 'odds', d.odds, 'won', d.won, 'at', d.created_at, 'legs', d.legs)
from public.diary_entries d
where not d.hidden and d.won is not null
and (p_from is null or d.date >= p_from)
and (p_to is null or d.date < p_to)
and (d.category = 'free' or (select ok from mem) or public.std_month_open(d.date))
)
select coalesce(jsonb_agg(r order by r->>'date', r->>'at'), '[]'::jsonb) from rows;
$$;
revoke all on function public.diary_rows(date, date) from public;
grant execute on function public.diary_rows(date, date) to anon, authenticated;
-- stari naziv (koristi ga admin panel i starija verzija sajta) – sada poštuje ista pravila
create or replace function public.ticket_diary()
returns jsonb
language sql
stable
security definer
set search_path = public
as $$ select public.diary_rows(null, null); $$;
revoke all on function public.ticket_diary() from public;
grant execute on function public.ticket_diary() to anon, authenticated;
-- spisak meseci u dnevniku
create or replace function public.diary_months()
returns jsonb
language sql
stable
security definer
set search_path = public
as $$
with mem as (select public.is_std_member() as ok)
select coalesce(jsonb_agg(x.m order by x.m desc), '[]'::jsonb)
from (
select to_char(t.date::date, 'YYYY-MM') as m
from public.tickets t
where t.won is not null
and (t.category = 'free' or (select ok from mem) or public.std_month_open(t.date::date))
and not exists (select 1 from public.diary_entries d where d.ticket_id = t.id)
union
select to_char(d.date, 'YYYY-MM')
from public.diary_entries d
where not d.hidden and d.won is not null
and (d.category = 'free' or (select ok from mem) or public.std_month_open(d.date))
) x;
$$;
revoke all on function public.diary_months() from public;
grant execute on function public.diary_months() to anon, authenticated;