@@ -9,7 +9,6 @@ import { orgService } from './org.service.js';
99import { getLogMessage } from '../utils/index.js' ;
1010import customLogger from '../utils/custom-logger.utils.js' ;
1111
12-
1312const append = "a" ;
1413const baseDirName = MIGRATION_DATA_CONFIG . DATA
1514const {
@@ -50,6 +49,18 @@ const AssetsPathSpliter = ({ path, id }: any) => {
5049 return newPath ;
5150}
5251
52+ const mapLocales = ( { masterLocale, locale, locales } : any ) => {
53+ if ( locales ?. masterLocale ?. [ masterLocale ?? '' ] === locale ) {
54+ return Object ?. keys ( locales ?. masterLocale ) ?. [ 0 ]
55+ }
56+ for ( const [ key , value ] of Object ?. entries ?.( locales ) ?? { } ) {
57+ if ( typeof value !== 'object' && value === locale ) {
58+ return key ;
59+ }
60+ }
61+ return locale ?. toLowerCase ?.( ) ;
62+ }
63+
5364
5465
5566async function writeOneFile ( indexPath : string , fileMeta : any ) {
@@ -195,7 +206,7 @@ const cretaeAssets = async ({ packagePath, baseDir, destinationStackId, projectI
195206 return allAssetJSON ;
196207}
197208
198- const createEntry = async ( { packagePath, contentTypes, master_locale, destinationStackId, projectId, keyMapper } : { packagePath : any ; contentTypes : any ; master_locale ?: string , destinationStackId : string , projectId : string , keyMapper : any } ) => {
209+ const createEntry = async ( { packagePath, contentTypes, master_locale, destinationStackId, projectId, keyMapper, project } : { packagePath : any ; contentTypes : any ; master_locale ?: string , destinationStackId : string , projectId : string , keyMapper : any , project : any } ) => {
199210 try {
200211 const srcFunc = 'createEntry' ;
201212 const baseDir = path . join ( baseDirName , destinationStackId ) ;
@@ -216,7 +227,11 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
216227 const templateIndex = entriesData ?. findIndex ( ( ele : any ) => ele ?. template === template ) ;
217228 if ( templateIndex >= 0 ) {
218229 const entry = entriesData ?. [ templateIndex ] ?. locale ?. [ language ] ;
219- entry [ id ] = { meta : jsonData ?. item ?. $ , fields : jsonData ?. item ?. fields } ;
230+ if ( entry !== undefined ) {
231+ entry [ id ] = { meta : jsonData ?. item ?. $ , fields : jsonData ?. item ?. fields } ;
232+ } else {
233+ entriesData [ templateIndex ] . locale [ language ] = entries ;
234+ }
220235 } else {
221236 const locale : any = { } ;
222237 locale [ language ] = entries ;
@@ -228,61 +243,59 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
228243 for await ( const ctType of contentTypes ) {
229244 const message = getLogMessage (
230245 srcFunc ,
231- `Transforming entries of Content Type ${ keyMapper [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } has begun.` ,
246+ `Transforming entries of Content Type ${ keyMapper ?. [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } has begun.` ,
232247 { }
233248 )
234249 await customLogger ( projectId , destinationStackId , 'info' , message ) ;
235250 const entryPresent : any = entriesData ?. find ( ( item : any ) => uidCorrector ( { uid : item ?. template } ) === ctType ?. contentstackUid )
236251 if ( entryPresent ) {
237252 const locales : any = Object ?. keys ( entryPresent ?. locale ) ;
253+ const allLocales : any = { masterLocale : project ?. master_locale ?? LOCALE_MAPPER ?. masterLocale , ...project ?. locales ?? { } }
238254 for await ( const locale of locales ) {
239- let newLocale = locale ;
255+ const newLocale = mapLocales ( { masterLocale : master_locale , locale, locales : allLocales } ) ;
240256 const entryLocale : any = { } ;
241- if ( typeof LOCALE_MAPPER ?. masterLocale === 'object' && LOCALE_MAPPER ?. masterLocale !== null && LOCALE_MAPPER ?. masterLocale ?. [ master_locale ?? '' ] === locale ) {
242- newLocale = Object ?. keys ( LOCALE_MAPPER ?. masterLocale ) ?. [ 0 ] ;
243- Object . entries ( entryPresent ?. locale ?. [ locale ] || { } ) . map ( async ( [ uid , entry ] : any ) => {
244- const entryObj : any = { } ;
245- entryObj . uid = uid ;
246- for await ( const field of entry ?. fields ?. field ?? [ ] ) {
247- for await ( const fsc of ctType ?. fieldMapping ?? [ ] ) {
248- if ( fsc ?. contentstackFieldType !== 'group' && ! field ?. $ ?. key ?. includes ( '__' ) ) {
249- if ( fsc ?. contentstackFieldUid === 'title' ) {
250- entryObj [ fsc ?. contentstackFieldUid ] = entry ?. meta ?. name ;
251- }
252- if ( fsc ?. contentstackFieldUid === 'url' ) {
253- entryObj [ fsc ?. contentstackFieldUid ] = `/${ entry ?. meta ?. key } ` ;
254- }
255- if ( getLastKey ( fsc ?. uid ) === field ?. $ ?. key ) {
256- const content : any = await entriesFieldCreator ( { field : fsc , content : field ?. content , idCorrector, allAssetJSON, contentTypes, entriesData, locale } ) ;
257- const gpData : any = ctType ?. fieldMapping ?. find ( ( elemant : any ) => elemant ?. uid === fsc ?. uid ?. split ( '.' ) ?. [ 0 ] ) ;
258- if ( gpData ?. uid ) {
259- const ctUid = uidCorrector ( { uid : gpData ?. uid } ) ;
260- if ( ctUid !== gpData ?. contentstackFieldUid && fsc ?. contentstackFieldUid ?. includes ( ctUid ) ) {
261- const newUid : any = fsc ?. contentstackFieldUid ?. replace ( ctUid , gpData ?. contentstackFieldUid ) ;
262- entryObj [ newUid ] = content ;
263- } else {
264- entryObj [ fsc ?. contentstackFieldUid ] = content ;
265- }
257+ Object . entries ( entryPresent ?. locale ?. [ locale ] || { } ) . map ( async ( [ uid , entry ] : any ) => {
258+ const entryObj : any = { } ;
259+ entryObj . uid = uid ;
260+ for await ( const field of entry ?. fields ?. field ?? [ ] ) {
261+ for await ( const fsc of ctType ?. fieldMapping ?? [ ] ) {
262+ if ( fsc ?. contentstackFieldType !== 'group' && ! field ?. $ ?. key ?. includes ( '__' ) ) {
263+ if ( fsc ?. contentstackFieldUid === 'title' ) {
264+ entryObj [ fsc ?. contentstackFieldUid ] = entry ?. meta ?. name ;
265+ }
266+ if ( fsc ?. contentstackFieldUid === 'url' ) {
267+ entryObj [ fsc ?. contentstackFieldUid ] = `/${ entry ?. meta ?. key } ` ;
268+ }
269+ if ( getLastKey ( fsc ?. uid ) === field ?. $ ?. key ) {
270+ const content : any = await entriesFieldCreator ( { field : fsc , content : field ?. content , idCorrector, allAssetJSON, contentTypes, entriesData, locale } ) ;
271+ const gpData : any = ctType ?. fieldMapping ?. find ( ( elemant : any ) => elemant ?. uid === fsc ?. uid ?. split ( '.' ) ?. [ 0 ] ) ;
272+ if ( gpData ?. uid ) {
273+ const ctUid = uidCorrector ( { uid : gpData ?. uid } ) ;
274+ if ( ctUid !== gpData ?. contentstackFieldUid && fsc ?. contentstackFieldUid ?. includes ( ctUid ) ) {
275+ const newUid : any = fsc ?. contentstackFieldUid ?. replace ( ctUid , gpData ?. contentstackFieldUid ) ;
276+ entryObj [ newUid ] = content ;
266277 } else {
267278 entryObj [ fsc ?. contentstackFieldUid ] = content ;
268279 }
280+ } else {
281+ entryObj [ fsc ?. contentstackFieldUid ] = content ;
269282 }
270283 }
271284 }
272285 }
273- entryObj . publish_details = [ ] ;
274- if ( Object . keys ?. ( entryObj ) ?. length > 1 ) {
275- entryLocale [ uid ] = unflatten ( entryObj ) ?? { } ;
276- const message = getLogMessage (
277- srcFunc ,
278- `Entry title " ${ entryObj ?. title } "( ${ keyMapper [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } ) in the ${ newLocale } locale has been successfully transformed.` ,
279- { }
280- )
281- await customLogger ( projectId , destinationStackId , 'info' , message )
282- }
283- } ) ;
284- }
285- const mapperCt : string = ( keyMapper [ ctType ?. contentstackUid ] !== "" && keyMapper [ ctType ?. contentstackUid ] !== undefined ) ? keyMapper [ ctType ?. contentstackUid ]
286+ }
287+ entryObj . publish_details = [ ] ;
288+ if ( Object . keys ?. ( entryObj ) ?. length > 1 ) {
289+ entryLocale [ uid ] = unflatten ( entryObj ) ?? { } ;
290+ const message = getLogMessage (
291+ srcFunc ,
292+ `Entry title " ${ entryObj ?. title } "( ${ keyMapper ?. [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } ) in the ${ newLocale } locale has been successfully transformed.` ,
293+ { }
294+ )
295+ await customLogger ( projectId , destinationStackId , 'info' , message )
296+ }
297+ } ) ;
298+ const mapperCt : string = ( keyMapper ?. [ ctType ?. contentstackUid ] !== "" && keyMapper ?. [ ctType ?. contentstackUid ] !== undefined ) ? keyMapper ?. [ ctType ?. contentstackUid ]
286299 : ctType ?. contentstackUid ;
287300 const fileMeta = { "1" : `${ newLocale } .json` } ;
288301 const entryPath = path . join (
@@ -296,11 +309,11 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
296309 } else {
297310 const message = getLogMessage (
298311 srcFunc ,
299- `No entries found for the content type ${ keyMapper [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } .` ,
312+ `No entries found for the content type ${ keyMapper ?. [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid } .` ,
300313 { }
301314 )
302315 await customLogger ( projectId , destinationStackId , 'error' , message )
303- console . info ( 'Entries missing for' , keyMapper [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid )
316+ console . info ( 'Entries missing for' , keyMapper ?. [ ctType ?. contentstackUid ] ?? ctType ?. contentstackUid )
304317 }
305318 }
306319 return true ;
@@ -309,13 +322,13 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
309322 }
310323}
311324
312- const createLocale = async ( req : any , destinationStackId : string , projectId : string ) => {
325+ const createLocale = async ( req : any , destinationStackId : string , projectId : string , project : any ) => {
313326 const srcFunc = 'createLocale' ;
314327 try {
315328 const baseDir = path . join ( baseDirName , destinationStackId ) ;
316329 const localeSave = path . join ( baseDir , LOCALE_DIR_NAME ) ;
317330 const allLocalesResp = await orgService . getLocales ( req )
318- const masterLocale = Object ?. keys ?.( LOCALE_MAPPER ?. masterLocale ) ?. [ 0 ] ;
331+ const masterLocale = Object ?. keys ?.( project ?. master_locale ?? LOCALE_MAPPER ?. masterLocale ) ?. [ 0 ] ;
319332 const msLocale : any = { } ;
320333 const uid = uuidv4 ( ) ;
321334 msLocale [ uid ] = {
@@ -331,14 +344,14 @@ const createLocale = async (req: any, destinationStackId: string, projectId: str
331344 )
332345 await customLogger ( projectId , destinationStackId , 'info' , message ) ;
333346 const allLocales : any = { } ;
334- for ( const [ key , value ] of Object . entries ( LOCALE_MAPPER ) ) {
347+ for ( const [ key , value ] of Object . entries ( project ?. locales ?? LOCALE_MAPPER ) ) {
335348 const localeUid = uuidv4 ( ) ;
336349 if ( key !== 'masterLocale' && typeof value === 'string' ) {
337350 allLocales [ localeUid ] = {
338- "code" : value ,
351+ "code" : key ,
339352 "fallback_locale" : masterLocale ,
340353 "uid" : localeUid ,
341- "name" : allLocalesResp ?. data ?. locales ?. [ value ] ?? ''
354+ "name" : allLocalesResp ?. data ?. locales ?. [ key ] ?? ''
342355 }
343356 const message = getLogMessage (
344357 srcFunc ,
0 commit comments