Skip to content

Commit 42fe960

Browse files
Merge pull request #812 from contentstack/fix/CMG-723
Fix/cmg-723
2 parents 35fea9d + c26e00c commit 42fe960

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

api/src/services/aem.service.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,16 @@ async function writeFiles(
122122
try {
123123
const indexPath = path.join(entryPath, 'index.json');
124124
const localePath = path.join(entryPath, `${locale}.json`);
125-
125+
126126
try {
127127
await fs.promises.access(entryPath);
128128
} catch {
129129
await fs.promises.mkdir(entryPath, { recursive: true });
130130
}
131-
131+
132132
await fs.promises.writeFile(indexPath, JSON.stringify(fileMeta));
133133
await fs.promises.writeFile(localePath, JSON.stringify(entryLocale));
134-
} catch (error) {
134+
} catch (error) {
135135
console.error('Error writing files:', error);
136136
throw error;
137137
}
@@ -456,8 +456,9 @@ function processFieldsRecursive(fields: any[], items: any, title: string, assetJ
456456
break;
457457
}
458458
case 'boolean': {
459-
const value = items?.[field?.uid];
460-
const uid = getLastKey(field?.contentstackFieldUid);
459+
const aemFieldName = field?.otherCmsField ? getLastKey(field.otherCmsField, ' > ') : getLastKey(field?.uid);
460+
const value = items?.[aemFieldName];
461+
const uid = getLastKey(field?.contentstackFieldUid);
461462
if (typeof value === 'boolean' || (typeof value === 'object' && value?.[':type']?.includes('separator'))) {
462463
obj[uid] = typeof value === 'boolean' ? value : true;
463464
}
@@ -482,19 +483,16 @@ function processFieldsRecursive(fields: any[], items: any, title: string, assetJ
482483
case 'reference': {
483484
const fieldKey = getLastKey(field?.contentstackFieldUid);
484485
const refCtUid = field?.referenceTo?.[0] || field?.uid;
485-
486486
for (const [key, val] of Object.entries(items) as [string, Record<string, unknown>][]) {
487487
if (!val?.configured || (val[':type'] as string) === 'nt:folder') {
488488
continue;
489489
}
490-
491490
if (
492491
(val[':type'] as string)?.includes('experiencefragment') &&
493492
typeof val?.localizedFragmentVariationPath === 'string'
494493
) {
495494
const pathMatchesField = val.localizedFragmentVariationPath.includes(`/${field?.uid}`);
496495
const pathMatchesRefType = val.localizedFragmentVariationPath.includes(`/${refCtUid}`);
497-
498496
if (pathMatchesField || pathMatchesRefType) {
499497
obj[fieldKey] = [{
500498
"uid": val?.id,
@@ -572,14 +570,14 @@ const createEntry = async ({
572570
const assetJson = path.join(assetsSave, ASSETS_SCHEMA_FILE);
573571
const exists = await isAssetJsonCreated(assetJson);
574572
let assetJsonData: Record<string, AssetJSON> = {};
575-
573+
576574
if (exists) {
577575
const assetData = await fs.promises.readFile(assetJson, 'utf-8');
578576
if (typeof assetData === 'string') {
579577
assetJsonData = JSON.parse(assetData);
580578
}
581579
}
582-
580+
583581
const entriesDir = path.resolve(packagePath ?? '');
584582
const damPath = path.join(entriesDir, AEM_DAM_DIR);
585583
const entriesData: Record<string, Record<string, any[]>> = {};
@@ -606,7 +604,7 @@ const createEntry = async ({
606604
const data = containerCreator(contentType?.fieldMapping, items, title, assetJsonData);
607605
data.uid = uid;
608606
data.publish_details = [];
609-
607+
610608
if (contentType?.contentstackUid && data && mappedLocale) {
611609
const message = getLogMessage(
612610
srcFunc,
@@ -635,7 +633,7 @@ const createEntry = async ({
635633
if (key.endsWith('._content_type_uid') && typeof value === 'string') {
636634
const uidField = key.replace('._content_type_uid', '');
637635
const refs: string[] = entryMapping?.[value];
638-
636+
639637
if (refs?.length) {
640638
_.set(entry, `${uidField}.uid`, refs[0]);
641639
} else {
@@ -647,7 +645,7 @@ const createEntry = async ({
647645
const entriesObject: Record<string, any> = {};
648646
for (const entry of entries) {
649647
entriesObject[entry.uid] = entry;
650-
}
648+
}
651649
const fileMeta = { '1': `${locale}.json` };
652650
const entryPath = path.join(
653651
process.cwd(),

upload-api/migration-aem/helper/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ export const uidCorrector = (uid: string) => {
203203
// Remove leading colon if present (e.g., ':items' becomes 'items')
204204
let newUid = uid.replace(/^:/, '');
205205

206+
// Insert underscore before uppercase letters, then lowercase everything
207+
newUid = newUid.replace(/([a-z0-9])([A-Z])/g, '$1_$2');
208+
206209
// Replace spaces, hyphens, and colons with underscores
207210
newUid = newUid.replace(/[ :-]/g, '_');
208211

0 commit comments

Comments
 (0)