79 lines
2.0 KiB
TypeScript
79 lines
2.0 KiB
TypeScript
import { db } from '../lib/db';
|
|
import { settings, metrics } from '../lib/db/schema';
|
|
import { randomUUID } from 'crypto';
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
async function initializeAdminData() {
|
|
try {
|
|
console.log('Initializing admin dashboard data...');
|
|
|
|
const now = new Date();
|
|
|
|
// Initialize default settings
|
|
console.log('Setting up default settings...');
|
|
|
|
const defaultSettings = [
|
|
{
|
|
id: randomUUID(),
|
|
key: 'booking_window_days',
|
|
value: '14',
|
|
updatedAt: now,
|
|
},
|
|
{
|
|
id: randomUUID(),
|
|
key: 'max_booking_duration_hours',
|
|
value: '2',
|
|
updatedAt: now,
|
|
},
|
|
{
|
|
id: randomUUID(),
|
|
key: 'max_bookings_per_user_per_hour_per_day',
|
|
value: '1',
|
|
updatedAt: now,
|
|
},
|
|
];
|
|
|
|
// Insert settings if they don't exist
|
|
for (const setting of defaultSettings) {
|
|
const existingSetting = await db.select().from(settings).where(eq(settings.key, setting.key)).limit(1);
|
|
|
|
if (existingSetting.length === 0) {
|
|
await db.insert(settings).values(setting);
|
|
console.log(`✓ Setting '${setting.key}' initialized with value '${setting.value}'`);
|
|
} else {
|
|
console.log(`- Setting '${setting.key}' already exists`);
|
|
}
|
|
}
|
|
|
|
// Initialize current month's metrics
|
|
console.log('Initializing monthly metrics...');
|
|
|
|
const currentMonth = now.toISOString().substring(0, 7); // "2025-09"
|
|
|
|
const existingMetric = await db.select().from(metrics).where(eq(metrics.period, currentMonth)).limit(1);
|
|
|
|
if (existingMetric.length === 0) {
|
|
const monthlyMetric = {
|
|
id: randomUUID(),
|
|
metricType: 'monthly_bookings',
|
|
period: currentMonth,
|
|
value: 0,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
};
|
|
|
|
await db.insert(metrics).values(monthlyMetric);
|
|
console.log(`✓ Monthly metrics initialized for ${currentMonth}`);
|
|
} else {
|
|
console.log(`- Monthly metrics for ${currentMonth} already exist`);
|
|
}
|
|
|
|
console.log('Admin dashboard data initialization complete!');
|
|
} catch (error) {
|
|
console.error('Error initializing admin data:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
initializeAdminData();
|