feat(about): sync FE-BE data and image upload

This commit is contained in:
2026-02-06 01:58:56 +07:00
parent fb8676879d
commit 8232a36e71
16 changed files with 1096 additions and 2821 deletions

48
scripts/migrateAboutUs.js Normal file
View File

@@ -0,0 +1,48 @@
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const fs = require("fs");
const path = require("path");
// Load environment variables
dotenv.config();
const AboutUs = require("../models/aboutUs");
const migrate = async () => {
try {
console.log("🚀 Starting About Us migration...");
// 1. Connect to MongoDB
await mongoose.connect(process.env.MONGODB_URI);
console.log("✅ MongoDB Connected");
// 2. Read about.json from Backend (Source of Truth)
const jsonPath = path.join(__dirname, "../data/about.json");
if (!fs.existsSync(jsonPath)) {
throw new Error(`Source about.json not found at: ${jsonPath}`);
}
const rawData = fs.readFileSync(jsonPath, "utf8");
const jsonData = JSON.parse(rawData);
console.log("✅ Read about.json successfully");
// 3. Delete existing AboutUs documents (Singleton pattern)
await AboutUs.deleteMany({});
console.log("✅ Cleared existing AboutUs collection");
// 4. Create new AboutUs document with JSON data
const newAboutUs = new AboutUs(jsonData);
await newAboutUs.save();
console.log("✅ Successfully migrated about.json data to MongoDB");
} catch (error) {
console.error("❌ Migration failed:", error.message);
} finally {
// 5. Close connection
await mongoose.connection.close();
console.log("👋 Database connection closed");
process.exit(0);
}
};
migrate();

52
scripts/seedAbout.js Normal file
View File

@@ -0,0 +1,52 @@
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const fs = require("fs");
const path = require("path");
// Load environment variables
dotenv.config();
const AboutUs = require("../models/aboutUs");
const seedAbout = async () => {
try {
console.log("🚀 Starting About section seeding...");
// 1. Connect to MongoDB
if (!process.env.MONGODB_URI) {
throw new Error("MONGODB_URI is not defined in environment variables");
}
await mongoose.connect(process.env.MONGODB_URI);
console.log("✅ MongoDB Connected");
// 2. Read about.json (Single Source of Truth)
const jsonPath = path.join(__dirname, "../data/about.json");
if (!fs.existsSync(jsonPath)) {
throw new Error(`Source about.json not found at: ${jsonPath}`);
}
const rawData = fs.readFileSync(jsonPath, "utf8");
const jsonData = JSON.parse(rawData);
console.log("✅ Read data/about.json successfully");
// 3. Upsert logic (Singleton pattern)
// We look for any existing document and update it, or create a new one if none exists.
await AboutUs.findOneAndUpdate(
{},
jsonData,
{ upsert: true, new: true, setDefaultsOnInsert: true }
);
console.log("✅ Successfully seeded about.json data to MongoDB (Upserted)");
} catch (error) {
console.error("❌ Seeding failed:", error.message);
} finally {
// 4. Close connection
await mongoose.connection.close();
console.log("👋 Database connection closed");
process.exit(0);
}
};
seedAbout();