Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 60 additions & 58 deletions api/src/services/wordpress.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,9 @@ let blog_base_url = "";
//helper function to convert entries with content type
async function mapContentTypeToEntry(contentType: any, data: any) {
const result: { [key: string]: any } = {};

for (const field of contentType?.fieldMapping || []) {
const fieldValue = data?.[field?.uid];
let formattedValue;

const fieldValue = data?.[field?.uid] ?? null;
let formattedValue ;
switch (field?.contentstackFieldType) {
case "single_line_text":
case "text":
Expand All @@ -126,10 +124,14 @@ async function mapContentTypeToEntry(contentType: any, data: any) {
? await convertJsonToHtml(fieldValue)
: fieldValue;
break;
case "json":

formattedValue = await convertHtmlToJson(fieldValue);
break;
case "json":
try {
formattedValue = typeof fieldValue !== 'object' ? await convertHtmlToJson(fieldValue) : fieldValue;
} catch (err) {
console.error(`Error converting HTML to JSON for field ${field?.uid}:`, err);
formattedValue = null;
}
break;
case "reference":
formattedValue = getParent(data,data[field.uid]);
break;
Expand Down Expand Up @@ -1482,26 +1484,22 @@ async function saveTerms(termsDetails: any[], destinationStackId: string, projec
termsFolderPath,
`${master_locale}.json`
);
const termsdata = termsDetails.reduce(
async (acc: { [key: string]: any }, data) => {

const { id } = data;
const uid = `terms_${id}`;
const termsdata: { [key: string]: any } = {};
for (const data of termsDetails) {
const { id } = data;
const uid = `terms_${id}`;
const customId = uid;
// const title = name ?? `Terms - ${id}`;
//const url = `/${title.toLowerCase().replace(/ /g, "_")}`;

const customId = uid;

acc[customId] = {
...acc[customId],

termsdata[customId] = {
...termsdata[customId],
uid: customId,
...(await mapContentTypeToEntry(contentType, data)), // Pass individual term object
...(await mapContentTypeToEntry(contentType, data)),
};
acc[customId].publish_details = [];
return acc;
},
{}
);
termsdata[customId].publish_details = [];
}

await writeFileAsync(termsFilePath, termsdata, 4);
await writeFileAsync(path.join(termsFolderPath, "index.json"), {"1": `${master_locale}.json`}, 4);
Expand Down Expand Up @@ -1608,24 +1606,25 @@ async function saveTags(tagDetails: any[], destinationStackId: string, projectId
tagsFolderPath,
`${master_locale}.json`
);
const tagdata = tagDetails.reduce(async (acc: { [key: string]: any }, data) => {
const tagsdata: { [key: string]: any } = {};

for(const data of tagDetails) {
const { id } = data;
const uid = `tags_${id}`;
//const title = `Tags - ${id}`;
// const url = `/tags/${uid}`;
//const url = `/${title.toLowerCase().replace(/ /g, "_")}`;
const customId = idCorrector(uid);

acc[customId]={
...acc[customId],
tagsdata[customId]={
...tagsdata[customId],
uid:customId,
...( await mapContentTypeToEntry(contenttype,data)),
};
acc[customId].publish_details = [];
tagsdata[customId].publish_details = [];

return acc;
}, {});
await writeFileAsync(tagsFilePath, tagdata, 4);
}
await writeFileAsync(tagsFilePath, tagsdata, 4);
await writeFileAsync(path.join(tagsFolderPath, "index.json"), {"1": `${master_locale}.json`}, 4);
const message = getLogMessage(
srcFunc,
Expand Down Expand Up @@ -1724,15 +1723,14 @@ async function startingDirCategories(affix: string, ct: string, master_locale:st
}
}

const convertHtmlToJson = (htmlString: any) => {
if(typeof htmlString === 'string'){
const dom = new JSDOM(htmlString?.replace(/&/g, "&"));
const convertHtmlToJson = (htmlString: unknown): any => {
if (typeof htmlString === 'string') {
const dom = new JSDOM(htmlString.replace(/&/g, "&"));
const htmlDoc = dom.window.document.querySelector("body");
const jsonValue = htmlToJson(htmlDoc)
return jsonValue;

return htmlToJson(htmlDoc);
}
else return htmlString;

return htmlString;
};

const convertJsonToHtml = async (json: any) => {
Expand Down Expand Up @@ -1765,24 +1763,21 @@ function getParent(data: any,id: string) {
async function saveCategories(categoryDetails: any[], destinationStackId:string, projectId: string, contenttype:any, master_locale:string) {
const srcFunc = 'saveCategories';
try {
const categorydata = categoryDetails.reduce(
async (acc: { [key: string]: any }, data) => {
const categorydata: { [key: string]: any } = {}
for(const data of categoryDetails){

const uid = `category_${data["id"]}`;

const customId = uid

// Accumulate category data
acc[customId]={
...acc[customId],
categorydata[customId]={
...categorydata[customId],
uid:customId,
...(await mapContentTypeToEntry(contenttype,data)),
}
acc[customId].publish_details = [];

return acc;
},
{}
);
categorydata[customId].publish_details = [];
}

await writeFileAsync(
path.join(
Expand Down Expand Up @@ -2011,6 +2006,9 @@ async function processChunkData(
contenttype: any
) {
const postdata: any = {};
const formattedPosts: any = {};
let postdataCombined = {}

try {
const writePromises = [];

Expand Down Expand Up @@ -2073,24 +2071,23 @@ async function processChunkData(
featured_image: '',
publish_details:[]
};
const formatted_posts = await featuredImageMapping(
`posts_${data["wp:post_id"]}`,
data,
postdata
);
const formattedPosts: any = {};


for (const [key, value] of Object.entries(formatted_posts as {[key: string]: any})) {
for (const [key, value] of Object.entries(postdata as {[key: string]: any})) {
const customId = idCorrector(value?.uid);

formattedPosts[customId] = {
...formattedPosts[customId],
uid: customId,
...(await mapContentTypeToEntry(contenttype, value)),
};
formattedPosts[customId].publish_details = [];
}
Object.assign(postdata,formattedPosts);
const formatted_posts = await featuredImageMapping(
`posts_${data["wp:post_id"]}`,
data,
formattedPosts
);
postdataCombined = { ...postdataCombined, ...formatted_posts };

// await writeFileAsync(
// path.join(postFolderPath, filename),
Expand All @@ -2111,8 +2108,8 @@ async function processChunkData(
if (isLastChunk && allSuccess) {
console.info("last data");
}
console.info("postData ---> ", postdata)
return postdata

return postdataCombined
} catch (error) {
console.error(error);
console.error("Error saving posts", error);
Expand Down Expand Up @@ -2162,6 +2159,11 @@ async function extractPosts( packagePath: string, destinationStackId: string, pr
postdataCombined,
4
);
await writeFileAsync(
path.join(postFolderPath, "index.json"),
{ "1": `${master_locale}.json` },
4
);
return;
} catch (error) {
const message = getLogMessage(
Expand Down
8 changes: 0 additions & 8 deletions ui/src/pages/Migration/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -625,19 +625,11 @@ const Migration = () => {
} else {

const res = await updateCurrentStepData(selectedOrganisation.value, projectId);
if (res?.status === 200) {
setIsLoading(false);
event.preventDefault();
handleStepChange(3);
const url = `/projects/${projectId}/migration/steps/4`;
navigate(url, { replace: true });
} else {
setIsLoading(false);
Notification({
notificationContent: { text: res?.data?.error?.message },
type: 'error'
});
}

}
};
Expand Down
4 changes: 3 additions & 1 deletion upload-api/migration-sitecore/libs/contenttypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ const contentTypeMapper = ({
};

const contentTypeMaker = ({ template, basePath, sitecore_folder, affix }) => {
const isPresent = restrictedUid?.find((item) => item === template?.key);
const correctedUid = isPresent ? `${affix}_${uidCorrector({ uid: template?.key })}`: uidCorrector({ uid: template?.key })
const content_type = {
id: template?.id,
status: 1,
Expand All @@ -598,7 +600,7 @@ const contentTypeMaker = ({ template, basePath, sitecore_folder, affix }) => {
isUpdated: false,
updateAt: '',
contentstackTitle: template?.name,
contentstackUid: uidCorrector({ uid: template?.key })
contentstackUid: correctedUid
};
template?.field?.forEach((item) => {
if (item?.$?.key === '__base template' && item?.$?.type === 'tree list') {
Expand Down