"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); class DataService { constructor() { this.dataDir = path_1.default.join(process.cwd(), 'data'); this.dataFile = path_1.default.join(this.dataDir, 'vip-coordinator.json'); // Ensure data directory exists if (!fs_1.default.existsSync(this.dataDir)) { fs_1.default.mkdirSync(this.dataDir, { recursive: true }); } this.data = this.loadData(); } loadData() { try { if (fs_1.default.existsSync(this.dataFile)) { const fileContent = fs_1.default.readFileSync(this.dataFile, 'utf8'); const loadedData = JSON.parse(fileContent); console.log(`✅ Loaded data from ${this.dataFile}`); console.log(` - VIPs: ${loadedData.vips?.length || 0}`); console.log(` - Drivers: ${loadedData.drivers?.length || 0}`); console.log(` - Users: ${loadedData.users?.length || 0}`); console.log(` - Schedules: ${Object.keys(loadedData.schedules || {}).length} VIPs with schedules`); // Ensure users array exists for backward compatibility if (!loadedData.users) { loadedData.users = []; } return loadedData; } } catch (error) { console.error('Error loading data file:', error); } // Return default empty data structure console.log('📝 Starting with empty data store'); return { vips: [], drivers: [], schedules: {}, users: [], adminSettings: { apiKeys: { aviationStackKey: process.env.AVIATIONSTACK_API_KEY || '', googleMapsKey: '', twilioKey: '' }, systemSettings: { defaultPickupLocation: '', defaultDropoffLocation: '', timeZone: 'America/New_York', notificationsEnabled: false } } }; } saveData() { try { const dataToSave = JSON.stringify(this.data, null, 2); fs_1.default.writeFileSync(this.dataFile, dataToSave, 'utf8'); console.log(`💾 Data saved to ${this.dataFile}`); } catch (error) { console.error('Error saving data file:', error); } } // VIP operations getVips() { return this.data.vips; } addVip(vip) { this.data.vips.push(vip); this.saveData(); return vip; } updateVip(id, updatedVip) { const index = this.data.vips.findIndex(vip => vip.id === id); if (index !== -1) { this.data.vips[index] = updatedVip; this.saveData(); return this.data.vips[index]; } return null; } deleteVip(id) { const index = this.data.vips.findIndex(vip => vip.id === id); if (index !== -1) { const deletedVip = this.data.vips.splice(index, 1)[0]; // Also delete the VIP's schedule delete this.data.schedules[id]; this.saveData(); return deletedVip; } return null; } // Driver operations getDrivers() { return this.data.drivers; } addDriver(driver) { this.data.drivers.push(driver); this.saveData(); return driver; } updateDriver(id, updatedDriver) { const index = this.data.drivers.findIndex(driver => driver.id === id); if (index !== -1) { this.data.drivers[index] = updatedDriver; this.saveData(); return this.data.drivers[index]; } return null; } deleteDriver(id) { const index = this.data.drivers.findIndex(driver => driver.id === id); if (index !== -1) { const deletedDriver = this.data.drivers.splice(index, 1)[0]; this.saveData(); return deletedDriver; } return null; } // Schedule operations getSchedule(vipId) { return this.data.schedules[vipId] || []; } addScheduleEvent(vipId, event) { if (!this.data.schedules[vipId]) { this.data.schedules[vipId] = []; } this.data.schedules[vipId].push(event); this.saveData(); return event; } updateScheduleEvent(vipId, eventId, updatedEvent) { if (!this.data.schedules[vipId]) { return null; } const index = this.data.schedules[vipId].findIndex(event => event.id === eventId); if (index !== -1) { this.data.schedules[vipId][index] = updatedEvent; this.saveData(); return this.data.schedules[vipId][index]; } return null; } deleteScheduleEvent(vipId, eventId) { if (!this.data.schedules[vipId]) { return null; } const index = this.data.schedules[vipId].findIndex(event => event.id === eventId); if (index !== -1) { const deletedEvent = this.data.schedules[vipId].splice(index, 1)[0]; this.saveData(); return deletedEvent; } return null; } getAllSchedules() { return this.data.schedules; } // Admin settings operations getAdminSettings() { return this.data.adminSettings; } updateAdminSettings(settings) { this.data.adminSettings = { ...this.data.adminSettings, ...settings }; this.saveData(); } // Backup and restore operations createBackup() { const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const backupFile = path_1.default.join(this.dataDir, `backup-${timestamp}.json`); try { fs_1.default.copyFileSync(this.dataFile, backupFile); console.log(`📦 Backup created: ${backupFile}`); return backupFile; } catch (error) { console.error('Error creating backup:', error); throw error; } } // User operations getUsers() { return this.data.users; } getUserByEmail(email) { return this.data.users.find(user => user.email === email) || null; } getUserById(id) { return this.data.users.find(user => user.id === id) || null; } addUser(user) { // Add timestamps const userWithTimestamps = { ...user, created_at: new Date().toISOString(), last_sign_in_at: new Date().toISOString() }; this.data.users.push(userWithTimestamps); this.saveData(); console.log(`👤 Added user: ${user.name} (${user.email}) as ${user.role}`); return userWithTimestamps; } updateUser(email, updatedUser) { const index = this.data.users.findIndex(user => user.email === email); if (index !== -1) { this.data.users[index] = { ...this.data.users[index], ...updatedUser }; this.saveData(); console.log(`👤 Updated user: ${this.data.users[index].name} (${email})`); return this.data.users[index]; } return null; } updateUserRole(email, role) { const index = this.data.users.findIndex(user => user.email === email); if (index !== -1) { this.data.users[index].role = role; this.saveData(); console.log(`👤 Updated user role: ${this.data.users[index].name} (${email}) -> ${role}`); return this.data.users[index]; } return null; } updateUserLastSignIn(email) { const index = this.data.users.findIndex(user => user.email === email); if (index !== -1) { this.data.users[index].last_sign_in_at = new Date().toISOString(); this.saveData(); return this.data.users[index]; } return null; } deleteUser(email) { const index = this.data.users.findIndex(user => user.email === email); if (index !== -1) { const deletedUser = this.data.users.splice(index, 1)[0]; this.saveData(); console.log(`👤 Deleted user: ${deletedUser.name} (${email})`); return deletedUser; } return null; } getUserCount() { return this.data.users.length; } getDataStats() { return { vips: this.data.vips.length, drivers: this.data.drivers.length, users: this.data.users.length, scheduledEvents: Object.values(this.data.schedules).reduce((total, events) => total + events.length, 0), vipsWithSchedules: Object.keys(this.data.schedules).length, dataFile: this.dataFile, lastModified: fs_1.default.existsSync(this.dataFile) ? fs_1.default.statSync(this.dataFile).mtime : null }; } } exports.default = new DataService(); //# sourceMappingURL=dataService.js.map