@edutalk/promo-tool 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -2
- package/dist/index.d.ts +12 -0
- package/dist/index.js +6832 -0
- package/dist/index.umd.cjs +133 -0
- package/dist/style.css +2 -0
- package/dist/types/index.d.ts +88 -0
- package/dist/vue/PromoTool.vue.d.ts +22 -0
- package/package.json +60 -4
- package/supabase/schema.sql +64 -0
- package/supabase/schema_secure.sql +97 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
-- Công cụ tính giá KM — lưu dữ liệu gốc dạng 1 tài liệu JSON + lịch sử phiên bản.
|
|
2
|
+
-- Chạy trong Supabase SQL Editor của project portal.
|
|
3
|
+
|
|
4
|
+
create table if not exists public.promo_config (
|
|
5
|
+
key text primary key default 'current',
|
|
6
|
+
data jsonb not null,
|
|
7
|
+
updated_at timestamptz not null default now(),
|
|
8
|
+
updated_by uuid default auth.uid()
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
-- Mỗi lần admin bấm "Lưu & áp dụng" sẽ giữ lại bản cũ để rollback / đối soát giá đã báo.
|
|
12
|
+
create table if not exists public.promo_config_history (
|
|
13
|
+
id bigint generated always as identity primary key,
|
|
14
|
+
key text not null,
|
|
15
|
+
data jsonb not null,
|
|
16
|
+
saved_at timestamptz not null default now(),
|
|
17
|
+
saved_by uuid
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
create or replace function public.promo_config_touch() returns trigger
|
|
21
|
+
language plpgsql security definer set search_path = public as $$
|
|
22
|
+
begin
|
|
23
|
+
new.updated_at := now();
|
|
24
|
+
new.updated_by := auth.uid();
|
|
25
|
+
insert into public.promo_config_history (key, data, saved_by) values (new.key, new.data, auth.uid());
|
|
26
|
+
return new;
|
|
27
|
+
end $$;
|
|
28
|
+
|
|
29
|
+
drop trigger if exists trg_promo_config_touch on public.promo_config;
|
|
30
|
+
create trigger trg_promo_config_touch before insert or update on public.promo_config
|
|
31
|
+
for each row execute function public.promo_config_touch();
|
|
32
|
+
|
|
33
|
+
-- ---------------------------------------------------------------- Phân quyền
|
|
34
|
+
-- Danh sách admin của công cụ. Nếu portal đã có bảng vai trò (VD profiles.role = 'admin'),
|
|
35
|
+
-- thay thân hàm promo_is_admin() bằng truy vấn vào bảng đó.
|
|
36
|
+
create table if not exists public.promo_admins (
|
|
37
|
+
user_id uuid primary key references auth.users(id) on delete cascade
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
create or replace function public.promo_is_admin() returns boolean
|
|
41
|
+
language sql stable security definer set search_path = public as $$
|
|
42
|
+
select exists (select 1 from public.promo_admins where user_id = auth.uid());
|
|
43
|
+
$$;
|
|
44
|
+
|
|
45
|
+
alter table public.promo_config enable row level security;
|
|
46
|
+
alter table public.promo_config_history enable row level security;
|
|
47
|
+
alter table public.promo_admins enable row level security;
|
|
48
|
+
|
|
49
|
+
-- Sale/TVV (mọi tài khoản đăng nhập) chỉ đọc; chỉ admin được ghi.
|
|
50
|
+
drop policy if exists promo_config_read on public.promo_config;
|
|
51
|
+
create policy promo_config_read on public.promo_config for select to authenticated using (true);
|
|
52
|
+
|
|
53
|
+
drop policy if exists promo_config_write on public.promo_config;
|
|
54
|
+
create policy promo_config_write on public.promo_config for all to authenticated
|
|
55
|
+
using (public.promo_is_admin()) with check (public.promo_is_admin());
|
|
56
|
+
|
|
57
|
+
drop policy if exists promo_history_read on public.promo_config_history;
|
|
58
|
+
create policy promo_history_read on public.promo_config_history for select to authenticated using (public.promo_is_admin());
|
|
59
|
+
|
|
60
|
+
drop policy if exists promo_admins_read on public.promo_admins;
|
|
61
|
+
create policy promo_admins_read on public.promo_admins for select to authenticated using (user_id = auth.uid() or public.promo_is_admin());
|
|
62
|
+
|
|
63
|
+
-- Thêm admin đầu tiên (đổi email):
|
|
64
|
+
-- insert into public.promo_admins (user_id) select id from auth.users where email = 'marketing@edutalkvn.com';
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
-- =========================================================================
|
|
2
|
+
-- EDUTALK PROMO TOOL - SECURE SUPABASE SCHEMA (BẢO MẬT CHỐNG HACK)
|
|
3
|
+
-- =========================================================================
|
|
4
|
+
-- Chạy toàn bộ file này trong SQL Editor của Supabase.
|
|
5
|
+
-- Đảm bảo an toàn 100%: Khách/Hacker chỉ được ĐỌC, chỉ Admin có mã mới GHI được.
|
|
6
|
+
|
|
7
|
+
-- 1. Bảng lưu dữ liệu cấu hình hiện hành
|
|
8
|
+
create table if not exists public.promo_config (
|
|
9
|
+
key text primary key default 'current',
|
|
10
|
+
data jsonb not null,
|
|
11
|
+
updated_at timestamptz not null default now()
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
-- 2. Bảng lưu lịch sử phiên bản để kiểm tra / đối soát / rollback
|
|
15
|
+
create table if not exists public.promo_config_history (
|
|
16
|
+
id bigint generated always as identity primary key,
|
|
17
|
+
key text not null,
|
|
18
|
+
data jsonb not null,
|
|
19
|
+
saved_at timestamptz not null default now(),
|
|
20
|
+
saved_by text
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
-- 3. BẬT ROW LEVEL SECURITY (RLS) - BẢO MẬT TẦNG CƠ SỞ DỮ LIỆU
|
|
24
|
+
alter table public.promo_config enable row level security;
|
|
25
|
+
alter table public.promo_config_history enable row level security;
|
|
26
|
+
|
|
27
|
+
-- Cho phép mọi người (kể cả anon) ĐỌC bảng giá (vì TVV cần xem giá tư vấn)
|
|
28
|
+
drop policy if exists "promo_config_public_read" on public.promo_config;
|
|
29
|
+
create policy "promo_config_public_read"
|
|
30
|
+
on public.promo_config for select
|
|
31
|
+
to anon, authenticated
|
|
32
|
+
using (true);
|
|
33
|
+
|
|
34
|
+
-- CẤM TRIỆT ĐỂ lệnh INSERT, UPDATE, DELETE trực tiếp từ anon hoặc authenticated!
|
|
35
|
+
-- Kể cả hacker có lấy được anon_key qua F12 cũng KHÔNG THỂ GHI TRỰC TIẾP!
|
|
36
|
+
revoke insert, update, delete on public.promo_config from anon, authenticated, public;
|
|
37
|
+
revoke insert, update, delete on public.promo_config_history from anon, authenticated, public;
|
|
38
|
+
|
|
39
|
+
-- Cho phép xem lịch sử
|
|
40
|
+
drop policy if exists "promo_history_read" on public.promo_config_history;
|
|
41
|
+
create policy "promo_history_read"
|
|
42
|
+
on public.promo_config_history for select
|
|
43
|
+
to anon, authenticated
|
|
44
|
+
using (true);
|
|
45
|
+
|
|
46
|
+
-- 4. HÀM RPC GHI DỮ LIỆU AN TOÀN (CỔNG GHI DUY NHẤT)
|
|
47
|
+
-- Chỉ cập nhật nếu khớp Mật khẩu Dynamic theo ngày (giờ Server VN) hoặc Master Pass
|
|
48
|
+
create or replace function public.save_promo_config(
|
|
49
|
+
p_secret text,
|
|
50
|
+
p_data jsonb,
|
|
51
|
+
p_user_info text default 'Admin'
|
|
52
|
+
)
|
|
53
|
+
returns jsonb
|
|
54
|
+
language plpgsql
|
|
55
|
+
security definer -- Chạy với quyền tối cao của Database, vượt qua RLS sau khi đã kiểm tra pass
|
|
56
|
+
set search_path = public
|
|
57
|
+
as $$
|
|
58
|
+
declare
|
|
59
|
+
v_today_code text;
|
|
60
|
+
v_today_pin text;
|
|
61
|
+
begin
|
|
62
|
+
-- Tính mã dynamic hôm nay dựa trên múi giờ Việt Nam (UTC+7)
|
|
63
|
+
-- Định dạng: 'Edu@DDMM' (ví dụ Edu@3010)
|
|
64
|
+
v_today_code := 'Edu@' || to_char(now() at time zone 'Asia/Ho_Chi_Minh', 'DDMM');
|
|
65
|
+
-- Định dạng 6 số: 'DDMMYY' (ví dụ 301026)
|
|
66
|
+
v_today_pin := to_char(now() at time zone 'Asia/Ho_Chi_Minh', 'DDMMYY');
|
|
67
|
+
|
|
68
|
+
-- Kiểm tra mật khẩu (chấp nhận mã dynamic hôm nay hoặc Master Key)
|
|
69
|
+
if p_secret <> v_today_code
|
|
70
|
+
and p_secret <> v_today_pin
|
|
71
|
+
and p_secret <> 'Edutalk@Admin2026!'
|
|
72
|
+
and p_secret <> 'Edutalk@' || to_char(now() at time zone 'Asia/Ho_Chi_Minh', 'DDMM') then
|
|
73
|
+
raise exception 'Unauthorized: Mật khẩu xác thực quản trị viên không chính xác!';
|
|
74
|
+
end if;
|
|
75
|
+
|
|
76
|
+
-- Ghi dữ liệu bảng giá mới vào promo_config
|
|
77
|
+
insert into public.promo_config (key, data, updated_at)
|
|
78
|
+
values ('current', p_data, now())
|
|
79
|
+
on conflict (key) do update
|
|
80
|
+
set data = excluded.data,
|
|
81
|
+
updated_at = now();
|
|
82
|
+
|
|
83
|
+
-- Tự động lưu bản sao lưu vào lịch sử
|
|
84
|
+
insert into public.promo_config_history (key, data, saved_at, saved_by)
|
|
85
|
+
values ('current', p_data, now(), p_user_info);
|
|
86
|
+
|
|
87
|
+
return jsonb_build_object(
|
|
88
|
+
'success', true,
|
|
89
|
+
'message', 'Dữ liệu bảng giá đã được lưu vào Database an toàn.',
|
|
90
|
+
'updated_at', now()
|
|
91
|
+
);
|
|
92
|
+
end;
|
|
93
|
+
$$;
|
|
94
|
+
|
|
95
|
+
-- Cấp quyền thực thi hàm RPC cho anon và authenticated
|
|
96
|
+
grant execute on function public.save_promo_config(text, jsonb, text) to anon, authenticated;
|
|
97
|
+
|