Skip to content

Commit 2df121d

Browse files
Merge pull request #845 from contentstack/bugfix/cmg-752
Bugfix/cmg 752
2 parents 0aeec46 + 3cc0630 commit 2df121d

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

api/src/services/contentMapper.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ const resetToInitialMapping = async (req: Request) => {
751751
const fieldMappingData = contentTypeData.fieldMapping.map((itemId: any) => {
752752
const fieldData = FieldMapperModel.chain
753753
.get("field_mapper")
754-
.find({ id: itemId, projectId: projectId })
754+
.find({ id: itemId, projectId: projectId , contentTypeId: contentTypeId})
755755
.value();
756756
return fieldData;
757757
});
@@ -771,7 +771,7 @@ const resetToInitialMapping = async (req: Request) => {
771771
//await FieldMapperModel.read();
772772
(fieldMappingData || []).forEach((field: any) => {
773773
const fieldIndex = FieldMapperModel.data.field_mapper.findIndex(
774-
(f: any) => f?.id === field?.id
774+
(f: any) => f?.id === field?.id && f?.projectId === projectId && f?.contentTypeId === contentTypeId
775775
);
776776
if (fieldIndex > -1) {
777777
FieldMapperModel.update((data: any) => {

api/src/services/sitecore.service.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,32 @@ async function writeFiles(
105105
console.error('Error writing files:', error);
106106
}
107107
}
108+
const uidCorrector = ({ uid } :{uid : string}) => {
109+
if (!uid || typeof uid !== 'string') {
110+
return '';
111+
}
112+
113+
let newUid = uid;
114+
115+
// Note: UIDs starting with numbers and restricted keywords are handled externally in Sitecore
116+
// The prefix is applied in contentTypeMaker function when needed
108117

109-
const uidCorrector = ({ uid }: any) => {
110-
if (startsWithNumber(uid)) {
111-
return `${append}_${_.replace(
112-
uid,
113-
new RegExp('[ -]', 'g'),
114-
'_'
115-
)?.toLowerCase()}`;
118+
// Clean up the UID
119+
newUid = newUid
120+
.replace(/[ -]/g, '_') // Replace spaces and hyphens with underscores
121+
.replace(/[^a-zA-Z0-9_]+/g, '_') // Replace non-alphanumeric characters (except underscore)
122+
.replace(/\$/g, '') // Remove dollar signs
123+
.toLowerCase() // Convert to lowercase
124+
.replace(/([A-Z])/g, (match) => `_${match.toLowerCase()}`) // Handle camelCase
125+
.replace(/_+/g, '_') // Replace multiple underscores with single
126+
.replace(/^_|_$/g, ''); // Remove leading/trailing underscores
127+
128+
// Ensure UID doesn't start with underscore (Contentstack requirement)
129+
if (newUid.startsWith('_')) {
130+
newUid = newUid.substring(1);
116131
}
117-
return _.replace(uid, new RegExp('[ -]', 'g'), '_')?.toLowerCase();
132+
133+
return newUid;
118134
};
119135

120136
const createAssets = async ({

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,33 @@ function startsWithNumber(str: string) {
7474
return /^\d/.test(str);
7575
}
7676

77-
const uidCorrector = ({ uid }: any) => {
78-
if (startsWithNumber(uid)) {
79-
return `a_${_.replace(uid, new RegExp("[ -]", "g"), '_')?.toLowerCase()}`
77+
const uidCorrector = ({ uid } : {uid : string}) => {
78+
if (!uid || typeof uid !== 'string') {
79+
return '';
8080
}
81-
return _.replace(uid, new RegExp("[ -]", "g"), '_')?.toLowerCase()
82-
}
81+
82+
let newUid = uid;
83+
84+
// Note: UIDs starting with numbers and restricted keywords are handled externally in Sitecore
85+
// The prefix is applied in contentTypeMaker function when needed
86+
87+
// Clean up the UID
88+
newUid = newUid
89+
.replace(/[ -]/g, '_') // Replace spaces and hyphens with underscores
90+
.replace(/[^a-zA-Z0-9_]+/g, '_') // Replace non-alphanumeric characters (except underscore)
91+
.replace(/\$/g, '') // Remove dollar signs
92+
.toLowerCase() // Convert to lowercase
93+
.replace(/([A-Z])/g, (match) => `_${match.toLowerCase()}`) // Handle camelCase
94+
.replace(/_+/g, '_') // Replace multiple underscores with single
95+
.replace(/^_|_$/g, ''); // Remove leading/trailing underscores
96+
97+
// Ensure UID doesn't start with underscore (Contentstack requirement)
98+
if (newUid.startsWith('_')) {
99+
newUid = newUid.substring(1);
100+
}
101+
102+
return newUid;
103+
};
83104

84105

85106
function buildFieldSchema(item: any, marketPlacePath: string, parentUid = ''): any {

upload-api/migration-sitecore/libs/contenttypes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ const groupFlat = (data, item) => {
413413
contentstackField: item?.meta?.name,
414414
contentstackFieldUid: data?.uid, // Use the corrected UID from groupSchema
415415
contentstackFieldType: 'group',
416-
backupFieldType: 'group'
416+
backupFieldType: 'group',
417+
backupFieldUid: data?.uid
417418
};
418419
flat?.push(group);
419420
data?.schema?.forEach((element) => {

0 commit comments

Comments
 (0)