Skip to content

Commit 34fe224

Browse files
feat: enhance AEM service and content type utilities for improved schema merging and UID handling; update ContentMapper to support nested modular blocks and prevent duplicate selections fix - CMG-812, CMG-818, CMG-823, CMG-824, CMG-831
1 parent f30cfba commit 34fe224

File tree

4 files changed

+210
-139
lines changed

4 files changed

+210
-139
lines changed

api/src/services/aem.service.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,11 @@ function processFieldsRecursive(
690690

691691
case 'group': {
692692
const uid = getLastKey(field?.contentstackFieldUid);
693+
const aemGroupSourcePath = field?.backupFieldUid || field?.otherCmsField?.replace?.(/ > /g, '.') || '';
694+
const aemGroupSourceKey = getLastKey(aemGroupSourcePath) || field?.uid;
693695

694696
const isMultiple =
695-
(field?.multiple === true) ||
696-
(field?.advanced && field.advanced.multiple === true) ||
697+
(field?.advanced?.multiple !== undefined ? field.advanced.multiple === true : field?.multiple === true) ||
697698
(field?.maxInstance && field.maxInstance > 1);
698699

699700
const isCarouselItems =
@@ -710,7 +711,7 @@ function processFieldsRecursive(
710711
if (isCarouselItems) {
711712
groupValue = items;
712713
} else {
713-
groupValue = items?.[field?.uid]?.items ?? items?.[field?.uid];
714+
groupValue = items?.[aemGroupSourceKey]?.items ?? items?.[aemGroupSourceKey];
714715
}
715716

716717
if (isMultiple) {
@@ -897,7 +898,7 @@ function processFieldsRecursive(
897898
const order2 = Array.isArray(items?.[':itemsOrder']) ? items[':itemsOrder'] : null;
898899
const map2 = items?.[':items'] || items;
899900
if (order2 && map2) {
900-
const baseUid = field?.uid;
901+
const baseUid = aemGroupSourceKey;
901902
const keysForThisGroup = order2.filter(
902903
(k) => k === baseUid || new RegExp(`^${baseUid}_`).test(k)
903904
);
@@ -925,15 +926,12 @@ function processFieldsRecursive(
925926
}
926927
} else {
927928
if (Array.isArray(groupValue)) {
928-
const groupData: unknown[] = [];
929-
if (Array.isArray(field?.schema)) {
930-
for (const element of groupValue) {
931-
groupData.push(
932-
processFieldsRecursive(field.schema, element, title, pathToUidMap, assetDetailsMap)
933-
);
934-
}
929+
const firstElement = groupValue[0];
930+
if (Array.isArray(field?.schema) && firstElement) {
931+
obj[uid] = processFieldsRecursive(field.schema, firstElement, title, pathToUidMap, assetDetailsMap);
932+
} else {
933+
obj[uid] = {};
935934
}
936-
obj[uid] = groupData;
937935
} else {
938936
if (Array.isArray(field?.schema)) {
939937
const value = processFieldsRecursive(field.schema, groupValue, title, pathToUidMap, assetDetailsMap);

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

Lines changed: 40 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,93 +1081,61 @@ const mergeArrays = async (a: any[], b: any[]) => {
10811081
return a;
10821082
}
10831083

1084-
const mergeTwoCts = async (ct: any, mergeCts: any) => {
1085-
const ctData: any = {
1086-
...ct,
1087-
title: mergeCts?.title,
1088-
uid: mergeCts?.uid,
1089-
options: {
1090-
"singleton": false,
1091-
}
1092-
}
1093-
1094-
for await (const field of ctData?.schema ?? []) {
1095-
// Handle regular groups
1084+
function mergeSchemaFields(sourceSchema: any[], targetSchema: any[]) {
1085+
for (const field of sourceSchema) {
10961086
if (field?.data_type === 'group') {
1097-
const currentGroup = mergeCts?.schema?.find((grp: any) =>
1087+
const targetGroup = targetSchema?.find((grp: any) =>
10981088
grp?.uid === field?.uid && grp?.data_type === 'group'
10991089
);
1100-
1101-
if (currentGroup) {
1102-
const group = [];
1103-
for await (const fieldGp of currentGroup?.schema ?? []) {
1104-
const fieldNst = field?.schema?.find((fld: any) =>
1105-
fld?.uid === fieldGp?.uid && fld?.data_type === fieldGp?.data_type
1106-
);
1107-
if (fieldNst === undefined) {
1108-
group?.push(fieldGp);
1109-
}
1110-
}
1111-
field.schema = removeDuplicateFields([...field?.schema ?? [], ...group]);
1090+
1091+
if (targetGroup) {
1092+
const additional = (targetGroup?.schema ?? []).filter((tField: any) =>
1093+
!field?.schema?.find((sField: any) => sField?.uid === tField?.uid && sField?.data_type === tField?.data_type)
1094+
);
1095+
field.schema = removeDuplicateFields([...field?.schema ?? [], ...additional]);
1096+
mergeSchemaFields(field?.schema, targetGroup?.schema ?? []);
11121097
}
11131098
}
11141099

1115-
// Handle modular blocks
11161100
if (field?.data_type === 'blocks') {
1117-
const currentModularBlock = mergeCts?.schema?.find((mb: any) =>
1101+
const targetMB = targetSchema?.find((mb: any) =>
11181102
mb?.uid === field?.uid && mb?.data_type === 'blocks'
11191103
);
1120-
1121-
if (currentModularBlock && currentModularBlock?.blocks) {
1122-
// Iterate through each child block in the source
1104+
1105+
if (targetMB?.blocks) {
11231106
for (const sourceBlock of field?.blocks ?? []) {
1124-
// Find matching child block in target by UID
1125-
const targetBlock = currentModularBlock?.blocks?.find((tb: any) =>
1126-
tb?.uid === sourceBlock?.uid
1127-
);
1128-
1129-
if (targetBlock && targetBlock?.schema) {
1130-
// Merge the schemas of matching child blocks
1131-
const additionalFields = [];
1132-
1133-
for (const targetField of targetBlock?.schema ?? []) {
1134-
// Check if this field already exists in source block
1135-
const existsInSource = sourceBlock?.schema?.find((sf: any) =>
1136-
sf?.uid === targetField?.uid && sf?.data_type === targetField?.data_type
1137-
);
1138-
1139-
if (!existsInSource) {
1140-
additionalFields.push(targetField);
1141-
}
1142-
}
1143-
1144-
// Merge source and target fields, removing duplicates
1145-
sourceBlock.schema = removeDuplicateFields([
1146-
...sourceBlock?.schema ?? [],
1147-
...additionalFields
1148-
]);
1149-
}
1150-
}
1151-
1152-
// Add any child blocks from target that don't exist in source
1153-
const additionalBlocks = [];
1154-
for (const targetBlock of currentModularBlock?.blocks ?? []) {
1155-
const existsInSource = field?.blocks?.find((sb: any) =>
1156-
sb?.uid === targetBlock?.uid
1157-
);
1158-
1159-
if (!existsInSource) {
1160-
additionalBlocks.push(targetBlock);
1107+
const targetBlock = targetMB?.blocks?.find((tb: any) => tb?.uid === sourceBlock?.uid);
1108+
1109+
if (targetBlock?.schema) {
1110+
const additional = (targetBlock?.schema ?? [])?.filter((tField: any) =>
1111+
!sourceBlock?.schema?.find((sField: any) => sField?.uid === tField?.uid && sField?.data_type === tField?.data_type)
1112+
);
1113+
sourceBlock.schema = removeDuplicateFields([...sourceBlock?.schema ?? [], ...additional]);
1114+
mergeSchemaFields(sourceBlock.schema, targetBlock.schema ?? []);
11611115
}
11621116
}
1163-
1164-
field.blocks = removeDuplicateFields([
1165-
...field?.blocks ?? [],
1166-
...additionalBlocks
1167-
]);
1117+
1118+
const additionalBlocks = (targetMB?.blocks ?? []).filter((tb: any) =>
1119+
!field?.blocks?.find((sb: any) => sb?.uid === tb?.uid)
1120+
);
1121+
field.blocks = removeDuplicateFields([...field?.blocks ?? [], ...additionalBlocks]);
11681122
}
11691123
}
11701124
}
1125+
}
1126+
1127+
const mergeTwoCts = async (ct: any, mergeCts: any) => {
1128+
const ctData: any = {
1129+
...ct,
1130+
title: mergeCts?.title,
1131+
uid: mergeCts?.uid,
1132+
options: {
1133+
"singleton": false,
1134+
}
1135+
}
1136+
1137+
mergeSchemaFields(ctData?.schema ?? [], mergeCts?.schema ?? []);
1138+
11711139
ctData.schema = await mergeArrays(ctData?.schema, mergeCts?.schema) ?? [];
11721140

11731141
return ctData;

ui/src/components/ContentMapper/contentMapper.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export interface OptionsType {
174174
label?: string;
175175
value?: ContentTypesSchema;
176176
isDisabled?: boolean;
177+
uid?: string;
177178
}
178179

179180
export interface ExstingContentTypeMatch {

0 commit comments

Comments
 (0)