111 lines
4.6 KiB
JavaScript
Executable File
111 lines
4.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
dotenv.config({ path: path.resolve(__dirname, '../.env.development') });
|
|
dotenv.config({ path: path.resolve(__dirname, '../.env.local') });
|
|
dotenv.config({ path: path.resolve(__dirname, '../.env') });
|
|
|
|
const { models, initializeDatabaseModels, sequelize } = await import('../src/shared/database/models.js');
|
|
initializeDatabaseModels();
|
|
|
|
const { seedTenantStarterData } = await import('../src/seeders/tenantStarterData.service.js');
|
|
|
|
async function run() {
|
|
const args = process.argv.slice(2);
|
|
let targetTenantId = null;
|
|
let seedAll = false;
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
if (args[i] === '--all') {
|
|
seedAll = true;
|
|
} else if (args[i] === '--tenant-id' && args[i + 1]) {
|
|
targetTenantId = parseInt(args[i + 1], 10);
|
|
i++;
|
|
} else if (args[i] === '--canonical-id' && args[i + 1]) {
|
|
const canonical = args[i + 1].trim().toLowerCase();
|
|
const tenant = await models.Tenant.findOne({ where: { canonical_tenant_id: canonical } });
|
|
if (!tenant) {
|
|
console.error(`Tenant with canonical ID "${canonical}" not found.`);
|
|
process.exit(1);
|
|
}
|
|
targetTenantId = tenant.id;
|
|
console.log(`Resolved canonical ID "${canonical}" -> Tenant ID: ${targetTenantId}`);
|
|
i++;
|
|
} else if (args[i] === '--email' && args[i + 1]) {
|
|
const email = args[i + 1].trim().toLowerCase();
|
|
const user = await models.User.findOne({ where: { email } });
|
|
if (user && user.tenant_id) {
|
|
targetTenantId = user.tenant_id;
|
|
console.log(`Resolved user "${email}" -> Tenant ID: ${targetTenantId}`);
|
|
} else {
|
|
const tenant = await models.Tenant.findOne({ where: { contact_email: email } });
|
|
if (tenant) {
|
|
targetTenantId = tenant.id;
|
|
console.log(`Resolved contact email "${email}" -> Tenant ID: ${targetTenantId}`);
|
|
} else {
|
|
console.error(`Tenant or User with email "${email}" not found.`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
let tenantsToSeed = [];
|
|
if (targetTenantId) {
|
|
const t = await models.Tenant.findByPk(targetTenantId);
|
|
if (!t) {
|
|
console.error(`Tenant ID ${targetTenantId} not found.`);
|
|
process.exit(1);
|
|
}
|
|
tenantsToSeed = [t];
|
|
} else {
|
|
// Default to all active tenants
|
|
tenantsToSeed = await models.Tenant.findAll({ where: { status: true } });
|
|
if (tenantsToSeed.length === 0) {
|
|
console.error('No active tenants found in PIM.');
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
console.log(`\n======================================================`);
|
|
console.log(`🌱 PIM TENANT STARTER PACK SEEDER`);
|
|
console.log(`Targeting ${tenantsToSeed.length} tenant(s)...`);
|
|
console.log(`======================================================\n`);
|
|
|
|
let successCount = 0;
|
|
for (const tenant of tenantsToSeed) {
|
|
const tenantLabel = tenant.tenant_name || tenant.tenant_code || `Tenant #${tenant.id}`;
|
|
console.log(`▶ Seeding Tenant: "${tenantLabel}" (ID: ${tenant.id}, Canonical: ${tenant.canonical_tenant_id || 'N/A'})...`);
|
|
const tx = await sequelize.transaction();
|
|
try {
|
|
const result = await seedTenantStarterData(tenant.id, { transaction: tx });
|
|
await tx.commit();
|
|
console.log(` ✅ Units (${result.units.length}): ${result.units.map(u => u.name).join(', ')}`);
|
|
console.log(` ✅ Brands (${result.brands.length}): ${result.brands.map(b => b.name).join(', ')}`);
|
|
console.log(` ✅ Categories (${result.categories.length}): ${result.categories.map(c => c.name).join(', ')}`);
|
|
console.log(` ✅ Families (${result.families.length}): ${result.families.map(f => f.name).join(', ')}`);
|
|
console.log(` ✅ Channels (${result.channels.length}): ${result.channels.map(ch => ch.name).join(', ')}`);
|
|
console.log(` ✅ Products (${result.products.length}): ${result.products.map(p => p.name).join(', ')}`);
|
|
console.log(` 🎉 Successfully seeded starter pack for Tenant ${tenant.id}!\n`);
|
|
successCount++;
|
|
} catch (err) {
|
|
await tx.rollback();
|
|
console.error(` ❌ Failed seeding Tenant ${tenant.id}:`, err.message);
|
|
}
|
|
}
|
|
|
|
console.log(`======================================================`);
|
|
console.log(`✨ Completed: ${successCount}/${tenantsToSeed.length} tenants seeded successfully.`);
|
|
console.log(`======================================================\n`);
|
|
process.exit(0);
|
|
}
|
|
|
|
run().catch(err => {
|
|
console.error('Seeder execution error:', err);
|
|
process.exit(1);
|
|
});
|