Skip to content

Commit c96f44c

Browse files
Merge pull request #549 from contentstack/feature/locale-mapper-fix
added mapper
2 parents 0de055b + a1e9fa1 commit c96f44c

File tree

4 files changed

+69
-56
lines changed

4 files changed

+69
-56
lines changed

api/src/services/migration.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ const startTestMigration = async (req: Request): Promise<any> => {
230230
case CMS.SITECORE_V9:
231231
case CMS.SITECORE_V10: {
232232
if (packagePath) {
233-
await siteCoreService?.createEntry({ packagePath, contentTypes, master_locale: project?.stackDetails?.master_locale, destinationStackId: project?.current_test_stack_id, projectId, keyMapper: project?.mapperKeys });
234-
await siteCoreService?.createLocale(req, project?.current_test_stack_id, projectId);
233+
await siteCoreService?.createEntry({ packagePath, contentTypes, master_locale: project?.stackDetails?.master_locale, destinationStackId: project?.current_test_stack_id, projectId, keyMapper: project?.mapperKey, project });
234+
await siteCoreService?.createLocale(req, project?.current_test_stack_id, projectId, project);
235235
await siteCoreService?.createVersionFile(project?.current_test_stack_id);
236236
}
237237
break;
@@ -306,8 +306,8 @@ const startMigration = async (req: Request): Promise<any> => {
306306
case CMS.SITECORE_V9:
307307
case CMS.SITECORE_V10: {
308308
if (packagePath) {
309-
await siteCoreService?.createEntry({ packagePath, contentTypes, master_locale: project?.stackDetails?.master_locale, destinationStackId: project?.destination_stack_id, projectId, keyMapper: project?.mapperKeys });
310-
await siteCoreService?.createLocale(req, project?.destination_stack_id, projectId);
309+
await siteCoreService?.createEntry({ packagePath, contentTypes, master_locale: project?.stackDetails?.master_locale, destinationStackId: project?.destination_stack_id, projectId, keyMapper: project?.mapperKeys, project });
310+
await siteCoreService?.createLocale(req, project?.destination_stack_id, projectId, project);
311311
await siteCoreService?.createVersionFile(project?.destination_stack_id);
312312
}
313313
break;

api/src/services/sitecore.service.ts

Lines changed: 63 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ const AssetsPathSpliter = ({ path, id }: any) => {
4949
return newPath;
5050
}
5151

52+
const mapLocales = ({ masterLocale, locale, locales }: any) => {
53+
if (locales?.masterLocale?.[masterLocale ?? ''] === locale) {
54+
return Object?.keys(locales?.masterLocale)?.[0]
55+
}
56+
for (const [key, value] of Object?.entries?.(locales) ?? {}) {
57+
if (typeof value !== 'object' && value === locale) {
58+
return key;
59+
}
60+
}
61+
return locale?.toLowerCase?.();
62+
}
63+
5264

5365

5466
async function writeOneFile(indexPath: string, fileMeta: any) {
@@ -194,7 +206,7 @@ const cretaeAssets = async ({ packagePath, baseDir, destinationStackId, projectI
194206
return allAssetJSON;
195207
}
196208

197-
const createEntry = async ({ packagePath, contentTypes, master_locale, destinationStackId, projectId, keyMapper }: { packagePath: any; contentTypes: any; master_locale?: string, destinationStackId: string, projectId: string, keyMapper: any }) => {
209+
const createEntry = async ({ packagePath, contentTypes, master_locale, destinationStackId, projectId, keyMapper, project }: { packagePath: any; contentTypes: any; master_locale?: string, destinationStackId: string, projectId: string, keyMapper: any, project: any }) => {
198210
try {
199211
const srcFunc = 'createEntry';
200212
const baseDir = path.join(baseDirName, destinationStackId);
@@ -215,7 +227,11 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
215227
const templateIndex = entriesData?.findIndex((ele: any) => ele?.template === template);
216228
if (templateIndex >= 0) {
217229
const entry = entriesData?.[templateIndex]?.locale?.[language];
218-
entry[id] = { meta: jsonData?.item?.$, fields: jsonData?.item?.fields };
230+
if (entry !== undefined) {
231+
entry[id] = { meta: jsonData?.item?.$, fields: jsonData?.item?.fields };
232+
} else {
233+
entriesData[templateIndex].locale[language] = entries;
234+
}
219235
} else {
220236
const locale: any = {};
221237
locale[language] = entries;
@@ -227,61 +243,59 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
227243
for await (const ctType of contentTypes) {
228244
const message = getLogMessage(
229245
srcFunc,
230-
`Transforming entries of Content Type ${keyMapper[ctType?.contentstackUid] ?? ctType?.contentstackUid} has begun.`,
246+
`Transforming entries of Content Type ${keyMapper?.[ctType?.contentstackUid] ?? ctType?.contentstackUid} has begun.`,
231247
{}
232248
)
233249
await customLogger(projectId, destinationStackId, 'info', message);
234250
const entryPresent: any = entriesData?.find((item: any) => uidCorrector({ uid: item?.template }) === ctType?.contentstackUid)
235251
if (entryPresent) {
236252
const locales: any = Object?.keys(entryPresent?.locale);
253+
const allLocales: any = { masterLocale: project?.master_locale ?? LOCALE_MAPPER?.masterLocale, ...project?.locales ?? {} }
237254
for await (const locale of locales) {
238-
let newLocale = locale;
255+
const newLocale = mapLocales({ masterLocale: master_locale, locale, locales: allLocales });
239256
const entryLocale: any = {};
240-
if (typeof LOCALE_MAPPER?.masterLocale === 'object' && LOCALE_MAPPER?.masterLocale !== null && LOCALE_MAPPER?.masterLocale?.[master_locale ?? ''] === locale) {
241-
newLocale = Object?.keys(LOCALE_MAPPER?.masterLocale)?.[0];
242-
Object.entries(entryPresent?.locale?.[locale] || {}).map(async ([uid, entry]: any) => {
243-
const entryObj: any = {};
244-
entryObj.uid = uid;
245-
for await (const field of entry?.fields?.field ?? []) {
246-
for await (const fsc of ctType?.fieldMapping ?? []) {
247-
if (fsc?.contentstackFieldType !== 'group' && !field?.$?.key?.includes('__')) {
248-
if (fsc?.contentstackFieldUid === 'title') {
249-
entryObj[fsc?.contentstackFieldUid] = entry?.meta?.name;
250-
}
251-
if (fsc?.contentstackFieldUid === 'url') {
252-
entryObj[fsc?.contentstackFieldUid] = `/${entry?.meta?.key}`;
253-
}
254-
if (getLastKey(fsc?.uid) === field?.$?.key) {
255-
const content: any = await entriesFieldCreator({ field: fsc, content: field?.content, idCorrector, allAssetJSON, contentTypes, entriesData, locale });
256-
const gpData: any = ctType?.fieldMapping?.find((elemant: any) => elemant?.uid === fsc?.uid?.split('.')?.[0]);
257-
if (gpData?.uid) {
258-
const ctUid = uidCorrector({ uid: gpData?.uid });
259-
if (ctUid !== gpData?.contentstackFieldUid && fsc?.contentstackFieldUid?.includes(ctUid)) {
260-
const newUid: any = fsc?.contentstackFieldUid?.replace(ctUid, gpData?.contentstackFieldUid);
261-
entryObj[newUid] = content;
262-
} else {
263-
entryObj[fsc?.contentstackFieldUid] = content;
264-
}
257+
Object.entries(entryPresent?.locale?.[locale] || {}).map(async ([uid, entry]: any) => {
258+
const entryObj: any = {};
259+
entryObj.uid = uid;
260+
for await (const field of entry?.fields?.field ?? []) {
261+
for await (const fsc of ctType?.fieldMapping ?? []) {
262+
if (fsc?.contentstackFieldType !== 'group' && !field?.$?.key?.includes('__')) {
263+
if (fsc?.contentstackFieldUid === 'title') {
264+
entryObj[fsc?.contentstackFieldUid] = entry?.meta?.name;
265+
}
266+
if (fsc?.contentstackFieldUid === 'url') {
267+
entryObj[fsc?.contentstackFieldUid] = `/${entry?.meta?.key}`;
268+
}
269+
if (getLastKey(fsc?.uid) === field?.$?.key) {
270+
const content: any = await entriesFieldCreator({ field: fsc, content: field?.content, idCorrector, allAssetJSON, contentTypes, entriesData, locale });
271+
const gpData: any = ctType?.fieldMapping?.find((elemant: any) => elemant?.uid === fsc?.uid?.split('.')?.[0]);
272+
if (gpData?.uid) {
273+
const ctUid = uidCorrector({ uid: gpData?.uid });
274+
if (ctUid !== gpData?.contentstackFieldUid && fsc?.contentstackFieldUid?.includes(ctUid)) {
275+
const newUid: any = fsc?.contentstackFieldUid?.replace(ctUid, gpData?.contentstackFieldUid);
276+
entryObj[newUid] = content;
265277
} else {
266278
entryObj[fsc?.contentstackFieldUid] = content;
267279
}
280+
} else {
281+
entryObj[fsc?.contentstackFieldUid] = content;
268282
}
269283
}
270284
}
271285
}
272-
entryObj.publish_details = [];
273-
if (Object.keys?.(entryObj)?.length > 1) {
274-
entryLocale[uid] = unflatten(entryObj) ?? {};
275-
const message = getLogMessage(
276-
srcFunc,
277-
`Entry title "${entryObj?.title}"(${keyMapper[ctType?.contentstackUid] ?? ctType?.contentstackUid}) in the ${newLocale} locale has been successfully transformed.`,
278-
{}
279-
)
280-
await customLogger(projectId, destinationStackId, 'info', message)
281-
}
282-
});
283-
}
284-
const mapperCt: string = (keyMapper[ctType?.contentstackUid] !== "" && keyMapper[ctType?.contentstackUid] !== undefined) ? keyMapper[ctType?.contentstackUid]
286+
}
287+
entryObj.publish_details = [];
288+
if (Object.keys?.(entryObj)?.length > 1) {
289+
entryLocale[uid] = unflatten(entryObj) ?? {};
290+
const message = getLogMessage(
291+
srcFunc,
292+
`Entry title "${entryObj?.title}"(${keyMapper?.[ctType?.contentstackUid] ?? ctType?.contentstackUid}) in the ${newLocale} locale has been successfully transformed.`,
293+
{}
294+
)
295+
await customLogger(projectId, destinationStackId, 'info', message)
296+
}
297+
});
298+
const mapperCt: string = (keyMapper?.[ctType?.contentstackUid] !== "" && keyMapper?.[ctType?.contentstackUid] !== undefined) ? keyMapper?.[ctType?.contentstackUid]
285299
: ctType?.contentstackUid;
286300
const fileMeta = { "1": `${newLocale}.json` };
287301
const entryPath = path.join(
@@ -295,11 +309,11 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
295309
} else {
296310
const message = getLogMessage(
297311
srcFunc,
298-
`No entries found for the content type ${keyMapper[ctType?.contentstackUid] ?? ctType?.contentstackUid}.`,
312+
`No entries found for the content type ${keyMapper?.[ctType?.contentstackUid] ?? ctType?.contentstackUid}.`,
299313
{}
300314
)
301315
await customLogger(projectId, destinationStackId, 'error', message)
302-
console.info('Entries missing for', keyMapper[ctType?.contentstackUid] ?? ctType?.contentstackUid)
316+
console.info('Entries missing for', keyMapper?.[ctType?.contentstackUid] ?? ctType?.contentstackUid)
303317
}
304318
}
305319
return true;
@@ -308,13 +322,13 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
308322
}
309323
}
310324

311-
const createLocale = async (req: any, destinationStackId: string, projectId: string) => {
325+
const createLocale = async (req: any, destinationStackId: string, projectId: string, project: any) => {
312326
const srcFunc = 'createLocale';
313327
try {
314328
const baseDir = path.join(baseDirName, destinationStackId);
315329
const localeSave = path.join(baseDir, LOCALE_DIR_NAME);
316330
const allLocalesResp = await orgService.getLocales(req)
317-
const masterLocale = Object?.keys?.(LOCALE_MAPPER?.masterLocale)?.[0];
331+
const masterLocale = Object?.keys?.(project?.master_locale ?? LOCALE_MAPPER?.masterLocale)?.[0];
318332
const msLocale: any = {};
319333
const uid = uuidv4();
320334
msLocale[uid] = {
@@ -330,14 +344,14 @@ const createLocale = async (req: any, destinationStackId: string, projectId: str
330344
)
331345
await customLogger(projectId, destinationStackId, 'info', message);
332346
const allLocales: any = {};
333-
for (const [key, value] of Object.entries(LOCALE_MAPPER)) {
347+
for (const [key, value] of Object.entries(project?.locales ?? LOCALE_MAPPER)) {
334348
const localeUid = uuidv4();
335349
if (key !== 'masterLocale' && typeof value === 'string') {
336350
allLocales[localeUid] = {
337-
"code": value,
351+
"code": key,
338352
"fallback_locale": masterLocale,
339353
"uid": localeUid,
340-
"name": allLocalesResp?.data?.locales?.[value] ?? ''
354+
"name": allLocalesResp?.data?.locales?.[key] ?? ''
341355
}
342356
const message = getLogMessage(
343357
srcFunc,

upload-api/src/controllers/sitecore/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const createSitecoreMapper = async (filePath: string = "", projectId: string | s
4949
},
5050
data: JSON.stringify(fieldMapping),
5151
};
52-
const response = await axios.request(config)
52+
const response = await axios?.request?.(config)
5353
if (response?.data?.content_mapper?.length) {
5454
deleteFolderSync(infoMap?.path);
5555
logger.info('Validation success:', {
@@ -67,7 +67,7 @@ const createSitecoreMapper = async (filePath: string = "", projectId: string | s
6767
'Content-Type': 'application/json'
6868
},
6969
data: {
70-
locale: Array.from(localeData)
70+
locale: Array?.from?.(localeData) ?? []
7171
},
7272
};
7373

upload-api/src/services/createMapper.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const createMapper = async (filePath: string = "", projectId: string | string[],
77
const CMSIdentifier = config?.cmsType?.toLowerCase();
88
switch (CMSIdentifier) {
99
case 'sitecore': {
10-
console.error('🚀 ~ sitecore create mapper ~ initial mapper');
1110
return await createSitecoreMapper(filePath, projectId, app_token, affix, config);
1211
}
1312

0 commit comments

Comments
 (0)