Skip to content

Commit 65d2ef6

Browse files
committed
fix:resolved the bug [CMG-622] mapping of nested group in content mapper
1 parent 9a4b280 commit 65d2ef6

File tree

2 files changed

+85
-34
lines changed

2 files changed

+85
-34
lines changed

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

Lines changed: 84 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,19 @@ const arrangGroups = ({ schema, newStack }: any) => {
7474
schema?.forEach((item: any) => {
7575
if (item?.contentstackFieldType === 'group') {
7676
const groupSchema: any = { ...item, schema: [] }
77+
if (item?.contentstackFieldUid?.includes('.')) {
78+
const parts = item.contentstackFieldUid.split('.');
79+
groupSchema.contentstackFieldUid = parts[parts.length - 1];
80+
}
7781
schema?.forEach((et: any) => {
7882
if (et?.contentstackFieldUid?.includes(`${item?.contentstackFieldUid}.`) ||
7983
(newStack === false && et?.uid?.includes(`${item?.uid}.`))) {
84+
const target = groupSchema?.contentstackFieldUid;
85+
const index = et?.contentstackFieldUid?.indexOf(target);
86+
87+
if (index > 0) {
88+
et.contentstackFieldUid = et?.contentstackFieldUid?.substring(index);
89+
}
8090
groupSchema?.schema?.push(et);
8191
}
8292
})
@@ -112,7 +122,7 @@ const saveAppMapper = async ({ marketPlacePath, data, fileName }: any) => {
112122
}
113123
}
114124

115-
const convertToSchemaFormate = ({ field, advanced = true, marketPlacePath }: any) => {
125+
const convertToSchemaFormate = ({ field, advanced = true, marketPlacePath, keyMapper }: any) => {
116126
switch (field?.contentstackFieldType) {
117127
case 'single_line_text': {
118128
return {
@@ -435,7 +445,7 @@ const convertToSchemaFormate = ({ field, advanced = true, marketPlacePath }: any
435445
return {
436446
data_type: "reference",
437447
display_name: field?.title,
438-
reference_to: field?.refrenceTo ?? [],
448+
reference_to: field?.refrenceTo?.map((item:string) => keyMapper[item] || item) ?? [],
439449
field_metadata: {
440450
ref_multiple: true,
441451
ref_multiple_content_types: true
@@ -654,45 +664,85 @@ const existingCtMapper = async ({ keyMapper, contentTypeUid, projectId, region,
654664
}
655665
}
656666

657-
const mergeArrays = async (a: any[], b: any[]) => {
658-
for await (const fieldGp of b) {
659-
const exists = a.some(fld =>
660-
fld?.uid === fieldGp?.uid &&
661-
fld?.data_type === fieldGp?.data_type
667+
const mergeArrays = (a: any[], b: any[]): any[] => {
668+
const result = [...a];
669+
670+
for (const field of b) {
671+
const exists = result.some(f =>
672+
f?.uid === field?.uid &&
673+
f?.data_type === field?.data_type
662674
);
663675
if (!exists) {
664-
a.push(fieldGp);
676+
result.push(field);
677+
}
678+
}
679+
680+
return result;
681+
};
682+
683+
const mergeFields = async (schema1: any[], schema2: any[]): Promise<any[]> => {
684+
const result: any[] = [];
685+
686+
for (const field2 of schema2) {
687+
if (field2.data_type === 'group') {
688+
const machingGroup = findGroupByUid(schema1, field2?.uid);
689+
if(machingGroup){
690+
const schema = await mergeArrays(machingGroup?.schema ?? [],field2?.schema ?? [] );
691+
result.push({
692+
...field2,
693+
schema: schema
694+
});
695+
}
696+
else{
697+
result.push({
698+
...field2,
699+
schema: await mergeFields(schema1, field2?.schema)
700+
})
701+
}
702+
}
703+
else{
704+
const exists = schema1.find(
705+
(fld) =>
706+
fld.uid === field2.uid &&
707+
fld.data_type === field2.data_type
708+
);
709+
if (!exists) {
710+
result.push(field2);
711+
}
712+
665713
}
714+
666715
}
667-
return a;
716+
return result;
668717
}
669718

719+
720+
const findGroupByUid = (
721+
schema: any[],
722+
uid: string,
723+
excludeRef: any = null
724+
): any | undefined => {
725+
for (const field of schema) {
726+
if (field.data_type === 'group') {
727+
if (field.uid === uid && field !== excludeRef) return field;
728+
const nested = findGroupByUid(field.schema ?? [], uid, excludeRef);
729+
if (nested) return nested;
730+
}
731+
}
732+
return undefined;
733+
};
734+
670735
const mergeTwoCts = async (ct: any, mergeCts: any) => {
671-
const ctData: any = {
672-
...ct,
736+
const merged :any = {
673737
title: mergeCts?.title,
674738
uid: mergeCts?.uid,
675739
options: {
676740
"singleton": false,
677-
}
678-
}
679-
for await (const field of ctData?.schema ?? []) {
680-
if (field?.data_type === 'group') {
681-
const currentGroup = mergeCts?.schema?.find((grp: any) => grp?.uid === field?.uid &&
682-
grp?.data_type === 'group');
683-
const group = [];
684-
for await (const fieldGp of currentGroup?.schema ?? []) {
685-
const fieldNst = field?.schema?.find((fld: any) => fld?.uid === fieldGp?.uid &&
686-
fld?.data_type === fieldGp?.data_type);
687-
if (fieldNst === undefined) {
688-
group?.push(fieldGp);
689-
}
690-
}
691-
field.schema = [...field?.schema ?? [], ...group];
692-
}
693-
}
694-
ctData.schema = await mergeArrays(ctData?.schema, mergeCts?.schema) ?? [];
695-
return ctData;
741+
},
742+
schema: await mergeFields(ct?.schema ?? [], mergeCts?.schema ?? [])
743+
};
744+
745+
return merged;
696746
}
697747

698748
export const contenTypeMaker = async ({ contentType, destinationStackId, projectId, newStack, keyMapper, region, user_id }: any) => {
@@ -717,7 +767,7 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
717767
"display_name": item?.contentstackField,
718768
"field_metadata": {},
719769
"schema": [],
720-
"uid": item?.contentstackFieldUid,
770+
"uid": item?.contentstackFieldUid?.replace(/\./g, '_'),
721771
"multiple": false,
722772
"mandatory": false,
723773
"unique": false
@@ -728,7 +778,7 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
728778
uid: extractValue(element?.contentstackFieldUid, item?.contentstackFieldUid, '.'),
729779
title: extractValue(element?.contentstackField, item?.contentstackField, ' >')?.trim(),
730780
}
731-
const schema: any = convertToSchemaFormate({ field, marketPlacePath });
781+
const schema: any = convertToSchemaFormate({ field, marketPlacePath, keyMapper});
732782
if (typeof schema === 'object' && Array.isArray(group?.schema) && element?.isDeleted === false) {
733783
group.schema.push(schema);
734784
}
@@ -741,7 +791,8 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
741791
title: item?.contentstackField,
742792
uid: item?.contentstackFieldUid
743793
},
744-
marketPlacePath
794+
marketPlacePath,
795+
keyMapper
745796
});
746797
if (dt && item?.isDeleted === false) {
747798
ct?.schema?.push(dt);

api/src/utils/field-attacher.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const fieldAttacher = async ({ projectId, orgId, destinationStackId, regi
2222
contentType.fieldMapping = contentType?.fieldMapping?.map((fieldUid: any) => {
2323
const field = FieldMapperModel.chain
2424
.get("field_mapper")
25-
.find({ id: fieldUid, projectId: projectId })
25+
.find({ id: fieldUid, projectId: projectId , contentTypeId: contentId})
2626
.value()
2727
return field;
2828
})

0 commit comments

Comments
 (0)