forked from UKSOURCE/cms.lams
header
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
require('dotenv').config();
|
||||
const mongoose = require('mongoose');
|
||||
const slugify = require('slugify');
|
||||
|
||||
// Data from lams/app/components/layout/Header/header.json
|
||||
const headerJsonData = {
|
||||
"logo": {
|
||||
"image": "/uploads/header/logo.jp",
|
||||
"href": "/"
|
||||
},
|
||||
"navLinks": [
|
||||
{
|
||||
"label": "About Us",
|
||||
"href": "/about",
|
||||
"children": [
|
||||
{
|
||||
"label": "History & Milestones",
|
||||
"href": "/about/history"
|
||||
},
|
||||
{
|
||||
"label": "Accreditation",
|
||||
"href": "/about/accreditation"
|
||||
},
|
||||
{
|
||||
"label": "Partnerships",
|
||||
"href": "/about/partnerships"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"label": "Programs",
|
||||
"href": "/programmes"
|
||||
},
|
||||
{
|
||||
"label": "Student Support",
|
||||
"href": "/student-support"
|
||||
},
|
||||
{
|
||||
"label": "Admissions & Tuition",
|
||||
"href": "/admissions"
|
||||
},
|
||||
{
|
||||
"label": "Blog",
|
||||
"href": "/blog"
|
||||
},
|
||||
{
|
||||
"label": "Contact",
|
||||
"href": "/contact"
|
||||
}
|
||||
],
|
||||
"actions": {
|
||||
"signIn": {
|
||||
"label": "Sign In",
|
||||
"href": "/signin"
|
||||
},
|
||||
"cta": {
|
||||
"label": "Request Info",
|
||||
"href": "/request"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
async function seedHeader() {
|
||||
try {
|
||||
console.log('=== Seed Header Data from JSON ===');
|
||||
console.log('Connecting to MongoDB...');
|
||||
|
||||
await mongoose.connect(process.env.MONGODB_URI);
|
||||
console.log('✓ Connected to MongoDB');
|
||||
|
||||
const Header = require('../models/header');
|
||||
const HeaderMenu = require('../models/headerMenu');
|
||||
|
||||
// Step 1: Seed Header document
|
||||
console.log('\nStep 1: Seeding Header document...');
|
||||
|
||||
const existingHeader = await Header.findOne();
|
||||
|
||||
if (existingHeader) {
|
||||
console.log('⚠ Header document already exists. Updating...');
|
||||
existingHeader.logo = {
|
||||
light: headerJsonData.logo.image,
|
||||
dark: '',
|
||||
alt: 'LAMS Logo',
|
||||
};
|
||||
existingHeader.signInButton = {
|
||||
label: headerJsonData.actions.signIn.label,
|
||||
href: headerJsonData.actions.signIn.href,
|
||||
};
|
||||
existingHeader.ctaButton = {
|
||||
label: headerJsonData.actions.cta.label,
|
||||
href: headerJsonData.actions.cta.href,
|
||||
style: 'primary',
|
||||
};
|
||||
existingHeader.status = 'active';
|
||||
await existingHeader.save();
|
||||
console.log('✓ Header document updated');
|
||||
} else {
|
||||
const header = new Header({
|
||||
logo: {
|
||||
light: headerJsonData.logo.image,
|
||||
dark: '',
|
||||
alt: 'LAMS Logo',
|
||||
},
|
||||
signInButton: {
|
||||
label: headerJsonData.actions.signIn.label,
|
||||
href: headerJsonData.actions.signIn.href,
|
||||
},
|
||||
ctaButton: {
|
||||
label: headerJsonData.actions.cta.label,
|
||||
href: headerJsonData.actions.cta.href,
|
||||
style: 'primary',
|
||||
},
|
||||
status: 'active',
|
||||
order: 1,
|
||||
});
|
||||
await header.save();
|
||||
console.log('✓ Header document created');
|
||||
}
|
||||
|
||||
// Step 2: Seed HeaderMenu documents
|
||||
console.log('\nStep 2: Seeding HeaderMenu documents...');
|
||||
|
||||
const existingMenuCount = await HeaderMenu.countDocuments();
|
||||
|
||||
if (existingMenuCount > 0) {
|
||||
console.log(`⚠ Found ${existingMenuCount} existing menu items. Skipping menu seed.`);
|
||||
console.log(' To re-seed menu, delete existing menu items first.');
|
||||
} else {
|
||||
let order = 0;
|
||||
|
||||
for (const navLink of headerJsonData.navLinks) {
|
||||
// Create parent menu item
|
||||
const parentSlug = slugify(navLink.label, { lower: true, strict: true });
|
||||
const parentMenu = new HeaderMenu({
|
||||
title: navLink.label,
|
||||
slug: parentSlug,
|
||||
url: navLink.href,
|
||||
parentId: null,
|
||||
order: order++,
|
||||
status: 'active',
|
||||
type: 'internal',
|
||||
});
|
||||
await parentMenu.save();
|
||||
console.log(` ✓ Created parent menu: ${navLink.label}`);
|
||||
|
||||
// Create children menu items if they exist
|
||||
if (navLink.children && navLink.children.length > 0) {
|
||||
let childOrder = 0;
|
||||
for (const child of navLink.children) {
|
||||
const childSlug = slugify(child.label, { lower: true, strict: true });
|
||||
const childMenu = new HeaderMenu({
|
||||
title: child.label,
|
||||
slug: childSlug,
|
||||
url: child.href,
|
||||
parentId: parentMenu._id,
|
||||
order: childOrder++,
|
||||
status: 'active',
|
||||
type: 'internal',
|
||||
});
|
||||
await childMenu.save();
|
||||
console.log(` ✓ Created child menu: ${child.label}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✓ Created ${order} parent menu items with their children`);
|
||||
}
|
||||
|
||||
console.log('\n=== Seed Complete ===');
|
||||
await mongoose.disconnect();
|
||||
console.log('✓ Disconnected from MongoDB');
|
||||
|
||||
} catch (error) {
|
||||
console.error('✗ Seed failed:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Run seed if this script is executed directly
|
||||
if (require.main === module) {
|
||||
seedHeader()
|
||||
.then(() => process.exit(0))
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = seedHeader;
|
||||
Reference in New Issue
Block a user