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
+83
View File
@@ -0,0 +1,83 @@
const mongoose = require('mongoose');
const connectDB = require('../config/db');
const Scene = require('../models/Scene');
const Tour = require('../models/Tour');
/**
* Script migration Giai đoạn 3:
* 1. Tạo các bản ghi Tour tương ứng cho mỗi "Cảnh gốc" (Dựa trên tourId cũ).
* 2. Gán lại tourId của tất cả các cảnh con vào bản ghi Tour mới (Ref: Tour).
* 3. Chuyển thông tin chia sẻ từ Scene gốc sang Tour làm nguồn dữ liệu chính.
*/
const migrateToTours = async () => {
try {
console.log('--- Bắt đầu quy trình migration sang cấu trúc Tour-centric ---');
await connectDB();
// Lấy danh sách tất cả các tourId hiện có (trước đây trỏ đến Scene ID)
// Sử dụng distinct để lọc ra các nhóm Tour độc lập
const oldTourIds = await Scene.distinct('tourId');
console.log(`Tìm thấy ${oldTourIds.length} nhóm cảnh cần chuyển đổi sang Tour.`);
for (const oldId of oldTourIds) {
if (!oldId) continue;
// Tìm "Cảnh gốc" của tour này (Cảnh có ID trùng với tourId cũ)
// Nếu không tìm thấy (do dữ liệu cũ lỗi), lấy cảnh đầu tiên trong nhóm làm gốc
let rootScene = await Scene.findById(oldId).lean();
if (!rootScene) {
rootScene = await Scene.findOne({ tourId: oldId }).lean();
}
if (!rootScene) {
console.warn(`[!] Không tìm thấy dữ liệu cảnh cho tourId cũ: ${oldId}. Bỏ qua.`);
continue;
}
console.log(`Đang tạo Tour cho: ${rootScene.name || rootScene.title} (${rootScene._id})`);
// 1. Khởi tạo Tour mới và sao chép thông tin chia sẻ từ Scene gốc
const newTour = new Tour({
name: rootScene.name || rootScene.title || "Tour mới",
description: rootScene.description || "",
location: {
lat: rootScene.gps?.lat || 0,
lng: rootScene.gps?.lng || 0
},
createdBy: rootScene.createdBy,
rootSceneId: rootScene._id,
privacy: rootScene.privacy || 'private',
shareToken: rootScene.shareToken,
shareTokenExpires: rootScene.shareTokenExpires,
sharedWith: rootScene.sharedWith || [],
sharedEmails: rootScene.sharedEmails || [],
scenes: [] // Sẽ được cập nhật danh sách ID cảnh con bên dưới
});
// 2. Thu thập tất cả các cảnh con thuộc tour này
const memberScenes = await Scene.find({ tourId: oldId });
newTour.scenes = memberScenes.map(s => s._id);
await newTour.save();
// 3. Cập nhật tourId của tất cả cảnh trỏ về bản ghi Tour (ObjectId) mới tạo
// Việc này chuyển đổi từ quan hệ Scene -> Scene sang Scene -> Tour
await Scene.updateMany(
{ tourId: oldId },
{ $set: { tourId: newTour._id } }
);
console.log(` -> Thành công: Tour [${newTour._id}] đã nhận ${memberScenes.length} cảnh.`);
}
console.log('--- Hoàn tất migration sang cấu trúc Tour! ---');
mongoose.connection.close();
process.exit(0);
} catch (error) {
console.error('Lỗi Migration:', error.message);
if (mongoose.connection) mongoose.connection.close();
process.exit(1);
}
};
migrateToTours();
+69
View File
@@ -0,0 +1,69 @@
const mongoose = require('mongoose');
const connectDB = require('../config/db');
const User = require('../models/User');
const Asset = require('../models/Asset');
/**
* Script migration để chuẩn hóa thông tin người dùng:
* 1. Chuyển đổi các Role cũ (Chủ sở hữu, editor, Thành viên) sang enum mới.
* 2. Khởi tạo/Cập nhật object storage (used/quota) dựa trên dữ liệu thực tế từ Asset.
*/
const migrateUsers = async () => {
try {
console.log('--- Bắt đầu quy trình migration User ---');
await connectDB();
const users = await User.find({});
console.log(`Tìm thấy ${users.length} người dùng cần rà soát.`);
for (const user of users) {
console.log(`Đang xử lý user: ${user.username} (${user._id})`);
// 1. Chuẩn hóa Role
// Bản cũ có thể có: 'admin', 'Chủ sở hữu', 'editor', 'moderator', 'Thành viên'
let oldRole = user.role;
if (oldRole === 'Chủ sở hữu') user.role = 'admin';
else if (oldRole === 'editor' || oldRole === 'Thành viên') user.role = 'user';
const validRoles = ['admin', 'moderator', 'user'];
if (!validRoles.includes(user.role)) {
user.role = 'user';
}
// 1.1. Đảm bảo trường agreedToRules tồn tại và có giá trị
if (user.agreedToRules === undefined || user.agreedToRules === null) {
user.agreedToRules = true; // Giả định người dùng cũ đã đồng ý
}
// 2. Tính toán dung lượng đã sử dụng từ Asset thực tế
const usage = await Asset.aggregate([
{ $match: { uploadedBy: user._id } },
{ $group: { _id: null, total: { $sum: "$fileSize" } } }
]);
const usedBytes = usage.length > 0 ? usage[0].total : 0;
// 3. Cập nhật cấu trúc storage
// Nếu user đã có quota riêng thì giữ lại, nếu không dùng mặc định 5GB (5368709120 bytes)
const currentQuota = user.storage && user.storage.quota ? user.storage.quota : 5368709120;
user.storage = {
used: usedBytes,
quota: currentQuota
};
// Lưu thay đổi (Middleware hash password sẽ không chạy vì password không bị sửa)
await user.save();
console.log(` -> Cập nhật: Role [${oldRole} -> ${user.role}] | Storage: ${(usedBytes / (1024*1024)).toFixed(2)} MB / ${(currentQuota / (1024*1024*1024)).toFixed(0)} GB`);
}
console.log('--- Hoàn tất migration User! ---');
mongoose.connection.close();
process.exit(0);
} catch (error) {
console.error('Lỗi Migration:', error.message);
if (mongoose.connection) mongoose.connection.close();
process.exit(1);
}
};
migrateUsers();