@@ -25,7 +25,6 @@ import { extensionService } from "./extension.service.js";
2525
2626
2727
28-
2928/**
3029 * Creates a test stack.
3130 *
@@ -411,10 +410,111 @@ const getLogs = async (req: Request): Promise<any> => {
411410
412411}
413412
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+ */
420+ export const createSourceLocales = async ( req : Request ) => {
421+
422+ const projectFilePath = path . join ( process . cwd ( ) , 'database' , 'project.json' ) ; // Adjusted path to project.json
423+ const projectId = req . params . projectId ;
424+
425+ const locales = req . body . locale
426+
427+ try {
428+ // Check if the project.json file exists
429+ if ( ! fs . existsSync ( projectFilePath ) ) {
430+ console . error ( `project.json not found at ${ projectFilePath } ` ) ;
431+ throw new Error ( `project.json not found.` ) ;
432+ }
433+
434+ // Find the project with the specified projectId
435+ const project : any = ProjectModelLowdb . chain . get ( "projects" ) . find ( { id : projectId } ) . value ( ) ;
436+ if ( project ) {
437+ const index = ProjectModelLowdb . chain . get ( "projects" ) . findIndex ( { id : projectId } ) . value ( ) ;
438+ if ( index > - 1 ) {
439+
440+ ProjectModelLowdb . update ( ( data : any ) => {
441+ data . projects [ index ] . source_locales = locales ;
442+ } ) ;
443+ } // Write back the updated projects
444+ } else {
445+ logger . error ( `Project with ID: ${ projectId } not found` , {
446+ status : HTTP_CODES ?. NOT_FOUND ,
447+ message : HTTP_TEXTS ?. INVALID_ID
448+ } )
449+ }
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+ ) ;
460+ }
461+ }
462+
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+
414512export const migrationService = {
415513 createTestStack,
416514 deleteTestStack,
417515 startTestMigration,
418516 startMigration,
419517 getLogs,
518+ createSourceLocales,
519+ updateLocaleMapper
420520} ;
0 commit comments