Refactor BookRepo methods and improve error handling
- Add JSDoc comments for better maintainability and code clarity in `BookRepo` methods. - Streamline query definitions using variables to improve readability. - Consolidate error handling logic across all methods. - Ensure multilingual support in error messages for consistent user feedback. - Remove redundant error branches and simplify unknown error processing.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {Database, RunResult, SQLiteValue} from 'node-sqlite3-wasm';
|
||||
import { Database, RunResult, SQLiteValue } from 'node-sqlite3-wasm';
|
||||
import System from "../System.js";
|
||||
|
||||
export interface UserInfosQueryResponse extends Record<string, SQLiteValue> {
|
||||
@@ -45,15 +45,39 @@ export interface GuideTourResult extends Record<string, SQLiteValue> {
|
||||
|
||||
export default class UserRepo {
|
||||
|
||||
public static insertUser(uuId: string, firstName: string, lastName: string, username: string, originUsername: string, email: string, originEmail: string, lang: 'fr' | 'en' = 'fr'): string {
|
||||
let result: RunResult;
|
||||
/**
|
||||
* Inserts a new user into the database.
|
||||
* @param uuId - The unique identifier for the user
|
||||
* @param firstName - The user's first name
|
||||
* @param lastName - The user's last name
|
||||
* @param username - The user's username
|
||||
* @param originUsername - The original username from the source platform
|
||||
* @param email - The user's email address
|
||||
* @param originEmail - The original email from the source platform
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns The user's UUID if insertion was successful
|
||||
* @throws Error if the user cannot be registered
|
||||
*/
|
||||
public static insertUser(
|
||||
uuId: string,
|
||||
firstName: string,
|
||||
lastName: string,
|
||||
username: string,
|
||||
originUsername: string,
|
||||
email: string,
|
||||
originEmail: string,
|
||||
lang: 'fr' | 'en' = 'fr'
|
||||
): string {
|
||||
let insertResult: RunResult;
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
const query = `INSERT INTO erit_users (user_id, first_name, last_name, username, email, origin_email,
|
||||
origin_username, plateform, term_accepted,
|
||||
account_verified, reg_date)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
|
||||
const values: (string | null | number)[] = [
|
||||
const query: string = `
|
||||
INSERT INTO erit_users (
|
||||
user_id, first_name, last_name, username, email, origin_email,
|
||||
origin_username, plateform, term_accepted, account_verified, reg_date
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
const params: SQLiteValue[] = [
|
||||
uuId,
|
||||
firstName,
|
||||
lastName,
|
||||
@@ -61,56 +85,113 @@ export default class UserRepo {
|
||||
email,
|
||||
originEmail,
|
||||
originUsername,
|
||||
'desktop', // plateform
|
||||
0, // term_accepted
|
||||
1, // account_verified
|
||||
Date.now() // reg_date (current timestamp)
|
||||
'desktop',
|
||||
0,
|
||||
1,
|
||||
Date.now()
|
||||
];
|
||||
result = db.run(query, values);
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
console.error(`DB Error: ${e.message}`);
|
||||
insertResult = db.run(query, params);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.message}`);
|
||||
throw new Error(lang === 'fr' ? `Impossible d'enregistrer l'utilisateur.` : `Unable to register user.`);
|
||||
} else {
|
||||
console.error("An unknown error occurred.");
|
||||
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
||||
}
|
||||
}
|
||||
if (result.changes > 0) {
|
||||
if (insertResult.changes > 0) {
|
||||
return uuId;
|
||||
} else {
|
||||
throw new Error(lang === 'fr' ? `Une erreur s'est produite lors de l'enregistrement de l'utilisateur.` : `Error registering user.`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches user information from the database.
|
||||
* @param userId - The unique identifier of the user to fetch
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns The user information object
|
||||
* @throws Error if the user is not found or cannot be retrieved
|
||||
*/
|
||||
public static fetchUserInfos(userId: string, lang: 'fr' | 'en' = 'fr'): UserInfosQueryResponse {
|
||||
let result;
|
||||
let userInfo: Record<string, SQLiteValue> | undefined;
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
result = db.get('SELECT first_name, last_name, username, email, plateform, term_accepted, account_verified, 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}`);
|
||||
const query: string = `
|
||||
SELECT first_name, last_name, username, email, plateform, term_accepted,
|
||||
account_verified, author_name, erite_points AS rite_points, user_group
|
||||
FROM erit_users
|
||||
WHERE user_id = ?
|
||||
`;
|
||||
const params: SQLiteValue[] = [userId];
|
||||
userInfo = db.get(query, params);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.message}`);
|
||||
throw new Error(lang === 'fr' ? `Impossible de récupérer les informations utilisateur.` : `Unable to retrieve user information.`);
|
||||
} else {
|
||||
console.error("An unknown error occurred.");
|
||||
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
||||
}
|
||||
}
|
||||
if (!result) {
|
||||
if (!userInfo) {
|
||||
throw new Error(lang === 'fr' ? `Utilisateur non trouvé.` : `User not found.`);
|
||||
}
|
||||
return result as UserInfosQueryResponse;
|
||||
return userInfo as UserInfosQueryResponse;
|
||||
}
|
||||
|
||||
public static updateUserInfos(userId: string, firstName: string, lastName: string, username: string, originUsername: string, email: string, originEmail: string, originalAuthorName: string, authorName: string, lang: 'fr' | 'en' = 'fr'): boolean {
|
||||
/**
|
||||
* Updates user information in the database.
|
||||
* @param userId - The unique identifier of the user to update
|
||||
* @param firstName - The new first name
|
||||
* @param lastName - The new last name
|
||||
* @param username - The new username
|
||||
* @param originUsername - The original username from the source platform
|
||||
* @param email - The new email address
|
||||
* @param originEmail - The original email from the source platform
|
||||
* @param originalAuthorName - The original author name
|
||||
* @param authorName - The new author name
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns True if the update was successful, false otherwise
|
||||
* @throws Error if the update fails
|
||||
*/
|
||||
public static updateUserInfos(
|
||||
userId: string,
|
||||
firstName: string,
|
||||
lastName: string,
|
||||
username: string,
|
||||
originUsername: string,
|
||||
email: string,
|
||||
originEmail: string,
|
||||
originalAuthorName: string,
|
||||
authorName: string,
|
||||
lang: 'fr' | 'en' = 'fr'
|
||||
): boolean {
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
const result: RunResult = db.run('UPDATE `erit_users` SET `first_name`=?, `last_name`=?, `username`=?, email=?,`origin_username`=?, origin_author_name=? ,author_name=? WHERE user_id=? AND `origin_email`=?', [firstName, lastName, username, email, originUsername, originalAuthorName, authorName, userId, originEmail]);
|
||||
return result.changes > 0;
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
console.error(`DB Error: ${e.message}`);
|
||||
const query: string = `
|
||||
UPDATE erit_users
|
||||
SET first_name = ?, last_name = ?, username = ?, email = ?,
|
||||
origin_username = ?, origin_author_name = ?, author_name = ?
|
||||
WHERE user_id = ? AND origin_email = ?
|
||||
`;
|
||||
const params: SQLiteValue[] = [
|
||||
firstName,
|
||||
lastName,
|
||||
username,
|
||||
email,
|
||||
originUsername,
|
||||
originalAuthorName,
|
||||
authorName,
|
||||
userId,
|
||||
originEmail
|
||||
];
|
||||
const updateResult: RunResult = db.run(query, params);
|
||||
return updateResult.changes > 0;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.message}`);
|
||||
throw new Error(lang === 'fr' ? `Impossible de mettre à jour les informations utilisateur.` : `Unable to update user information.`);
|
||||
} else {
|
||||
console.error("An unknown error occurred.");
|
||||
@@ -119,23 +200,36 @@ export default class UserRepo {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches account information for a user.
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param lang - The language for error messages ('fr' or 'en')
|
||||
* @returns The user account information object
|
||||
* @throws Error if the account is not found or cannot be retrieved
|
||||
*/
|
||||
public static fetchAccountInformation(userId: string, lang: 'fr' | 'en' = 'fr'): UserAccountQuery {
|
||||
let result;
|
||||
let accountInfo: Record<string, SQLiteValue> | undefined;
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
result = db.get('SELECT first_name, last_name, username, author_name, email FROM erit_users WHERE user_id=?', [userId]);
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
console.error(`DB Error: ${e.message}`);
|
||||
const query: string = `
|
||||
SELECT first_name, last_name, username, author_name, email
|
||||
FROM erit_users
|
||||
WHERE user_id = ?
|
||||
`;
|
||||
const params: SQLiteValue[] = [userId];
|
||||
accountInfo = db.get(query, params);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.message}`);
|
||||
throw new Error(lang === 'fr' ? `Impossible de récupérer les informations du compte.` : `Unable to retrieve account information.`);
|
||||
} else {
|
||||
console.error("An unknown error occurred.");
|
||||
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
||||
}
|
||||
}
|
||||
if (!result) {
|
||||
if (!accountInfo) {
|
||||
throw new Error(lang === 'fr' ? `Compte non trouvé.` : `Account not found.`);
|
||||
}
|
||||
return result as UserAccountQuery;
|
||||
return accountInfo as UserAccountQuery;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user