134 lines
3.0 KiB
TypeScript
134 lines
3.0 KiB
TypeScript
import { db } from '@/lib/db';
|
|
import { users, courts, timeSlots, settings } from '@/lib/db/schema';
|
|
import { hashPassword } from '@/lib/auth';
|
|
import { generateId } from '@/lib/utils';
|
|
|
|
async function setupDatabase() {
|
|
try {
|
|
console.log('Setting up database...');
|
|
|
|
// Create admin user
|
|
const adminEmail = process.env.ADMIN_EMAIL || 'admin@example.com';
|
|
const adminPassword = process.env.ADMIN_PASSWORD || 'admin123';
|
|
|
|
const hashedAdminPassword = await hashPassword(adminPassword);
|
|
const now = new Date();
|
|
|
|
await db
|
|
.insert(users)
|
|
.values({
|
|
id: generateId(),
|
|
email: adminEmail,
|
|
name: 'Admin',
|
|
surname: 'User',
|
|
password: hashedAdminPassword,
|
|
role: 'admin',
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.onConflictDoNothing();
|
|
|
|
// Create default courts
|
|
await db
|
|
.insert(courts)
|
|
.values([
|
|
{
|
|
id: generateId(),
|
|
name: 'Court 1',
|
|
isActive: true,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
{
|
|
id: generateId(),
|
|
name: 'Court 2',
|
|
isActive: true,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
])
|
|
.onConflictDoNothing();
|
|
|
|
// Create default time slots
|
|
// Monday (1) and Tuesday (2): 19:00-23:00
|
|
const mondayTuesdaySlots = [];
|
|
for (let day of [1, 2]) {
|
|
for (let hour = 19; hour < 23; hour++) {
|
|
const hourStr = hour < 10 ? '0' + hour : hour.toString();
|
|
const nextHourStr = hour + 1 < 10 ? '0' + (hour + 1) : (hour + 1).toString();
|
|
mondayTuesdaySlots.push({
|
|
id: generateId(),
|
|
dayOfWeek: day,
|
|
startTime: `${hourStr}:00`,
|
|
endTime: `${nextHourStr}:00`,
|
|
isActive: true,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
}
|
|
}
|
|
|
|
// Sunday (0): 12:00-17:00
|
|
const sundaySlots = [];
|
|
for (let hour = 12; hour < 17; hour++) {
|
|
const hourStr = hour < 10 ? '0' + hour : hour.toString();
|
|
const nextHourStr = hour + 1 < 10 ? '0' + (hour + 1) : (hour + 1).toString();
|
|
sundaySlots.push({
|
|
id: generateId(),
|
|
dayOfWeek: 0,
|
|
startTime: `${hourStr}:00`,
|
|
endTime: `${nextHourStr}:00`,
|
|
isActive: true,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
}
|
|
|
|
await db
|
|
.insert(timeSlots)
|
|
.values([...mondayTuesdaySlots, ...sundaySlots])
|
|
.onConflictDoNothing();
|
|
|
|
// Create default settings
|
|
await db
|
|
.insert(settings)
|
|
.values([
|
|
{
|
|
id: generateId(),
|
|
key: 'booking_window_days',
|
|
value: '7',
|
|
updatedAt: now,
|
|
},
|
|
{
|
|
id: generateId(),
|
|
key: 'max_bookings_per_user',
|
|
value: '3',
|
|
updatedAt: now,
|
|
},
|
|
{
|
|
id: generateId(),
|
|
key: 'booking_cancellation_hours',
|
|
value: '2',
|
|
updatedAt: now,
|
|
},
|
|
])
|
|
.onConflictDoNothing();
|
|
|
|
console.log('Database setup completed successfully!');
|
|
console.log(`Admin user created: ${adminEmail}`);
|
|
console.log(`Admin password: ${adminPassword}`);
|
|
} catch (error) {
|
|
console.error('Database setup error:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Run setup if this file is executed directly
|
|
if (require.main === module) {
|
|
setupDatabase()
|
|
.then(() => process.exit(0))
|
|
.catch(() => process.exit(1));
|
|
}
|
|
|
|
export { setupDatabase };
|