Skip to content

Commit 62048e7

Browse files
authored
Merge pull request #607 from contentstack/bugfix/cmg586-revisit
Bugfix/cmg586 revisit
2 parents 2833328 + 9b7b44d commit 62048e7

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

api/src/services/contentful.service.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function makeChunks(assetData: any) {
8484
return chunks;
8585
}
8686

87-
const mapLocales = ({ masterLocale, locale, locales }: any) => {
87+
const mapLocales = ({ masterLocale, locale, locales, isNull = false }: any) => {
8888
if (locales?.masterLocale?.[masterLocale ?? ''] === locale) {
8989
return Object?.keys(locales?.masterLocale)?.[0]
9090
}
@@ -93,7 +93,11 @@ const mapLocales = ({ masterLocale, locale, locales }: any) => {
9393
return key;
9494
}
9595
}
96-
return locale.toLowerCase();
96+
if (isNull !== true) {
97+
return locale?.toLowerCase?.();
98+
} else {
99+
return null;
100+
}
97101
}
98102

99103
const transformCloudinaryObject = (input: any) => {
@@ -842,7 +846,7 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
842846
for await (const [localeKey, localeValues] of Object.entries(
843847
values as { [key: string]: any }
844848
)) {
845-
const localeCode = mapLocales({ masterLocale: master_locale, locale: localeKey, locales: LocaleMapper });
849+
const localeCode = mapLocales({ masterLocale: master_locale, locale: localeKey, locales: LocaleMapper, isNull: true });
846850
const chunks = makeChunks(localeValues);
847851
for (const [entryKey, entryValue] of Object.entries(localeValues)) {
848852
const message = getLogMessage(
@@ -854,15 +858,18 @@ const createEntry = async (packagePath: any, destination_stack_id: string, proje
854858
}
855859
const refs: { [key: string]: any } = {};
856860
let chunkIndex = 1;
857-
const filePath = path.join(
858-
entriesSave,
859-
ctName, localeCode
860-
);
861-
for await (const [chunkId, chunkData] of Object.entries(chunks)) {
862-
refs[chunkIndex++] = `${chunkId}-entries.json`;
863-
await writeFile(filePath, `${chunkId}-entries.json`, chunkData);
861+
if (localeCode) {
862+
const filePath = path.join(
863+
entriesSave,
864+
ctName,
865+
localeCode
866+
);
867+
for await (const [chunkId, chunkData] of Object.entries(chunks)) {
868+
refs[chunkIndex++] = `${chunkId}-entries.json`;
869+
await writeFile(filePath, `${chunkId}-entries.json`, chunkData);
870+
}
871+
await writeFile(filePath, ENTRIES_MASTER_FILE, refs);
864872
}
865-
await writeFile(filePath, ENTRIES_MASTER_FILE, refs);
866873
}
867874
}
868875
} else {
@@ -957,7 +964,8 @@ const createLocale = async (packagePath: string, destination_stack_id: string, p
957964
)
958965
await customLogger(projectId, destination_stack_id, 'info', message);
959966
} else {
960-
if (project?.locales?.[localeData?.code]) {
967+
const newValueLocale = getKeyByValue(project?.locales, localeData?.code);
968+
if (newValueLocale) {
961969
allLocales[title] = newLocale;
962970
const message = getLogMessage(
963971
srcFunc,

api/src/services/migration.service.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,13 @@ const startTestMigration = async (req: Request): Promise<any> => {
254254
break;
255255
}
256256
case CMS.CONTENTFUL: {
257-
await contentfulService?.createLocale(file_path, project?.current_test_stack_id, projectId, project);
258-
await contentfulService?.createRefrence(file_path, project?.current_test_stack_id, projectId);
259-
await contentfulService?.createWebhooks(file_path, project?.current_test_stack_id, projectId);
260-
await contentfulService?.createEnvironment(file_path, project?.current_test_stack_id, projectId);
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, project?.stackDetails?.master_locale, project);
257+
const cleanLocalPath = file_path?.replace?.(/\/$/, '');
258+
await contentfulService?.createLocale(cleanLocalPath, project?.current_test_stack_id, projectId, project);
259+
await contentfulService?.createRefrence(cleanLocalPath, project?.current_test_stack_id, projectId);
260+
await contentfulService?.createWebhooks(cleanLocalPath, project?.current_test_stack_id, projectId);
261+
await contentfulService?.createEnvironment(cleanLocalPath, project?.current_test_stack_id, projectId);
262+
await contentfulService?.createAssets(cleanLocalPath, project?.current_test_stack_id, projectId, true);
263+
await contentfulService?.createEntry(cleanLocalPath, project?.current_test_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale, project);
263264
await contentfulService?.createVersionFile(project?.current_test_stack_id, projectId);
264265
break;
265266
}
@@ -330,12 +331,13 @@ const startMigration = async (req: Request): Promise<any> => {
330331
break;
331332
}
332333
case CMS.CONTENTFUL: {
333-
await contentfulService?.createLocale(file_path, project?.destination_stack_id, projectId, project);
334-
await contentfulService?.createRefrence(file_path, project?.destination_stack_id, projectId);
335-
await contentfulService?.createWebhooks(file_path, project?.destination_stack_id, projectId);
336-
await contentfulService?.createEnvironment(file_path, project?.destination_stack_id, projectId);
337-
await contentfulService?.createAssets(file_path, project?.destination_stack_id, projectId);
338-
await contentfulService?.createEntry(file_path, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale, project);
334+
const cleanLocalPath = file_path?.replace?.(/\/$/, '');
335+
await contentfulService?.createLocale(cleanLocalPath, project?.destination_stack_id, projectId, project);
336+
await contentfulService?.createRefrence(cleanLocalPath, project?.destination_stack_id, projectId);
337+
await contentfulService?.createWebhooks(cleanLocalPath, project?.destination_stack_id, projectId);
338+
await contentfulService?.createEnvironment(cleanLocalPath, project?.destination_stack_id, projectId);
339+
await contentfulService?.createAssets(cleanLocalPath, project?.destination_stack_id, projectId);
340+
await contentfulService?.createEntry(cleanLocalPath, project?.destination_stack_id, projectId, contentTypes, project?.mapperKeys, project?.stackDetails?.master_locale, project);
339341
await contentfulService?.createVersionFile(project?.destination_stack_id, projectId);
340342
break;
341343
}
@@ -419,7 +421,6 @@ export const createSourceLocales = async (req: Request) => {
419421

420422
const projectId = req?.params?.projectId;
421423
const locales = req?.body?.locale;
422-
console.info("🚀 ~ createSourceLocales ~ locales:", locales);
423424

424425
try {
425426
// Find the project with the specified projectId

upload-api/src/services/contentful/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ const createContentfulMapper = async (
1515
) => {
1616
try {
1717
const { localPath } = config;
18-
const fetchedLocales: [] = await extractLocale(localPath);
18+
const cleanLocalPath = localPath?.replace?.(/\/$/, '');
19+
const fetchedLocales: [] = await extractLocale(cleanLocalPath);
1920

20-
await extractContentTypes(localPath, affix);
21+
await extractContentTypes(cleanLocalPath, affix);
2122
const initialMapper = await createInitialMapper();
2223
const req = {
2324
method: 'post',
@@ -29,7 +30,7 @@ const createContentfulMapper = async (
2930
},
3031
data: JSON.stringify(initialMapper)
3132
};
32-
const {data, status} = await axios.request(req);
33+
const { data, status } = await axios.request(req);
3334
if (data?.data?.content_mapper?.length) {
3435
logger.info('Validation success:', {
3536
status: HTTP_CODES?.OK,

0 commit comments

Comments
 (0)