Skip to content

Commit 0757bd7

Browse files
committed
feat: integrate chatbot's ability to create task
1 parent c4b702b commit 0757bd7

2 files changed

Lines changed: 95 additions & 4 deletions

File tree

src/lib/features/chatbot/services/chatbot_services.dart

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ class ChatBotAssistantService {
4141
description:
4242
'Danh sách các thẻ phân loại (ví dụ: ["Học tập", "Gấp", "Backend"]). Gửi mảng rỗng [] nếu không có.',
4343
),
44+
'start_time': Schema(
45+
SchemaType.string,
46+
description:
47+
'Thời gian bắt đầu công việc theo định dạng ISO 8601 (VD: 2026-04-18T15:00:00Z). Nếu người dùng không nói, hãy bỏ qua hoặc để rỗng.',
48+
),
49+
'due_time': Schema(
50+
SchemaType.string,
51+
description:
52+
'Thời gian kết thúc (deadline) theo định dạng ISO 8601. Nếu không có, bỏ qua.',
53+
),
54+
'category_name': Schema(
55+
SchemaType.string,
56+
description:
57+
'Phân loại công việc vào 1 trong 4 nhóm: "Cá nhân", "Học tập", "Công việc", hoặc "Giải trí". Nếu không phân loại được, mặc định trả về "Cá nhân".',
58+
),
4459
},
4560
requiredProperties: ['title', 'priority', 'tags'],
4661
),
@@ -77,26 +92,38 @@ class ChatBotAssistantService {
7792
final priority = (args['priority'] as num?)?.toInt() ?? 1;
7893
final rawTags = args['tags'] as List<dynamic>? ?? [];
7994
final tags = rawTags.map((e) => e.toString()).toList();
95+
final startTime = args['start_time'] as String?;
96+
final dueTime = args['due_time'] as String?;
8097

8198
final userId = Supabase.instance.client.auth.currentUser?.id;
8299
if (userId == null) {
83100
return 'Vui lòng đăng nhập để tạo công việc.';
84101
}
85-
102+
final categoryName = args['category_name'] as String? ?? 'Cá nhân';
86103
final dbResponse = await Supabase.instance.client.rpc(
87104
'create_task_full',
88105
params: {
89-
'p_title': title,
90-
'p_priority': priority,
106+
'p_title': args['title'],
107+
'p_priority': (args['priority'] as num?)?.toInt() ?? 1,
91108
'p_profile_id': userId,
92-
'p_tag_names': tags,
109+
'p_tag_names':
110+
(args['tags'] as List?)?.map((e) => e.toString()).toList() ??
111+
[],
112+
'p_category_name': categoryName,
113+
'p_start_time': args['start_time'],
114+
'p_due_time': args['due_time'],
93115
},
94116
);
95117

96118
final isSuccess = dbResponse['success'] == true;
119+
if (!isSuccess) {
120+
debugPrint("Lỗi từ Supabase: ${dbResponse['error']}");
121+
}
97122
final functionResponse = await _chatSession!.sendMessage(
98123
Content.functionResponse('create_task_full', {
99124
'status': isSuccess ? 'Thành công' : 'Thất bại',
125+
126+
'reason': isSuccess ? '' : dbResponse['error'].toString(),
100127
}),
101128
);
102129
return functionResponse.text ?? 'Đã xử lý xong yêu cầu của bạn!';
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
CREATE OR REPLACE FUNCTION create_task_full(
2+
p_title TEXT,
3+
p_priority INT4,
4+
p_profile_id UUID,
5+
p_tag_names TEXT[],
6+
p_category_name TEXT,
7+
p_start_time TIMESTAMPTZ default null,
8+
p_due_time TIMESTAMPTZ default null
9+
)
10+
RETURNS JSON
11+
LANGUAGE plpgsql
12+
AS $$
13+
DECLARE
14+
v_task_id INT8;
15+
v_tag_name TEXT;
16+
v_tag_id INT8;
17+
v_category_id INT8;
18+
BEGIN
19+
SELECT id INTO v_category_id
20+
FROM category
21+
WHERE profile_id = p_profile_id AND name ILIKE p_category_name
22+
LIMIT 1;
23+
24+
IF v_category_id IS NULL THEN
25+
SELECT id INTO v_category_id FROM category WHERE profile_id = p_profile_id AND name = 'Cá nhân' LIMIT 1;
26+
END IF;
27+
28+
INSERT INTO task (title, priority, profile_id, status, category_id,start_time,due_time)
29+
VALUES (p_title, p_priority, p_profile_id, 0,v_category_id,p_start_time,p_due_time)
30+
RETURNING id INTO v_task_id;
31+
32+
IF p_tag_names IS NOT NULL THEN
33+
FOREACH v_tag_name IN ARRAY p_tag_names
34+
LOOP
35+
v_tag_name := trim(v_tag_name);
36+
37+
IF v_tag_name != '' THEN
38+
INSERT INTO tag (name, profile_id, color_code)
39+
VALUES (v_tag_name, p_profile_id, '#6200EE')
40+
ON CONFLICT (name, profile_id)
41+
DO UPDATE SET name = EXCLUDED.name
42+
RETURNING id INTO v_tag_id;
43+
44+
45+
INSERT INTO task_tags (task_id, tag_id)
46+
VALUES (v_task_id, v_tag_id)
47+
ON CONFLICT DO NOTHING;
48+
END IF;
49+
END LOOP;
50+
END IF;
51+
52+
RETURN json_build_object(
53+
'success', true,
54+
'task_id', v_task_id,
55+
'message', 'Đã tạo task với priority ' || p_priority
56+
);
57+
58+
EXCEPTION WHEN OTHERS THEN
59+
RETURN json_build_object(
60+
'success', false,
61+
'error', SQLERRM
62+
);
63+
END;
64+
$$;

0 commit comments

Comments
 (0)