feat(multi-tenant): configurar las 3 empresas oficiales (Telcotronics, XSYSTEM, DOMUS) y selector rapido de entorno
This commit is contained in:
+46
-30
@@ -27,23 +27,37 @@ async function setup() {
|
||||
)
|
||||
`);
|
||||
|
||||
// Ensure demo company exists FIRST (before migrations reference it)
|
||||
const [existing] = await conn.query('SELECT id FROM companies WHERE slug = ?', ['demo']);
|
||||
let companyId;
|
||||
if (existing.length === 0) {
|
||||
await conn.query(
|
||||
'INSERT INTO companies (name, slug, plan, max_users, max_tickets) VALUES (?, ?, ?, ?, ?)',
|
||||
['Demo Company', 'demo', 'enterprise', 100, 10000]
|
||||
);
|
||||
companyId = 1;
|
||||
console.log('+ Empresa demo creada');
|
||||
} else {
|
||||
companyId = existing[0].id;
|
||||
console.log('~ Empresa demo ya existe (id=' + companyId + ')');
|
||||
// Ensure official ecosystem companies exist: Telcotronics, XSYSTEM, DOMUS
|
||||
// Migrate legacy demo / testcorp / nuevaemp if present
|
||||
await conn.query(`UPDATE companies SET name = 'Telcotronics', slug = 'telcotronics', plan = 'enterprise', max_users = 100, max_tickets = 10000 WHERE slug = 'demo' OR id = 1`);
|
||||
await conn.query(`UPDATE companies SET name = 'XSYSTEM', slug = 'xsystem', plan = 'enterprise', max_users = 100, max_tickets = 10000 WHERE slug = 'testcorp' OR id = 2`);
|
||||
await conn.query(`UPDATE companies SET name = 'DOMUS', slug = 'domus', plan = 'enterprise', max_users = 100, max_tickets = 10000 WHERE slug = 'nuevaemp' OR id = 3`);
|
||||
|
||||
const ecosystemCompanies = [
|
||||
{ id: 1, name: 'Telcotronics', slug: 'telcotronics', plan: 'enterprise', max_users: 100, max_tickets: 10000 },
|
||||
{ id: 2, name: 'XSYSTEM', slug: 'xsystem', plan: 'enterprise', max_users: 100, max_tickets: 10000 },
|
||||
{ id: 3, name: 'DOMUS', slug: 'domus', plan: 'enterprise', max_users: 100, max_tickets: 10000 }
|
||||
];
|
||||
|
||||
for (const ec of ecosystemCompanies) {
|
||||
const [found] = await conn.query('SELECT id FROM companies WHERE slug = ? OR id = ?', [ec.slug, ec.id]);
|
||||
if (found.length === 0) {
|
||||
await conn.query(
|
||||
'INSERT INTO companies (id, name, slug, plan, max_users, max_tickets) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[ec.id, ec.name, ec.slug, ec.plan, ec.max_users, ec.max_tickets]
|
||||
);
|
||||
console.log(`+ Empresa ${ec.name} creada (id=${ec.id})`);
|
||||
} else {
|
||||
await conn.query(
|
||||
'UPDATE companies SET name = ?, slug = ?, plan = ?, max_users = ?, max_tickets = ? WHERE id = ?',
|
||||
[ec.name, ec.slug, ec.plan, ec.max_users, ec.max_tickets, found[0].id]
|
||||
);
|
||||
console.log(`~ Empresa ${ec.name} sincronizada (id=${found[0].id})`);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure company_id default value references existing company
|
||||
const defCompanyId = companyId;
|
||||
const companyId = 1;
|
||||
const defCompanyId = 1;
|
||||
|
||||
// Migrate existing tables
|
||||
const migrateColumn = async (table, column, definition) => {
|
||||
@@ -129,21 +143,23 @@ async function setup() {
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
`);
|
||||
|
||||
// Seed departments
|
||||
const [depts] = await conn.query('SELECT COUNT(*) as c FROM departments WHERE company_id = ?', [companyId]);
|
||||
if (depts[0].c === 0) {
|
||||
await conn.query(
|
||||
'INSERT INTO departments (company_id, nombre, descripcion) VALUES ?',
|
||||
[[
|
||||
[companyId, 'Soporte Técnico', 'Problemas técnicos y de software'],
|
||||
[companyId, 'Facturación', 'Problemas de facturación y pagos'],
|
||||
[companyId, 'Ventas', 'Consultas sobre ventas y productos'],
|
||||
[companyId, 'General', 'Consultas generales']
|
||||
]]
|
||||
);
|
||||
console.log('+ Departamentos seed insertados');
|
||||
} else {
|
||||
console.log('~ Departamentos ya existen');
|
||||
// Seed departments for all ecosystem companies
|
||||
for (const ec of ecosystemCompanies) {
|
||||
const [depts] = await conn.query('SELECT COUNT(*) as c FROM departments WHERE company_id = ?', [ec.id]);
|
||||
if (depts[0].c === 0) {
|
||||
await conn.query(
|
||||
'INSERT INTO departments (company_id, nombre, descripcion) VALUES ?',
|
||||
[[
|
||||
[ec.id, 'Soporte Técnico', 'Problemas técnicos y de software'],
|
||||
[ec.id, 'Facturación', 'Problemas de facturación y pagos'],
|
||||
[ec.id, 'Ventas', 'Consultas sobre ventas y productos'],
|
||||
[ec.id, 'General', 'Consultas generales']
|
||||
]]
|
||||
);
|
||||
console.log(`+ Departamentos seed insertados para ${ec.name}`);
|
||||
} else {
|
||||
console.log(`~ Departamentos ya existen para ${ec.name}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Seed users
|
||||
|
||||
Reference in New Issue
Block a user