Add repository classes for character, location, and user data management

- Implement `CharacterRepo`, `LocationRepo`, and `UserRepo` for managing character, location, and user data with SQLite.
- Add methods for performing CRUD operations on characters, locations, and users, including attributes and metadata handling.
- Integrate error handling with multilingual support (`fr` and `en`).
- Support data encryption and synchronization using the existing database utilities.
This commit is contained in:
natreex
2025-11-17 19:57:36 -05:00
parent a8693973e0
commit 4cd4f68d1a
3 changed files with 659 additions and 0 deletions

View File

@@ -0,0 +1,174 @@
import {Database, RunResult, SQLiteValue} from 'node-sqlite3-wasm';
import System from "../System";
export interface CharacterResult extends Record<string, SQLiteValue> {
character_id: string;
first_name: string;
last_name: string;
title: string;
category: string;
image: string;
role: string;
biography: string;
history: string;
char_meta: string;
}
export interface AttributeResult extends Record<string, SQLiteValue> {
attr_id: string;
attribute_name: string;
attribute_value: string;
attr_meta: string;
}
export interface CompleteCharacterResult extends Record<string, SQLiteValue> {
character_id: string;
first_name: string;
last_name: string;
category: string;
title: string;
role: string;
biography: string;
history: string;
char_meta: string;
attribute_name: string;
attribute_value: string;
attr_meta: string;
}
export default class CharacterRepo {
public static fetchCharacters(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): CharacterResult[] {
let result: CharacterResult[];
try {
const db: Database = System.getDb();
result = db.all('SELECT character_id,`first_name`,`last_name`,`title`,`category`,`image`,`role`,`biography`,`history`,`char_meta` FROM `book_characters` WHERE `book_id`=? AND user_id=?', [bookId, userId]) as CharacterResult[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de récupérer les personnages.` : `Unable to retrieve characters.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
return result;
}
public static addNewCharacter(userId: string, characterId: string, encryptedName: string, encryptedLastName: string, encryptedTitle: string, encryptedCategory: string, encryptedImage: string, encryptedRole: string, encryptedBiography: string, encryptedHistory: string, bookId: string, meta: string, lang: 'fr' | 'en' = 'fr'): string {
let result: RunResult;
try {
const db: Database = System.getDb();
result = db.run('INSERT INTO `book_characters` (character_id, book_id, user_id, first_name, last_name, category, title, image, role, biography, history, char_meta) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)', [characterId, bookId, userId, encryptedName, encryptedLastName, encryptedCategory, encryptedTitle, encryptedImage, encryptedRole, encryptedBiography, encryptedHistory, meta]);
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible d'ajouter le personnage.` : `Unable to add character.`);
} 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) {
return characterId;
} else {
throw new Error(lang === 'fr' ? `Une erreur s'est produite lors de l'ajout du personnage.` : `Error adding character.`);
}
}
static insertAttribute(attributeId: string, characterId: string, userId: string, type: string, name: string, meta: string, lang: 'fr' | 'en' = 'fr'): string {
let result: RunResult;
try {
const db: Database = System.getDb();
result = db.run('INSERT INTO `book_characters_attributes` (attr_id, character_id, user_id, attribute_name, attribute_value,attr_meta) VALUES (?,?,?,?,?,?)', [attributeId, characterId, userId, type, name, meta]);
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible d'ajouter l'attribut.` : `Unable to add attribute.`);
} 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) {
return attributeId;
} else {
throw new Error(lang === 'fr' ? `Une erreur s'est produite lors de l'ajout de l'attribut.` : `Error adding attribute.`);
}
}
static updateCharacter(userId: string, id: number | null, encryptedName: string, encryptedLastName: string, encryptedTitle: string, encryptedCategory: string, encryptedImage: string, encryptedRole: string, encryptedBiography: string, encryptedHistory: string, meta: string, lang: 'fr' | 'en' = 'fr'): boolean {
try {
const db: Database = System.getDb();
const result: RunResult = db.run('UPDATE `book_characters` SET `first_name`=?,`last_name`=?,`title`=?,`category`=?,`image`=?,`role`=?,`biography`=?,`history`=?, char_meta=? WHERE `character_id`=? AND `user_id`=?', [encryptedName, encryptedLastName, encryptedTitle, encryptedCategory, encryptedImage, encryptedRole, encryptedBiography, encryptedHistory, meta, id, userId]);
return result.changes > 0;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de mettre à jour le personnage.` : `Unable to update character.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
static deleteAttribute(userId: string, attributeId: string, lang: 'fr' | 'en' = 'fr'): boolean {
try {
const db: Database = System.getDb();
const result: RunResult = db.run('DELETE FROM `book_characters_attributes` WHERE `attr_id`=? AND `user_id`=?', [attributeId, userId]);
return result.changes > 0;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de supprimer l'attribut.` : `Unable to delete attribute.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
static fetchAttributes(characterId: string, userId: string, lang: 'fr' | 'en' = 'fr'): AttributeResult[] {
let result: AttributeResult[];
try {
const db: Database = System.getDb();
result = db.all('SELECT attr_id, attribute_name, attribute_value, attr_meta FROM `book_characters_attributes` WHERE `character_id`=? AND `user_id`=?', [characterId, userId]) as AttributeResult[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de récupérer les attributs.` : `Unable to retrieve attributes.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
return result;
}
static fetchCompleteCharacters(userId: string, bookId: string, tags: string[], lang: 'fr' | 'en' = 'fr'): CompleteCharacterResult[] {
let result: CompleteCharacterResult[];
try {
const db: Database = System.getDb();
let query: string = 'SELECT charac.`character_id`,`first_name`,`last_name`,`category`,`title`,`role`,`biography`,`history`,`char_meta`,`attribute_name`,`attribute_value`,attr_meta FROM book_characters AS charac LEFT JOIN book_characters_attributes AS attr ON charac.character_id=attr.character_id WHERE charac.user_id=? AND charac.book_id=?';
let values: any[] = [userId, bookId];
if (tags && tags.length > 0) {
const placeholders: string = tags.map((): string => '?').join(',');
query += ` AND charac.character_id IN (${placeholders})`;
values.push(...tags);
}
result = db.all(query, values) as CompleteCharacterResult[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de récupérer les personnages complets.` : `Unable to retrieve complete characters.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
if (result.length === 0) {
throw new Error(lang === 'fr' ? `Aucun personnage complet trouvé.` : `No complete characters found.`);
}
return result;
}
}

View File

@@ -0,0 +1,270 @@
import {Database, RunResult, SQLiteValue} from 'node-sqlite3-wasm';
import System from "../System";
export interface LocationQueryResult extends Record<string, SQLiteValue> {
loc_id: string;
loc_name: string;
loc_meta: string;
element_id: string;
element_name: string;
element_description: string;
element_meta: string;
sub_element_id: string;
sub_elem_name: string;
sub_elem_description: string;
sub_elem_meta: string;
}
export interface LocationElementQueryResult extends Record<string, SQLiteValue> {
sub_element_id: string;
sub_elem_name: string;
sub_elem_description: string;
sub_elem_meta: string;
element_id: string;
element_name: string;
element_description: string;
element_meta: string;
}
export interface LocationByTagResult extends Record<string, SQLiteValue> {
element_name: string;
element_description: string;
element_meta: string;
sub_elem_name: string;
sub_elem_description: string;
sub_elem_meta: string;
}
export default class LocationRepo {
static getLocation(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): LocationQueryResult[] {
let result: LocationQueryResult[];
try {
const db: Database = System.getDb();
const query = 'SELECT loc_id,loc_name, loc_meta, element.element_id AS element_id, element.element_name, element.element_description, element.element_meta, sub_elem.sub_element_id AS sub_element_id,sub_elem.sub_elem_name,sub_elem.sub_elem_description,sub_elem.sub_elem_meta FROM `book_location` AS location LEFT JOIN location_element AS element ON location.loc_id=element.location LEFT JOIN location_sub_element AS sub_elem ON element.element_id=sub_elem.element_id WHERE location.user_id=? AND location.book_id=?';
result = db.all(query, [userId, bookId]) as LocationQueryResult[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de récupérer les emplacements.` : `Unable to retrieve locations.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
return result;
}
static insertLocation(userId: string, locationId: string, bookId: string, encryptedName: string, originalName: string, meta: string, lang: 'fr' | 'en' = 'fr'): string {
let result: RunResult;
try {
const db: Database = System.getDb();
result = db.run('INSERT INTO book_location (loc_id, book_id, user_id, loc_name, loc_original_name, loc_meta) VALUES (?, ?, ?, ?, ?, ?)', [locationId, bookId, userId, encryptedName, originalName, meta]);
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible d'ajouter la section d'emplacement.` : `Unable to add location section.`);
} 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) {
return locationId;
} else {
throw new Error(lang === 'fr' ? `Une erreur s'est produite lors de l'ajout de la section d'emplacement.` : `Error adding location section.`);
}
}
static insertLocationElement(userId: string, elementId: string, locationId: string, encryptedName: string, originalName: string, meta: string, lang: 'fr' | 'en' = 'fr'): string {
let result: RunResult;
try {
const db: Database = System.getDb();
result = db.run('INSERT INTO location_element (element_id, location, user_id, element_name, original_name, element_description, element_meta) VALUES (?,?,?,?,?,?,?)', [elementId, locationId, userId, encryptedName, originalName, '', meta]);
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible d'ajouter l'élément d'emplacement.` : `Unable to add location element.`);
} 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) {
return elementId;
} else {
throw new Error(lang === 'fr' ? `Une erreur s'est produite lors de l'ajout de l'élément d'emplacement.` : `Error adding location element.`);
}
}
static insertLocationSubElement(userId: string, subElementId: string, elementId: string, encryptedName: string, originalName: string, meta: string, lang: 'fr' | 'en' = 'fr'): string {
let result: RunResult;
try {
const db: Database = System.getDb();
result = db.run('INSERT INTO location_sub_element (sub_element_id, element_id, user_id, sub_elem_name, original_name, sub_elem_description, sub_elem_meta) VALUES (?,?,?,?,?,?,?)', [subElementId, elementId, userId, encryptedName, originalName, '', meta]);
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible d'ajouter le sous-élément d'emplacement.` : `Unable to add location sub-element.`);
} 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) {
return subElementId;
} else {
throw new Error(lang === 'fr' ? `Une erreur s'est produite lors de l'ajout du sous-élément d'emplacement.` : `Error adding location sub-element.`);
}
}
static updateLocationSubElement(userId: string, id: string, encryptedName: string, originalName: string, encryptDescription: string, meta: string, lang: 'fr' | 'en' = 'fr'): boolean {
try {
const db: Database = System.getDb();
const result: RunResult = db.run('UPDATE location_sub_element SET sub_elem_name=?, original_name=?, sub_elem_description=?, sub_elem_meta=? WHERE sub_element_id=? AND user_id=?', [encryptedName, originalName, encryptDescription, meta, id, userId]);
return result.changes > 0;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de mettre à jour le sous-élément d'emplacement.` : `Unable to update location sub-element.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
static updateLocationElement(userId: string, id: string, encryptedName: string, originalName: string, encryptedDescription: string, meta: string, lang: 'fr' | 'en' = 'fr'): boolean {
try {
const db: Database = System.getDb();
const result: RunResult = db.run('UPDATE location_element SET element_name=?, original_name=?, element_description=?, element_meta=? WHERE element_id=? AND user_id=?', [encryptedName, originalName, encryptedDescription, meta, id, userId]);
return result.changes > 0;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de mettre à jour l'élément d'emplacement.` : `Unable to update location element.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
static updateLocationSection(userId: string, id: string, encryptedName: string, originalName: string, meta: string, lang: 'fr' | 'en' = 'fr'): boolean {
try {
const db: Database = System.getDb();
const result: RunResult = db.run('UPDATE book_location SET loc_name=?, loc_original_name=?, loc_meta=? WHERE loc_id=? AND user_id=?', [encryptedName, originalName, meta, id, userId]);
return result.changes > 0;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de mettre à jour la section d'emplacement.` : `Unable to update location section.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
static deleteLocationSection(userId: string, locationId: string, lang: 'fr' | 'en' = 'fr'): boolean {
try {
const db: Database = System.getDb();
const result: RunResult = db.run('DELETE FROM book_location WHERE loc_id=? AND user_id=?', [locationId, userId]);
return result.changes > 0;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de supprimer la section d'emplacement.` : `Unable to delete location section.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
static deleteLocationElement(userId: string, elementId: string, lang: 'fr' | 'en' = 'fr'): boolean {
try {
const db: Database = System.getDb();
const result: RunResult = db.run('DELETE FROM location_element WHERE element_id=? AND user_id=?', [elementId, userId]);
return result.changes > 0;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de supprimer l'élément d'emplacement.` : `Unable to delete location element.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
static deleteLocationSubElement(userId: string, subElementId: string, lang: 'fr' | 'en' = 'fr'): boolean {
try {
const db: Database = System.getDb();
const result: RunResult = db.run('DELETE FROM location_sub_element WHERE sub_element_id=? AND user_id=?', [subElementId, userId]);
return result.changes > 0;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de supprimer le sous-élément d'emplacement.` : `Unable to delete location sub-element.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
static fetchLocationTags(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): LocationElementQueryResult[] {
let result: LocationElementQueryResult[];
try {
const db: Database = System.getDb();
const query = 'SELECT se.sub_element_id AS sub_element_id, se.sub_elem_name, se.sub_elem_description, se.sub_elem_meta, el.element_id AS element_id, el.element_name, el.element_description, el.element_meta FROM location_sub_element AS se RIGHT JOIN location_element AS el ON se.element_id = el.element_id LEFT JOIN book_location AS lo ON el.location = lo.loc_id WHERE lo.book_id = ? AND lo.user_id = ?';
result = db.all(query, [bookId, userId]) as LocationElementQueryResult[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de récupérer les tags d'emplacement.` : `Unable to retrieve location tags.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
return result;
}
static fetchLocationsByTags(userId: string, locations: string[], lang: 'fr' | 'en' = 'fr'): LocationByTagResult[] {
if (locations.length === 0) {
throw new Error(lang === 'fr' ? `Aucun tag fourni.` : `No tags provided.`);
}
let result: LocationByTagResult[];
try {
const db: Database = System.getDb();
const locationIds: string = locations.map((): string => '?').join(',');
const query: string = `
SELECT el.element_name,
el.element_description,
el.element_meta,
se.sub_elem_name,
se.sub_elem_description,
se.sub_elem_meta
FROM location_element AS el
LEFT JOIN location_sub_element AS se ON el.element_id = se.element_id
WHERE el.user_id = ?
AND (el.element_id IN (${locationIds}) OR se.sub_element_id IN (${locationIds}))
`;
const values: any[] = [userId, ...locations, ...locations];
result = db.all(query, values) as LocationByTagResult[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de récupérer les emplacements par tags.` : `Unable to retrieve locations by tags.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
if (result.length === 0) {
throw new Error(lang === 'fr' ? `Aucun emplacement trouvé avec ces tags.` : `No locations found with these tags.`);
}
return result;
}
}

View File

@@ -0,0 +1,215 @@
import {Database, RunResult, SQLiteValue} from 'node-sqlite3-wasm';
import System from "../System";
export interface UserInfosQueryResponse extends Record<string, SQLiteValue> {
first_name: string;
last_name: string;
username: string;
email: string;
plateform: string;
term_accepted: number;
account_verified: number;
user_meta: string;
author_name: string;
writing_lang: number,
writing_level: number,
rite_points: number,
user_group: number,
credits_balance: number,
}
export interface CredentialResponse {
valid: boolean;
message?: string;
user?: UserResponse;
}
interface UserResponse {
id: string;
name: string;
last_name: string;
username: string;
email: string;
account_verified: boolean;
}
export interface UserQueryResponse extends Record<string, SQLiteValue> {
user_id: string;
first_name: string;
last_name: string;
username: string;
email: string;
plateform: string;
accountVerified: boolean;
password: string;
}
export interface UserAccountQuery extends Record<string, SQLiteValue> {
first_name: string;
last_name: string;
username: string
author_name: string;
email: string;
user_meta: string;
}
export interface TOTPQuery extends Record<string, SQLiteValue> {
user_id: number;
email: string;
totp_code: string;
totp_meta: string;
}
export interface PasswordResponse extends Record<string, SQLiteValue> {
user_id: string;
password: string;
}
export interface GuideTourResult extends Record<string, SQLiteValue> {
step_tour: string;
}
export interface UserSubscription extends Record<string, SQLiteValue> {
user_id: string;
sub_type: string;
sub_tier: number;
start_date: string;
end_date: string;
status: number;
}
export interface UserAPIKeyResult extends Record<string, SQLiteValue> {
brand: AIBrand,
key: string | null,
actif: number
}
export interface UserAiUsageResult extends Record<string, SQLiteValue> {
token_in: string;
token_out: string;
total_price: number;
}
export interface BasicUserCredentials extends Record<string, SQLiteValue> {
user_id: string,
first_name: string,
last_name: string,
username: string,
email: string,
user_meta: string
}
export default class UserRepo {
public static updatePoints(points: number, userId: string, lang: 'fr' | 'en' = 'fr'): void {
try {
const db: Database = System.getDb();
db.run('UPDATE `erit_users` SET `erite_points`=erite_points+? WHERE `user_id`=?', [points, userId]);
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
throw new Error(lang === 'fr' ? `Impossible de mettre à jour les points.` : `Unable to update points.`);
} else {
console.error("An unknown error occurred.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
public static insertUser(uuId: string, firstName: string, lastName: string, username: string, originUsername: string, email: string, originEmail: string, password: string, meta: string, socialId: string | null, provider: string, lang: 'fr' | 'en' = 'fr'): string {
let result: 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, social_id, password, term_accepted,
account_verified, user_meta)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 1, ?)`;
const values: (string | null | number)[] = [uuId, firstName, lastName, username, email, originEmail, originUsername, provider, socialId, password, meta];
result = db.run(query, values);
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.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) {
return uuId;
} else {
throw new Error(lang === 'fr' ? `Une erreur s'est produite lors de l'enregistrement de l'utilisateur.` : `Error registering user.`);
}
}
public static fetchUserInfos(userId: string, lang: 'fr' | 'en' = 'fr'): UserInfosQueryResponse {
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, writing_lang, writing_level, 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]);
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.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) {
throw new Error(lang === 'fr' ? `Utilisateur non trouvé.` : `User not found.`);
}
return result as UserInfosQueryResponse;
}
public static updateUserInfos(userId: string, firstName: string, lastName: string, username: string, originUsername: string, email: string, originEmail: string, userMeta: 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`=?, user_meta=?, origin_author_name=? ,author_name=? WHERE user_id=? AND `origin_email`=?', [firstName, lastName, username, email, originUsername, userMeta, originalAuthorName, authorName, userId, originEmail]);
return result.changes > 0;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.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.");
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
}
}
}
public static fetchAccountInformation(userId: string, lang: 'fr' | 'en' = 'fr'): UserAccountQuery {
let result;
try {
const db: Database = System.getDb();
result = db.get('SELECT `first_name`, `last_name`, `username`,`author_name`, `email`,`user_meta` FROM `erit_users` WHERE user_id=?', [userId]);
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.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) {
throw new Error(lang === 'fr' ? `Compte non trouvé.` : `Account not found.`);
}
return result as UserAccountQuery;
}
static fetchGuideTour(userId: string, plateforme: string): GuideTourResult[] {
try {
const db: Database = System.getDb();
const result = db.all('SELECT `step_tour` FROM `logs_guide_tour` WHERE user_id=? AND plateforme=?', [userId, plateforme]) as GuideTourResult[];
return result.length > 0 ? result : [];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(e.message);
return [];
} else {
console.error(`Une erreur inconnue est survenue lors de la récupération du guide tour.`);
return [];
}
}
}
}