diff --git a/api/src/services/contentMapper.service.ts b/api/src/services/contentMapper.service.ts index bef1b1a5f..6943c700d 100644 --- a/api/src/services/contentMapper.service.ts +++ b/api/src/services/contentMapper.service.ts @@ -751,7 +751,7 @@ const resetToInitialMapping = async (req: Request) => { const fieldMappingData = contentTypeData.fieldMapping.map((itemId: any) => { const fieldData = FieldMapperModel.chain .get("field_mapper") - .find({ id: itemId, projectId: projectId }) + .find({ id: itemId, projectId: projectId , contentTypeId: contentTypeId}) .value(); return fieldData; }); @@ -771,7 +771,7 @@ const resetToInitialMapping = async (req: Request) => { //await FieldMapperModel.read(); (fieldMappingData || []).forEach((field: any) => { const fieldIndex = FieldMapperModel.data.field_mapper.findIndex( - (f: any) => f?.id === field?.id + (f: any) => f?.id === field?.id && f?.projectId === projectId && f?.contentTypeId === contentTypeId ); if (fieldIndex > -1) { FieldMapperModel.update((data: any) => { diff --git a/api/src/services/sitecore.service.ts b/api/src/services/sitecore.service.ts index 3b3775078..03da4b23a 100644 --- a/api/src/services/sitecore.service.ts +++ b/api/src/services/sitecore.service.ts @@ -105,16 +105,32 @@ async function writeFiles( console.error('Error writing files:', error); } } +const uidCorrector = ({ uid } :{uid : string}) => { + if (!uid || typeof uid !== 'string') { + return ''; + } + + let newUid = uid; + + // Note: UIDs starting with numbers and restricted keywords are handled externally in Sitecore + // The prefix is applied in contentTypeMaker function when needed -const uidCorrector = ({ uid }: any) => { - if (startsWithNumber(uid)) { - return `${append}_${_.replace( - uid, - new RegExp('[ -]', 'g'), - '_' - )?.toLowerCase()}`; + // Clean up the UID + newUid = newUid + .replace(/[ -]/g, '_') // Replace spaces and hyphens with underscores + .replace(/[^a-zA-Z0-9_]+/g, '_') // Replace non-alphanumeric characters (except underscore) + .replace(/\$/g, '') // Remove dollar signs + .toLowerCase() // Convert to lowercase + .replace(/([A-Z])/g, (match) => `_${match.toLowerCase()}`) // Handle camelCase + .replace(/_+/g, '_') // Replace multiple underscores with single + .replace(/^_|_$/g, ''); // Remove leading/trailing underscores + + // Ensure UID doesn't start with underscore (Contentstack requirement) + if (newUid.startsWith('_')) { + newUid = newUid.substring(1); } - return _.replace(uid, new RegExp('[ -]', 'g'), '_')?.toLowerCase(); + + return newUid; }; const createAssets = async ({ diff --git a/api/src/utils/content-type-creator.utils.ts b/api/src/utils/content-type-creator.utils.ts index 60ed055ce..503281331 100644 --- a/api/src/utils/content-type-creator.utils.ts +++ b/api/src/utils/content-type-creator.utils.ts @@ -74,12 +74,33 @@ function startsWithNumber(str: string) { return /^\d/.test(str); } -const uidCorrector = ({ uid }: any) => { - if (startsWithNumber(uid)) { - return `a_${_.replace(uid, new RegExp("[ -]", "g"), '_')?.toLowerCase()}` +const uidCorrector = ({ uid } : {uid : string}) => { + if (!uid || typeof uid !== 'string') { + return ''; } - return _.replace(uid, new RegExp("[ -]", "g"), '_')?.toLowerCase() -} + + let newUid = uid; + + // Note: UIDs starting with numbers and restricted keywords are handled externally in Sitecore + // The prefix is applied in contentTypeMaker function when needed + + // Clean up the UID + newUid = newUid + .replace(/[ -]/g, '_') // Replace spaces and hyphens with underscores + .replace(/[^a-zA-Z0-9_]+/g, '_') // Replace non-alphanumeric characters (except underscore) + .replace(/\$/g, '') // Remove dollar signs + .toLowerCase() // Convert to lowercase + .replace(/([A-Z])/g, (match) => `_${match.toLowerCase()}`) // Handle camelCase + .replace(/_+/g, '_') // Replace multiple underscores with single + .replace(/^_|_$/g, ''); // Remove leading/trailing underscores + + // Ensure UID doesn't start with underscore (Contentstack requirement) + if (newUid.startsWith('_')) { + newUid = newUid.substring(1); + } + + return newUid; +}; function buildFieldSchema(item: any, marketPlacePath: string, parentUid = ''): any { diff --git a/upload-api/migration-sitecore/libs/contenttypes.js b/upload-api/migration-sitecore/libs/contenttypes.js index 7d632d3a7..8802e039b 100644 --- a/upload-api/migration-sitecore/libs/contenttypes.js +++ b/upload-api/migration-sitecore/libs/contenttypes.js @@ -413,7 +413,8 @@ const groupFlat = (data, item) => { contentstackField: item?.meta?.name, contentstackFieldUid: data?.uid, // Use the corrected UID from groupSchema contentstackFieldType: 'group', - backupFieldType: 'group' + backupFieldType: 'group', + backupFieldUid: data?.uid }; flat?.push(group); data?.schema?.forEach((element) => {