@@ -6,49 +6,35 @@ import log from './helpers/log';
66import protectedFunction from './helpers/protectedFunction' ;
77import sdk from '../services/sdk' ;
88
9- async function fetchAndSaveTranslations ( language : string , db : any ) : Promise < void > {
10- const result = await sdk . get ( 'apps.translations' , { language } ) ;
11-
12- if ( ! result ?. success || ! result . translations ) {
13- return ;
14- }
15-
16- await db . write ( async ( ) => {
17- const collection = db . get ( 'app_translations' ) ;
18-
19- const existing = await collection . query ( Q . where ( 'language' , result . language ) ) . fetch ( ) ;
20- const toDelete = existing . map ( ( r : any ) => r . prepareDestroyPermanently ( ) ) ;
21-
22- const toCreate = Object . entries ( result . translations ) . map ( ( [ key , value ] ) =>
23- collection . prepareCreate (
24- protectedFunction ( ( r : any ) => {
25- r . _raw = sanitizedRaw ( { id : `${ result . language } _${ key } ` } , collection . schema ) ;
26- r . key = key ;
27- r . value = value as string ;
28- r . language = result . language ;
29- } )
30- )
31- ) ;
32-
33- await db . batch ( ...toDelete , ...toCreate ) ;
34- } ) ;
35- }
36-
379export async function getAppTranslations ( language = 'en' ) : Promise < void > {
3810 try {
3911 const db = database . active ;
40- const collection = db . get ( 'app_translations' ) ;
4112
42- // check if translations already exist in DB for this language
43- const existing = await collection . query ( Q . where ( 'language' , language ) ) . fetchCount ( ) ;
13+ const result = await sdk . get ( 'apps.translations' , { language } ) ;
4414
45- if ( existing > 0 ) {
46- // already have translations — skip fetch
15+ if ( ! result ?. success || ! result . translations ) {
4716 return ;
4817 }
4918
50- // nothing in DB — fetch from server and save
51- await fetchAndSaveTranslations ( language , db ) ;
19+ await db . write ( async ( ) => {
20+ const collection = db . get ( 'app_translations' ) ;
21+
22+ const existing = await collection . query ( Q . where ( 'language' , result . language ) ) . fetch ( ) ;
23+ const toDelete = existing . map ( ( r : any ) => r . prepareDestroyPermanently ( ) ) ;
24+
25+ const toCreate = Object . entries ( result . translations ) . map ( ( [ key , value ] ) =>
26+ collection . prepareCreate (
27+ protectedFunction ( ( r : any ) => {
28+ r . _raw = sanitizedRaw ( { id : `${ result . language } _${ key } ` } , collection . schema ) ;
29+ r . key = key ;
30+ r . value = value as string ;
31+ r . language = result . language ;
32+ } )
33+ )
34+ ) ;
35+
36+ await db . batch ( ...toDelete , ...toCreate ) ;
37+ } ) ;
5238 } catch ( e ) {
5339 log ( e ) ;
5440 }
0 commit comments