Add multi-language support and new repository methods for book synchronization

- Extend repository methods to handle API requests for fetching books, chapters, characters, and other entities with multilingual support (`lang: 'fr' | 'en'`).
- Add `uploadBookForSync` logic to consolidate and decrypt book data for synchronization.
- Refactor schema migration logic to remove console logs and streamline table recreation.
- Enhance error handling across database repositories and IPC methods.
This commit is contained in:
natreex
2025-12-22 16:44:12 -05:00
parent ff530f3442
commit 515d469ba7
17 changed files with 666 additions and 113 deletions

View File

@@ -432,7 +432,7 @@ export default class Book {
return BookRepo.insertBook(id,userId,encryptedTitle,hashedTitle,encryptedSubTitle,hashedSubTitle,encryptedSummary,type,serie,publicationDate,desiredWordCount,lang);
}
public static async getBook(userId:string,bookId: string): Promise<BookProps> {
public static async getBook(userId:string,bookId: string, lang: 'fr' | 'en'): Promise<BookProps> {
const book:Book = new Book(bookId);
await book.getBookInfos(userId);
return {
@@ -1334,6 +1334,217 @@ export default class Book {
});
}
static async uploadBookForSync(userId:string,bookId: string,lang: "fr" | "en"): Promise<CompleteBook> {
const userKey: string = getUserEncryptionKey(userId);
const [
eritBooksRaw,
actSummariesRaw,
aiGuideLineRaw,
chaptersRaw,
charactersRaw,
guideLineRaw,
incidentsRaw,
issuesRaw,
locationsRaw,
plotPointsRaw,
worldsRaw
]: [
EritBooksTable[],
BookActSummariesTable[],
BookAIGuideLineTable[],
BookChaptersTable[],
BookCharactersTable[],
BookGuideLineTable[],
BookIncidentsTable[],
BookIssuesTable[],
BookLocationTable[],
BookPlotPointsTable[],
BookWorldTable[]
] = await Promise.all([
BookRepo.fetchEritBooksTable(userId, bookId,lang),
BookRepo.fetchBookActSummaries(userId, bookId,lang),
BookRepo.fetchBookAIGuideLine(userId, bookId,lang),
BookRepo.fetchBookChapters(userId, bookId,lang),
BookRepo.fetchBookCharacters(userId, bookId,lang),
BookRepo.fetchBookGuideLineTable(userId, bookId,lang),
BookRepo.fetchBookIncidents(userId, bookId,lang),
BookRepo.fetchBookIssues(userId, bookId,lang),
BookRepo.fetchBookLocations(userId, bookId,lang),
BookRepo.fetchBookPlotPoints(userId, bookId,lang),
BookRepo.fetchBookWorlds(userId, bookId,lang)
]);
const [
chapterContentsNested,
chapterInfosNested,
characterAttributesNested,
worldElementsNested,
locationElementsNested
]: [
BookChapterContentTable[][],
BookChapterInfosTable[][],
BookCharactersAttributesTable[][],
BookWorldElementsTable[][],
LocationElementTable[][]
] = await Promise.all([
Promise.all(chaptersRaw.map((chapter: BookChaptersTable): Promise<BookChapterContentTable[]> => BookRepo.fetchBookChapterContents(userId, chapter.chapter_id,lang))),
Promise.all(chaptersRaw.map((chapter: BookChaptersTable): Promise<BookChapterInfosTable[]> => BookRepo.fetchBookChapterInfos(userId, chapter.chapter_id,lang))),
Promise.all(charactersRaw.map((character: BookCharactersTable): Promise<BookCharactersAttributesTable[]> => BookRepo.fetchBookCharactersAttributes(userId, character.character_id,lang))),
Promise.all(worldsRaw.map((world: BookWorldTable): Promise<BookWorldElementsTable[]> => BookRepo.fetchBookWorldElements(userId, world.world_id,lang))),
Promise.all(locationsRaw.map((location: BookLocationTable): Promise<LocationElementTable[]> => BookRepo.fetchLocationElements(userId, location.loc_id,lang)))
]);
const chapterContentsRaw: BookChapterContentTable[] = chapterContentsNested.flat();
const chapterInfosRaw: BookChapterInfosTable[] = chapterInfosNested.flat();
const characterAttributesRaw: BookCharactersAttributesTable[] = characterAttributesNested.flat();
const worldElementsRaw: BookWorldElementsTable[] = worldElementsNested.flat();
const locationElementsRaw: LocationElementTable[] = locationElementsNested.flat();
const locationSubElementsNested: LocationSubElementTable[][] = await Promise.all(
locationElementsRaw.map((element: LocationElementTable): Promise<LocationSubElementTable[]> => BookRepo.fetchLocationSubElements(userId, element.element_id,lang))
);
const locationSubElementsRaw: LocationSubElementTable[] = locationSubElementsNested.flat();
const eritBooks: EritBooksTable[] = eritBooksRaw.map((book: EritBooksTable): EritBooksTable => ({
...book,
title: System.decryptDataWithUserKey(book.title, userKey),
sub_title: book.sub_title ? System.decryptDataWithUserKey(book.sub_title, userKey) : null,
summary: book.summary ? System.decryptDataWithUserKey(book.summary, userKey) : null,
cover_image: book.cover_image ? System.decryptDataWithUserKey(book.cover_image, userKey) : null
}));
const actSummaries: BookActSummariesTable[] = actSummariesRaw.map((actSummary: BookActSummariesTable): BookActSummariesTable => ({
...actSummary,
summary: actSummary.summary ? System.decryptDataWithUserKey(actSummary.summary, userKey) : null
}));
const aiGuideLine: BookAIGuideLineTable[] = aiGuideLineRaw.map((guideLine: BookAIGuideLineTable): BookAIGuideLineTable => ({
...guideLine,
global_resume: guideLine.global_resume ? System.decryptDataWithUserKey(guideLine.global_resume, userKey) : null,
themes: guideLine.themes ? System.decryptDataWithUserKey(guideLine.themes, userKey) : null,
tone: guideLine.tone ? System.decryptDataWithUserKey(guideLine.tone, userKey) : null,
atmosphere: guideLine.atmosphere ? System.decryptDataWithUserKey(guideLine.atmosphere, userKey) : null,
current_resume: guideLine.current_resume ? System.decryptDataWithUserKey(guideLine.current_resume, userKey) : null
}));
const chapters: BookChaptersTable[] = chaptersRaw.map((chapter: BookChaptersTable): BookChaptersTable => ({
...chapter,
title: System.decryptDataWithUserKey(chapter.title, userKey)
}));
const chapterContents: BookChapterContentTable[] = chapterContentsRaw.map((chapterContent: BookChapterContentTable): BookChapterContentTable => ({
...chapterContent,
content: chapterContent.content ? JSON.parse(System.decryptDataWithUserKey(chapterContent.content, userKey)) : null
}));
const chapterInfos: BookChapterInfosTable[] = chapterInfosRaw.map((chapterInfo: BookChapterInfosTable): BookChapterInfosTable => ({
...chapterInfo,
summary: chapterInfo.summary ? System.decryptDataWithUserKey(chapterInfo.summary, userKey) : null,
goal: chapterInfo.goal ? System.decryptDataWithUserKey(chapterInfo.goal, userKey) : null
}));
const characters: BookCharactersTable[] = charactersRaw.map((character: BookCharactersTable): BookCharactersTable => ({
...character,
first_name: System.decryptDataWithUserKey(character.first_name, userKey),
last_name: character.last_name ? System.decryptDataWithUserKey(character.last_name, userKey) : null,
category: System.decryptDataWithUserKey(character.category, userKey),
title: character.title ? System.decryptDataWithUserKey(character.title, userKey) : null,
role: character.role ? System.decryptDataWithUserKey(character.role, userKey) : null,
biography: character.biography ? System.decryptDataWithUserKey(character.biography, userKey) : null,
history: character.history ? System.decryptDataWithUserKey(character.history, userKey) : null
}));
const characterAttributes: BookCharactersAttributesTable[] = characterAttributesRaw.map((attribute: BookCharactersAttributesTable): BookCharactersAttributesTable => ({
...attribute,
attribute_name: System.decryptDataWithUserKey(attribute.attribute_name, userKey),
attribute_value: System.decryptDataWithUserKey(attribute.attribute_value, userKey)
}));
const guideLine: BookGuideLineTable[] = guideLineRaw.map((guide: BookGuideLineTable): BookGuideLineTable => ({
...guide,
tone: guide.tone ? System.decryptDataWithUserKey(guide.tone, userKey) : null,
atmosphere: guide.atmosphere ? System.decryptDataWithUserKey(guide.atmosphere, userKey) : null,
writing_style: guide.writing_style ? System.decryptDataWithUserKey(guide.writing_style, userKey) : null,
themes: guide.themes ? System.decryptDataWithUserKey(guide.themes, userKey) : null,
symbolism: guide.symbolism ? System.decryptDataWithUserKey(guide.symbolism, userKey) : null,
motifs: guide.motifs ? System.decryptDataWithUserKey(guide.motifs, userKey) : null,
narrative_voice: guide.narrative_voice ? System.decryptDataWithUserKey(guide.narrative_voice, userKey) : null,
pacing: guide.pacing ? System.decryptDataWithUserKey(guide.pacing, userKey) : null,
intended_audience: guide.intended_audience ? System.decryptDataWithUserKey(guide.intended_audience, userKey) : null,
key_messages: guide.key_messages ? System.decryptDataWithUserKey(guide.key_messages, userKey) : null
}));
const incidents: BookIncidentsTable[] = incidentsRaw.map((incident: BookIncidentsTable): BookIncidentsTable => ({
...incident,
title: System.decryptDataWithUserKey(incident.title, userKey),
summary: incident.summary ? System.decryptDataWithUserKey(incident.summary, userKey) : null
}));
const issues: BookIssuesTable[] = issuesRaw.map((issue: BookIssuesTable): BookIssuesTable => ({
...issue,
name: System.decryptDataWithUserKey(issue.name, userKey)
}));
const locations: BookLocationTable[] = locationsRaw.map((location: BookLocationTable): BookLocationTable => ({
...location,
loc_name: System.decryptDataWithUserKey(location.loc_name, userKey)
}));
const plotPoints: BookPlotPointsTable[] = plotPointsRaw.map((plotPoint: BookPlotPointsTable): BookPlotPointsTable => ({
...plotPoint,
title: System.decryptDataWithUserKey(plotPoint.title, userKey),
summary: plotPoint.summary ? System.decryptDataWithUserKey(plotPoint.summary, userKey) : null
}));
const worlds: BookWorldTable[] = worldsRaw.map((world: BookWorldTable): BookWorldTable => ({
...world,
name: System.decryptDataWithUserKey(world.name, userKey),
history: world.history ? System.decryptDataWithUserKey(world.history, userKey) : null,
politics: world.politics ? System.decryptDataWithUserKey(world.politics, userKey) : null,
economy: world.economy ? System.decryptDataWithUserKey(world.economy, userKey) : null,
religion: world.religion ? System.decryptDataWithUserKey(world.religion, userKey) : null,
languages: world.languages ? System.decryptDataWithUserKey(world.languages, userKey) : null
}));
const worldElements: BookWorldElementsTable[] = worldElementsRaw.map((worldElement: BookWorldElementsTable): BookWorldElementsTable => ({
...worldElement,
name: System.decryptDataWithUserKey(worldElement.name, userKey),
description: worldElement.description ? System.decryptDataWithUserKey(worldElement.description, userKey) : null
}));
const locationElements: LocationElementTable[] = locationElementsRaw.map((locationElement: LocationElementTable): LocationElementTable => ({
...locationElement,
element_name: System.decryptDataWithUserKey(locationElement.element_name, userKey),
element_description: locationElement.element_description ? System.decryptDataWithUserKey(locationElement.element_description, userKey) : null
}));
const locationSubElements: LocationSubElementTable[] = locationSubElementsRaw.map((locationSubElement: LocationSubElementTable): LocationSubElementTable => ({
...locationSubElement,
sub_elem_name: System.decryptDataWithUserKey(locationSubElement.sub_elem_name, userKey),
sub_elem_description: locationSubElement.sub_elem_description ? System.decryptDataWithUserKey(locationSubElement.sub_elem_description, userKey) : null
}));
return {
eritBooks,
actSummaries,
aiGuideLine,
chapters,
chapterContents,
chapterInfos,
characters,
characterAttributes,
guideLine,
incidents,
issues,
locations,
plotPoints,
worlds,
worldElements,
locationElements,
locationSubElements
};
}
static async saveCompleteBook(userId: string, data: CompleteBook, lang: "fr" | "en"):Promise<boolean> {
const userKey: string = getUserEncryptionKey(userId);
@@ -1723,7 +1934,6 @@ export default class Book {
}
}
}
console.log(data.id)
const book: EritBooksTable[] = await BookRepo.fetchCompleteBookById(data.id, lang);
if (book.length>0) {
const bookDataItem: EritBooksTable = book[0];