Spaces:
Sleeping
Sleeping
File size: 4,964 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import { deleteLocalMovie, restoreLocalMovie, resetLocalMovie } from '../../../../lib/localDb.js';
import { txasupabase } from '../../../../lib/txasupabase.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 { slugs, action, value } = await request.json();
if (!slugs || !Array.isArray(slugs) || slugs.length === 0) {
return new Response(
JSON.stringify({ error: 'Danh sách slug rỗng hoặc không hợp lệ!' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
const validActions = ['delete', 'restore', 'reset', 'hard-delete', 'status', 'require-login', 'free-login'];
if (!validActions.includes(action)) {
return new Response(
JSON.stringify({ error: 'Hành động hàng loạt không hợp lệ!' }),
{ status: 400, headers: { 'Content-Type': 'application/json' } }
);
}
let processedCount = 0;
for (const slug of slugs) {
if (action === 'delete') {
await deleteLocalMovie(slug);
processedCount++;
} else if (action === 'restore') {
const success = await restoreLocalMovie(slug);
if (success) processedCount++;
} else if (action === 'reset') {
const success = await resetLocalMovie(slug);
if (success) processedCount++;
} else if (action === 'hard-delete') {
// Xóa hoàn toàn phim khỏi cả bảng movies và deleted_movies
await txasupabase.supabase.from('movies').delete().eq('slug', slug);
await txasupabase.supabase.from('deleted_movies').delete().eq('slug', slug);
processedCount++;
} else if (action === 'status') {
const movieObj = await txasupabase.getMovieBySlug(slug);
if (movieObj) {
movieObj.movie.status = value;
// Cập nhật text hiển thị tương ứng
if (value === 'completed') {
movieObj.movie.episode_current = 'Hoàn Thành';
} else if (value === 'ongoing') {
movieObj.movie.episode_current = 'Đang Chiếu';
} else if (value === 'trailer') {
movieObj.movie.episode_current = 'Trailer / Sắp chiếu';
}
await txasupabase.saveLocalMovie(slug, movieObj);
processedCount++;
}
} else if (action === 'require-login') {
const movieObj = await txasupabase.getMovieBySlug(slug);
if (movieObj) {
movieObj.movie.require_login = true;
movieObj.movie.only_login = true;
await txasupabase.saveLocalMovie(slug, movieObj);
processedCount++;
}
} else if (action === 'free-login') {
const movieObj = await txasupabase.getMovieBySlug(slug);
if (movieObj) {
movieObj.movie.require_login = false;
movieObj.movie.only_login = false;
await txasupabase.saveLocalMovie(slug, movieObj);
processedCount++;
}
}
}
let message = '';
if (action === 'delete') {
message = `Đã ẩn thành công ${processedCount} phim được chọn!`;
} else if (action === 'restore') {
message = `Đã khôi phục hiển thị thành công ${processedCount} phim được chọn!`;
} else if (action === 'reset') {
message = `Đã đặt lại thành công ${processedCount} phim được chọn về dữ liệu gốc!`;
} else if (action === 'hard-delete') {
message = `Đã xóa vĩnh viễn thành công ${processedCount} phim được chọn khỏi cơ sở dữ liệu!`;
} else if (action === 'status') {
const statusNames = { completed: 'Hoàn thành', ongoing: 'Đang chiếu', trailer: 'Sắp ra mắt' };
message = `Đã đổi nhanh trạng thái thành công cho ${processedCount} phim thành "${statusNames[value] || value}"!`;
} else if (action === 'require-login') {
message = `Đã thiết lập bắt buộc đăng nhập thành công cho ${processedCount} phim!`;
} else if (action === 'free-login') {
message = `Đã mở khóa tự do xem phim không cần đăng nhập cho ${processedCount} phim!`;
}
return new Response(
JSON.stringify({ message, processedCount }),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
} catch (error) {
return new Response(
JSON.stringify({ error: 'Gặp lỗi khi thực hiện bulk action: ' + error.message }),
{ status: 500, headers: { 'Content-Type': 'application/json' } }
);
}
}
|