42 lines
1.3 KiB
SQL
42 lines
1.3 KiB
SQL
-- 推广员关系表:记录下级与上级(邀请人)的绑定关系
|
|
CREATE TABLE IF NOT EXISTS public.ak_promoter_relations (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
uid UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
|
inviter_uid UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
|
bind_time TIMESTAMPTZ DEFAULT now(),
|
|
created_at TIMESTAMPTZ DEFAULT now(),
|
|
|
|
CONSTRAINT chk_ak_promoter_relations_no_self CHECK (uid <> inviter_uid),
|
|
CONSTRAINT uq_ak_promoter_relations_uid UNIQUE (uid)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_ak_promoter_relations_inviter_uid ON public.ak_promoter_relations(inviter_uid);
|
|
|
|
-- 启用 RLS
|
|
ALTER TABLE public.ak_promoter_relations ENABLE ROW LEVEL SECURITY;
|
|
|
|
-- Admin 可读写
|
|
CREATE POLICY "Admins can manage promoter relations"
|
|
ON public.ak_promoter_relations
|
|
FOR ALL
|
|
TO authenticated
|
|
USING (
|
|
EXISTS (
|
|
SELECT 1 FROM public.ak_users
|
|
WHERE id = auth.uid() AND role = 'admin'
|
|
)
|
|
)
|
|
WITH CHECK (
|
|
EXISTS (
|
|
SELECT 1 FROM public.ak_users
|
|
WHERE id = auth.uid() AND role = 'admin'
|
|
)
|
|
);
|
|
|
|
-- 普通用户可查看与自己相关的关系(可选,便于移动端展示上级/下级)
|
|
CREATE POLICY "Users can view their promoter relation"
|
|
ON public.ak_promoter_relations
|
|
FOR SELECT
|
|
TO authenticated
|
|
USING (uid = auth.uid() OR inviter_uid = auth.uid());
|