@@ -5,11 +5,101 @@ import { Transaction } from '@op-engineering/op-sqlite';
55
66const log = logger . create ( 'DBRepository' ) ;
77
8+ async function insertLanguageTx ( tx : Transaction , lang : DBTypes . Language ) {
9+ await tx . execute (
10+ `INSERT OR REPLACE INTO languages
11+ (id, lang_name, lang_name_localized, lang_code_iso_639_3, script_direction)
12+ VALUES (?, ?, ?, ?, ?)` ,
13+ [
14+ lang . id ,
15+ lang . langName ,
16+ lang . langNameLocalized ?? null ,
17+ lang . langCode ?? null ,
18+ lang . scriptDirection ?? 'ltr' ,
19+ ] ,
20+ ) ;
21+ }
22+
23+ async function insertBookTx ( tx : Transaction , book : DBTypes . Book ) {
24+ if ( ! book . eng_display_name ) return ;
25+
26+ await tx . execute (
27+ `INSERT OR REPLACE INTO books
28+ (id, code, eng_display_name)
29+ VALUES (?, ?, ?)` ,
30+ [ book . id , book . code , book . eng_display_name ] ,
31+ ) ;
32+ }
33+
34+ async function insertBibleTx ( tx : Transaction , bible : DBTypes . Bible ) {
35+ if ( ! bible . name || ! bible . abbreviation ) return ;
36+
37+ await tx . execute (
38+ `INSERT OR REPLACE INTO bibles
39+ (id, language_id, name, abbreviation)
40+ VALUES (?, ?, ?, ?)` ,
41+ [ bible . id , bible . languageId , bible . name , bible . abbreviation ] ,
42+ ) ;
43+ }
44+
45+ async function insertProjectUnitTx (
46+ tx : Transaction ,
47+ unit : { id : number ; projectId : number } ,
48+ ) {
49+ await tx . execute (
50+ `INSERT OR REPLACE INTO project_units
51+ (id, project_id, status)
52+ VALUES (?, ?, ?)` ,
53+ [ unit . id , unit . projectId , 'not_started' ] ,
54+ ) ;
55+ }
56+
57+ async function insertChapterAssignmentTx (
58+ tx : Transaction ,
59+ assignment : DBTypes . ChapterAssignment ,
60+ ) {
61+ if ( ! assignment ?. chapterAssignmentId ) return ;
62+
63+ await tx . execute (
64+ `INSERT OR REPLACE INTO chapter_assignments
65+ (id, project_unit_id, bible_id, book_id, chapter_number,
66+ assigned_user_id, status, submitted_time, updated_at)
67+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)` ,
68+ [
69+ assignment . chapterAssignmentId ,
70+ assignment . projectUnitId ,
71+ assignment . bibleId ,
72+ assignment . bookId ,
73+ assignment . chapterNumber ,
74+ assignment . assignedUserId ?? null ,
75+ assignment . chapterStatus ?? 'not_started' ,
76+ assignment . submittedTime ?? null ,
77+ assignment . updatedAt ?? new Date ( ) . toISOString ( ) ,
78+ ] ,
79+ ) ;
80+ }
81+
82+ function getUniqueProjectUnits ( assignments : DBTypes . ChapterAssignment [ ] ) {
83+ const unitsMap = new Map < number , { id : number ; projectId : number } > ( ) ;
84+
85+ for ( const assignment of assignments ) {
86+ if ( ! assignment . projectUnitId || ! assignment . projectId ) continue ;
87+ if ( unitsMap . has ( assignment . projectUnitId ) ) continue ;
88+
89+ unitsMap . set ( assignment . projectUnitId , {
90+ id : assignment . projectUnitId ,
91+ projectId : assignment . projectId ,
92+ } ) ;
93+ }
94+
95+ return unitsMap ;
96+ }
97+
898export async function insertUser ( user : DBTypes . User ) {
999 const db = getDatabase ( ) ;
10100
11- try {
12- await db . execute (
101+ await db . transaction ( async ( tx : Transaction ) => {
102+ await tx . execute (
13103 `INSERT OR REPLACE INTO users
14104 (id, username, email, first_name, last_name)
15105 VALUES (?, ?, ?, ?, ?)` ,
@@ -21,28 +111,15 @@ export async function insertUser(user: DBTypes.User) {
21111 user . lastName ?? null ,
22112 ] ,
23113 ) ;
24- } catch ( error ) {
25- log . error ( 'Error inserting user' , { error } ) ;
26- }
114+ } ) ;
27115}
28116
29117export async function insertLanguages ( data : DBTypes . Language [ ] ) {
30118 const db = getDatabase ( ) ;
31119
32120 await db . transaction ( async ( tx : Transaction ) => {
33121 for ( const lang of data ) {
34- await tx . execute (
35- `INSERT OR REPLACE INTO languages
36- (id, lang_name, lang_name_localized, lang_code_iso_639_3, script_direction)
37- VALUES (?, ?, ?, ?, ?)` ,
38- [
39- lang . id ,
40- lang . langName ,
41- lang . langNameLocalized ?? null ,
42- lang . langCode ?? null ,
43- lang . scriptDirection ?? 'ltr' ,
44- ] ,
45- ) ;
122+ await insertLanguageTx ( tx , lang ) ;
46123 }
47124 } ) ;
48125}
@@ -52,14 +129,7 @@ export async function insertBooks(data: DBTypes.Book[]) {
52129
53130 await db . transaction ( async ( tx : Transaction ) => {
54131 for ( const book of data ) {
55- if ( ! book . eng_display_name ) continue ;
56-
57- await tx . execute (
58- `INSERT OR REPLACE INTO books
59- (id, code, eng_display_name)
60- VALUES (?, ?, ?)` ,
61- [ book . id , book . code , book . eng_display_name ] ,
62- ) ;
132+ await insertBookTx ( tx , book ) ;
63133 }
64134 } ) ;
65135}
@@ -69,14 +139,29 @@ export async function insertBibles(data: DBTypes.Bible[]) {
69139
70140 await db . transaction ( async ( tx : Transaction ) => {
71141 for ( const bible of data ) {
72- if ( ! bible . name || ! bible . abbreviation ) continue ;
142+ await insertBibleTx ( tx , bible ) ;
143+ }
144+ } ) ;
145+ }
73146
74- await tx . execute (
75- `INSERT OR REPLACE INTO bibles
76- (id, language_id, name, abbreviation)
77- VALUES (?, ?, ?, ?)` ,
78- [ bible . id , bible . languageId , bible . name , bible . abbreviation ] ,
79- ) ;
147+ export async function insertMasterData (
148+ languages : DBTypes . Language [ ] ,
149+ books : DBTypes . Book [ ] ,
150+ bibles : DBTypes . Bible [ ] ,
151+ ) {
152+ const db = getDatabase ( ) ;
153+
154+ await db . transaction ( async ( tx : Transaction ) => {
155+ for ( const lang of languages ) {
156+ await insertLanguageTx ( tx , lang ) ;
157+ }
158+
159+ for ( const book of books ) {
160+ await insertBookTx ( tx , book ) ;
161+ }
162+
163+ for ( const bible of bibles ) {
164+ await insertBibleTx ( tx , bible ) ;
80165 }
81166 } ) ;
82167}
@@ -88,8 +173,10 @@ export async function insertProjects(data: DBTypes.Project[]) {
88173 for ( const project of data ) {
89174 if ( ! project ?. id || ! project ?. name ) continue ;
90175
91- const sourceLangId = project . sourceLanguageId ?? project . sourceLanguageId ;
92- const targetLangId = project . targetLanguageId ?? project . targetLanguageId ;
176+ const sourceLangId =
177+ project . sourceLanguageId ?? project . source_language_id ;
178+ const targetLangId =
179+ project . targetLanguageId ?? project . target_language_id ;
93180
94181 if ( ! sourceLangId || ! targetLangId ) continue ;
95182
@@ -118,25 +205,7 @@ export async function insertChapterAssignments(
118205
119206 await db . transaction ( async ( tx : Transaction ) => {
120207 for ( const assignment of data ) {
121- if ( ! assignment ?. chapterAssignmentId ) continue ;
122-
123- await tx . execute (
124- `INSERT OR REPLACE INTO chapter_assignments
125- (id, project_unit_id, bible_id, book_id, chapter_number,
126- assigned_user_id, status, submitted_time, updated_at)
127- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)` ,
128- [
129- assignment . chapterAssignmentId ,
130- assignment . projectUnitId ,
131- assignment . bibleId ,
132- assignment . bookId ,
133- assignment . chapterNumber ,
134- assignment . assignedUserId ?? null ,
135- assignment . chapterStatus ?? 'not_started' ,
136- assignment . submittedTime ?? null ,
137- assignment . updatedAt ?? new Date ( ) . toISOString ( ) ,
138- ] ,
139- ) ;
208+ await insertChapterAssignmentTx ( tx , assignment ) ;
140209 }
141210 } ) ;
142211}
@@ -146,32 +215,34 @@ export async function insertProjectUnits(
146215) {
147216 const db = getDatabase ( ) ;
148217
149- const unitsMap = new Map < number , { id : number ; projectId : number } > ( ) ;
150-
151- for ( const assignment of assignments ) {
152- if ( ! assignment . projectUnitId || ! assignment . projectId ) continue ;
153- if ( unitsMap . has ( assignment . projectUnitId ) ) continue ;
154-
155- unitsMap . set ( assignment . projectUnitId , {
156- id : assignment . projectUnitId ,
157- projectId : assignment . projectId ,
158- } ) ;
159- }
218+ const unitsMap = getUniqueProjectUnits ( assignments ) ;
160219
161220 if ( unitsMap . size > 0 ) {
162221 await db . transaction ( async ( tx : Transaction ) => {
163222 for ( const unit of unitsMap . values ( ) ) {
164- await tx . execute (
165- `INSERT OR REPLACE INTO project_units
166- (id, project_id, status)
167- VALUES (?, ?, ?)` ,
168- [ unit . id , unit . projectId , 'not_started' ] ,
169- ) ;
223+ await insertProjectUnitTx ( tx , unit ) ;
170224 }
171225 } ) ;
172226 }
173227}
174228
229+ export async function insertChapterAssignmentSyncData (
230+ assignments : DBTypes . ChapterAssignment [ ] ,
231+ ) {
232+ const db = getDatabase ( ) ;
233+ const unitsMap = getUniqueProjectUnits ( assignments ) ;
234+
235+ await db . transaction ( async ( tx : Transaction ) => {
236+ for ( const unit of unitsMap . values ( ) ) {
237+ await insertProjectUnitTx ( tx , unit ) ;
238+ }
239+
240+ for ( const assignment of assignments ) {
241+ await insertChapterAssignmentTx ( tx , assignment ) ;
242+ }
243+ } ) ;
244+ }
245+
175246export async function getChaptersToSync ( ) {
176247 const db = getDatabase ( ) ;
177248
0 commit comments