Skip to content
22 changes: 10 additions & 12 deletions api/src/services/aem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,10 +690,11 @@ function processFieldsRecursive(

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

const isMultiple =
(field?.multiple === true) ||
(field?.advanced && field.advanced.multiple === true) ||
(field?.advanced?.multiple !== undefined ? field.advanced.multiple === true : field?.multiple === true) ||
(field?.maxInstance && field.maxInstance > 1);

const isCarouselItems =
Expand All @@ -710,7 +711,7 @@ function processFieldsRecursive(
if (isCarouselItems) {
groupValue = items;
} else {
groupValue = items?.[field?.uid]?.items ?? items?.[field?.uid];
groupValue = items?.[aemGroupSourceKey]?.items ?? items?.[aemGroupSourceKey];
}

if (isMultiple) {
Expand Down Expand Up @@ -897,7 +898,7 @@ function processFieldsRecursive(
const order2 = Array.isArray(items?.[':itemsOrder']) ? items[':itemsOrder'] : null;
const map2 = items?.[':items'] || items;
if (order2 && map2) {
const baseUid = field?.uid;
const baseUid = aemGroupSourceKey;
const keysForThisGroup = order2.filter(
(k) => k === baseUid || new RegExp(`^${baseUid}_`).test(k)
);
Expand Down Expand Up @@ -925,15 +926,12 @@ function processFieldsRecursive(
}
} else {
if (Array.isArray(groupValue)) {
const groupData: unknown[] = [];
if (Array.isArray(field?.schema)) {
for (const element of groupValue) {
groupData.push(
processFieldsRecursive(field.schema, element, title, pathToUidMap, assetDetailsMap)
);
}
const firstElement = groupValue[0];
if (Array.isArray(field?.schema) && firstElement) {
obj[uid] = processFieldsRecursive(field.schema, firstElement, title, pathToUidMap, assetDetailsMap);
} else {
obj[uid] = {};
}
obj[uid] = groupData;
} else {
if (Array.isArray(field?.schema)) {
const value = processFieldsRecursive(field.schema, groupValue, title, pathToUidMap, assetDetailsMap);
Expand Down
112 changes: 40 additions & 72 deletions api/src/utils/content-type-creator.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,93 +1081,61 @@ const mergeArrays = async (a: any[], b: any[]) => {
return a;
}

const mergeTwoCts = async (ct: any, mergeCts: any) => {
const ctData: any = {
...ct,
title: mergeCts?.title,
uid: mergeCts?.uid,
options: {
"singleton": false,
}
}

for await (const field of ctData?.schema ?? []) {
// Handle regular groups
function mergeSchemaFields(sourceSchema: any[], targetSchema: any[]) {
for (const field of sourceSchema) {
if (field?.data_type === 'group') {
const currentGroup = mergeCts?.schema?.find((grp: any) =>
const targetGroup = targetSchema?.find((grp: Group) =>
grp?.uid === field?.uid && grp?.data_type === 'group'
);

if (currentGroup) {
const group = [];
for await (const fieldGp of currentGroup?.schema ?? []) {
const fieldNst = field?.schema?.find((fld: any) =>
fld?.uid === fieldGp?.uid && fld?.data_type === fieldGp?.data_type
);
if (fieldNst === undefined) {
group?.push(fieldGp);
}
}
field.schema = removeDuplicateFields([...field?.schema ?? [], ...group]);

if (targetGroup) {
const additional = (targetGroup?.schema ?? []).filter((tField: Group) =>
!field?.schema?.find((sField: Group) => sField?.uid === tField?.uid && sField?.data_type === tField?.data_type)
);
field.schema = removeDuplicateFields([...field?.schema ?? [], ...additional]);
mergeSchemaFields(field?.schema, targetGroup?.schema ?? []);
}
}

// Handle modular blocks
if (field?.data_type === 'blocks') {
const currentModularBlock = mergeCts?.schema?.find((mb: any) =>
const targetMB = targetSchema?.find((mb: any) =>
mb?.uid === field?.uid && mb?.data_type === 'blocks'
);

if (currentModularBlock && currentModularBlock?.blocks) {
// Iterate through each child block in the source

if (targetMB?.blocks) {
for (const sourceBlock of field?.blocks ?? []) {
// Find matching child block in target by UID
const targetBlock = currentModularBlock?.blocks?.find((tb: any) =>
tb?.uid === sourceBlock?.uid
);

if (targetBlock && targetBlock?.schema) {
// Merge the schemas of matching child blocks
const additionalFields = [];

for (const targetField of targetBlock?.schema ?? []) {
// Check if this field already exists in source block
const existsInSource = sourceBlock?.schema?.find((sf: any) =>
sf?.uid === targetField?.uid && sf?.data_type === targetField?.data_type
);

if (!existsInSource) {
additionalFields.push(targetField);
}
}

// Merge source and target fields, removing duplicates
sourceBlock.schema = removeDuplicateFields([
...sourceBlock?.schema ?? [],
...additionalFields
]);
}
}

// Add any child blocks from target that don't exist in source
const additionalBlocks = [];
for (const targetBlock of currentModularBlock?.blocks ?? []) {
const existsInSource = field?.blocks?.find((sb: any) =>
sb?.uid === targetBlock?.uid
);

if (!existsInSource) {
additionalBlocks.push(targetBlock);
const targetBlock = targetMB?.blocks?.find((tb: any) => tb?.uid === sourceBlock?.uid);

if (targetBlock?.schema) {
const additional = (targetBlock?.schema ?? [])?.filter((tField: any) =>
!sourceBlock?.schema?.find((sField: any) => sField?.uid === tField?.uid && sField?.data_type === tField?.data_type)
);
sourceBlock.schema = removeDuplicateFields([...sourceBlock?.schema ?? [], ...additional]);
mergeSchemaFields(sourceBlock.schema, targetBlock.schema ?? []);
}
}
field.blocks = removeDuplicateFields([
...field?.blocks ?? [],
...additionalBlocks
]);

const additionalBlocks = (targetMB?.blocks ?? []).filter((tb: any) =>
!field?.blocks?.find((sb: any) => sb?.uid === tb?.uid)
);
field.blocks = removeDuplicateFields([...field?.blocks ?? [], ...additionalBlocks]);
}
}
}
}

const mergeTwoCts = async (ct: any, mergeCts: any) => {
const ctData: any = {
...ct,
title: mergeCts?.title,
uid: mergeCts?.uid,
options: {
"singleton": false,
}
}

mergeSchemaFields(ctData?.schema ?? [], mergeCts?.schema ?? []);

ctData.schema = await mergeArrays(ctData?.schema, mergeCts?.schema) ?? [];

return ctData;
Expand Down
Loading