Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions api/src/services/globalField.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ const createGlobalField = async ({
}
}

const safeFileGlobalFields = fileGlobalFields;

const existingUids = new Set(
safeFileGlobalFields?.map?.((gf: { uid: string }) => gf?.uid)
);

const mergedGlobalFields = [
...globalfields,
...(fileGlobalFields?.filter(
(fileField: { uid: string }) =>
!globalfields?.some((gf: { uid: string }) => gf?.uid === fileField?.uid)
) || [])
...globalfields.filter(
(fileField: { uid: string }) => !existingUids?.has(fileField?.uid)
),
...safeFileGlobalFields,
];

await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
await fs.promises.writeFile(filePath, JSON.stringify(mergedGlobalFields, null, 2));

Expand Down
45 changes: 32 additions & 13 deletions api/src/utils/content-type-creator.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,23 +964,42 @@ const writeGlobalField = async (schema: any, globalSave: string) => {
}
};

const existingCtMapper = async ({ keyMapper, contentTypeUid, projectId, region, user_id }: any) => {
const existingCtMapper = async ({ keyMapper, contentTypeUid, projectId, region, user_id, type}: any) => {
try {
const ctUid = keyMapper?.[contentTypeUid];
const req: any = {
params: {
projectId,
contentTypeUid: ctUid
},
body: {
token_payload: {
region,
user_id

if(type === 'global_field') {

const req: any = {
params: {
projectId,
globalFieldUid: ctUid
},
body: {
token_payload: {
region,
user_id
}
}
}
const contentTypeSchema = await contentMapperService.getSingleGlobalField(req);
return contentTypeSchema ?? null;
} else {
const req: any = {
params: {
projectId,
contentTypeUid: ctUid
},
body: {
token_payload: {
region,
user_id
}
}
}
const contentTypeSchema = await contentMapperService.getExistingContentTypes(req);
return contentTypeSchema?.selectedContentType ?? null;
}
const contentTypeSchema = await contentMapperService.getExistingContentTypes(req);
return contentTypeSchema?.selectedContentType;
} catch (err) {
console.error("Error while getting the existing contentType from contenstack", err)
return {};
Expand Down Expand Up @@ -1042,7 +1061,7 @@ export const contenTypeMaker = async ({ contentType, destinationStackId, project
if (Object?.keys?.(keyMapper)?.length &&
keyMapper?.[contentType?.contentstackUid] !== "" &&
keyMapper?.[contentType?.contentstackUid] !== undefined) {
currentCt = await existingCtMapper({ keyMapper, contentTypeUid: contentType?.contentstackUid, projectId, region, user_id });
currentCt = await existingCtMapper({ keyMapper, contentTypeUid: contentType?.contentstackUid, projectId, region, user_id , type: contentType?.type});
}

// Safe: ensures we never pass undefined to the builder
Expand Down