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