@@ -74,9 +74,19 @@ const arrangGroups = ({ schema, newStack }: any) => {
7474 schema ?. forEach ( ( item : any ) => {
7575 if ( item ?. contentstackFieldType === 'group' ) {
7676 const groupSchema : any = { ...item , schema : [ ] }
77+ if ( item ?. contentstackFieldUid ?. includes ( '.' ) ) {
78+ const parts = item ?. contentstackFieldUid ?. split ( '.' ) ;
79+ groupSchema . contentstackFieldUid = parts ?. [ parts ?. length - 1 ] ;
80+ }
7781 schema ?. forEach ( ( et : any ) => {
7882 if ( et ?. contentstackFieldUid ?. includes ( `${ item ?. contentstackFieldUid } .` ) ||
7983 ( newStack === false && et ?. uid ?. includes ( `${ item ?. uid } .` ) ) ) {
84+ const target = groupSchema ?. contentstackFieldUid ;
85+ const index = et ?. contentstackFieldUid ?. indexOf ?.( target ) ;
86+
87+ if ( index > 0 ) {
88+ et . contentstackFieldUid = et ?. contentstackFieldUid ?. substring ( index ) ;
89+ }
8090 groupSchema ?. schema ?. push ( et ) ;
8191 }
8292 } )
@@ -112,7 +122,7 @@ const saveAppMapper = async ({ marketPlacePath, data, fileName }: any) => {
112122 }
113123}
114124
115- const convertToSchemaFormate = ( { field, advanced = true , marketPlacePath } : any ) => {
125+ const convertToSchemaFormate = ( { field, advanced = true , marketPlacePath, keyMapper } : any ) => {
116126 switch ( field ?. contentstackFieldType ) {
117127 case 'single_line_text' : {
118128 return {
@@ -435,7 +445,7 @@ const convertToSchemaFormate = ({ field, advanced = true, marketPlacePath }: any
435445 return {
436446 data_type : "reference" ,
437447 display_name : field ?. title ,
438- reference_to : field ?. refrenceTo ?? [ ] ,
448+ reference_to : field ?. refrenceTo ?. map ( ( item : string ) => keyMapper [ item ] || item ) ?? [ ] ,
439449 field_metadata : {
440450 ref_multiple : true ,
441451 ref_multiple_content_types : true
@@ -654,45 +664,98 @@ const existingCtMapper = async ({ keyMapper, contentTypeUid, projectId, region,
654664 }
655665}
656666
657- const mergeArrays = async ( a : any [ ] , b : any [ ] ) => {
658- for await ( const fieldGp of b ) {
659- const exists = a . some ( fld =>
660- fld ?. uid === fieldGp ?. uid &&
661- fld ?. data_type === fieldGp ?. data_type
667+ const mergeArrays = ( a : any [ ] , b : any [ ] ) : any [ ] => {
668+ const result = [ ...a ] ;
669+
670+ for ( const field of b ) {
671+ const exists = result ?. some ( f =>
672+ f ?. uid === field ?. uid &&
673+ f ?. data_type === field ?. data_type
662674 ) ;
663675 if ( ! exists ) {
664- a . push ( fieldGp ) ;
676+ result ? .push ( field ) ;
665677 }
666678 }
667- return a ;
679+
680+ return result ;
681+ } ;
682+
683+ const mergeFields = async ( schema1 : any [ ] , schema2 : any [ ] ) : Promise < any [ ] > => {
684+ const result : any [ ] = [ ] ;
685+
686+ for ( const field2 of schema2 ) {
687+ if ( field2 ?. data_type === 'group' ) {
688+ const machingGroup = findGroupByUid ( schema1 , field2 ?. uid ) ;
689+ if ( machingGroup ) {
690+ const schema = await mergeArrays ( machingGroup ?. schema ?? [ ] , field2 ?. schema ?? [ ] ) ;
691+ result ?. push ( {
692+ ...field2 ,
693+ schema : schema
694+ } ) ;
695+ }
696+ else {
697+ result ?. push ( {
698+ ...field2 ,
699+ schema : await mergeFields ( schema1 , field2 ?. schema )
700+ } )
701+ }
702+ }
703+ else {
704+ const exists = schema1 ?. find (
705+ ( fld ) =>
706+ fld ?. uid === field2 ?. uid &&
707+ fld ?. data_type === field2 ?. data_type
708+ ) ;
709+ result ?. push ( {
710+ ...field2
711+ } ) ;
712+
713+ }
714+
715+ }
716+
717+ for ( const field1 of schema1 ) {
718+ const isMatched = schema2 ?. some (
719+ ( field2 ) =>
720+ field1 ?. uid === field2 ?. uid &&
721+ field1 ?. data_type === field2 ?. data_type
722+ ) ;
723+
724+ if ( ! isMatched ) {
725+ result ?. push ( field1 ) ;
726+ }
727+ }
728+
729+ return result ;
668730}
669731
732+
733+ const findGroupByUid = (
734+ schema : any [ ] ,
735+ uid : string ,
736+ excludeRef : any = null
737+ ) : any | undefined => {
738+ for ( const field of schema ) {
739+ if ( field ?. data_type === 'group' ) {
740+ if ( field ?. uid === uid && field !== excludeRef ) return field ;
741+ const nested = findGroupByUid ( field ?. schema ?? [ ] , uid , excludeRef ) ;
742+ if ( nested ) return nested ;
743+ }
744+ }
745+ return undefined ;
746+ } ;
747+
670748const mergeTwoCts = async ( ct : any , mergeCts : any ) => {
671- const ctData : any = {
672- ...ct ,
749+ const merged :any = {
673750 title : mergeCts ?. title ,
674751 uid : mergeCts ?. uid ,
675752 options : {
676753 "singleton" : false ,
677- }
678- }
679- for await ( const field of ctData ?. schema ?? [ ] ) {
680- if ( field ?. data_type === 'group' ) {
681- const currentGroup = mergeCts ?. schema ?. find ( ( grp : any ) => grp ?. uid === field ?. uid &&
682- grp ?. data_type === 'group' ) ;
683- const group = [ ] ;
684- for await ( const fieldGp of currentGroup ?. schema ?? [ ] ) {
685- const fieldNst = field ?. schema ?. find ( ( fld : any ) => fld ?. uid === fieldGp ?. uid &&
686- fld ?. data_type === fieldGp ?. data_type ) ;
687- if ( fieldNst === undefined ) {
688- group ?. push ( fieldGp ) ;
689- }
690- }
691- field . schema = [ ...field ?. schema ?? [ ] , ...group ] ;
692- }
693- }
694- ctData . schema = await mergeArrays ( ctData ?. schema , mergeCts ?. schema ) ?? [ ] ;
695- return ctData ;
754+ } ,
755+ schema : await mergeFields ( ct ?. schema ?? [ ] , mergeCts ?. schema ?? [ ] )
756+ } ;
757+
758+ return merged ;
696759}
697760
698761export const contenTypeMaker = async ( { contentType, destinationStackId, projectId, newStack, keyMapper, region, user_id } : any ) => {
@@ -717,7 +780,7 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
717780 "display_name" : item ?. contentstackField ,
718781 "field_metadata" : { } ,
719782 "schema" : [ ] ,
720- "uid" : item ?. contentstackFieldUid ,
783+ "uid" : item ?. contentstackFieldUid ?. replace ( / \. / g , '_' ) ,
721784 "multiple" : false ,
722785 "mandatory" : false ,
723786 "unique" : false
@@ -728,7 +791,7 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
728791 uid : extractValue ( element ?. contentstackFieldUid , item ?. contentstackFieldUid , '.' ) ,
729792 title : extractValue ( element ?. contentstackField , item ?. contentstackField , ' >' ) ?. trim ( ) ,
730793 }
731- const schema : any = convertToSchemaFormate ( { field, marketPlacePath } ) ;
794+ const schema : any = convertToSchemaFormate ( { field, marketPlacePath, keyMapper } ) ;
732795 if ( typeof schema === 'object' && Array . isArray ( group ?. schema ) && element ?. isDeleted === false ) {
733796 group . schema . push ( schema ) ;
734797 }
@@ -741,7 +804,8 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
741804 title : item ?. contentstackField ,
742805 uid : item ?. contentstackFieldUid
743806 } ,
744- marketPlacePath
807+ marketPlacePath,
808+ keyMapper
745809 } ) ;
746810 if ( dt && item ?. isDeleted === false ) {
747811 ct ?. schema ?. push ( dt ) ;
0 commit comments