feat: implement chat and conversation history management with database schema and API endpoints
All checks were successful
Build and Release / release (push) Successful in 1m30s

This commit is contained in:
2026-05-08 21:03:26 +07:00
parent 4d4640c20a
commit 600246426b
17 changed files with 1306 additions and 33 deletions

View File

@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS chatbot_histories CASCADE;
DROP TABLE IF EXISTS messages CASCADE;
DROP TABLE IF EXISTS conversations CASCADE;

View File

@@ -0,0 +1,34 @@
CREATE TABLE IF NOT EXISTS conversations (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
mod_id UUID REFERENCES users(id) ON DELETE SET NULL,
status SMALLINT NOT NULL DEFAULT 1, -- 1: waiting, 2: active, 3: resolved
closed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_conversations_user_id ON conversations (user_id);
CREATE INDEX idx_conversations_mod_id ON conversations (mod_id);
CREATE INDEX idx_conversations_status ON conversations (status);
CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY DEFAULT uuidv7(),
conversation_id UUID NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_messages_conversation_id ON messages (conversation_id);
CREATE INDEX idx_messages_created_at ON messages (created_at DESC);
CREATE TABLE IF NOT EXISTS chatbot_histories (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
question TEXT NOT NULL,
answer TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_chatbot_histories_user_id ON chatbot_histories (user_id, created_at DESC);

46
db/query/chat.sql Normal file
View File

@@ -0,0 +1,46 @@
-- name: CreateConversation :one
INSERT INTO conversations (user_id, status)
VALUES ($1, $2)
RETURNING *;
-- name: UpdateConversationStatus :one
UPDATE conversations
SET status = $2, mod_id = COALESCE($3, mod_id), closed_at = $4, updated_at = NOW()
WHERE id = $1
RETURNING *;
-- name: CreateMessage :one
INSERT INTO messages (conversation_id, sender_id, content)
VALUES ($1, $2, $3)
RETURNING *;
-- name: GetMessagesByConversation :many
SELECT * FROM messages
WHERE conversation_id = $1
AND (sqlc.narg('cursor_id')::uuid IS NULL OR id < sqlc.narg('cursor_id')::uuid)
ORDER BY created_at DESC
LIMIT sqlc.arg('limit');
-- name: GetConversationsByIDs :many
SELECT * FROM conversations WHERE id = ANY($1::uuid[]);
-- name: GetMessagesByIDs :many
SELECT * FROM messages WHERE id = ANY($1::uuid[]);
-- name: CreateChatbotHistory :one
INSERT INTO chatbot_histories (user_id, question, answer)
VALUES ($1, $2, $3)
RETURNING *;
-- name: GetChatbotHistory :many
SELECT * FROM (
SELECT * FROM chatbot_histories
WHERE user_id = $1
AND (sqlc.narg('cursor_id')::uuid IS NULL OR id < sqlc.narg('cursor_id')::uuid)
ORDER BY created_at DESC
LIMIT sqlc.arg('limit')
) sub
ORDER BY created_at ASC;
-- name: GetChatbotHistoriesByIDs :many
SELECT * FROM chatbot_histories WHERE id = ANY($1::uuid[]);

View File

@@ -208,3 +208,30 @@ CREATE TABLE IF NOT EXISTS system_statistics (
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS conversations (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
mod_id UUID REFERENCES users(id) ON DELETE SET NULL,
status SMALLINT NOT NULL DEFAULT 1,
closed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY DEFAULT uuidv7(),
conversation_id UUID NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS chatbot_histories (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
question TEXT NOT NULL,
answer TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);