Skip to content

Commit c483699

Browse files
Enhance global field handling and content type mapping
- Updated createGlobalField function to improve merging logic by utilizing a Set for existing UIDs. - Modified existingCtMapper to handle 'global_field' type, adjusting request parameters accordingly for better content type retrieval. - Ensured compatibility with different content type structures by passing the type parameter in the existingCtMapper call.
2 parents 3728932 + c08a6c1 commit c483699

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

api/src/services/globalField.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,19 @@ const createGlobalField = async ({
7272
}
7373
}
7474

75+
const safeFileGlobalFields = fileGlobalFields;
76+
77+
const existingUids = new Set(
78+
safeFileGlobalFields?.map?.((gf: { uid: string }) => gf?.uid)
79+
);
80+
7581
const mergedGlobalFields = [
76-
...globalfields,
77-
...(fileGlobalFields?.filter(
78-
(fileField: { uid: string }) =>
79-
!globalfields?.some((gf: { uid: string }) => gf?.uid === fileField?.uid)
80-
) || [])
82+
...globalfields.filter(
83+
(fileField: { uid: string }) => !existingUids?.has(fileField?.uid)
84+
),
85+
...safeFileGlobalFields,
8186
];
87+
8288
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
8389
await fs.promises.writeFile(filePath, JSON.stringify(mergedGlobalFields, null, 2));
8490

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -964,23 +964,42 @@ const writeGlobalField = async (schema: any, globalSave: string) => {
964964
}
965965
};
966966

967-
const existingCtMapper = async ({ keyMapper, contentTypeUid, projectId, region, user_id }: any) => {
967+
const existingCtMapper = async ({ keyMapper, contentTypeUid, projectId, region, user_id, type}: any) => {
968968
try {
969969
const ctUid = keyMapper?.[contentTypeUid];
970-
const req: any = {
971-
params: {
972-
projectId,
973-
contentTypeUid: ctUid
974-
},
975-
body: {
976-
token_payload: {
977-
region,
978-
user_id
970+
971+
if(type === 'global_field') {
972+
973+
const req: any = {
974+
params: {
975+
projectId,
976+
globalFieldUid: ctUid
977+
},
978+
body: {
979+
token_payload: {
980+
region,
981+
user_id
982+
}
983+
}
984+
}
985+
const contentTypeSchema = await contentMapperService.getSingleGlobalField(req);
986+
return contentTypeSchema ?? null;
987+
} else {
988+
const req: any = {
989+
params: {
990+
projectId,
991+
contentTypeUid: ctUid
992+
},
993+
body: {
994+
token_payload: {
995+
region,
996+
user_id
997+
}
979998
}
980999
}
1000+
const contentTypeSchema = await contentMapperService.getExistingContentTypes(req);
1001+
return contentTypeSchema?.selectedContentType ?? null;
9811002
}
982-
const contentTypeSchema = await contentMapperService.getExistingContentTypes(req);
983-
return contentTypeSchema?.selectedContentType;
9841003
} catch (err) {
9851004
console.error("Error while getting the existing contentType from contenstack", err)
9861005
return {};
@@ -1042,7 +1061,7 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
10421061
if (Object?.keys?.(keyMapper)?.length &&
10431062
keyMapper?.[contentType?.contentstackUid] !== "" &&
10441063
keyMapper?.[contentType?.contentstackUid] !== undefined) {
1045-
currentCt = await existingCtMapper({ keyMapper, contentTypeUid: contentType?.contentstackUid, projectId, region, user_id });
1064+
currentCt = await existingCtMapper({ keyMapper, contentTypeUid: contentType?.contentstackUid, projectId, region, user_id , type: contentType?.type});
10461065
}
10471066

10481067
// Safe: ensures we never pass undefined to the builder

0 commit comments

Comments
 (0)