Spaces:
Sleeping
Sleeping
| 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' } | |
| }); | |
| } | |
| }; | |