139 lines
3.5 KiB
TypeScript
139 lines
3.5 KiB
TypeScript
import { PrismaClient as CentralPrismaClient } from '../node_modules/.prisma/central';
|
|
import * as bcrypt from 'bcrypt';
|
|
import { Knex, knex } from 'knex';
|
|
|
|
// Central database client
|
|
const centralPrisma = new CentralPrismaClient();
|
|
|
|
async function createTenantUser() {
|
|
const tenantSlug = 'tenant1';
|
|
const email = 'user@example.com';
|
|
const password = 'user123';
|
|
const firstName = 'Test';
|
|
const lastName = 'User';
|
|
|
|
try {
|
|
// Get tenant database connection info
|
|
const tenant = await centralPrisma.tenant.findFirst({
|
|
where: { slug: tenantSlug },
|
|
});
|
|
|
|
if (!tenant) {
|
|
console.log(`Tenant ${tenantSlug} not found. Creating tenant...`);
|
|
|
|
// Create tenant in central database
|
|
const newTenant = await centralPrisma.tenant.create({
|
|
data: {
|
|
name: 'Default Tenant',
|
|
slug: tenantSlug,
|
|
dbHost: 'db',
|
|
dbPort: 3306,
|
|
dbName: 'platform',
|
|
dbUsername: 'platform',
|
|
dbPassword: 'platform',
|
|
status: 'active',
|
|
},
|
|
});
|
|
|
|
console.log('Tenant created:', newTenant.slug);
|
|
} else {
|
|
console.log('Tenant found:', tenant.slug);
|
|
}
|
|
|
|
const tenantInfo = tenant || {
|
|
dbHost: 'db',
|
|
dbPort: 3306,
|
|
dbName: 'platform',
|
|
dbUsername: 'platform',
|
|
dbPassword: 'platform',
|
|
};
|
|
|
|
// Connect to tenant database (using root for now since tenant password is encrypted)
|
|
const tenantDb: Knex = knex({
|
|
client: 'mysql2',
|
|
connection: {
|
|
host: tenantInfo.dbHost,
|
|
port: tenantInfo.dbPort,
|
|
database: tenantInfo.dbName,
|
|
user: 'root',
|
|
password: 'asjdnfqTash37faggT',
|
|
},
|
|
});
|
|
|
|
// Check if user already exists
|
|
const existingUser = await tenantDb('users')
|
|
.where({ email })
|
|
.first();
|
|
|
|
if (existingUser) {
|
|
console.log(`User ${email} already exists in tenant ${tenantSlug}`);
|
|
await tenantDb.destroy();
|
|
return;
|
|
}
|
|
|
|
// Hash password
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
// Create user
|
|
await tenantDb('users').insert({
|
|
email,
|
|
password: hashedPassword,
|
|
firstName,
|
|
lastName,
|
|
isActive: true,
|
|
created_at: new Date(),
|
|
updated_at: new Date(),
|
|
});
|
|
|
|
console.log(`\nUser created successfully in tenant ${tenantSlug}!`);
|
|
console.log('Email:', email);
|
|
console.log('Password:', password);
|
|
|
|
// Create admin role if it doesn't exist
|
|
let adminRole = await tenantDb('roles')
|
|
.where({ name: 'admin' })
|
|
.first();
|
|
|
|
if (!adminRole) {
|
|
await tenantDb('roles').insert({
|
|
name: 'admin',
|
|
guardName: 'api',
|
|
description: 'Administrator role with full access',
|
|
created_at: new Date(),
|
|
updated_at: new Date(),
|
|
});
|
|
|
|
adminRole = await tenantDb('roles')
|
|
.where({ name: 'admin' })
|
|
.first();
|
|
|
|
console.log('Admin role created');
|
|
}
|
|
|
|
// Get the created user
|
|
const user = await tenantDb('users')
|
|
.where({ email })
|
|
.first();
|
|
|
|
// Assign admin role to user
|
|
if (adminRole && user) {
|
|
await tenantDb('user_roles').insert({
|
|
userId: user.id,
|
|
roleId: adminRole.id,
|
|
created_at: new Date(),
|
|
updated_at: new Date(),
|
|
});
|
|
|
|
console.log('Admin role assigned to user');
|
|
}
|
|
|
|
await tenantDb.destroy();
|
|
} catch (error) {
|
|
console.error('Error creating tenant user:', error);
|
|
} finally {
|
|
await centralPrisma.$disconnect();
|
|
}
|
|
}
|
|
|
|
createTenantUser();
|