Skip to content

Commit ec4c7e3

Browse files
authored
Merge pull request #503 from contentstack/feature/CMG-485-entries
fixed test migartion
2 parents 9f82172 + a2e06a4 commit ec4c7e3

File tree

5 files changed

+58
-45
lines changed

5 files changed

+58
-45
lines changed

api/src/services/contentful.service.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { JSDOM } from "jsdom";
1010
import { jsonToHtml, jsonToMarkdown, htmlToJson } from '@contentstack/json-rte-serializer';
1111

1212

13-
import { CHUNK_SIZE, MIGRATION_DATA_CONFIG } from "../constants/index.js";
13+
import { CHUNK_SIZE, MIGRATION_DATA_CONFIG, LOCALE_MAPPER } from "../constants/index.js";
1414
import { Locale } from "../models/types.js";
1515
import jsonRTE from "./contentful/jsonRTE.js";
1616
import { getAllLocales, getLogMessage } from "../utils/index.js";
@@ -234,7 +234,7 @@ const processField = (
234234
return assetsData;
235235
} else {
236236
const id = lang_value?.sys?.id;
237-
if (id in assetId) {
237+
if (assetId && id in assetId) {
238238
return assetId?.[id]
239239
}
240240
return null;
@@ -253,7 +253,7 @@ const processField = (
253253
return refs;
254254
}
255255
const id = lang_value?.sys?.id;
256-
if (id in entryId) return [[entryId?.[id]]];
256+
if (entryId && id in entryId) return [[entryId?.[id]]];
257257
return null;
258258
}
259259

@@ -437,11 +437,11 @@ const saveAsset = async (
437437
const publishDetails: { environment: any; version: number; locale: any }[] =
438438
[];
439439
const assetsSave = path.join(DATA, destination_stack_id, ASSETS_DIR_NAME);
440-
const environmentsId = await readFile(path.join(DATA, destination_stack_id, ENVIRONMENTS_DIR_NAME), ENVIRONMENTS_FILE_NAME);
440+
const environmentsId = await readFile(path.join(DATA, destination_stack_id, ENVIRONMENTS_DIR_NAME), ENVIRONMENTS_FILE_NAME) ?? {};
441441
const localeId = await readFile(path.join(DATA, destination_stack_id, LOCALE_DIR_NAME), LOCALE_CF_LANGUAGE);
442442

443443
if (assets.fields.file && assets.fields.title) {
444-
Object.values(environmentsId).forEach((env: any) => {
444+
Object?.values?.(environmentsId)?.length && Object.values(environmentsId).forEach((env: any) => {
445445
if (env?.name === assets?.sys?.environment?.sys?.id) {
446446
Object.values(localeId).forEach((locale: any) => {
447447
publishDetails.push({
@@ -697,6 +697,18 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
697697
}
698698
};
699699

700+
const mapLocales = ({ masterLocale, locale, locales }: any) => {
701+
if (locales?.masterLocale?.[masterLocale ?? ''] === locale) {
702+
return Object?.keys(locales?.masterLocale)?.[0]
703+
}
704+
for (const [key, value] of Object?.entries?.(locales) ?? {}) {
705+
if (typeof value !== 'object' && value === locale) {
706+
return key;
707+
}
708+
}
709+
return locale.toLowerCase();
710+
}
711+
700712
/**
701713
* Creates and processes entries from a given package file and saves them to the destination stack directory.
702714
*
@@ -723,7 +735,8 @@ const createEnvironment = async (packagePath: any, destination_stack_id: string,
723735
*
724736
* @throws Will log errors encountered during file reading, processing, or writing of entries.
725737
*/
726-
const createEntry = async (packagePath: any, destination_stack_id: string, projectId: string, contentTypes: any, mapperKeys: any): Promise<void> => {
738+
const createEntry = async (packagePath: any, destination_stack_id: string, projectId: string, contentTypes: any, mapperKeys: any, master_locale: string): Promise<void> => {
739+
console.info("🚀 ~ createEntry ~ master_locale:", master_locale)
727740
const srcFunc = 'createEntry';
728741
try {
729742
const entriesSave = path.join(DATA, destination_stack_id, ENTRIES_DIR_NAME);
@@ -736,7 +749,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
736749
if (entries && entries.length > 0) {
737750
const assetId = await readFile(assetsSave, ASSETS_SCHEMA_FILE) ?? [];
738751
const entryId = await readFile(path.join(DATA, destination_stack_id, REFERENCES_DIR_NAME), REFERENCES_FILE_NAME);
739-
const environmentsId = await readFile(environmentSave, ENVIRONMENTS_FILE_NAME);
752+
const environmentsId = await readFile(environmentSave, ENVIRONMENTS_FILE_NAME) ?? {};
740753
const displayField: { [key: string]: any } = {}
741754
content.map((item: any) => {
742755
displayField[item.name.toLowerCase().replace(/[^a-z0-9]+/g, "_")] =
@@ -782,13 +795,15 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
782795
});
783796
const pathName = getDisplayName(name, displayField);
784797
locales.forEach((locale) => {
785-
const publishDetails = Object?.values?.(environmentsId)
798+
const localeCode = mapLocales({ masterLocale: master_locale, locale, locales: LOCALE_MAPPER });
799+
console.info("🚀 ~ locales.forEach ~ localeCode:", localeCode);
800+
const publishDetails = Object?.values?.(environmentsId)?.length ? Object?.values?.(environmentsId)
786801
.filter((env: any) => env?.name === environment_id)
787802
?.map((env: any) => ({
788803
environment: env?.uid,
789804
version: 1,
790-
locale: locale.toLowerCase(),
791-
}));
805+
locale: locale?.toLowerCase(),
806+
})) : [];
792807

793808
const title = entryData[name][locale][id][pathName] || "";
794809
const urlTitle = title
@@ -815,21 +830,18 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
815830
);
816831
});
817832
});
818-
819833
return entryData;
820834
},
821835
{}
822836
);
823-
const writePromises = [];
824-
825-
for (const [newKey, values] of Object.entries(result)) {
837+
for await (const [newKey, values] of Object.entries(result)) {
826838
const currentCT = contentTypes?.find((ct: any) => ct?.otherCmsUid === newKey);
827839
const ctName = currentCT?.contentstackUid in mapperKeys ?
828840
mapperKeys?.[currentCT?.contentstackUid] : (currentCT?.contentstackUid ?? newKey.replace(/([A-Z])/g, "_$1").toLowerCase());
829-
for (const [localeKey, localeValues] of Object.entries(
841+
for await (const [localeKey, localeValues] of Object.entries(
830842
values as { [key: string]: any }
831843
)) {
832-
const chunks = await makeChunks(localeValues);
844+
const chunks = makeChunks(localeValues);
833845
for (const [entryKey, entryValue] of Object.entries(localeValues)) {
834846
const message = getLogMessage(
835847
srcFunc,
@@ -844,14 +856,13 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
844856
entriesSave,
845857
ctName, localeKey.toLowerCase()
846858
);
847-
for (const [chunkId, chunkData] of Object.entries(chunks)) {
859+
for await (const [chunkId, chunkData] of Object.entries(chunks)) {
848860
refs[chunkIndex++] = `${chunkId}-entries.json`;
849-
writePromises.push(writeFile(filePath, `${chunkId}-entries.json`, chunkData))
861+
await writeFile(filePath, `${chunkId}-entries.json`, chunkData)
850862
}
851-
writePromises.push(writeFile(filePath, ENTRIES_MASTER_FILE, refs));
863+
await writeFile(filePath, ENTRIES_MASTER_FILE, refs);
852864
}
853865
}
854-
await Promise.all(writePromises);
855866
} else {
856867
const message = getLogMessage(
857868
srcFunc,

api/src/services/contentful/jsonRTE.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function parseBlockReference(obj: any, lang?: LangType, destination_stack_id?: S
250250

251251
function parseInlineReference(obj: any, lang?: LangType, destination_stack_id?: StackId): any {
252252
const entryId: { [key: string]: any } = destination_stack_id && readFile(path.join(process.cwd(), DATA, destination_stack_id, RTE_REFERENCES_DIR_NAME, RTE_REFERENCES_FILE_NAME));
253-
const entry = Object.entries(entryId).find(([arrayKey, arrayValue]) => arrayKey === lang && arrayValue[obj.data.target.sys.id]);
253+
const entry = entryId && Object.entries(entryId).find(([arrayKey, arrayValue]) => arrayKey === lang && arrayValue[obj.data.target.sys.id]);
254254

255255
if (entry) {
256256
const [arrayKey, arrayValue] = entry;

api/src/services/migration.service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { extensionService } from "./extension.service.js";
2525

2626

2727

28+
2829
/**
2930
* Creates a test stack.
3031
*
@@ -259,7 +260,7 @@ const startTestMigration = async (req: Request): Promise<any> => {
259260
await contentfulService?.createWebhooks(file_path, project?.current_test_stack_id, projectId);
260261
await contentfulService?.createEnvironment(file_path, project?.current_test_stack_id, projectId);
261262
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);
263+
await contentfulService?.createEntry(file_path, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale);
263264
await contentfulService?.createVersionFile(project?.current_test_stack_id, projectId);
264265
break;
265266
}
@@ -299,8 +300,8 @@ const startMigration = async (req: Request): Promise<any> => {
299300
await customLogger(projectId, project?.destination_stack_id, 'info', message);
300301
await setLogFilePath(loggerPath);
301302
const contentTypes = await fieldAttacher({ orgId, projectId, destinationStackId: project?.destination_stack_id, region, user_id });
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 });
303+
await marketPlaceAppService?.createAppManifest({ orgId, destinationStackId: project?.destination_stack_id, region, userId: user_id });
304+
await extensionService?.createExtension({ destinationStackId: project?.destination_stack_id });
304305
switch (cms) {
305306
case CMS.SITECORE_V8:
306307
case CMS.SITECORE_V9:
@@ -337,7 +338,7 @@ const startMigration = async (req: Request): Promise<any> => {
337338
await contentfulService?.createWebhooks(file_path, project?.destination_stack_id, projectId);
338339
await contentfulService?.createEnvironment(file_path, project?.destination_stack_id, projectId);
339340
await contentfulService?.createAssets(file_path, project?.destination_stack_id, projectId);
340-
await contentfulService?.createEntry(file_path, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys);
341+
await contentfulService?.createEntry(file_path, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale);
341342
await contentfulService?.createVersionFile(project?.destination_stack_id, projectId);
342343
break;
343344
}

api/src/utils/content-type-creator.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
748748
if (currentCt?.uid) {
749749
ct = await mergeTwoCts(ct, currentCt);
750750
}
751-
if (ct?.uid) {
751+
if (ct?.uid && ct?.schema?.length) {
752752
if (contentType?.type === 'global_field') {
753753
const globalSave = path.join(MIGRATION_DATA_CONFIG.DATA, destinationStackId, GLOBAL_FIELDS_DIR_NAME);
754754
const message = getLogMessage(srcFunc, `Global Field ${ct?.uid} has been successfully Transformed.`, {});

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
ENTRIES_DIR_NAME,
99
ASSETS_DIR_NAME,
1010
ASSETS_SCHEMA_FILE,
11-
CONTENT_TYPES_DIR_NAME,
1211
CONTENT_TYPES_SCHEMA_FILE,
1312
ENTRIES_MASTER_FILE,
1413
GLOBAL_FIELDS_DIR_NAME,
@@ -240,22 +239,24 @@ const sortGlobalField = async (baseDir: string, finalData: any) => {
240239
}
241240
}
242241

243-
const sortContentType = async (baseDir: string, finalData: any) => {
244-
const contentTypePath: string = path.join(process.cwd(), baseDir, CONTENT_TYPES_DIR_NAME);
245-
const contentSave = path.join(baseDir, CONTENT_TYPES_DIR_NAME);
246-
const ctData = await JSON.parse(await fs.promises.readFile(path.join(contentTypePath, CONTENT_TYPES_SCHEMA_FILE), 'utf8'));
247-
await sortGlobalField(baseDir, finalData);
248-
const contentTypes: any = [];
249-
for await (const ct of finalData) {
250-
const findCtData = ctData?.find((ele: any) => ele?.uid === ct?.contentType);
251-
await lookForReference(findCtData, finalData);
252-
contentTypes?.push(findCtData);
253-
}
254-
await deleteFolderAsync(contentTypePath);
255-
for await (const ctItem of contentTypes) {
256-
await saveContent(ctItem, contentSave);
257-
}
258-
}
242+
//this code can be used in feature
243+
244+
// const sortContentType = async (baseDir: string, finalData: any) => {
245+
// const contentTypePath: string = path.join(process.cwd(), baseDir, CONTENT_TYPES_DIR_NAME);
246+
// const contentSave = path.join(baseDir, CONTENT_TYPES_DIR_NAME);
247+
// const ctData = await JSON.parse(await fs.promises.readFile(path.join(contentTypePath, CONTENT_TYPES_SCHEMA_FILE), 'utf8'));
248+
// await sortGlobalField(baseDir, finalData);
249+
// const contentTypes: any = [];
250+
// for await (const ct of finalData) {
251+
// const findCtData = ctData?.find((ele: any) => ele?.uid === ct?.contentType);
252+
// await lookForReference(findCtData, finalData);
253+
// contentTypes?.push(findCtData);
254+
// }
255+
// await deleteFolderAsync(contentTypePath);
256+
// for await (const ctItem of contentTypes) {
257+
// await saveContent(ctItem, contentSave);
258+
// }
259+
// }
259260

260261

261262

@@ -275,7 +276,7 @@ export const testFolderCreator = async ({ destinationStackId }: any) => {
275276
const pathParts = normalizedPath.split(path.sep);
276277
const ct = pathParts?.[0]; // First directory
277278
const locale = pathParts?.[1]; // Second directory
278-
allData?.push({ contentType: ct, count: Object?.keys?.(entryData)?.length, entryData, filePath, locale })
279+
allData?.push({ contentType: ct, count: Object?.keys?.(entryData)?.length ?? 0, entryData, filePath, locale })
279280
}
280281
}
281282
}
@@ -292,7 +293,7 @@ export const testFolderCreator = async ({ destinationStackId }: any) => {
292293
finalData?.push({ contentType: et?.contentType, entryObj, locale: et?.locale });
293294
});
294295
await sortAssets(baseDir);
295-
await sortContentType(baseDir, finalData);
296+
// await sortContentType(baseDir, finalData);
296297
await deleteFolderAsync(entryDelete);
297298
for await (const entry of finalData) {
298299
const fileMeta = { "1": `${entry?.locale}.json` };

0 commit comments

Comments
 (0)