Refactor giai đoạn 1: test các tính năng vừa thay đổi như tour, scene...

This commit is contained in:
2026-06-10 21:58:45 +07:00
parent 3f1b31b233
commit 358a98b21b
31 changed files with 1391 additions and 638 deletions
+25 -1
View File
@@ -38,9 +38,19 @@ router.post('/create', protect, async (req, res) => {
return res.status(403).json({ message: 'Không có quyền tạo hotspot cho scene này' });
}
// [NEW LOGIC] Xử lý liên kết chéo giữa các Tour
let target_tour_id = undefined;
if (target_scene_id) {
const targetScene = await Scene.findById(target_scene_id);
if (targetScene && targetScene.tourId && parentScene.tourId && targetScene.tourId.toString() !== parentScene.tourId.toString()) {
target_tour_id = targetScene.tourId;
}
}
const hotspot = new Hotspot({
parent_scene_id,
target_scene_id,
target_tour_id,
title,
description,
coordinates: {
@@ -87,7 +97,7 @@ router.post('/create', protect, async (req, res) => {
*/
router.put('/update/:id', protect, async (req, res) => {
try {
const { title, description, coordinates } = req.body;
const { title, description, coordinates, target_scene_id } = req.body;
const hotspot = await Hotspot.findById(req.params.id);
if (!hotspot) return res.status(404).json({ message: 'Hotspot không tồn tại' });
@@ -96,6 +106,20 @@ router.put('/update/:id', protect, async (req, res) => {
return res.status(403).json({ message: 'Không có quyền cập nhật' });
}
// Cập nhật target_scene_id và tính toán lại target_tour_id nếu có thay đổi
if (target_scene_id) {
const targetScene = await Scene.findById(target_scene_id);
if (targetScene) {
hotspot.target_scene_id = target_scene_id;
// Kiểm tra liên kết chéo
if (targetScene.tourId && parentScene.tourId && targetScene.tourId.toString() !== parentScene.tourId.toString()) {
hotspot.target_tour_id = targetScene.tourId;
} else {
hotspot.target_tour_id = undefined;
}
}
}
if (title) hotspot.title = title;
if (description) hotspot.description = description;
if (coordinates) hotspot.coordinates = coordinates;