Skip to content

Commit 472a2c6

Browse files
committed
feat: Define database schema and seed data for user, group, and channel management
- Created SQL tables for managing users, groups, warnings, approved users, channels, blacklists, group rules, and group message settings. - Added foreign key relationships between `User`, `Group`, `Warning`, and `ApprovedUser` tables. - Implemented constraints for roles in the `User` table and blacklisting in the `Blacklist` table. - Defined rules and permissions for managing groups, including arrays for `approved_users`, `black_list`, and `chat_permissions`. - Seeded initial data for `User`, `Group`, `Warning`, `ApprovedUser`, `Channel`, `Blacklist`, `GroupRule`, and `GroupMessageSettings` tables. - Provided default values for roles, warnings, and timestamps in various tables. - Included sample group data with rules, permissions, and blacklisting, as well as user information with associated warnings and approved groups. This feature sets up the foundational database structure and initial seed data for managing users, groups, warnings, and other bot-related functionalities.
1 parent 98d6aab commit 472a2c6

4 files changed

Lines changed: 72 additions & 127 deletions

File tree

src/database/index.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/database/seed/SeedDataTables.sql

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,77 @@
1+
-- User table: Stores user information
2+
CREATE TABLE IF NOT EXISTS "User" (
3+
id SERIAL PRIMARY KEY,
4+
telegram_id BIGINT UNIQUE NOT NULL, -- Telegram user ID
5+
username TEXT DEFAULT NULL,
6+
first_name TEXT,
7+
role TEXT CHECK (role IN ('admin','owner', 'user', 'approved_user')), -- User roles for managing permissions
8+
warnings INT DEFAULT 0, -- Number of warnings the user has received
9+
approved_groups BIGINT[] -- Array of approved group IDs for the user
10+
);
11+
112
-- Group table: Stores information about groups managed by the bot
2-
CREATE TABLE Group (
13+
CREATE TABLE IF NOT EXISTS "Group" (
314
id SERIAL PRIMARY KEY,
415
group_id BIGINT UNIQUE NOT NULL, -- Telegram group ID
516
group_name TEXT,
617
rules TEXT[], -- Array of rules for the group
7-
added_by_id BIGINT REFERENCES User(id), -- ID of the admin who added the group
818
black_list TEXT[], -- Array of blacklisted words or users
919
chat_permissions JSONB, -- JSON to store permissions
1020
updated_at TIMESTAMP DEFAULT NOW(),
1121
joined_at TIMESTAMP DEFAULT NOW(),
1222
approved_users BIGINT[], -- Array of approved user IDs
13-
warnings INT DEFAULT 0, -- Number of warnings in the group
23+
warnings BIGINT[], -- Number of warnings in the group
1424
is_spam_time BOOLEAN DEFAULT FALSE, -- Flag for spam detection
15-
members INT -- Number of members in the group
16-
);
17-
18-
-- User table: Stores user information
19-
CREATE TABLE User (
20-
id SERIAL PRIMARY KEY,
21-
telegram_id BIGINT UNIQUE NOT NULL, -- Telegram user ID
22-
username TEXT,
23-
role TEXT CHECK (role IN ('admin', 'user', 'moderator')), -- User roles for managing permissions
24-
warnings INT DEFAULT 0, -- Number of warnings the user has received
25-
approved_groups BIGINT[] -- Array of approved group IDs for the user
25+
members BIGINT[]
2626
);
2727

2828
-- Warning table: Stores information on user warnings
29-
CREATE TABLE Warning (
29+
CREATE TABLE IF NOT EXISTS "Warning" (
3030
id SERIAL PRIMARY KEY,
31-
user_id BIGINT REFERENCES User(id), -- User who was warned
32-
group_id BIGINT REFERENCES Group(id), -- Group where the warning was issued
31+
user_id BIGINT REFERENCES "User"(id), -- User who was warned
32+
group_id BIGINT REFERENCES "Group"(id), -- Group where the warning was issued
3333
warned_at TIMESTAMP DEFAULT NOW(),
3434
reason TEXT
3535
);
3636

3737
-- ApprovedUser table: Tracks users approved in specific groups
38-
CREATE TABLE ApprovedUser (
38+
CREATE TABLE IF NOT EXISTS "ApprovedUser" (
3939
id SERIAL PRIMARY KEY,
40-
user_id BIGINT REFERENCES User(id), -- User who was approved
41-
group_id BIGINT REFERENCES Group(id), -- Group where the user was approved
40+
user_id BIGINT REFERENCES "User"(id), -- User who was approved
41+
group_id BIGINT REFERENCES "Group"(id), -- Group where the user was approved
4242
username TEXT,
4343
approved_at TIMESTAMP DEFAULT NOW()
4444
);
4545

4646
-- Channel table: Stores channel details managed by the bot
47-
CREATE TABLE Channel (
47+
CREATE TABLE IF NOT EXISTS "Channel" (
4848
id SERIAL PRIMARY KEY,
4949
name TEXT,
5050
channel_id BIGINT UNIQUE NOT NULL, -- Telegram channel ID
5151
admins BIGINT[] -- Array of admin user IDs for the channel
5252
);
5353

54-
-- GroupAdminPermissions table: Defines admin permissions for specific groups
55-
CREATE TABLE GroupAdminPermissions (
56-
id SERIAL PRIMARY KEY,
57-
group_id BIGINT REFERENCES Group(id),
58-
user_id BIGINT REFERENCES User(id),
59-
can_manage_approvals BOOLEAN DEFAULT FALSE,
60-
can_manage_users BOOLEAN DEFAULT FALSE,
61-
can_manage_blacklist BOOLEAN DEFAULT FALSE,
62-
can_manage_rules BOOLEAN DEFAULT FALSE,
63-
can_manage_pinning BOOLEAN DEFAULT FALSE,
64-
can_manage_messages BOOLEAN DEFAULT FALSE,
65-
can_manage_group_settings BOOLEAN DEFAULT FALSE,
66-
can_view_group_stats BOOLEAN DEFAULT FALSE,
67-
can_broadcast_message BOOLEAN DEFAULT FALSE,
68-
can_view_admin_list BOOLEAN DEFAULT FALSE
69-
);
70-
7154
-- Blacklist table: Stores blacklisted words and users for each group
72-
CREATE TABLE Blacklist (
55+
CREATE TABLE IF NOT EXISTS "Blacklist" (
7356
id SERIAL PRIMARY KEY,
74-
group_id BIGINT REFERENCES Group(id),
57+
group_id BIGINT REFERENCES "Group"(id),
7558
blacklisted_word TEXT, -- Word to be blacklisted
76-
blacklisted_user_id BIGINT REFERENCES User(id) -- User to be blacklisted
59+
blacklisted_user_id BIGINT REFERENCES "User"(id) -- User to be blacklisted
7760
);
7861

7962
-- GroupRule table: Stores individual rules for each group
80-
CREATE TABLE GroupRule (
63+
CREATE TABLE IF NOT EXISTS "GroupRule" (
8164
id SERIAL PRIMARY KEY,
82-
group_id BIGINT REFERENCES Group(id),
65+
group_id BIGINT REFERENCES "Group"(id),
8366
rule_text TEXT, -- Text of the rule
8467
added_at TIMESTAMP DEFAULT NOW(),
85-
added_by BIGINT REFERENCES User(id)
68+
added_by BIGINT REFERENCES "User"(id)
8669
);
8770

8871
-- GroupMessageSettings table: Stores group message settings (e.g., slow mode)
89-
CREATE TABLE GroupMessageSettings (
72+
CREATE TABLE IF NOT EXISTS "GroupMessageSettings" (
9073
id SERIAL PRIMARY KEY,
91-
group_id BIGINT REFERENCES Group(id),
74+
group_id BIGINT REFERENCES "Group"(id),
9275
is_locked BOOLEAN DEFAULT FALSE,
9376
welcome_message TEXT,
9477
last_updated TIMESTAMP DEFAULT NOW()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Seeding the User table
2+
INSERT INTO "User" (telegram_id, username, first_name,role, warnings, approved_groups)
3+
VALUES
4+
(111222333, 'user1','fuser1' ,'admin', 0, ARRAY[123456789, 987654321]),
5+
(444555666, 'user2', 'fuser2','owner', 2, ARRAY[987654321]),
6+
(777888999, 'user3','fuser3' ,'user', 0, ARRAY[123456789]),
7+
(333444555, 'user4', 'fuser4','user', 1, ARRAY[123456789, 987654321]);
8+
-- Seeding the Group table
9+
INSERT INTO "Group" (group_id, group_name, rules, black_list, chat_permissions, updated_at, joined_at, approved_users, warnings, is_spam_time,members)
10+
VALUES
11+
(123456789, 'Test Group 1', ARRAY['Rule 1', 'Rule 2'], ARRAY['BadWord1', 'BadWord2'], '{"can_post": true, "can_message": true}', NOW(), NOW(), ARRAY[1, 2], ARRAY[0], FALSE,ARRAY[0,2]),
12+
(987654321, 'Test Group 2', ARRAY['Rule A', 'Rule B'], ARRAY['OffensiveUser1'], '{"can_post": false, "can_message": false}', NOW(), NOW(), ARRAY[3, 4], ARRAY[1], TRUE,ARRAY[3,4]);
13+
-- Seeding the Warning table after Group data has been inserted
14+
INSERT INTO "Warning" (user_id, group_id, warned_at, reason)
15+
VALUES
16+
(1, 1, NOW(), 'Spamming'),
17+
(2, 2, NOW(), 'Offensive behavior');
18+
-- Seeding the ApprovedUser table
19+
INSERT INTO "ApprovedUser" (user_id, group_id, username)
20+
VALUES
21+
(1, 1, 'user1'),
22+
(2, 2, 'user2');
23+
24+
-- Seeding the Channel table
25+
INSERT INTO "Channel" (name, channel_id, admins)
26+
VALUES
27+
('Test Channel 1', 100010001, ARRAY[1, 2]),
28+
('Test Channel 2', 200020002, ARRAY[3, 4]);
29+
-- Seeding the Blacklist table
30+
INSERT INTO "Blacklist" (group_id, blacklisted_word, blacklisted_user_id)
31+
VALUES
32+
(1, 'BadWord1', NULL),
33+
(2, 'OffensiveUser1', 4);
34+
-- Seeding the GroupRule table
35+
INSERT INTO "GroupRule" (group_id, rule_text, added_by)
36+
VALUES
37+
(1, 'No spamming allowed', 1),
38+
(2, 'Be respectful to others', 2);
39+
-- Seeding the GroupMessageSettings table
40+
INSERT INTO "GroupMessageSettings" (group_id, is_locked, welcome_message)
41+
VALUES
42+
(1, FALSE, 'Welcome to the group!'),
43+
(2, TRUE, 'Group is locked, no messages allowed');

0 commit comments

Comments
 (0)