Skip to content

Commit cbd3514

Browse files
committed
refactor;added optional chaining and await in asset function in wordpress code
1 parent e930368 commit cbd3514

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

api/src/services/wordpress.service.ts

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "fs";
22
import path from "path";
33
import { fileURLToPath } from "url";
44
import axios from "axios";
5-
import _ from "lodash";
5+
//import _ from "lodash";
66
import { MIGRATION_DATA_CONFIG } from "../constants/index.js";
77
import jsdom from "jsdom";
88
import { htmlToJson } from "@contentstack/json-rte-serializer";
@@ -113,7 +113,7 @@ let blog_base_url = "";
113113
//helper function to convert entries with content type
114114
function mapContentTypeToEntry(contentType: any, data: any) {
115115
return contentType?.fieldMapping?.reduce((acc: { [key: string]: any }, field: FieldMapping) => {
116-
const fieldValue = data[field.uid];
116+
const fieldValue = data?.[field?.uid];
117117
let formattedValue;
118118

119119
switch (field?.contentstackFieldType) {
@@ -131,6 +131,9 @@ function mapContentTypeToEntry(contentType: any, data: any) {
131131
default:
132132
formattedValue = fieldValue;
133133
}
134+
if(field?.advanced?.multiple){
135+
formattedValue = Array.isArray(formattedValue) ? formattedValue : [formattedValue];
136+
}
134137

135138
acc[field.contentstackFieldUid] = formattedValue;
136139
return acc;
@@ -366,7 +369,7 @@ async function saveAsset(assets: any, retryCount: number, affix: string, destina
366369
await writeFileAsync(failedJSONFilePath, failedJSON, 4);
367370

368371
if (retryCount === 0) {
369-
return saveAsset(assets, 1, affix, destinationStackId, projectId, baseSiteUrl);
372+
return await saveAsset(assets, 1, affix, destinationStackId, projectId, baseSiteUrl);
370373
} else {
371374
const message = getLogMessage(
372375
srcFunc,
@@ -384,15 +387,15 @@ async function getAsset(attachments: any[], affix: string, destinationStackId: s
384387
const BATCH_SIZE = 5; // 5 promises at a time
385388
const results = [];
386389

387-
for (let i = 0; i < attachments.length; i += BATCH_SIZE) {
388-
const batch = attachments.slice(i, i + BATCH_SIZE);
390+
for (let i = 0; i < attachments?.length; i += BATCH_SIZE) {
391+
const batch = attachments?.slice(i, i + BATCH_SIZE);
389392

390393
const batchResults = await Promise.allSettled(
391-
batch.map((data) => {
392-
saveAsset(data, 0, affix, destinationStackId, projectId, baseSiteUrl)
394+
batch?.map(async (data) => {
395+
await saveAsset(data, 0, affix, destinationStackId, projectId, baseSiteUrl)
393396
})
394397
);
395-
results.push(...batchResults);
398+
results?.push(...batchResults);
396399
}
397400
await writeFileAsync(
398401
path.join(assetsSave, MIGRATION_DATA_CONFIG.ASSETS_FILE_NAME),
@@ -419,7 +422,7 @@ async function getAllAssets(
419422
const assets: Asset[] =
420423
alldataParsed?.rss?.channel?.item ?? alldataParsed?.channel?.item;
421424
// await writeFileAsync(path.join(assetsSave, MIGRATION_DATA_CONFIG.ASSETS_FILE_NAME), assets, 4);
422-
if (!assets || assets.length === 0) {
425+
if (!assets || assets?.length === 0) {
423426
const message = getLogMessage(
424427
"createAssetFolderFile",
425428
`No assets found.`,
@@ -430,10 +433,10 @@ async function getAllAssets(
430433
return;
431434
}
432435

433-
const attachments = assets.filter(
436+
const attachments = assets?.filter(
434437
({ "wp:post_type": postType }) => postType === "attachment"
435438
);
436-
if (attachments.length > 0) {
439+
if (attachments?.length > 0) {
437440
await getAsset(attachments, affix, destinationStackId, projectId,baseSiteUrl);
438441
}
439442
return;
@@ -756,42 +759,45 @@ async function saveAuthors(authorDetails: any[], destinationStackId: string, pro
756759
const srcFunc = "saveAuthors";
757760
try {
758761

759-
const authordata = authorDetails.reduce(
760-
async (acc: { [key: string]: any }, data) => {
761-
const uid = `authors_${data["wp:author_id"] || data["wp:author_login"]
762-
}`;
763-
764-
const title = data["wp:author_login"] || `Authors - ${data["wp:author_id"]}`;
765-
const url = `/${title.toLowerCase().replace(/ /g, "_")}`;
766-
const customId = idCorrector(uid);
767-
const authordata: any = {
768-
uid: uid,
769-
title: data["wp:author_login"],
770-
url: url,
771-
email: data["wp:author_email"],
772-
first_name: data["wp:author_first_name"],
773-
last_name: data["wp:author_last_name"],
774-
};
775-
acc[customId] = {
776-
...acc[customId],
777-
uid: customId,
778-
...mapContentTypeToEntry(contentType, authordata),
779-
};
780-
acc[customId].publish_details = [];
762+
const authordata: { [key: string]: any } = {};
781763

782-
const message = getLogMessage(
783-
srcFunc,
784-
`Entry title ${data["wp:author_login"]} (authors) in the ${master_locale} locale has been successfully transformed.`,
785-
{}
786-
);
787-
788-
await customLogger(projectId, destinationStackId, 'info', message);
789-
return acc;
790-
},
791-
{}
792-
);
764+
for (const data of authorDetails) {
765+
const uid = `authors_${data["wp:author_id"] || data["wp:author_login"]}`;
766+
const title = data["wp:author_login"] || `Authors - ${data["wp:author_id"]}`;
767+
const url = `/${title.toLowerCase().replace(/ /g, "_")}`;
768+
const customId = idCorrector(uid);
769+
770+
const authordataEntry: any = {
771+
uid: uid,
772+
title: data["wp:author_login"],
773+
url: url,
774+
email: data["wp:author_email"],
775+
first_name: data["wp:author_first_name"],
776+
last_name: data["wp:author_last_name"],
777+
};
778+
779+
authordata[customId] = {
780+
...authordata[customId],
781+
uid: customId,
782+
...mapContentTypeToEntry(contentType, authordataEntry),
783+
};
784+
authordata[customId].publish_details = [];
785+
786+
const message = getLogMessage(
787+
srcFunc,
788+
`Entry title ${data["wp:author_login"]} (authors) in the ${master_locale} locale has been successfully transformed.`,
789+
{}
790+
);
791+
792+
await customLogger(projectId, destinationStackId, 'info', message);
793+
}
793794
await writeFileAsync(authorsFilePath, authordata, 4);
794-
await writeFileAsync(path.join(authorsFolderPath, "index.json"), {"1": `${master_locale}.json`}, 4);
795+
await writeFileAsync(
796+
path.join(authorsFolderPath, "index.json"),
797+
{ "1": `${master_locale}.json` },
798+
4
799+
);
800+
795801
const message = getLogMessage(
796802
srcFunc,
797803
`${authorDetails.length} Authors exported successfully`,
@@ -1522,11 +1528,11 @@ async function getAllTerms(affix: string, packagePath: string, destinationStackI
15221528
const alldata: any = await fs.promises.readFile(packagePath, "utf8");
15231529
const alldataParsed = JSON.parse(alldata);
15241530
const terms =
1525-
alldataParsed?.rss?.channel["wp:term"] ||
1526-
alldataParsed?.channel["wp:term"] ||
1531+
alldataParsed?.rss?.channel?.["wp:term"] ||
1532+
alldataParsed?.channel?.["wp:term"] ||
15271533
"";
15281534

1529-
if (!terms || terms.length === 0) {
1535+
if (!terms || terms?.length === 0) {
15301536
const message = getLogMessage(
15311537
srcFunc,
15321538
`No terms found`,
@@ -1738,7 +1744,7 @@ function getParent(data: any,id: string) {
17381744

17391745
return catParent;
17401746
}
1741-
async function saveCategories(categoryDetails: any[], destinationStackId:string, projectId: string, contenttype:any, master_locale:stting) {
1747+
async function saveCategories(categoryDetails: any[], destinationStackId:string, projectId: string, contenttype:any, master_locale:string) {
17421748
const srcFunc = 'saveCategories';
17431749
try {
17441750
const categorydata = categoryDetails.reduce(
@@ -1922,7 +1928,7 @@ async function featuredImageMapping(postid: string, post: any, postdata: any) {
19221928
.filter(Boolean); // Filter out undefined matches
19231929

19241930
if (assetsDetails.length > 0) {
1925-
postdata[postid]["featured_image"] = assetsDetails;
1931+
postdata[postid]["featured_image"] = assetsDetails[0];
19261932
}
19271933
return postdata;
19281934
} catch (error) {
@@ -2030,13 +2036,13 @@ async function processChunkData(
20302036
const base = blog_base_url.split("/").filter(Boolean);
20312037
const blogname = base[base.length - 1];
20322038
const url = data["link"].split(blogname)[1];
2033-
const title = data["title"] ?? `Posts - ${data["wp:post_id"]}`;
2039+
//const title = data["title"] ?? `Posts - ${data["wp:post_id"]}`;
20342040
const uid = `posts_${data["wp:post_id"]}`
20352041
const customId = idCorrector(uid)
20362042
postdata[customId] = {
20372043
title: data["title"] || `Posts - ${data["wp:post_id"]}`,
20382044
uid: customId,
2039-
url: "/"+title.toLowerCase().replace(/ /g, "_"),
2045+
url: url,
20402046
date: postDate,
20412047
full_description: jsonValue,
20422048
excerpt: data["excerpt:encoded"]

0 commit comments

Comments
 (0)