133 lines
3.4 KiB
TypeScript
133 lines
3.4 KiB
TypeScript
// backend/prisma/seed.ts
|
|
import { PrismaClient } from '@prisma/client';
|
|
import { config } from 'dotenv';
|
|
import path from 'path';
|
|
|
|
// Load environment variables from the correct path
|
|
const envPath = path.join(__dirname, '..', '.env');
|
|
console.log('Loading .env from:', envPath);
|
|
config({ path: envPath });
|
|
|
|
// Verify DATABASE_URL is loaded
|
|
if (!process.env.DATABASE_URL) {
|
|
console.error('❌ DATABASE_URL is not set in environment');
|
|
console.log('Current directory:', process.cwd());
|
|
console.log('.env path:', envPath);
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('✅ DATABASE_URL found:', process.env.DATABASE_URL.substring(0, 30) + '...');
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
console.log('🌱 Starting seeding...');
|
|
|
|
// Test the connection
|
|
await prisma.$connect();
|
|
console.log('✅ Database connected successfully');
|
|
|
|
// Create organization types
|
|
await prisma.organizationType.upsert({
|
|
where: { name: 'CLINIC' },
|
|
update: {},
|
|
create: { name: 'CLINIC' },
|
|
});
|
|
console.log('✅ Created clinic type');
|
|
|
|
await prisma.organizationType.upsert({
|
|
where: { name: 'LAB' },
|
|
update: {},
|
|
create: { name: 'LAB' },
|
|
});
|
|
console.log('✅ Created lab type');
|
|
|
|
// Create plans
|
|
const plans = [
|
|
{ name: 'trial', maxUsers: 5, price: 0, features: {} },
|
|
{ name: 'Small', maxUsers: 5, price: 150, features: {} },
|
|
{ name: 'Medium', maxUsers: 10, price: 250, features: {} },
|
|
{ name: 'Large', maxUsers: 15, price: 400, features: {} },
|
|
{ name: 'Enterprise', maxUsers: 999999, price: 1000, features: {} },
|
|
];
|
|
|
|
for (const plan of plans) {
|
|
await prisma.plan.upsert({
|
|
where: { name: plan.name },
|
|
update: {},
|
|
create: plan,
|
|
});
|
|
}
|
|
console.log('✅ Created plans');
|
|
|
|
// Minimal permission model (confirmed):
|
|
// - Sidebar tabs use READ/EDIT
|
|
// - EDIT implies READ in app logic
|
|
// - Owners effectively get all permissions
|
|
const features = [
|
|
{
|
|
name: 'Today',
|
|
permissions: ['TAB_TODAY_READ', 'TAB_TODAY_EDIT'],
|
|
},
|
|
{
|
|
name: 'Staff',
|
|
permissions: ['TAB_STAFF_READ', 'TAB_STAFF_EDIT'],
|
|
},
|
|
{
|
|
name: 'Labs / Clinics',
|
|
permissions: ['TAB_LAB_READ', 'TAB_LAB_EDIT'],
|
|
},
|
|
{
|
|
name: 'Patients',
|
|
permissions: ['TAB_PATIENTS_READ', 'TAB_PATIENTS_EDIT'],
|
|
},
|
|
{
|
|
name: 'Appointment',
|
|
permissions: ['TAB_APPOINTMENTS_READ', 'TAB_APPOINTMENTS_EDIT'],
|
|
},
|
|
{
|
|
name: 'Treatment',
|
|
permissions: ['TAB_TREATMENT_READ', 'TAB_TREATMENT_EDIT'],
|
|
},
|
|
{
|
|
name: 'Billing',
|
|
permissions: ['TAB_BILLING_READ', 'TAB_BILLING_EDIT'],
|
|
},
|
|
{
|
|
name: 'Reports',
|
|
permissions: ['TAB_REPORTS_READ', 'TAB_REPORTS_EDIT'],
|
|
},
|
|
];
|
|
|
|
for (const feature of features) {
|
|
const createdFeature = await prisma.feature.upsert({
|
|
where: { name: feature.name },
|
|
update: {},
|
|
create: { name: feature.name },
|
|
});
|
|
|
|
for (const permissionName of feature.permissions) {
|
|
await prisma.permission.upsert({
|
|
where: { name: permissionName },
|
|
update: {},
|
|
create: {
|
|
name: permissionName,
|
|
featureId: createdFeature.id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
console.log('✅ Created features and permissions');
|
|
|
|
console.log('🌱 Seeding completed successfully!');
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error('❌ Seeding failed:', e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|