File size: 2,619 Bytes
4bea261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { txasupabase } from '../../../lib/txasupabase.js';

export const POST = async ({ request, cookies }) => {
  try {
    // 1. Xác thực quyền Admin
    const token = cookies.get('auth_token')?.value;
    let user = null;
    if (token) {
      try {
        user = JSON.parse(Buffer.from(token, 'base64').toString('utf-8'));
      } catch(e) {}
    }

    if (!user || user.role !== 'admin') {
      return new Response(JSON.stringify({ error: 'Bạn không có quyền quản trị!' }), {
        status: 403,
        headers: { 'Content-Type': 'application/json' }
      });
    }

    const body = await request.json();
    const { id, action } = body;

    if (!id || !action) {
      return new Response(JSON.stringify({ error: 'Thiếu thông tin ID hoặc hành động!' }), {
        status: 400,
        headers: { 'Content-Type': 'application/json' }
      });
    }

    if (action === 'resolve') {
      // Cập nhật trạng thái báo cáo lỗi thành 'resolved'
      const { data, error } = await txasupabase.supabase
        .from('reports')
        .update({ status: 'resolved' })
        .eq('id', id);

      if (error) throw error;

      await txasupabase.createLog(
        'bug_report_resolved',
        'info',
        `Admin "${user.username}" đánh dấu báo cáo lỗi ID ${id} là Đã khắc phục`,
        { record_id: id, resolved_by: user.username }
      );

    } else if (action === 'delete') {
      // Xóa báo cáo lỗi khỏi cơ sở dữ liệu
      const { data, error } = await txasupabase.supabase
        .from('reports')
        .delete()
        .eq('id', id);

      if (error) throw error;

      await txasupabase.createLog(
        'bug_report_deleted',
        'warn',
        `Admin "${user.username}" xóa báo cáo lỗi ID ${id}`,
        { record_id: id, deleted_by: user.username }
      );
    } else {
      return new Response(JSON.stringify({ error: 'Hành động không hợp lệ!' }), {
        status: 400,
        headers: { 'Content-Type': 'application/json' }
      });
    }

    return new Response(JSON.stringify({ 
      success: true, 
      message: `Đã ${action === 'resolve' ? 'đánh dấu khắc phục' : 'xóa báo cáo'} thành công!` 
    }), {
      status: 200,
      headers: { 'Content-Type': 'application/json' }
    });
  } catch (err) {
    console.error('Error handling admin report status API:', err);
    return new Response(JSON.stringify({ error: err.message || 'Có lỗi xảy ra trên hệ thống!' }), {
      status: 500,
      headers: { 'Content-Type': 'application/json' }
    });
  }
};