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:
@@ -15,6 +15,26 @@ export interface GuideTour {
|
||||
[key: string]: boolean;
|
||||
}
|
||||
|
||||
interface BookSummary {
|
||||
bookId: string;
|
||||
title: string;
|
||||
subTitle?: string;
|
||||
}
|
||||
|
||||
export interface UserInfoResponse {
|
||||
id: string;
|
||||
name: string;
|
||||
lastName: string;
|
||||
username: string;
|
||||
email: string;
|
||||
accountVerified: boolean;
|
||||
authorName: string;
|
||||
groupId: number;
|
||||
termsAccepted: boolean;
|
||||
guideTour: any[];
|
||||
books: BookSummary[];
|
||||
}
|
||||
|
||||
export default class User{
|
||||
|
||||
private readonly id:string;
|
||||
@@ -52,7 +72,7 @@ export default class User{
|
||||
this.termsAccepted = data.term_accepted === 1;
|
||||
}
|
||||
|
||||
public static async returnUserInfos(userId: string) {
|
||||
public static async returnUserInfos(userId: string):Promise<UserInfoResponse> {
|
||||
const user: User = new User(userId);
|
||||
await user.getUserInfos();
|
||||
const books: BookProps[] = await Book.getBooks(userId);
|
||||
@@ -68,7 +88,7 @@ export default class User{
|
||||
groupId: user.getGroupId(),
|
||||
termsAccepted: user.isTermsAccepted(),
|
||||
guideTour: guideTour,
|
||||
books: books.map((book: BookProps) => {
|
||||
books: books.map((book: BookProps):BookSummary => {
|
||||
return {
|
||||
bookId: book.id,
|
||||
title: book.title,
|
||||
|
||||
@@ -15,7 +15,6 @@ export interface UserInfosQueryResponse extends Record<string, SQLiteValue> {
|
||||
writing_level: number,
|
||||
rite_points: number,
|
||||
user_group: number,
|
||||
credits_balance: number,
|
||||
}
|
||||
|
||||
export interface CredentialResponse {
|
||||
@@ -53,10 +52,23 @@ export default class UserRepo {
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
const query = `INSERT INTO erit_users (user_id, first_name, last_name, username, email, origin_email,
|
||||
origin_username, term_accepted,
|
||||
account_verified)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, 0, 1)`;
|
||||
const values: (string | null | number)[] = [uuId, firstName, lastName, username, email, originEmail, originUsername];
|
||||
origin_username, plateform, term_accepted,
|
||||
account_verified, user_meta, reg_date)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
|
||||
const values: (string | null | number)[] = [
|
||||
uuId,
|
||||
firstName,
|
||||
lastName,
|
||||
username,
|
||||
email,
|
||||
originEmail,
|
||||
originUsername,
|
||||
'desktop', // plateform
|
||||
0, // term_accepted
|
||||
1, // account_verified
|
||||
'{}', // user_meta (JSON empty object)
|
||||
Date.now() // reg_date (current timestamp)
|
||||
];
|
||||
result = db.run(query, values);
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
@@ -78,7 +90,7 @@ export default class UserRepo {
|
||||
let result;
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
result = db.get('SELECT `first_name`, `last_name`, `username`, `email`, `plateform`, `term_accepted`, `account_verified`, user_meta, author_name, erite_points AS rite_points, user_group, credits_balance FROM `erit_users` AS users LEFT JOIN user_preferences AS preference ON users.user_id=preference.user_id WHERE users.user_id=?', [userId]);
|
||||
result = db.get('SELECT `first_name`, `last_name`, `username`, `email`, `plateform`, `term_accepted`, `account_verified`, user_meta, author_name, erite_points AS rite_points, user_group FROM `erit_users` WHERE user_id=?', [userId]);
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
console.error(`DB Error: ${e.message}`);
|
||||
|
||||
Reference in New Issue
Block a user