fea Visa-VisaDetail

This commit is contained in:
r2xrzh9q2z-lab
2026-02-05 12:49:59 +07:00
parent c3a55b13f8
commit ae468e7f26
6 changed files with 1319 additions and 1270 deletions

View File

@@ -47,9 +47,9 @@ const Visa = require("../models/visa");
const slugify = require("slugify");
const createSlug = (text) => {
return slugify(text, {
lower: true, // Chuyển về chữ thường
strict: true, // Loại bỏ ký tự đặc biệt
locale: "vi", // Xử lý tiếng Việt chuẩn
lower: true,
strict: true,
locale: "en",
trim: true,
});
};
@@ -57,7 +57,7 @@ const createSlug = (text) => {
// Get visa data from MongoDB
const getVisaData = async () => {
const visa = await Visa.findOne().sort({ updatedAt: -1 }).lean();
const visa = await Visa.findOne().sort({ updatedAt: -1 });
return visa || {};
};
@@ -324,11 +324,11 @@ exports.updateCountry = async (req, res) => {
const { id } = req.params;
let visaData = await getVisaData();
// if (!visaData || !visaData.hero || !visaData.hero.summaryList) {
// return res
// .status(400)
// .json({ error: "Cấu trúc dữ liệu Visa không hợp lệ" });
// }
if (!visaData || !visaData.hero || !visaData.hero.summaryList) {
return res
.status(400)
.json({ error: "Cấu trúc dữ liệu Visa không hợp lệ" });
}
// 2. Tìm index theo ID (Chuyển về Number để so sánh chính xác)
const countryIndex = visaData.hero.summaryList.findIndex(
@@ -413,43 +413,51 @@ exports.updateCountry = async (req, res) => {
// Delete country
exports.deleteCountry = async (req, res) => {
try {
const { slug } = req.params;
// 1. Lấy id từ params
const { id } = req.params;
let visaData = await getVisaData();
if (!visaData.hero || !visaData.hero.summaryList) {
return res.status(400).json({ error: "Invalid visa data structure" });
if (!visaData || !visaData.hero || !visaData.hero.summaryList) {
return res
.status(400)
.json({ success: false, error: "Cấu trúc dữ liệu Visa không hợp lệ" });
}
// 2. Tìm index theo ID (Chuyển về Number để so sánh chính xác)
const countryIndex = visaData.hero.summaryList.findIndex(
(c) => c.slug === slug,
(c) => c.id === parseInt(id),
);
if (countryIndex === -1) {
return res.status(404).json({ error: `Country "${slug}" not found` });
return res.status(404).json({
success: false,
error: `Không tìm thấy quốc gia có ID: ${id}`,
});
}
// 3. Xóa phần tử khỏi mảng
const deletedCountry = visaData.hero.summaryList[countryIndex];
visaData.hero.summaryList.splice(countryIndex, 1);
// Update database
const updatedData = {
...(visaData.toObject ? visaData.toObject() : visaData),
};
if (visaData._id) {
await Visa.findByIdAndUpdate(visaData._id, updatedData, { new: true });
} else {
await Visa.create(updatedData);
// 4. Cập nhật vào Database
if (visaData.markModified) {
visaData.markModified("hero.summaryList");
}
console.log(`✅ Country "${deletedCountry.name}" deleted successfully`);
res.json({
if (visaData._id) {
await visaData.save();
} else {
await Visa.create(visaData);
}
console.log(`✅ Deleted Successfully: "${deletedCountry.name}"`);
return res.json({
success: true,
message: `Country "${deletedCountry.name}" deleted successfully`,
message: `Country "${deletedCountry.name}" Deleted Successfully`,
});
} catch (err) {
console.error("Delete country error:", err);
res.status(500).json({ error: err.message });
console.error("❌ Error Delete:", err);
return res.status(500).json({ success: false, error: err.message });
}
};
@@ -469,9 +477,7 @@ exports.api = async (req, res) => {
const heroData = visaData?.hero;
// 2. Lấy riêng phần hero (Dùng biến mới, không gán đè vào const)
const baseUrl =
process.env.BACKEND_URL || `${req.protocol}://${req.get("host")}`;
const processedData = addBaseUrlToImages(heroData, baseUrl);
const processedData = heroData;
return res.json({
success: true,
@@ -552,20 +558,16 @@ exports.apiCountry = async (req, res) => {
data: null,
});
}
const baseUrl =
process.env.BACKEND_URL || `${req.protocol}://${req.get("host")}`;
// 3. Chỉ lấy phần chi tiết (detailed view)
// Lưu ý: Chúng ta copy ra object mới để tránh tham chiếu dữ liệu gốc
const detailedData = JSON.parse(JSON.stringify(country.viewDetail));
// 4. Gắn baseUrl vào các ảnh nằm trong phần chi tiết này
const processedData = addBaseUrlToImages(detailedData, baseUrl);
const processedData = detailedData;
return res.json({
success: true,
data: processedData, // Trả về nội dung của detailedView
data: processedData,
});
} catch (err) {
console.error("Visa country API error:", err);