Skip to content

Commit cac0f6c

Browse files
Merge pull request #512 from contentstack/feature/dropdown-field-choices
Feature/dropdown field choices
2 parents 051d107 + f0c89d7 commit cac0f6c

File tree

9 files changed

+144
-126
lines changed

9 files changed

+144
-126
lines changed

api/src/services/wordpress.service.ts

Lines changed: 64 additions & 58 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,45 +759,48 @@ 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,
797-
`${authorDetails.length} Authors exported successfully`,
803+
`${authorDetails?.length} Authors exported successfully`,
798804
{}
799805
)
800806
await customLogger(projectId, destinationStackId, 'info', message);
@@ -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(
@@ -1921,8 +1927,8 @@ async function featuredImageMapping(postid: string, post: any, postdata: any) {
19211927
})
19221928
.filter(Boolean); // Filter out undefined matches
19231929

1924-
if (assetsDetails.length > 0) {
1925-
postdata[postid]["featured_image"] = assetsDetails;
1930+
if (assetsDetails?.length > 0) {
1931+
postdata[postid]["featured_image"] = assetsDetails[0];
19261932
}
19271933
return postdata;
19281934
} catch (error) {
@@ -2025,18 +2031,18 @@ async function processChunkData(
20252031
);
20262032
const htmlDoc = dom.window.document.querySelector("body");
20272033
const jsonValue = htmlToJson(htmlDoc);
2028-
const postDate = new Date(data["wp:post_date_gmt"]).toISOString();
2034+
const postDate = new Date(data["wp:post_date_gmt"])?.toISOString();
20292035

2030-
const base = blog_base_url.split("/").filter(Boolean);
2031-
const blogname = base[base.length - 1];
2032-
const url = data["link"].split(blogname)[1];
2033-
const title = data["title"] ?? `Posts - ${data["wp:post_id"]}`;
2036+
const base = blog_base_url?.split("/")?.filter(Boolean);
2037+
const blogname = base[base?.length - 1];
2038+
const url = data["link"]?.split(blogname)[1];
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"]

ui/src/components/ContentMapper/index.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,21 @@ const Fields: MappingFields = {
175175
options: {
176176
'Dropdown':'dropdown'
177177
},
178-
type: 'enum',
178+
type: 'dropdown',
179179
},
180180
'radio': {
181181
label :'Select',
182182
options: {
183183
'Select':'select'
184184
},
185-
type: 'enum',
185+
type: 'radio',
186186
},
187187
'checkbox': {
188188
label:'Select',
189189
options: {
190190
'Select':'checkbox'
191191
},
192-
type:'boolean'
192+
type:'display_type'
193193
},
194194
'global_field':{
195195
label : 'Global',
@@ -1256,10 +1256,14 @@ const ContentMapper = forwardRef(({handleStepChange}: contentMapperProps, ref: R
12561256
return value?.data_type === 'isodate';
12571257
case 'json':
12581258
return value?.data_type === 'json';
1259-
case 'enum':
1260-
return 'enum' in value;
1261-
case 'display_type':
1259+
// case 'enum':
1260+
// return 'enum' in value;
1261+
case 'radio':
1262+
return value?.display_type === 'radio';
1263+
case 'dropdown':
12621264
return value?.display_type === 'dropdown';
1265+
case 'checkbox':
1266+
return value?.display_type === 'checkbox';
12631267
case 'allow_rich_text':
12641268
return value?.field_metadata?.allow_rich_text === true;
12651269
case 'Group':

ui/src/components/LegacyCms/Actions/LoadFileFormat.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ const LoadFileFormat = (props: LoadFileFormatProps) => {
6161
};
6262

6363
const getFileExtension = (filePath: string): string => {
64+
const normalizedPath = filePath?.replace(/\\/g, "/")?.replace(/\/$/, "");
65+
66+
// Use regex to extract the file extension
67+
const match = normalizedPath?.match(/\.([a-zA-Z0-9]+)$/);
68+
const ext = match ? match[1]?.toLowerCase() : "";
69+
6470
const fileName = filePath?.split('/')?.pop();
65-
const ext = fileName?.split('.')?.pop();
71+
//const ext = fileName?.split('.')?.pop();
6672
const validExtensionRegex = /\.(pdf|zip|xml|json)$/i;
6773
return ext && validExtensionRegex?.test(`.${ext}`) ? `${ext}` : '';
6874
};

ui/src/components/LegacyCms/Actions/LoadSelectCms.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const LoadSelectCms = (props: LoadSelectCmsProps) => {
144144
}
145145
};
146146
//await updateLegacyCMSData(selectedOrganisation.value, projectId, { legacy_cms: newSelectedCard?.cms_id });
147-
dispatch(updateNewMigrationData(newMigrationDataObj));
147+
//dispatch(updateNewMigrationData(newMigrationDataObj));
148148
props?.handleStepChange(props?.currentStep);
149149
}
150150

ui/src/components/LegacyCms/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const LegacyCMSComponent = forwardRef(({ legacyCMSData, isCompleted, handleOnAll
140140
}
141141

142142
//Make Step 2 complete
143-
if (!isEmptyString(selectedCmsData?.cms_id) && !isEmptyString(legacyCMSData?.affix)) {
143+
if (!isEmptyString(selectedCmsData?.cms_id) && (!isEmptyString(legacyCMSData?.affix) || !isEmptyString(newMigrationData?.legacy_cms?.affix))) {
144144
setInternalActiveStepIndex(1);
145145
}
146146

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22

3-
const contentTypes = require("./libs/contenttypes.js")
3+
const contentTypes = require('./libs/contenttypes.js');
44
// eslint-disable-next-line @typescript-eslint/no-var-requires
5-
const ExtractConfiguration = require("./libs/configuration.js")
5+
const ExtractConfiguration = require('./libs/configuration.js');
66
// eslint-disable-next-line @typescript-eslint/no-var-requires
7-
const reference = require("./libs/reference.js")
7+
const reference = require('./libs/reference.js');
88
// eslint-disable-next-line @typescript-eslint/no-var-requires
9-
const ExtractFiles = require("./libs/convert.js")
9+
const ExtractFiles = require('./libs/convert.js');
1010
// eslint-disable-next-line @typescript-eslint/no-var-requires
11-
const extractLocales = require("./libs/extractLocales.js")
11+
const extractLocales = require('./libs/extractLocales.js');
1212

13-
module.exports = {
13+
module.exports = {
1414
contentTypes,
1515
ExtractConfiguration,
1616
reference,
17-
ExtractFiles,
17+
ExtractFiles,
1818
extractLocales
19-
}
19+
};

upload-api/migration-sitecore/libs/extractLocales.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const extractLocales = (dir) => {
1111
for (const item of items) {
1212
const fullPath = path?.join?.(dir, item?.name);
1313

14-
if (item.isDirectory()) {
14+
if (item?.isDirectory()) {
1515
extractLocales?.(fullPath); // Proper recursion
1616
} else if (item?.isFile() && item?.name === "data.json") {
1717
try {

0 commit comments

Comments
 (0)