Add user data synchronization and database IPC handlers
- Introduce `db:user:sync` to synchronize user data with the local database during initialization. - Add `db:user:info` and `db:user:update` IPC handlers for fetching and updating user information. - Update `User` model and repository to support extended user fields and improved structure. - Dynamically import user models in main process to avoid circular dependencies. - Refactor database initialization in the app to include user sync logic with error handling.
This commit is contained in:
@@ -8,6 +8,7 @@ import { getDatabaseService } from './database/database.service.js';
|
||||
|
||||
// Import IPC handlers
|
||||
import './ipc/book.ipc.js';
|
||||
import './ipc/user.ipc.js';
|
||||
|
||||
// Fix pour __dirname en ES modules
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
@@ -169,6 +170,52 @@ ipcMain.on('logout', () => {
|
||||
createLoginWindow();
|
||||
});
|
||||
|
||||
// ========== USER SYNC (PRE-AUTHENTICATION) ==========
|
||||
|
||||
interface SyncUserData {
|
||||
userId: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
username: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
ipcMain.handle('db:user:sync', async (_event, data: SyncUserData): Promise<boolean> => {
|
||||
try {
|
||||
// Import User models dynamically to avoid circular dependencies
|
||||
const { default: User } = await import('./database/models/User.js');
|
||||
const { default: UserRepo } = await import('./database/repositories/user.repository.js');
|
||||
|
||||
const lang: 'fr' | 'en' = 'fr';
|
||||
|
||||
// Check if user already exists in local DB
|
||||
try {
|
||||
UserRepo.fetchUserInfos(data.userId, lang);
|
||||
// User exists, no need to sync
|
||||
console.log(`[DB] User ${data.userId} already exists in local DB, skipping sync`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
// User doesn't exist, create it
|
||||
console.log(`[DB] User ${data.userId} not found, creating in local DB`);
|
||||
|
||||
await User.addUser(
|
||||
data.userId,
|
||||
data.firstName,
|
||||
data.lastName,
|
||||
data.username,
|
||||
data.email,
|
||||
'', // Password not needed for local DB
|
||||
lang
|
||||
);
|
||||
console.log(`[DB] User ${data.userId} synced successfully`);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[DB] Failed to sync user:', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
// ========== DATABASE IPC HANDLERS ==========
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user