File size: 3,223 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { txasupabase } from '../../../lib/txasupabase.js';

export const POST = async ({ request, cookies }) => {
  try {
    // 1. Kiểm tra quyền Admin bảo mật
    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, ids, action } = body;

    if ((!id && !ids) || !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' }
      });
    }

    const idList = ids ? ids : [id];

    if (action === 'approve') {
      const { data, error } = await txasupabase.supabase
        .from('zalo_access')
        .update({ status: 'approved' })
        .in('id', idList);

      if (error) throw error;

      // Ghi log hoạt động
      await txasupabase.createLog(
        'zalo_access_approved_bulk',
        'info',
        `Admin "${user.username}" phê duyệt đồng loạt quyền truy cập cho ${idList.length} yêu cầu`,
        { record_ids: idList, approved_by: user.username }
      );

    } else if (action === 'reject') {
      const { data, error } = await txasupabase.supabase
        .from('zalo_access')
        .update({ status: 'rejected' })
        .in('id', idList);

      if (error) throw error;

      await txasupabase.createLog(
        'zalo_access_rejected_bulk',
        'warn',
        `Admin "${user.username}" từ chối đồng loạt quyền truy cập cho ${idList.length} yêu cầu`,
        { record_ids: idList, rejected_by: user.username }
      );

    } else if (action === 'delete') {
      const { data, error } = await txasupabase.supabase
        .from('zalo_access')
        .delete()
        .in('id', idList);

      if (error) throw error;

      await txasupabase.createLog(
        'zalo_access_deleted_bulk',
        'warn',
        `Admin "${user.username}" xóa đồng loạt ${idList.length} yêu cầu truy cập`,
        { record_ids: idList, 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: `Đã thực hiện ${action === 'approve' ? 'phê duyệt' : action === 'reject' ? 'từ chối' : 'xóa'} thành công cho ${idList.length} yêu cầu!` 
    }), {
      status: 200,
      headers: { 'Content-Type': 'application/json' }
    });
  } catch (err) {
    console.error('Error handling admin duyet-zalo 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' }
    });
  }
};