Skip to content

Commit d5cb3b9

Browse files
committed
fixed json rte
1 parent a8669f3 commit d5cb3b9

File tree

4 files changed

+58
-45
lines changed

4 files changed

+58
-45
lines changed

api/src/services/contentful.service.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ const saveAsset = async (
552552
*
553553
* @throws Will log errors encountered during file reading, writing, or asset processing.
554554
*/
555-
const createAssets = async (packagePath: any, destination_stack_id: string, projectId: string,) => {
555+
const createAssets = async (packagePath: any, destination_stack_id: string, projectId: string, isTest = false) => {
556556
const srcFunc = 'createAssets';
557557
try {
558558
const assetsSave = path?.join?.(DATA, destination_stack_id, ASSETS_DIR_NAME);
@@ -561,9 +561,12 @@ const createAssets = async (packagePath: any, destination_stack_id: string, proj
561561
const assetData: any = {};
562562
const metadata: AssetMetaData[] = [];
563563
const fileMeta = { "1": ASSETS_SCHEMA_FILE };
564-
const assets = JSON.parse(data)?.assets;
564+
let assets = JSON.parse(data)?.assets;
565565

566566
if (assets && assets.length > 0) {
567+
if (isTest) {
568+
assets = assets?.slice(0, 10);
569+
}
567570
const limit = pLimit(10); // Limit concurrent operations to 10
568571
const tasks = assets.map((asset: any) =>
569572
limit(() => saveAsset(asset, failedJSON, assetData, metadata, projectId, destination_stack_id, 0))
@@ -708,7 +711,7 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
708711
*
709712
* @throws Will log errors encountered during file reading, processing, or writing of entries.
710713
*/
711-
const createEntry = async (packagePath: any, destination_stack_id: string, projectId: string, contentTypes: any): Promise<void> => {
714+
const createEntry = async (packagePath: any, destination_stack_id: string, projectId: string, contentTypes: any, mapperKeys: any): Promise<void> => {
712715
const srcFunc = 'createEntry';
713716
try {
714717
const entriesSave = path.join(DATA, destination_stack_id, ENTRIES_DIR_NAME);
@@ -721,7 +724,6 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
721724
if (entries && entries.length > 0) {
722725
const assetId = await readFile(assetsSave, ASSETS_SCHEMA_FILE) ?? [];
723726
const entryId = await readFile(path.join(DATA, destination_stack_id, REFERENCES_DIR_NAME), REFERENCES_FILE_NAME);
724-
console.info("🚀 ~ createEntry ~ entryId:", destination_stack_id)
725727
const environmentsId = await readFile(environmentSave, ENVIRONMENTS_FILE_NAME);
726728
const displayField: { [key: string]: any } = {}
727729
content.map((item: any) => {
@@ -768,7 +770,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
768770
});
769771
const pathName = getDisplayName(name, displayField);
770772
locales.forEach((locale) => {
771-
const publishDetails = Object.values(environmentsId)
773+
const publishDetails = Object?.values?.(environmentsId)
772774
.filter((env: any) => env?.name === environment_id)
773775
?.map((env: any) => ({
774776
environment: env?.uid,
@@ -778,15 +780,15 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
778780

779781
const title = entryData[name][locale][id][pathName] || "";
780782
const urlTitle = title
781-
.replace(/[^a-zA-Z0-9]+/g, "-")
782-
.toLowerCase();
783+
?.replace?.(/[^a-zA-Z0-9]+/g, "-")
784+
?.toLowerCase?.();
783785
entryData[name][locale][id] = {
784-
title: title.trim() === "" ? urlTitle || id : title,
786+
...entryData[name][locale][id],
787+
title: title?.trim?.() === "" ? (urlTitle || id) : title,
785788
uid: id,
786-
url: `/${name.toLowerCase()}/${urlTitle}`,
787-
locale: locale.toLowerCase(),
789+
url: `/${name?.toLowerCase?.()}/${urlTitle}`,
790+
locale: locale?.toLowerCase?.(),
788791
publish_details: publishDetails,
789-
...entryData[name][locale][id],
790792
};
791793
// Format object keys to snake_case
792794
Object.entries(entryData[name][locale][id]).forEach(
@@ -808,15 +810,18 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
808810
);
809811
const writePromises = [];
810812

811-
for (const [key, values] of Object.entries(result)) {
813+
for (const [newKey, values] of Object.entries(result)) {
814+
const currentCT = contentTypes?.find((ct: any) => ct?.otherCmsUid === newKey);
815+
const ctName = currentCT?.contentstackUid in mapperKeys ?
816+
mapperKeys?.[currentCT?.contentstackUid] : (currentCT?.contentstackUid ?? newKey.replace(/([A-Z])/g, "_$1").toLowerCase());
812817
for (const [localeKey, localeValues] of Object.entries(
813818
values as { [key: string]: any }
814819
)) {
815820
const chunks = await makeChunks(localeValues);
816821
for (const [entryKey, entryValue] of Object.entries(localeValues)) {
817822
const message = getLogMessage(
818823
srcFunc,
819-
`Entry title "${(entryValue as { title: string })?.title}"(${key}) in the ${localeKey} locale has been successfully transformed.`,
824+
`Entry title "${(entryValue as { title: string })?.title}"(${ctName}) in the ${localeKey} locale has been successfully transformed.`,
820825
{}
821826
);
822827
await customLogger(projectId, destination_stack_id, "info", message);
@@ -825,7 +830,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
825830
let chunkIndex = 1;
826831
const filePath = path.join(
827832
entriesSave,
828-
key.replace(/([A-Z])/g, "_$1").toLowerCase(), localeKey.toLowerCase()
833+
ctName, localeKey.toLowerCase()
829834
);
830835
for (const [chunkId, chunkData] of Object.entries(chunks)) {
831836
refs[chunkIndex++] = `${chunkId}-entries.json`;

api/src/services/contentful/jsonRTE.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const {
1414
RTE_REFERENCES_FILE_NAME,
1515

1616
} = MIGRATION_DATA_CONFIG;
17-
1817
type NodeType = string;
1918
type LangType = string;
2019
type StackId = string;
@@ -113,7 +112,7 @@ function parseTableRow(obj: any, lang?: LangType, destination_stack_id?: StackId
113112
return parsers.get(e.nodeType)?.(e, lang, destination_stack_id);
114113
}).filter(Boolean);
115114

116-
const type = types.has('table-header-cell') ? 'thead' : '';
115+
const type = types.has('table-header-cell') ? 'thead' : 'th';
117116
return children.length ? { type, attrs: {}, uid: generateUID('tabletype'), children } : null;
118117
}
119118

@@ -224,7 +223,7 @@ function parseBlockReference(obj: any, lang?: LangType, destination_stack_id?: S
224223

225224
if (masterLocale === lang || lang) {
226225
for (const [arrayKey, arrayValue] of Object.entries(entryId)) {
227-
if (arrayValue[obj.data.target.sys.id] && lang === arrayKey) {
226+
if (arrayValue?.[obj?.data?.target?.sys?.id]?._content_type_uid && lang === arrayKey) {
228227
return {
229228
type: 'reference',
230229
attrs: {
@@ -242,7 +241,7 @@ function parseBlockReference(obj: any, lang?: LangType, destination_stack_id?: S
242241
}
243242
}
244243
return {
245-
type: 'reference',
244+
type: 'p',
246245
attrs: {},
247246
uid: generateUID('reference'),
248247
children: [{ text: '' }],
@@ -255,23 +254,25 @@ function parseInlineReference(obj: any, lang?: LangType, destination_stack_id?:
255254

256255
if (entry) {
257256
const [arrayKey, arrayValue] = entry;
258-
return {
259-
type: 'reference',
260-
attrs: {
261-
'display-type': 'block',
262-
type: 'entry',
263-
'class-name': 'embedded-entry redactor-component block-entry',
264-
'entry-uid': obj.data.target.sys.id,
265-
locale: arrayKey,
266-
'content-type-uid': arrayValue._content_type_uid,
267-
},
268-
uid: generateUID('reference'),
269-
children: [{ text: '' }],
270-
};
257+
if (arrayValue?.[obj?.data?.target?.sys?.id]?._content_type_uid && arrayKey) {
258+
return {
259+
type: 'reference',
260+
attrs: {
261+
'display-type': 'block',
262+
type: 'entry',
263+
'class-name': 'embedded-entry redactor-component block-entry',
264+
'entry-uid': obj.data.target.sys.id,
265+
locale: arrayKey,
266+
'content-type-uid': arrayValue?.[obj?.data?.target?.sys?.id]?._content_type_uid,
267+
},
268+
uid: generateUID('reference'),
269+
children: [{ text: '' }],
270+
};
271+
}
271272
}
272273

273274
return {
274-
type: 'reference',
275+
type: 'p',
275276
attrs: {},
276277
uid: generateUID('reference'),
277278
children: [{ text: '' }],
@@ -281,26 +282,32 @@ function parseInlineReference(obj: any, lang?: LangType, destination_stack_id?:
281282
function parseBlockAsset(obj: any, lang?: LangType, destination_stack_id?: StackId): any {
282283
const assetId = destination_stack_id && readFile(path.join(process.cwd(), DATA, destination_stack_id, ASSETS_DIR_NAME, ASSETS_SCHEMA_FILE));
283284
const asset = assetId?.[obj?.data?.target?.sys?.id];
284-
// const attrs: any = {};
285285

286286
if (asset) {
287287
return {
288288
type: 'reference',
289289
attrs: {
290290
'display-type': 'download',
291+
"type": "asset",
291292
'asset-uid': obj.data.target.sys.id,
292293
'class-name': 'embedded-asset redactor-component block-asset',
293-
title: asset.title,
294-
filename: asset.fileName,
295-
locale: asset.locale,
294+
"asset-type": asset?.content_type,
295+
title: asset?.title,
296+
filename: asset?.fileName,
297+
locale: asset?.locale,
298+
"content-type-uid": "sys_assets",
299+
"asset-link": asset?.url,
300+
"asset-name": asset?.title,
301+
"alt": "",
302+
"asset-alt": "",
296303
},
297304
uid: generateUID('reference'),
298305
children: [{ text: '' }],
299306
};
300307
}
301308

302309
return {
303-
type: 'reference',
310+
type: 'p',
304311
attrs: {},
305312
uid: generateUID('reference'),
306313
children: [{ text: '' }],

api/src/services/migration.service.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,16 @@ const startTestMigration = async (req: Request): Promise<any> => {
258258
await contentfulService?.createRefrence(file_path, project?.current_test_stack_id, projectId);
259259
await contentfulService?.createWebhooks(file_path, project?.current_test_stack_id, projectId);
260260
await contentfulService?.createEnvironment(file_path, project?.current_test_stack_id, projectId);
261-
await contentfulService?.createAssets(file_path, project?.current_test_stack_id, projectId);
262-
await contentfulService?.createEntry(file_path, project?.current_test_stack_id, projectId, contentTypes);
261+
await contentfulService?.createAssets(file_path, project?.current_test_stack_id, projectId, true);
262+
await contentfulService?.createEntry(file_path, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys);
263263
await contentfulService?.createVersionFile(project?.current_test_stack_id, projectId);
264264
break;
265265
}
266266
default:
267267
break;
268268
}
269-
// await testFolderCreator?.({ destinationStackId: project?.current_test_stack_id });
270-
// await utilsCli?.runCli(region, user_id, project?.current_test_stack_id, projectId, true, loggerPath);
269+
await testFolderCreator?.({ destinationStackId: project?.current_test_stack_id });
270+
await utilsCli?.runCli(region, user_id, project?.current_test_stack_id, projectId, true, loggerPath);
271271
}
272272
}
273273

@@ -299,7 +299,8 @@ const startMigration = async (req: Request): Promise<any> => {
299299
await customLogger(projectId, project?.destination_stack_id, 'info', message);
300300
await setLogFilePath(loggerPath);
301301
const contentTypes = await fieldAttacher({ orgId, projectId, destinationStackId: project?.destination_stack_id, region, user_id });
302-
302+
await marketPlaceAppService?.createAppManifest({ orgId, destinationStackId: project?.current_test_stack_id, region, userId: user_id });
303+
await extensionService?.createExtension({ destinationStackId: project?.current_test_stack_id });
303304
switch (cms) {
304305
case CMS.SITECORE_V8:
305306
case CMS.SITECORE_V9:
@@ -336,7 +337,7 @@ const startMigration = async (req: Request): Promise<any> => {
336337
await contentfulService?.createWebhooks(file_path, project?.destination_stack_id, projectId);
337338
await contentfulService?.createEnvironment(file_path, project?.destination_stack_id, projectId);
338339
await contentfulService?.createAssets(file_path, project?.destination_stack_id, projectId);
339-
await contentfulService?.createEntry(file_path, project?.destination_stack_id, projectId);
340+
await contentfulService?.createEntry(file_path, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys);
340341
await contentfulService?.createVersionFile(project?.destination_stack_id, projectId);
341342
break;
342343
}

api/src/utils/test-folder-creator.utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ export const testFolderCreator = async ({ destinationStackId }: any) => {
276276
}
277277
}
278278
}
279-
const sortData = allData?.length > 3 ? allData.sort((a, b) => b?.count - a?.count).slice?.(1, 4) : allData;
279+
const sortData = allData?.length > 3 ? allData.sort((a, b) => b?.count - a?.count).slice?.(0, 3) : allData;
280280
const finalData: any = [];
281281
sortData.forEach((et: any) => {
282282
const entryObj: any = {};
283-
const ctData = et?.count > 4 ? Object?.values?.(et?.entryData)?.splice?.(0, 5) : et?.entryData;
283+
const ctData = et?.count > 4 ? Object?.values?.(et?.entryData)?.splice?.(0, 5) : Object?.values?.(et?.entryData);
284284
ctData?.forEach?.((entItem: any) => {
285285
entryObj[entItem?.uid] = entItem;
286286
})

0 commit comments

Comments
 (0)