File size: 1,578 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
import { resetLocalMovie } from '../../../../lib/localDb.js';

export async function POST({ request, cookies }) {
  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: 'Từ chối truy cập! Yêu cầu quyền quản trị viên.' }), 
      { status: 403, headers: { 'Content-Type': 'application/json' } }
    );
  }

  try {
    const { slug } = await request.json();
    if (!slug) {
      return new Response(
        JSON.stringify({ error: 'Thiếu thông số slug phim!' }), 
        { status: 400, headers: { 'Content-Type': 'application/json' } }
      );
    }

    const success = await resetLocalMovie(slug);
    if (success) {
      return new Response(
        JSON.stringify({ message: 'Đã xóa bỏ các thay đổi cục bộ, đưa phim về trạng thái gốc từ API!' }), 
        { status: 200, headers: { 'Content-Type': 'application/json' } }
      );
    } else {
      return new Response(
        JSON.stringify({ error: 'Phim này không có thay đổi cục bộ nào để đặt lại!' }), 
        { status: 400, headers: { 'Content-Type': 'application/json' } }
      );
    }
  } catch (error) {
    return new Response(
      JSON.stringify({ error: 'Gặp lỗi trong quá trình đặt lại: ' + error.message }), 
      { status: 500, headers: { 'Content-Type': 'application/json' } }
    );
  }
}