base con autenticacion, registro, modulo encuestas
This commit is contained in:
24
apps/api/src/database/seeds/admin-role.seed.ts
Normal file
24
apps/api/src/database/seeds/admin-role.seed.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||
import * as schema from '../index';
|
||||
import { roles, } from '../index';
|
||||
|
||||
|
||||
export async function seedAdminRole(db: NodePgDatabase<typeof schema>) {
|
||||
console.log('Seeding admin role...');
|
||||
|
||||
// Insert roles
|
||||
const roleNames = ['superadmin', 'admin', 'autoridad','manager','user','producers','organization'];
|
||||
|
||||
for (const roleName of roleNames) {
|
||||
try {
|
||||
await db.insert(roles).values({
|
||||
name: roleName
|
||||
}).onConflictDoNothing();
|
||||
console.log(`Role '${roleName}' created or already exists`);
|
||||
} catch (error) {
|
||||
console.error(`Error creating role '${roleName}':`, error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('roles seeded successfully');
|
||||
}
|
||||
37
apps/api/src/database/seeds/index.ts
Normal file
37
apps/api/src/database/seeds/index.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { Pool } from 'pg';
|
||||
import { envs } from 'src/common/config/envs';
|
||||
import * as schema from '../index';
|
||||
import { seedAdminRole } from './admin-role.seed';
|
||||
import { seedUserAdmin } from './user-admin.seed';
|
||||
import { seedStates } from './states.seed';
|
||||
import { seedMunicipalities } from './municipalities.seed';
|
||||
import { seedParishes } from './parishes.seed';
|
||||
|
||||
|
||||
async function main() {
|
||||
const pool = new Pool({
|
||||
connectionString: envs.dataBaseUrl,
|
||||
ssl:
|
||||
envs.node_env === 'production' ? { rejectUnauthorized: false } : false,
|
||||
});
|
||||
|
||||
const db = drizzle(pool, { schema });
|
||||
|
||||
try {
|
||||
// Run seeds in order
|
||||
await seedStates(db);
|
||||
await seedMunicipalities(db);
|
||||
await seedParishes(db);
|
||||
await seedAdminRole(db);
|
||||
await seedUserAdmin(db)
|
||||
|
||||
console.log('All seeds completed successfully');
|
||||
} catch (error) {
|
||||
console.error('Error seeding database:', error);
|
||||
} finally {
|
||||
await pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
25
apps/api/src/database/seeds/municipalities.seed.ts
Normal file
25
apps/api/src/database/seeds/municipalities.seed.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||
import * as schema from '../index';
|
||||
import { municipalities } from '../schema/general';
|
||||
|
||||
|
||||
export async function seedMunicipalities(db: NodePgDatabase<typeof schema>) {
|
||||
console.log('Seeding public municipalities...');
|
||||
|
||||
// Insert roles
|
||||
const municipalitiesArray = [{name:'municipio1',stateId:1}, {name:'municipio2',stateId:1}, {name:'municipio3',stateId:2}];
|
||||
|
||||
for (const item of municipalitiesArray) {
|
||||
try {
|
||||
await db.insert(municipalities).values({
|
||||
name: item.name,
|
||||
stateId: item.stateId
|
||||
}).onConflictDoNothing();
|
||||
// console.log(`Municipality '${item}' created or already exists`);
|
||||
} catch (error) {
|
||||
console.error(`Error creating municipality '${item.name}':`, error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('All municipalities seeded successfully');
|
||||
}
|
||||
25
apps/api/src/database/seeds/parishes.seed.ts
Normal file
25
apps/api/src/database/seeds/parishes.seed.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||
import * as schema from '../index';
|
||||
import { parishes } from '../schema/general';
|
||||
|
||||
|
||||
export async function seedParishes(db: NodePgDatabase<typeof schema>) {
|
||||
console.log('Seeding public parishes...');
|
||||
|
||||
// Insert roles
|
||||
const parishesArray = [{name:'parroquia1',municipalityId:1}, {name:'parroquia2',municipalityId:1}, {name:'parroquia3',municipalityId:2}];
|
||||
|
||||
for (const item of parishesArray) {
|
||||
try {
|
||||
await db.insert(parishes).values({
|
||||
name: item.name,
|
||||
municipalityId: item.municipalityId
|
||||
}).onConflictDoNothing();
|
||||
// console.log(`Parish '${item}' created or already exists`);
|
||||
} catch (error) {
|
||||
console.error(`Error creating parish '${item.name}':`, error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('All parishes seeded successfully');
|
||||
}
|
||||
24
apps/api/src/database/seeds/states.seed.ts
Normal file
24
apps/api/src/database/seeds/states.seed.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||
import * as schema from '../index';
|
||||
import { states } from '../schema/general';
|
||||
|
||||
|
||||
export async function seedStates(db: NodePgDatabase<typeof schema>) {
|
||||
console.log('Seeding public state...');
|
||||
|
||||
// Insert roles
|
||||
const statesArray = ['estado1', 'estado2', 'estado3'];
|
||||
|
||||
for (const item of statesArray) {
|
||||
try {
|
||||
await db.insert(states).values({
|
||||
name: item
|
||||
}).onConflictDoNothing();
|
||||
// console.log(`State '${item}' created or already exists`);
|
||||
} catch (error) {
|
||||
console.error(`Error creating state '${item}':`, error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('All states seeded successfully');
|
||||
}
|
||||
39
apps/api/src/database/seeds/user-admin.seed.ts
Normal file
39
apps/api/src/database/seeds/user-admin.seed.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||
import * as schema from '../index';
|
||||
import { users, usersRole } from '../index';
|
||||
|
||||
export async function seedUserAdmin(db: NodePgDatabase<typeof schema>) {
|
||||
|
||||
// Insert admin user
|
||||
try {
|
||||
// Password is already hashed in your SQL, but in a real application you might want to hash it here
|
||||
// const hashedPassword = await hash('your_password', 10);
|
||||
const hashedPassword = '$2b$10$6esl7d/BOINamScuReRoPuYFC8iSJgpk61LHm2X3PCU5hu/St8vHW';
|
||||
|
||||
const [adminUser] = await db.insert(users).values({
|
||||
username: 'superadmin',
|
||||
email: 'admin@zonastart.com',
|
||||
fullname: 'Super Administrador',
|
||||
password: hashedPassword,
|
||||
state: 1,
|
||||
municipality: 1,
|
||||
parish: 1,
|
||||
isTwoFactorEnabled: false,
|
||||
isEmailVerified: true,
|
||||
isActive: true
|
||||
}).returning({ id: users.id }).onConflictDoNothing();
|
||||
|
||||
if (adminUser) {
|
||||
// Assign superadmin role to the user
|
||||
await db.insert(usersRole).values({
|
||||
roleId: 1, // Assuming 'superadmin' has ID 1 based on the insert order
|
||||
userId: adminUser.id
|
||||
}).onConflictDoNothing();
|
||||
console.log('Admin user created and assigned superadmin role');
|
||||
} else {
|
||||
console.log('Admin user already exists, skipping');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error creating admin user:', error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user