@@ -410,14 +410,18 @@ const getLogs = async (req: Request): Promise<any> => {
410410
411411}
412412
413+ /**
414+ * @description - This function takes all the fetched locales from the exported data of the legacy CMS and stores/updates them in the project.json in DB
415+ *
416+ * @param req - A request body object with fetched locales [] as payload along with project ID in params
417+ * @return - void
418+ * @throws Exception if the project ID is invalid or the when the path to project.json is incorrect
419+ */
413420export const createSourceLocales = async ( req : Request ) => {
414421
415- console . info ( "ENTERED MIGRATION API LINE 415" )
416-
417422 const projectFilePath = path . join ( process . cwd ( ) , 'database' , 'project.json' ) ; // Adjusted path to project.json
418423 const projectId = req . params . projectId ;
419424
420- console . info ( "Request Object LINE 420" , req . body )
421425 const locales = req . body . locale
422426
423427 try {
@@ -426,36 +430,91 @@ export const createSourceLocales = async (req: Request) => {
426430 console . error ( `project.json not found at ${ projectFilePath } ` ) ;
427431 throw new Error ( `project.json not found.` ) ;
428432 }
429-
430- // const data = await fs.promises.readFile(projectFilePath, 'utf8');
431- // const projects = JSON.parse(data);
432433
433434 // Find the project with the specified projectId
434435 const project : any = ProjectModelLowdb . chain . get ( "projects" ) . find ( { id : projectId } ) . value ( ) ;
435436 if ( project ) {
436437 const index = ProjectModelLowdb . chain . get ( "projects" ) . findIndex ( { id : projectId } ) . value ( ) ;
437438 if ( index > - 1 ) {
438- console . info ( "INSIDE API IF STATEMENT LINE 437" , index ) ;
439439
440440 ProjectModelLowdb . update ( ( data : any ) => {
441- // console.info("DATA LINE 439 ", data?.projects?.[index])
442441 data . projects [ index ] . source_locales = locales ;
443- // console.info(data.projects[index] )
444442 } ) ;
445443 } // Write back the updated projects
446444 } else {
447- console . error ( `Project with id ${ projectId } not found.` ) ;
445+ logger . error ( `Project with ID: ${ projectId } not found` , {
446+ status : HTTP_CODES ?. NOT_FOUND ,
447+ message : HTTP_TEXTS ?. INVALID_ID
448+ } )
448449 }
449- } catch ( err ) {
450- console . error ( 'Error updating project.json:' , err ) ;
450+ } catch ( err : any ) {
451+ console . error ( "🚀 ~ createSourceLocales ~ err:" , err ?. response ?. data ?? err , err )
452+ logger . warn ( 'Bad Request' , {
453+ status : HTTP_CODES ?. BAD_REQUEST ,
454+ message : HTTP_TEXTS ?. INTERNAL_ERROR ,
455+ } ) ;
456+ throw new ExceptionFunction (
457+ err ?. message || HTTP_TEXTS . INTERNAL_ERROR ,
458+ err ?. statusCode || err ?. status || HTTP_CODES . SERVER_ERROR
459+ ) ;
451460 }
452461}
453462
463+
464+ /**
465+ * @description - Function retrieves the mapped locales and updates them in the project.json in DB
466+ * @param req - A request body object with mapped locales as payload and project ID in the params
467+ * @return - void
468+ * @throws Exception if the project ID is invalid or the when the path to project.json is incorrect
469+ */
470+ export const updateLocaleMapper = async ( req :Request ) => {
471+ const mapperObject = req . body . mapper ;
472+ const projectFilePath = path . join ( process . cwd ( ) , 'database' , 'project.json' ) ; // Adjusted path to project.json
473+ const projectId = req . params . projectId ;
474+
475+ try {
476+ // Check if the project.json file exists
477+ if ( ! fs . existsSync ( projectFilePath ) ) {
478+ console . error ( `project.json not found at ${ projectFilePath } ` ) ;
479+ throw new Error ( `project.json not found.` ) ;
480+ }
481+
482+ // Find the project with the specified projectId
483+ const project : any = ProjectModelLowdb . chain . get ( "projects" ) . find ( { id : projectId } ) . value ( ) ;
484+ if ( project ) {
485+ const index = ProjectModelLowdb . chain . get ( "projects" ) . findIndex ( { id : projectId } ) . value ( ) ;
486+ if ( index > - 1 ) {
487+ ProjectModelLowdb . update ( ( data : any ) => {
488+ data . projects [ index ] . master_locale = mapperObject ?. master_locale ;
489+ data . projects [ index ] . locales = mapperObject ?. locales ;
490+ } ) ;
491+ } // Write back the updated projects
492+ } else {
493+ logger . error ( `Project with ID: ${ projectId } not found` , {
494+ status : HTTP_CODES ?. NOT_FOUND ,
495+ message : HTTP_TEXTS ?. INVALID_ID
496+ } )
497+ }
498+ } catch ( err : any ) {
499+ console . error ( "🚀 ~ createSourceLocales ~ err:" , err ?. response ?. data ?? err , err )
500+ logger . warn ( 'Bad Request' , {
501+ status : HTTP_CODES ?. BAD_REQUEST ,
502+ message : HTTP_TEXTS ?. INTERNAL_ERROR ,
503+ } ) ;
504+ throw new ExceptionFunction (
505+ err ?. message || HTTP_TEXTS . INTERNAL_ERROR ,
506+ err ?. statusCode || err ?. status || HTTP_CODES . SERVER_ERROR
507+ ) ;
508+ }
509+
510+ }
511+
454512export const migrationService = {
455513 createTestStack,
456514 deleteTestStack,
457515 startTestMigration,
458516 startMigration,
459517 getLogs,
460- createSourceLocales
518+ createSourceLocales,
519+ updateLocaleMapper
461520} ;
0 commit comments