// Debug script to isolate tenant validation issue const { Sequelize } = require('sequelize'); const path = require('path'); async function debugTenant() { try { console.log('Setting up database...'); const sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false // Disable logging for cleaner output }); // Load models const modelsPath = path.join(__dirname, '..', 'models'); const Tenant = require(path.join(modelsPath, 'Tenant.js'))(sequelize); // Sync database await sequelize.sync({ force: true }); console.log('Attempting to create tenant1...'); const tenant1 = await Tenant.create({ name: 'Test Tenant 1', slug: 'tenant1', domain: 'test1.example.com', is_active: true }); console.log('✅ Tenant1 created successfully:', tenant1.id); console.log('Attempting to create tenant2...'); const tenant2 = await Tenant.create({ name: 'Test Tenant 2', slug: 'tenant2', domain: 'test2.example.com', is_active: true }); console.log('✅ Tenant2 created successfully:', tenant2.id); // Try to create a tenant with same slug (should fail) console.log('Attempting to create duplicate slug (should fail)...'); try { await Tenant.create({ name: 'Duplicate Tenant', slug: 'tenant1', // This should fail domain: 'test3.example.com', is_active: true }); console.log('❌ ERROR: Duplicate slug should have failed!'); } catch (error) { console.log('✅ Correctly caught duplicate slug error:', error.message); } // Try to create a tenant with same domain (should fail) console.log('Attempting to create duplicate domain (should fail)...'); try { await Tenant.create({ name: 'Duplicate Domain Tenant', slug: 'tenant3', domain: 'test1.example.com', // This should fail is_active: true }); console.log('❌ ERROR: Duplicate domain should have failed!'); } catch (error) { console.log('✅ Correctly caught duplicate domain error:', error.message); } } catch (error) { console.log('❌ Error creating tenant:'); console.log('Error name:', error.name); console.log('Error message:', error.message); console.log('Error details:', error); if (error.errors) { console.log('Validation errors:'); error.errors.forEach(err => { console.log(` - ${err.path}: ${err.message}`); }); } } } debugTenant().then(() => { console.log('Debug complete'); process.exit(0); }).catch(err => { console.error('Debug script failed:', err); process.exit(1); });