42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const Company = require('../models/Company');
|
|
const User = require('../models/User');
|
|
|
|
const seedTestData = async () => {
|
|
try {
|
|
// Find the superadmin user
|
|
const superadmin = await User.findOne({ role: 'superadmin' });
|
|
|
|
if (!superadmin) {
|
|
console.log('Superadmin not found. Please ensure superadmin is created first.');
|
|
return;
|
|
}
|
|
|
|
// Check if default company exists
|
|
let defaultCompany = await Company.findOne({ name: 'Default Company' });
|
|
|
|
if (!defaultCompany) {
|
|
// Create default company
|
|
defaultCompany = await Company.create({
|
|
name: 'Default Company',
|
|
createdBy: superadmin._id
|
|
});
|
|
|
|
console.log('Default company created:', defaultCompany.name);
|
|
|
|
// Update superadmin to have this company
|
|
await User.findByIdAndUpdate(superadmin._id, {
|
|
companyId: defaultCompany._id
|
|
});
|
|
|
|
console.log('Superadmin updated with default company');
|
|
} else {
|
|
console.log('Default company already exists');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('Error seeding test data:', error);
|
|
}
|
|
};
|
|
|
|
module.exports = seedTestData;
|