Skip to content

Commit a57b553

Browse files
committed
added mapper
1 parent 5fa4c9a commit a57b553

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
@@ -52,6 +52,18 @@ const AssetsPathSpliter = ({ path, id }: any) => {
5252
return newPath;
5353
}
5454

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

5668

5769
async function writeOneFile(indexPath: string, fileMeta: any) {
@@ -197,7 +209,7 @@ const cretaeAssets = async ({ packagePath, baseDir, destinationStackId, projectI
197209
return allAssetJSON;
198210
}
199211

200-
const createEntry = async ({ packagePath, contentTypes, master_locale, destinationStackId, projectId, keyMapper }: { packagePath: any; contentTypes: any; master_locale?: string, destinationStackId: string, projectId: string, keyMapper: any }) => {
212+
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 }) => {
201213
try {
202214
const srcFunc = 'createEntry';
203215
const baseDir = path.join(baseDirName, destinationStackId);
@@ -218,7 +230,11 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
218230
const templateIndex = entriesData?.findIndex((ele: any) => ele?.template === template);
219231
if (templateIndex >= 0) {
220232
const entry = entriesData?.[templateIndex]?.locale?.[language];
221-
entry[id] = { meta: jsonData?.item?.$, fields: jsonData?.item?.fields };
233+
if (entry !== undefined) {
234+
entry[id] = { meta: jsonData?.item?.$, fields: jsonData?.item?.fields };
235+
} else {
236+
entriesData[templateIndex].locale[language] = entries;
237+
}
222238
} else {
223239
const locale: any = {};
224240
locale[language] = entries;
@@ -230,61 +246,59 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
230246
for await (const ctType of contentTypes) {
231247
const message = getLogMessage(
232248
srcFunc,
233-
`Transforming entries of Content Type ${keyMapper[ctType?.contentstackUid] ?? ctType?.contentstackUid} has begun.`,
249+
`Transforming entries of Content Type ${keyMapper?.[ctType?.contentstackUid] ?? ctType?.contentstackUid} has begun.`,
234250
{}
235251
)
236252
await customLogger(projectId, destinationStackId, 'info', message);
237253
const entryPresent: any = entriesData?.find((item: any) => uidCorrector({ uid: item?.template }) === ctType?.contentstackUid)
238254
if (entryPresent) {
239255
const locales: any = Object?.keys(entryPresent?.locale);
256+
const allLocales: any = { masterLocale: project?.master_locale ?? LOCALE_MAPPER?.masterLocale, ...project?.locales ?? {} }
240257
for await (const locale of locales) {
241-
let newLocale = locale;
258+
const newLocale = mapLocales({ masterLocale: master_locale, locale, locales: allLocales });
242259
const entryLocale: any = {};
243-
if (typeof LOCALE_MAPPER?.masterLocale === 'object' && LOCALE_MAPPER?.masterLocale !== null && LOCALE_MAPPER?.masterLocale?.[master_locale ?? ''] === locale) {
244-
newLocale = Object?.keys(LOCALE_MAPPER?.masterLocale)?.[0];
245-
Object.entries(entryPresent?.locale?.[locale] || {}).map(async ([uid, entry]: any) => {
246-
const entryObj: any = {};
247-
entryObj.uid = uid;
248-
for await (const field of entry?.fields?.field ?? []) {
249-
for await (const fsc of ctType?.fieldMapping ?? []) {
250-
if (fsc?.contentstackFieldType !== 'group' && !field?.$?.key?.includes('__')) {
251-
if (fsc?.contentstackFieldUid === 'title') {
252-
entryObj[fsc?.contentstackFieldUid] = entry?.meta?.name;
253-
}
254-
if (fsc?.contentstackFieldUid === 'url') {
255-
entryObj[fsc?.contentstackFieldUid] = `/${entry?.meta?.key}`;
256-
}
257-
if (getLastKey(fsc?.uid) === field?.$?.key) {
258-
const content: any = await entriesFieldCreator({ field: fsc, content: field?.content, idCorrector, allAssetJSON, contentTypes, entriesData, locale });
259-
const gpData: any = ctType?.fieldMapping?.find((elemant: any) => elemant?.uid === fsc?.uid?.split('.')?.[0]);
260-
if (gpData?.uid) {
261-
const ctUid = uidCorrector({ uid: gpData?.uid });
262-
if (ctUid !== gpData?.contentstackFieldUid && fsc?.contentstackFieldUid?.includes(ctUid)) {
263-
const newUid: any = fsc?.contentstackFieldUid?.replace(ctUid, gpData?.contentstackFieldUid);
264-
entryObj[newUid] = content;
265-
} else {
266-
entryObj[fsc?.contentstackFieldUid] = content;
267-
}
260+
Object.entries(entryPresent?.locale?.[locale] || {}).map(async ([uid, entry]: any) => {
261+
const entryObj: any = {};
262+
entryObj.uid = uid;
263+
for await (const field of entry?.fields?.field ?? []) {
264+
for await (const fsc of ctType?.fieldMapping ?? []) {
265+
if (fsc?.contentstackFieldType !== 'group' && !field?.$?.key?.includes('__')) {
266+
if (fsc?.contentstackFieldUid === 'title') {
267+
entryObj[fsc?.contentstackFieldUid] = entry?.meta?.name;
268+
}
269+
if (fsc?.contentstackFieldUid === 'url') {
270+
entryObj[fsc?.contentstackFieldUid] = `/${entry?.meta?.key}`;
271+
}
272+
if (getLastKey(fsc?.uid) === field?.$?.key) {
273+
const content: any = await entriesFieldCreator({ field: fsc, content: field?.content, idCorrector, allAssetJSON, contentTypes, entriesData, locale });
274+
const gpData: any = ctType?.fieldMapping?.find((elemant: any) => elemant?.uid === fsc?.uid?.split('.')?.[0]);
275+
if (gpData?.uid) {
276+
const ctUid = uidCorrector({ uid: gpData?.uid });
277+
if (ctUid !== gpData?.contentstackFieldUid && fsc?.contentstackFieldUid?.includes(ctUid)) {
278+
const newUid: any = fsc?.contentstackFieldUid?.replace(ctUid, gpData?.contentstackFieldUid);
279+
entryObj[newUid] = content;
268280
} else {
269281
entryObj[fsc?.contentstackFieldUid] = content;
270282
}
283+
} else {
284+
entryObj[fsc?.contentstackFieldUid] = content;
271285
}
272286
}
273287
}
274288
}
275-
entryObj.publish_details = [];
276-
if (Object.keys?.(entryObj)?.length > 1) {
277-
entryLocale[uid] = unflatten(entryObj) ?? {};
278-
const message = getLogMessage(
279-
srcFunc,
280-
`Entry title "${entryObj?.title}"(${keyMapper[ctType?.contentstackUid] ?? ctType?.contentstackUid}) in the ${newLocale} locale has been successfully transformed.`,
281-
{}
282-
)
283-
await customLogger(projectId, destinationStackId, 'info', message)
284-
}
285-
});
286-
}
287-
const mapperCt: string = (keyMapper[ctType?.contentstackUid] !== "" && keyMapper[ctType?.contentstackUid] !== undefined) ? keyMapper[ctType?.contentstackUid]
289+
}
290+
entryObj.publish_details = [];
291+
if (Object.keys?.(entryObj)?.length > 1) {
292+
entryLocale[uid] = unflatten(entryObj) ?? {};
293+
const message = getLogMessage(
294+
srcFunc,
295+
`Entry title "${entryObj?.title}"(${keyMapper?.[ctType?.contentstackUid] ?? ctType?.contentstackUid}) in the ${newLocale} locale has been successfully transformed.`,
296+
{}
297+
)
298+
await customLogger(projectId, destinationStackId, 'info', message)
299+
}
300+
});
301+
const mapperCt: string = (keyMapper?.[ctType?.contentstackUid] !== "" && keyMapper?.[ctType?.contentstackUid] !== undefined) ? keyMapper?.[ctType?.contentstackUid]
288302
: ctType?.contentstackUid;
289303
const fileMeta = { "1": `${newLocale}.json` };
290304
const entryPath = path.join(
@@ -298,11 +312,11 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
298312
} else {
299313
const message = getLogMessage(
300314
srcFunc,
301-
`No entries found for the content type ${keyMapper[ctType?.contentstackUid] ?? ctType?.contentstackUid}.`,
315+
`No entries found for the content type ${keyMapper?.[ctType?.contentstackUid] ?? ctType?.contentstackUid}.`,
302316
{}
303317
)
304318
await customLogger(projectId, destinationStackId, 'error', message)
305-
console.info('Entries missing for', keyMapper[ctType?.contentstackUid] ?? ctType?.contentstackUid)
319+
console.info('Entries missing for', keyMapper?.[ctType?.contentstackUid] ?? ctType?.contentstackUid)
306320
}
307321
}
308322
return true;
@@ -311,13 +325,13 @@ const createEntry = async ({ packagePath, contentTypes, master_locale, destinati
311325
}
312326
}
313327

314-
const createLocale = async (req: any, destinationStackId: string, projectId: string) => {
328+
const createLocale = async (req: any, destinationStackId: string, projectId: string, project: any) => {
315329
const srcFunc = 'createLocale';
316330
try {
317331
const baseDir = path.join(baseDirName, destinationStackId);
318332
const localeSave = path.join(baseDir, LOCALE_DIR_NAME);
319333
const allLocalesResp = await orgService.getLocales(req)
320-
const masterLocale = Object?.keys?.(LOCALE_MAPPER?.masterLocale)?.[0];
334+
const masterLocale = Object?.keys?.(project?.master_locale ?? LOCALE_MAPPER?.masterLocale)?.[0];
321335
const msLocale: any = {};
322336
const uid = uuidv4();
323337
msLocale[uid] = {
@@ -333,14 +347,14 @@ const createLocale = async (req: any, destinationStackId: string, projectId: str
333347
)
334348
await customLogger(projectId, destinationStackId, 'info', message);
335349
const allLocales: any = {};
336-
for (const [key, value] of Object.entries(LOCALE_MAPPER)) {
350+
for (const [key, value] of Object.entries(project?.locales ?? LOCALE_MAPPER)) {
337351
const localeUid = uuidv4();
338352
if (key !== 'masterLocale' && typeof value === 'string') {
339353
allLocales[localeUid] = {
340-
"code": value,
354+
"code": key,
341355
"fallback_locale": masterLocale,
342356
"uid": localeUid,
343-
"name": allLocalesResp?.data?.locales?.[value] ?? ''
357+
"name": allLocalesResp?.data?.locales?.[key] ?? ''
344358
}
345359
const message = getLogMessage(
346360
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)