Add enable/disable management for book tools (characters, worlds, and locations)
- Introduced toggling functionality for managing `characters`, `worlds`, and `locations` tool availability per book. - Updated `CharacterComponent`, `WorldSetting`, and `LocationComponent` with toggle switches for tool enablement. - Added `book_tools` database table and related schema migration for storing tool settings. - Extended API calls, models, and IPC handlers to support tool enablement states. - Localized new strings for English with supporting descriptions and messages. - Adjusted conditional rendering logic across components to respect tool enablement.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import System from '../System.js';
|
||||
import { getUserEncryptionKey } from '../keyManager.js';
|
||||
import BookRepo, { BookQuery, EritBooksTable } from "../repositories/book.repository.js";
|
||||
import BookRepo, { BookQuery, BookToolsTable, BookToolsSettings, EritBooksTable } from "../repositories/book.repository.js";
|
||||
import { BookActSummariesTable } from "../repositories/act.repository.js";
|
||||
import { BookAIGuideLineTable, BookGuideLineTable } from "../repositories/guideline.repository.js";
|
||||
import ChapterRepo, {
|
||||
@@ -34,6 +34,12 @@ import { SyncedAIGuideLine, SyncedGuideLine } from "./GuideLine.js";
|
||||
import Cover from "./Cover.js";
|
||||
import UserRepo from "../repositories/user.repository.js";
|
||||
|
||||
export interface SyncedBookTools {
|
||||
charactersEnabled: boolean;
|
||||
worldsEnabled: boolean;
|
||||
locationsEnabled: boolean;
|
||||
}
|
||||
|
||||
export interface BookProps {
|
||||
id: string;
|
||||
type: string;
|
||||
@@ -47,6 +53,7 @@ export interface BookProps {
|
||||
wordCount?: number;
|
||||
coverImage?: string;
|
||||
bookMeta?: string;
|
||||
tools?: BookToolsSettings;
|
||||
}
|
||||
|
||||
export interface CompleteBook {
|
||||
@@ -67,6 +74,7 @@ export interface CompleteBook {
|
||||
worldElements: BookWorldElementsTable[];
|
||||
locationElements: LocationElementTable[];
|
||||
locationSubElements: LocationSubElementTable[];
|
||||
bookTools: BookToolsTable[];
|
||||
}
|
||||
|
||||
export interface SyncedBook {
|
||||
@@ -85,6 +93,7 @@ export interface SyncedBook {
|
||||
actSummaries: SyncedActSummary[];
|
||||
guideLine: SyncedGuideLine | null;
|
||||
aiGuideLine: SyncedAIGuideLine | null;
|
||||
bookTools: SyncedBookTools | null;
|
||||
}
|
||||
|
||||
export interface BookSyncCompare {
|
||||
@@ -105,6 +114,7 @@ export interface BookSyncCompare {
|
||||
actSummaries: string[];
|
||||
guideLine: boolean;
|
||||
aiGuideLine: boolean;
|
||||
bookTools: boolean;
|
||||
}
|
||||
|
||||
export interface CompleteBookData {
|
||||
@@ -242,6 +252,7 @@ export default class Book {
|
||||
public static async getBook(userId: string, bookId: string, lang: 'fr' | 'en'): Promise<BookProps> {
|
||||
const book: Book = new Book(bookId);
|
||||
book.getBookInfos(userId);
|
||||
const bookTools: BookToolsTable | null = BookRepo.fetchBookTools(userId, bookId, lang);
|
||||
return {
|
||||
id: book.getId(),
|
||||
type: book.getType(),
|
||||
@@ -253,7 +264,12 @@ export default class Book {
|
||||
desiredReleaseDate: book.getDesiredReleaseDate(),
|
||||
desiredWordCount: book.getDesiredWordCount(),
|
||||
wordCount: book.getWordCount(),
|
||||
coverImage: book.getCover()
|
||||
coverImage: book.getCover(),
|
||||
tools: {
|
||||
characters: bookTools ? bookTools.characters_enabled === 1 : false,
|
||||
worlds: bookTools ? bookTools.worlds_enabled === 1 : false,
|
||||
locations: bookTools ? bookTools.locations_enabled === 1 : false
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -290,6 +306,11 @@ export default class Book {
|
||||
return BookRepo.deleteBook(userId, bookId, lang);
|
||||
}
|
||||
|
||||
public static updateBookToolSetting(userId: string, bookId: string, toolName: 'characters' | 'worlds' | 'locations', enabled: boolean, lang: 'fr' | 'en' = 'fr'): boolean {
|
||||
const columnName: 'characters_enabled' | 'worlds_enabled' | 'locations_enabled' = `${toolName}_enabled` as 'characters_enabled' | 'worlds_enabled' | 'locations_enabled';
|
||||
return BookRepo.updateBookToolSetting(userId, bookId, columnName, enabled, lang);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the book ID.
|
||||
* @returns The book's unique identifier
|
||||
|
||||
@@ -3,6 +3,7 @@ import CharacterRepo, {
|
||||
CharacterResult,
|
||||
CompleteCharacterResult
|
||||
} from "../repositories/character.repository.js";
|
||||
import BookRepo, {BookToolsTable} from "../repositories/book.repository.js";
|
||||
import System from "../System.js";
|
||||
import {getUserEncryptionKey} from "../keyManager.js";
|
||||
|
||||
@@ -41,6 +42,11 @@ export interface CharacterProps {
|
||||
history: string;
|
||||
}
|
||||
|
||||
export interface CharacterListResponse {
|
||||
characters: CharacterProps[];
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export interface CompleteCharacterProps {
|
||||
id?: string;
|
||||
name: string;
|
||||
@@ -87,11 +93,15 @@ export default class Character {
|
||||
* @param lang - The language code for localization (defaults to 'fr')
|
||||
* @returns An array of decrypted character properties
|
||||
*/
|
||||
public static getCharacterList(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): CharacterProps[] {
|
||||
public static getCharacterList(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): CharacterListResponse {
|
||||
const bookTools: BookToolsTable | null = BookRepo.fetchBookTools(userId, bookId, lang);
|
||||
const enabled: boolean = bookTools ? bookTools.characters_enabled === 1 : false;
|
||||
|
||||
const userEncryptionKey: string = getUserEncryptionKey(userId);
|
||||
const encryptedCharacters: CharacterResult[] = CharacterRepo.fetchCharacters(userId, bookId, lang);
|
||||
if (!encryptedCharacters) return [];
|
||||
if (encryptedCharacters.length === 0) return [];
|
||||
if (!encryptedCharacters || encryptedCharacters.length === 0) {
|
||||
return { characters: [], enabled };
|
||||
}
|
||||
const decryptedCharacterList: CharacterProps[] = [];
|
||||
for (const encryptedCharacter of encryptedCharacters) {
|
||||
decryptedCharacterList.push({
|
||||
@@ -106,7 +116,7 @@ export default class Character {
|
||||
history: encryptedCharacter.history ? System.decryptDataWithUserKey(encryptedCharacter.history, userEncryptionKey) : '',
|
||||
})
|
||||
}
|
||||
return decryptedCharacterList;
|
||||
return { characters: decryptedCharacterList, enabled };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -358,4 +368,5 @@ export default class Character {
|
||||
}).join('\n\n');
|
||||
return formattedCharactersDescription;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {getUserEncryptionKey} from "../keyManager.js";
|
||||
import System from "../System.js";
|
||||
import {CompleteBook} from "./Book.js";
|
||||
import BookRepo, {EritBooksTable} from "../repositories/book.repository.js";
|
||||
import BookRepo, {EritBooksTable, BookToolsTable} from "../repositories/book.repository.js";
|
||||
import ChapterRepo, {
|
||||
BookChapterInfosTable,
|
||||
BookChaptersTable
|
||||
@@ -192,9 +192,14 @@ export default class Download {
|
||||
});
|
||||
if (!guidelinesInserted) return false;
|
||||
|
||||
return data.issues.every((issue: BookIssuesTable): boolean => {
|
||||
const issuesInserted: boolean = data.issues.every((issue: BookIssuesTable): boolean => {
|
||||
const encryptedIssueName: string = System.encryptDataWithUserKey(issue.name, userEncryptionKey);
|
||||
return IssueRepository.insertSyncIssue(issue.issue_id, userId, issue.book_id, encryptedIssueName, issue.hashed_issue_name, issue.last_update, lang);
|
||||
});
|
||||
if (!issuesInserted) return false;
|
||||
|
||||
return data.bookTools.every((bookTool: BookToolsTable): boolean => {
|
||||
return BookRepo.insertSyncBookTools(bookTool.book_id, userId, bookTool.characters_enabled, bookTool.worlds_enabled, bookTool.locations_enabled, lang);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import LocationRepo, {
|
||||
} from "../repositories/location.repository.js";
|
||||
import System from "../System.js";
|
||||
import {getUserEncryptionKey} from "../keyManager.js";
|
||||
import BookRepo, {BookToolsTable} from "../repositories/book.repository.js";
|
||||
|
||||
export interface SubElement {
|
||||
id: string;
|
||||
@@ -25,6 +26,11 @@ export interface LocationProps {
|
||||
elements: Element[];
|
||||
}
|
||||
|
||||
export interface LocationListResponse {
|
||||
locations: LocationProps[];
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export interface SyncedLocation {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -51,11 +57,16 @@ export default class Location {
|
||||
* @param userId - The user's unique identifier.
|
||||
* @param bookId - The book's unique identifier.
|
||||
* @param lang - The language for error messages ('fr' or 'en'). Defaults to 'fr'.
|
||||
* @returns An array of location properties with their elements and sub-elements.
|
||||
* @returns LocationListResponse containing an array of locations and enabled flag.
|
||||
*/
|
||||
static getAllLocations(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): LocationProps[] {
|
||||
static getAllLocations(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): LocationListResponse {
|
||||
const bookTools: BookToolsTable | null = BookRepo.fetchBookTools(userId, bookId, lang);
|
||||
const enabled: boolean = bookTools ? bookTools.locations_enabled === 1 : false;
|
||||
|
||||
const locationRecords: LocationQueryResult[] = LocationRepo.getLocation(userId, bookId, lang);
|
||||
if (!locationRecords || locationRecords.length === 0) return [];
|
||||
if (!locationRecords || locationRecords.length === 0) {
|
||||
return { locations: [], enabled };
|
||||
}
|
||||
const userKey: string = getUserEncryptionKey(userId);
|
||||
|
||||
const locationArray: LocationProps[] = [];
|
||||
@@ -104,7 +115,7 @@ export default class Location {
|
||||
}
|
||||
}
|
||||
}
|
||||
return locationArray;
|
||||
return { locations: locationArray, enabled };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -325,4 +336,5 @@ export default class Location {
|
||||
return descriptionFields.join('\n');
|
||||
}).join('\n\n');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { getUserEncryptionKey } from "../keyManager.js";
|
||||
import System from "../System.js";
|
||||
import { BookSyncCompare, CompleteBook, SyncedBook } from "./Book.js";
|
||||
import BookRepo, { EritBooksTable, SyncedBookResult } from "../repositories/book.repository.js";
|
||||
import { BookSyncCompare, CompleteBook, SyncedBook, SyncedBookTools } from "./Book.js";
|
||||
import BookRepo, { EritBooksTable, SyncedBookResult, BookToolsTable } from "../repositories/book.repository.js";
|
||||
import ChapterRepo, {
|
||||
BookChapterInfosTable,
|
||||
BookChaptersTable,
|
||||
@@ -350,6 +350,9 @@ export default class Sync {
|
||||
});
|
||||
}
|
||||
|
||||
const bookToolsResult: BookToolsTable | null = BookRepo.fetchBookTools(userId, syncCompareData.id, lang);
|
||||
const bookTools: BookToolsTable[] = bookToolsResult ? [bookToolsResult] : [];
|
||||
|
||||
return {
|
||||
eritBooks: decryptedBooks,
|
||||
chapters: decryptedChapters,
|
||||
@@ -367,7 +370,8 @@ export default class Sync {
|
||||
actSummaries: decryptedActSummaries,
|
||||
guideLine: decryptedGuideLines,
|
||||
aiGuideLine: decryptedAIGuideLines,
|
||||
issues: decryptedIssues
|
||||
issues: decryptedIssues,
|
||||
bookTools: bookTools
|
||||
};
|
||||
}
|
||||
|
||||
@@ -724,6 +728,23 @@ export default class Sync {
|
||||
}
|
||||
}
|
||||
|
||||
const serverBookTools: BookToolsTable[] = completeBook.bookTools;
|
||||
if (serverBookTools && serverBookTools.length > 0) {
|
||||
for (const serverBookTool of serverBookTools) {
|
||||
const bookToolsExists: BookToolsTable | null = BookRepo.fetchBookTools(userId, bookId, lang);
|
||||
if (bookToolsExists) {
|
||||
BookRepo.updateBookToolSetting(userId, bookId, 'characters_enabled', serverBookTool.characters_enabled === 1, lang);
|
||||
BookRepo.updateBookToolSetting(userId, bookId, 'worlds_enabled', serverBookTool.worlds_enabled === 1, lang);
|
||||
BookRepo.updateBookToolSetting(userId, bookId, 'locations_enabled', serverBookTool.locations_enabled === 1, lang);
|
||||
} else {
|
||||
const insertSuccessful: boolean = BookRepo.insertSyncBookTools(bookId, userId, serverBookTool.characters_enabled, serverBookTool.worlds_enabled, serverBookTool.locations_enabled, lang);
|
||||
if (!insertSuccessful) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -940,6 +961,13 @@ export default class Sync {
|
||||
lastUpdate: aiGuidelineRecord.last_update
|
||||
} : null;
|
||||
|
||||
const bookToolsRecord: BookToolsTable | null = BookRepo.fetchBookTools(userId, currentBookId, lang);
|
||||
const bookTools: SyncedBookTools | null = bookToolsRecord ? {
|
||||
charactersEnabled: bookToolsRecord.characters_enabled === 1,
|
||||
worldsEnabled: bookToolsRecord.worlds_enabled === 1,
|
||||
locationsEnabled: bookToolsRecord.locations_enabled === 1
|
||||
} : null;
|
||||
|
||||
return {
|
||||
id: currentBookId,
|
||||
type: bookRecord.type,
|
||||
@@ -955,7 +983,8 @@ export default class Sync {
|
||||
issues: bookIssues,
|
||||
actSummaries: bookActSummaries,
|
||||
guideLine: bookGuideLine,
|
||||
aiGuideLine: bookAIGuideLine
|
||||
aiGuideLine: bookAIGuideLine,
|
||||
bookTools: bookTools
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { getUserEncryptionKey } from "../keyManager.js";
|
||||
import System from "../System.js";
|
||||
import { CompleteBook } from "./Book.js";
|
||||
import BookRepo, { EritBooksTable } from "../repositories/book.repository.js";
|
||||
import BookRepo, { EritBooksTable, BookToolsTable } from "../repositories/book.repository.js";
|
||||
import ActRepository, { BookActSummariesTable } from "../repositories/act.repository.js";
|
||||
import GuidelineRepo, { BookAIGuideLineTable, BookGuideLineTable } from "../repositories/guideline.repository.js";
|
||||
import ChapterRepo, {
|
||||
@@ -51,7 +51,8 @@ export default class Upload {
|
||||
encryptedIssues,
|
||||
encryptedLocations,
|
||||
encryptedPlotPoints,
|
||||
encryptedWorlds
|
||||
encryptedWorlds,
|
||||
bookToolsData
|
||||
]: [
|
||||
EritBooksTable[],
|
||||
BookActSummariesTable[],
|
||||
@@ -63,7 +64,8 @@ export default class Upload {
|
||||
BookIssuesTable[],
|
||||
BookLocationTable[],
|
||||
BookPlotPointsTable[],
|
||||
BookWorldTable[]
|
||||
BookWorldTable[],
|
||||
BookToolsTable | null
|
||||
] = await Promise.all([
|
||||
BookRepo.fetchEritBooksTable(userId, bookId, lang),
|
||||
ActRepository.fetchBookActSummaries(userId, bookId, lang),
|
||||
@@ -75,7 +77,8 @@ export default class Upload {
|
||||
IssueRepository.fetchBookIssues(userId, bookId, lang),
|
||||
LocationRepo.fetchBookLocations(userId, bookId, lang),
|
||||
PlotPointRepository.fetchBookPlotPoints(userId, bookId, lang),
|
||||
WorldRepository.fetchBookWorlds(userId, bookId, lang)
|
||||
WorldRepository.fetchBookWorlds(userId, bookId, lang),
|
||||
BookRepo.fetchBookTools(userId, bookId, lang)
|
||||
]);
|
||||
|
||||
const [
|
||||
@@ -234,6 +237,8 @@ export default class Upload {
|
||||
sub_elem_description: locationSubElement.sub_elem_description ? System.decryptDataWithUserKey(locationSubElement.sub_elem_description, userEncryptionKey) : null
|
||||
}));
|
||||
|
||||
const bookTools: BookToolsTable[] = bookToolsData ? [bookToolsData] : [];
|
||||
|
||||
return {
|
||||
eritBooks,
|
||||
actSummaries,
|
||||
@@ -251,7 +256,8 @@ export default class Upload {
|
||||
worlds,
|
||||
worldElements,
|
||||
locationElements,
|
||||
locationSubElements
|
||||
locationSubElements,
|
||||
bookTools
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getUserEncryptionKey } from "../keyManager.js";
|
||||
import System from "../System.js";
|
||||
import WorldRepository, { WorldElementValue, WorldQuery } from "../repositories/world.repository.js";
|
||||
import BookRepo, {BookToolsTable} from "../repositories/book.repository.js";
|
||||
|
||||
export interface SyncedWorld {
|
||||
id: string;
|
||||
@@ -44,6 +45,11 @@ export interface WorldProps {
|
||||
importantCharacters: WorldElement[];
|
||||
}
|
||||
|
||||
export interface WorldListResponse {
|
||||
worlds: WorldProps[];
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapping of element type keys to their corresponding numeric type identifiers.
|
||||
*/
|
||||
@@ -107,9 +113,12 @@ export default class World {
|
||||
* @param userId - The unique identifier of the user
|
||||
* @param bookId - The unique identifier of the book
|
||||
* @param lang - The language for error messages ('fr' or 'en'), defaults to 'fr'
|
||||
* @returns An array of WorldProps objects containing all world data and their elements
|
||||
* @returns WorldListResponse containing an array of WorldProps and enabled flag
|
||||
*/
|
||||
public static getWorlds(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): WorldProps[] {
|
||||
public static getWorlds(userId: string, bookId: string, lang: 'fr' | 'en' = 'fr'): WorldListResponse {
|
||||
const bookTools: BookToolsTable | null = BookRepo.fetchBookTools(userId, bookId, lang);
|
||||
const enabled: boolean = bookTools ? bookTools.worlds_enabled === 1 : false;
|
||||
|
||||
const worldQueryResults: WorldQuery[] = WorldRepository.fetchWorlds(userId, bookId, lang);
|
||||
const userEncryptionKey: string = getUserEncryptionKey(userId);
|
||||
const worlds: WorldProps[] = [];
|
||||
@@ -167,7 +176,7 @@ export default class World {
|
||||
}
|
||||
}
|
||||
}
|
||||
return worlds;
|
||||
return { worlds, enabled };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,4 +274,5 @@ export default class World {
|
||||
public static removeElementFromWorld(userId: string, elementId: string, lang: 'fr' | 'en' = 'fr'): boolean {
|
||||
return WorldRepository.deleteElement(userId, elementId, lang);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,6 +46,20 @@ export interface BookCoverQuery extends Record<string, SQLiteValue> {
|
||||
cover_image: string;
|
||||
}
|
||||
|
||||
export interface BookToolsTable extends Record<string, SQLiteValue> {
|
||||
book_id: string;
|
||||
user_id: string;
|
||||
characters_enabled: number;
|
||||
worlds_enabled: number;
|
||||
locations_enabled: number;
|
||||
}
|
||||
|
||||
export interface BookToolsSettings {
|
||||
characters: boolean;
|
||||
worlds: boolean;
|
||||
locations: boolean;
|
||||
}
|
||||
|
||||
export default class BookRepo {
|
||||
/**
|
||||
* Retrieves all books for a user.
|
||||
@@ -361,4 +375,70 @@ export default class BookRepo {
|
||||
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
static fetchBookTools(userId: string, bookId: string, lang: 'fr' | 'en'): BookToolsTable | null {
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
const query: string = 'SELECT book_id, user_id, characters_enabled, worlds_enabled, locations_enabled FROM book_tools WHERE user_id=? AND book_id=?';
|
||||
const params: SQLiteValue[] = [userId, bookId];
|
||||
const result = db.get(query, params) as BookToolsTable | undefined;
|
||||
return result ?? null;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.message}`);
|
||||
throw new Error(lang === 'fr' ? 'Impossible de récupérer les paramètres des outils.' : 'Unable to fetch tools settings.');
|
||||
}
|
||||
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
static updateBookToolSetting(userId: string, bookId: string, toolName: 'characters_enabled' | 'worlds_enabled' | 'locations_enabled', enabled: boolean, lang: 'fr' | 'en'): boolean {
|
||||
const enabledValue: number = enabled ? 1 : 0;
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
const updateQuery: string = `UPDATE book_tools SET ${toolName}=? WHERE user_id=? AND book_id=?`;
|
||||
const updateResult: RunResult = db.run(updateQuery, [enabledValue, userId, bookId]);
|
||||
if (updateResult.changes > 0) {
|
||||
return true;
|
||||
}
|
||||
const charactersValue: number = toolName === 'characters_enabled' ? enabledValue : 0;
|
||||
const worldsValue: number = toolName === 'worlds_enabled' ? enabledValue : 0;
|
||||
const locationsValue: number = toolName === 'locations_enabled' ? enabledValue : 0;
|
||||
const insertQuery: string = 'INSERT INTO book_tools (book_id, user_id, characters_enabled, worlds_enabled, locations_enabled) VALUES (?, ?, ?, ?, ?)';
|
||||
const insertResult: RunResult = db.run(insertQuery, [bookId, userId, charactersValue, worldsValue, locationsValue]);
|
||||
return insertResult.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 paramètres des outils.' : 'Unable to update tools settings.');
|
||||
}
|
||||
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts book tools settings during sync.
|
||||
* @param bookId - The book identifier
|
||||
* @param userId - The user identifier
|
||||
* @param charactersEnabled - Whether characters tool is enabled
|
||||
* @param worldsEnabled - Whether worlds tool is enabled
|
||||
* @param locationsEnabled - Whether locations tool is enabled
|
||||
* @param lang - The language for error messages
|
||||
* @returns true if the insertion was successful
|
||||
*/
|
||||
static insertSyncBookTools(bookId: string, userId: string, charactersEnabled: number, worldsEnabled: number, locationsEnabled: number, lang: 'fr' | 'en'): boolean {
|
||||
try {
|
||||
const db: Database = System.getDb();
|
||||
const query: string = 'INSERT INTO book_tools (book_id, user_id, characters_enabled, worlds_enabled, locations_enabled) VALUES (?, ?, ?, ?, ?)';
|
||||
const params: SQLiteValue[] = [bookId, userId, charactersEnabled, worldsEnabled, locationsEnabled];
|
||||
const insertResult: RunResult = db.run(query, params);
|
||||
return insertResult.changes > 0;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
console.error(`DB Error: ${error.message}`);
|
||||
throw new Error(lang === 'fr' ? "Impossible d'insérer les paramètres des outils." : 'Unable to insert tools settings.');
|
||||
}
|
||||
throw new Error(lang === 'fr' ? "Une erreur inconnue s'est produite." : "An unknown error occurred.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ type Database = sqlite3.Database;
|
||||
* Data is encrypted before storage and decrypted on retrieval
|
||||
*/
|
||||
|
||||
export const SCHEMA_VERSION = 2;
|
||||
export const SCHEMA_VERSION = 3;
|
||||
|
||||
/**
|
||||
* Initialize the local SQLite database with all required tables
|
||||
@@ -412,6 +412,19 @@ export function initializeSchema(db: Database): void {
|
||||
);
|
||||
`);
|
||||
|
||||
// Book Tools
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS book_tools (
|
||||
book_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
characters_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
worlds_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
locations_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (book_id, user_id),
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
|
||||
// Create indexes for better performance
|
||||
createIndexes(db);
|
||||
|
||||
@@ -574,6 +587,20 @@ export function runMigrations(db: Database): void {
|
||||
`, 'chapter_info_id, chapter_id, act_id, incident_id, plot_point_id, book_id, author_id, summary, goal, last_update');
|
||||
}
|
||||
|
||||
if (currentVersion < 3) {
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS book_tools (
|
||||
book_id TEXT NOT NULL,
|
||||
user_id TEXT NOT NULL,
|
||||
characters_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
worlds_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
locations_enabled INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (book_id, user_id),
|
||||
FOREIGN KEY (book_id) REFERENCES erit_books(book_id) ON DELETE CASCADE
|
||||
);
|
||||
`);
|
||||
}
|
||||
|
||||
// Update schema version
|
||||
setDbSchemaVersion(db, SCHEMA_VERSION);
|
||||
}
|
||||
|
||||
@@ -111,6 +111,12 @@ interface UpdateWorldData {
|
||||
world: WorldProps;
|
||||
}
|
||||
|
||||
interface UpdateBookToolData {
|
||||
bookId: string;
|
||||
toolName: 'characters' | 'worlds' | 'locations';
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
// GET /books - Get all books
|
||||
ipcMain.handle('db:book:books', createHandler<void, BookProps[]>(
|
||||
async function(userId: string, _body: void, lang: 'fr' | 'en'):Promise<BookProps[]> {
|
||||
@@ -412,3 +418,11 @@ ipcMain.handle('db:book:world:update', createHandler<UpdateWorldData, boolean>(
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// PATCH /book/tool-setting - Update book tool setting
|
||||
ipcMain.handle('db:book:tool:update', createHandler<UpdateBookToolData, boolean>(
|
||||
function(userId: string, data: UpdateBookToolData, lang: 'fr' | 'en') {
|
||||
return Book.updateBookToolSetting(userId, data.bookId, data.toolName, data.enabled, lang);
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user