@@ -5,7 +5,7 @@ import axios from "axios";
55//import _ from "lodash";
66import { MIGRATION_DATA_CONFIG } from "../constants/index.js" ;
77import jsdom from "jsdom" ;
8- import { htmlToJson } from "@contentstack/json-rte-serializer" ;
8+ import { htmlToJson , jsonToHtml } from "@contentstack/json-rte-serializer" ;
99import customLogger from "../utils/custom-logger.utils.js" ;
1010import { getLogMessage } from "../utils/index.js" ;
1111import { Advanced } from "../models/FieldMapper.js" ;
@@ -108,20 +108,28 @@ let assetData: Record<string, any> | any = {};
108108let blog_base_url = "" ;
109109
110110//helper function to convert entries with content type
111- function mapContentTypeToEntry ( contentType : any , data : any ) {
112- return contentType ?. fieldMapping ?. reduce ( ( acc : { [ key : string ] : any } , field : FieldMapping ) => {
111+ async function mapContentTypeToEntry ( contentType : any , data : any ) {
112+ const result : { [ key : string ] : any } = { } ;
113+
114+ for ( const field of contentType ?. fieldMapping || [ ] ) {
113115 const fieldValue = data ?. [ field ?. uid ] ;
114116 let formattedValue ;
115117
116118 switch ( field ?. contentstackFieldType ) {
117119 case "single_line_text" :
118120 case "text" :
119- case 'html' :
120121 formattedValue = fieldValue ;
121122 break ;
122- case "json" :
123- formattedValue = convertHtmlToJson ( fieldValue ) ;
123+ case "html" :
124+ formattedValue =
125+ fieldValue && typeof fieldValue === "object"
126+ ? await convertJsonToHtml ( fieldValue )
127+ : fieldValue ;
124128 break ;
129+ case "json" :
130+
131+ formattedValue = await convertHtmlToJson ( fieldValue ) ;
132+ break ;
125133 case "reference" :
126134 formattedValue = getParent ( data , data [ field . uid ] ) ;
127135 break ;
@@ -132,10 +140,12 @@ function mapContentTypeToEntry(contentType: any, data: any) {
132140 formattedValue = Array . isArray ( formattedValue ) ? formattedValue : [ formattedValue ] ;
133141 }
134142
135- acc [ field . contentstackFieldUid ] = formattedValue ;
136- return acc ;
137- } , { } ) ;
143+ result [ field ?. contentstackFieldUid ] = formattedValue ;
144+ }
145+
146+ return result ;
138147}
148+
139149// helper functions
140150async function writeFileAsync ( filePath : string , data : any , tabSpaces : number ) {
141151 filePath = path . resolve ( filePath ) ;
@@ -596,7 +606,7 @@ async function getAllreference(affix: string, packagePath: string, destinationSt
596606 ...processReferenceData ( referenceTerms , "terms" , "wp:term_slug" , terms )
597607 ) ;
598608 referenceArray . push (
599- ...processReferenceData ( referenceTags , "tags " , "wp:tag_slug" , tag )
609+ ...processReferenceData ( referenceTags , "tag " , "wp:tag_slug" , tag )
600610 ) ;
601611
602612 if ( referenceArray . length > 0 ) {
@@ -776,7 +786,7 @@ async function saveAuthors(authorDetails: any[], destinationStackId: string, pro
776786 authordata [ customId ] = {
777787 ...authordata [ customId ] ,
778788 uid : customId ,
779- ...mapContentTypeToEntry ( contentType , authordataEntry ) ,
789+ ...( await mapContentTypeToEntry ( contentType , authordataEntry ) ) ,
780790 } ;
781791 authordata [ customId ] . publish_details = [ ] ;
782792
@@ -1473,7 +1483,7 @@ async function saveTerms(termsDetails: any[], destinationStackId: string, projec
14731483 `${ master_locale } .json`
14741484 ) ;
14751485 const termsdata = termsDetails . reduce (
1476- ( acc : { [ key : string ] : any } , data ) => {
1486+ async ( acc : { [ key : string ] : any } , data ) => {
14771487
14781488 const { id } = data ;
14791489 const uid = `terms_${ id } ` ;
@@ -1485,7 +1495,7 @@ async function saveTerms(termsDetails: any[], destinationStackId: string, projec
14851495 acc [ customId ] = {
14861496 ...acc [ customId ] ,
14871497 uid : customId ,
1488- ...mapContentTypeToEntry ( contentType , data ) , // Pass individual term object
1498+ ...( await mapContentTypeToEntry ( contentType , data ) ) , // Pass individual term object
14891499 } ;
14901500 acc [ customId ] . publish_details = [ ] ;
14911501 return acc ;
@@ -1598,7 +1608,7 @@ async function saveTags(tagDetails: any[], destinationStackId: string, projectId
15981608 tagsFolderPath ,
15991609 `${ master_locale } .json`
16001610 ) ;
1601- const tagdata = tagDetails . reduce ( ( acc : { [ key : string ] : any } , data ) => {
1611+ const tagdata = tagDetails . reduce ( async ( acc : { [ key : string ] : any } , data ) => {
16021612 const { id } = data ;
16031613 const uid = `tags_${ id } ` ;
16041614 //const title = `Tags - ${id}`;
@@ -1609,7 +1619,7 @@ async function saveTags(tagDetails: any[], destinationStackId: string, projectId
16091619 acc [ customId ] = {
16101620 ...acc [ customId ] ,
16111621 uid :customId ,
1612- ...mapContentTypeToEntry ( contenttype , data ) ,
1622+ ...( await mapContentTypeToEntry ( contenttype , data ) ) ,
16131623 } ;
16141624 acc [ customId ] . publish_details = [ ] ;
16151625
@@ -1715,11 +1725,22 @@ async function startingDirCategories(affix: string, ct: string, master_locale:st
17151725}
17161726
17171727const convertHtmlToJson = ( htmlString : any ) => {
1718- const dom = new JSDOM ( htmlString ?. replace ( / & a m p ; / g, "&" ) ) ;
1719- const htmlDoc = dom . window . document . querySelector ( "body" ) ;
1720- return htmlToJson ( htmlDoc ) ;
1728+ if ( typeof htmlString === 'string' ) {
1729+ const dom = new JSDOM ( htmlString ?. replace ( / & a m p ; / g, "&" ) ) ;
1730+ const htmlDoc = dom . window . document . querySelector ( "body" ) ;
1731+ const jsonValue = htmlToJson ( htmlDoc )
1732+ return jsonValue ;
1733+
1734+ }
1735+ else return htmlString ;
17211736} ;
17221737
1738+ const convertJsonToHtml = async ( json : any ) => {
1739+ const htmlValue = await jsonToHtml ( json ) ;
1740+ return htmlValue ;
1741+
1742+ }
1743+
17231744function getParent ( data : any , id : string ) {
17241745 const parentId : any = fs . readFileSync (
17251746 path . join ( referencesFolder , MIGRATION_DATA_CONFIG . REFERENCES_FILE_NAME ) ,
@@ -1745,7 +1766,7 @@ async function saveCategories(categoryDetails: any[], destinationStackId:string,
17451766 const srcFunc = 'saveCategories' ;
17461767 try {
17471768 const categorydata = categoryDetails . reduce (
1748- ( acc : { [ key : string ] : any } , data ) => {
1769+ async ( acc : { [ key : string ] : any } , data ) => {
17491770 const uid = `category_${ data [ "id" ] } ` ;
17501771
17511772 const customId = uid
@@ -1754,7 +1775,7 @@ async function saveCategories(categoryDetails: any[], destinationStackId:string,
17541775 acc [ customId ] = {
17551776 ...acc [ customId ] ,
17561777 uid :customId ,
1757- ...mapContentTypeToEntry ( contenttype , data ) ,
1778+ ...( await mapContentTypeToEntry ( contenttype , data ) ) ,
17581779 }
17591780 acc [ customId ] . publish_details = [ ] ;
17601781
@@ -2057,23 +2078,18 @@ async function processChunkData(
20572078 data ,
20582079 postdata
20592080 ) ;
2060- const formattedPosts = Object ?. entries ( formatted_posts ) ?. reduce (
2061- ( acc : { [ key : string ] : any } , data :any ) => {
2062-
2063- const customId = idCorrector ( data [ "uid" ] )
2064-
2065- // Accumulate category data
2066- acc [ customId ] = {
2067- ...acc [ customId ] ,
2068- uid : customId ,
2069- ...mapContentTypeToEntry ( contenttype , data ) ,
2070- } ;
2071- acc [ customId ] . publish_details = [ ] ;
2072-
2073- return acc ;
2074- } ,
2075- { }
2076- ) ;
2081+ const formattedPosts : any = { } ;
2082+
2083+ for ( const [ key , value ] of Object . entries ( formatted_posts as { [ key : string ] : any } ) ) {
2084+ const customId = idCorrector ( value ?. uid ) ;
2085+
2086+ formattedPosts [ customId ] = {
2087+ ...formattedPosts [ customId ] ,
2088+ uid : customId ,
2089+ ...( await mapContentTypeToEntry ( contenttype , value ) ) ,
2090+ } ;
2091+ formattedPosts [ customId ] . publish_details = [ ] ;
2092+ }
20772093 Object . assign ( postdata , formattedPosts ) ;
20782094
20792095 // await writeFileAsync(
@@ -2095,6 +2111,7 @@ async function processChunkData(
20952111 if ( isLastChunk && allSuccess ) {
20962112 console . info ( "last data" ) ;
20972113 }
2114+ console . info ( "postData ---> " , postdata )
20982115 return postdata
20992116 } catch ( error ) {
21002117 console . error ( error ) ;
@@ -2105,8 +2122,8 @@ async function processChunkData(
21052122
21062123async function extractPosts ( packagePath : string , destinationStackId : string , projectId : string , contentTypes :any , keyMapper :any , master_locale : string ) {
21072124 const srcFunc = "extractPosts" ;
2108- const ct :any = keyMapper ?. [ "categories " ] ;
2109- const contenttype = contentTypes ?. find ( ( item :any ) => item ?. otherCmsUid === 'categories ' ) ;
2125+ const ct :any = keyMapper ?. [ "posts " ] ;
2126+ const contenttype = contentTypes ?. find ( ( item :any ) => item ?. otherCmsUid === 'posts ' ) ;
21102127
21112128 try {
21122129 await startingDirPosts ( ct , master_locale ) ;
0 commit comments