Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[CMG-311], [CMG-326]
  • Loading branch information
sayalijoshi27 committed Oct 1, 2024
commit 10819d2261c659e5f1464d5cd7a335f6050283cb
15 changes: 14 additions & 1 deletion api/src/controllers/projects.contentMapper.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ const getSingleContentTypes = async (
res.status(201).json(resp);
};

/**
* Retrieves single global field.
*
* @param req - The request object.
* @param res - The response object.
* @returns A Promise that resolves to void.
*/
const getSingleGlobalField = async(req: Request, res: Response): Promise<void> => {
const resp = await contentMapperService.getSingleGlobalField(req);
res.status(201).json(resp);
}

/**
* update content mapping details a project.
*
Expand All @@ -148,5 +160,6 @@ export const contentMapperController = {
getSingleContentTypes,
removeContentMapper,
updateContentMapper,
getExistingGlobalFields
getExistingGlobalFields,
getSingleGlobalField
};
15 changes: 13 additions & 2 deletions api/src/routes/contentMapper.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ router.get(
* @route GET /:projectId
*/
router.get(
"/globalFields/:projectId",
"/:projectId/globalFields",
asyncRouter(contentMapperController.getExistingGlobalFields)
);

Expand Down Expand Up @@ -85,7 +85,18 @@ router.get(
asyncRouter(contentMapperController.removeContentMapper)
);

//update content mapper details
/**
* Update content mapper
* @route GET /:orgId/:projectId
*/
router.patch("/:orgId/:projectId/mapper_keys", asyncRouter(contentMapperController.updateContentMapper));

/**
* Get Single Global Field data
* @route GET /:projectId/:globalFieldUid
*/
router.get("/:projectId/globalFields/:globalFieldUid",
asyncRouter(contentMapperController.getSingleGlobalField)
);

export default router;
52 changes: 50 additions & 2 deletions api/src/services/contentMapper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,9 +863,56 @@ const getSingleContentTypes = async (req: Request) => {
return {
title: res?.data?.content_type?.title,
uid: res?.data?.content_type?.uid,
schema: res?.data?.content_type?.schema,
schema: res?.data?.content_type?.schema
};
};

/**
* Retrieves a single global field from the specified project.
* @param req - The request object containing the project ID, content type UID, and token payload.
* @returns An object containing the title, UID, and schema of the content type, or an error object if an error occurs.
*/
const getSingleGlobalField = async (req: Request) => {
const projectId = req?.params?.projectId;
const globalFieldUID = req?.params?.globalFieldUid;
const { token_payload } = req.body;

const authtoken = await getAuthtoken(
token_payload?.region,
token_payload?.user_id
);
await ProjectModelLowdb.read();
const project = ProjectModelLowdb.chain
.get("projects")
.find({ id: projectId })
.value();
const stackId = project?.destination_stack_id;

const [err, res] = await safePromise(
https({
method: "GET",
url: `${config.CS_API[
token_payload?.region as keyof typeof config.CS_API
]!}/global_fields/${globalFieldUID}`,
headers: {
api_key: stackId,
authtoken: authtoken,
},
})
);

if (err)
return {
data: err.response.data,
status: err.response.status,
};

return {
title: res?.data?.global_field?.title,
uid: res?.data?.global_field?.uid,
schema: res?.data?.global_field?.schema
};
}
/**
* Removes the content mapping for a project.
* @param req - The request object containing the project ID.
Expand Down Expand Up @@ -1037,5 +1084,6 @@ export const contentMapperService = {
removeMapping,
getSingleContentTypes,
updateContentMapper,
getExistingGlobalFields
getExistingGlobalFields,
getSingleGlobalField
};
175 changes: 107 additions & 68 deletions ui/src/components/ContentMapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
updateContentType,
resetToInitialMapping,
fetchExistingContentType,
updateContentMapper
updateContentMapper,
fetchGlobalField
} from '../../services/api/migration.service';

// Redux
Expand Down Expand Up @@ -223,8 +224,6 @@ const ContentMapper = forwardRef((props, ref: React.ForwardedRef<ContentTypeSave
}= {}
} = migrationData;

// const contentTypesList = awau

const [tableData, setTableData] = useState<FieldMapType[]>([]);
const [loading, setLoading] = useState(false);
const [isLoading, setIsLoading] = useState(newMigrationData?.isprojectMapped);
Expand All @@ -237,8 +236,8 @@ const ContentMapper = forwardRef((props, ref: React.ForwardedRef<ContentTypeSave
const [otherCmsTitle, setOtherCmsTitle] = useState(contentTypes[0]?.otherCmsTitle);
const [contentTypeUid, setContentTypeUid] = useState<string>('');

const [existingContentTypes, setExistingContentTypes] = useState<ContentTypeList[]>([]);
const [existingGlobalFields, setExistingGlobalFields] = useState<ContentTypeList[]>([])
const [existingContentTypes, setExistingContentTypes] = useState<ContentTypeList[]>(newMigrationData?.content_mapping?.existingCT);
const [existingGlobalFields, setExistingGlobalFields] = useState<ContentTypeList[]>(newMigrationData?.content_mapping?.existingGlobal)
const [isContentType, setIsContentType] = useState<boolean>(contentTypes?.[0]?.type === "content_type");
const [contentModels, setContentModels] = useState<ContentTypeList[]>([]);

Expand Down Expand Up @@ -316,8 +315,6 @@ const ContentMapper = forwardRef((props, ref: React.ForwardedRef<ContentTypeSave
});

fetchContentTypes(searchText || '');
fetchExistingContentTypes();
fetchExistingGlobalFields();
}, []);

// Make title and url field non editable
Expand Down Expand Up @@ -352,7 +349,7 @@ const ContentMapper = forwardRef((props, ref: React.ForwardedRef<ContentTypeSave

}

}, [contentTypeMapped, otherCmsTitle, contentModels]);
}, [contentTypeMapped, otherCmsTitle, existingContentTypes, existingGlobalFields]);

useEffect(()=>{
if(isContentDeleted) {
Expand Down Expand Up @@ -394,7 +391,6 @@ const ContentMapper = forwardRef((props, ref: React.ForwardedRef<ContentTypeSave

// 2nd level group nesting
if (childSchema?.schema) {
// console.log("!!!!!!!!!!!!!!!!!!!", row, childSchema);
childSchema?.schema?.forEach((nestedSchema) => {
if (row?.contentstackField === `${schema?.display_name} > ${childSchema?.display_name} > ${nestedSchema?.display_name}`) {
if(!isFieldDeleted) {
Expand Down Expand Up @@ -464,6 +460,17 @@ const ContentMapper = forwardRef((props, ref: React.ForwardedRef<ContentTypeSave
} else {
setContentModels(existingGlobalFields);
}

const mappedContentType = existingContentTypes?.find((item:ContentTypeList)=>item?.title === contentTypeMapped?.[otherCmsTitle]);

if (mappedContentType?.uid) {
setOtherContentType({
id: mappedContentType?.uid,
label: contentTypeMapped?.[otherCmsTitle],
value: contentTypeMapped?.[otherCmsTitle],
});
setIsContentDeleted(false);
}
}, [existingContentTypes, existingGlobalFields, isContentType])

// To close the filter panel on outside click
Expand Down Expand Up @@ -1620,39 +1627,107 @@ const ContentMapper = forwardRef((props, ref: React.ForwardedRef<ContentTypeSave

// Function to fetch single content type
const handleFetchContentType = async () => {
const { data } = await fetchExistingContentType(projectId,'') ;
if(data?.contentTypes?.length <= 0){
Notification({
notificationContent: { text: "No content found in the stack" },
notificationProps: {
position: 'bottom-center',
hideProgressBar: false
},
type: 'error'
});
}
const contentTypesArr: ContentTypeList[] = contentModels;
const index = contentModels.findIndex(ct => ct?.uid === data?.uid);
if (isContentType) {
const { data , status} = await fetchExistingContentType(projectId, otherContentType?.id ?? '');

if (status == 201 && data?.contentTypes?.length > 0) {
Notification({
notificationContent: { text: 'Content type data fetched successfully' },
notificationProps: {
position: 'bottom-center',
hideProgressBar: false
},
type: 'success'
});
} else if(status == 201 && data?.contentTypes?.length <= 0) {
Notification({
notificationContent: { text: "No content found in the stack" },
notificationProps: {
position: 'bottom-center',
hideProgressBar: false
},
type: 'error'
});
}

const contentTypesArr: ContentTypeList[] = contentModels;
const index = contentModels.findIndex(ct => ct?.uid === data?.uid);

if(index != -1) {
contentTypesArr[index] = data;
}
if(index != -1) {
contentTypesArr[index] = data;
}

setContentModels(data?.contentTypes);
setContentModels(data?.contentTypes);
setContentTypeSchema(data?.schema);


// const content_type = data?.contentTypes?.find((item: ContentTypeList)=>item?.title === otherContentType?.label);
// const contentTypeKey = Object.keys(contentTypeMapped).find(key => contentTypeMapped[key] === otherContentType?.label);


// if(! content_type && contentTypeKey) {
// const updatedState = { ...contentTypeMapped };
// delete updatedState[contentTypeKey];

// setContentTypeMapped((prevState: ContentTypeMap) => {
// const newState = { ...prevState };

// delete newState[contentTypeKey]

// return newState;
// });
// await updateContentMapper(selectedOrganisation?.value, projectId, {... updatedState} );
// setOtherContentType({
// label: `Select ${isContentType ? 'Content Type' : 'Global Field'} from Existing Stack`,
// value: `Select ${isContentType ? 'Content Type' : 'Global Field'} from Existing Stack`

// });
// }
} else {
const { data, status } = await fetchGlobalField(projectId, otherContentType?.id ?? '');

if (status == 201 && data?.globalFields?.length > 0) {
Notification({
notificationContent: { text: 'Global field data fetched successfully' },
notificationProps: {
position: 'bottom-center',
hideProgressBar: false
},
type: 'success'
});
} else if(status == 201 && data?.globalFields?.length <= 0) {
Notification({
notificationContent: { text: "No global field in the stack" },
notificationProps: {
position: 'bottom-center',
hideProgressBar: false
},
type: 'error'
});
}

const content_type = data?.contentTypes?.find((item: ContentTypeList)=>item?.title === otherContentType?.label);
const contentTypeKey = Object.keys(contentTypeMapped).find(key => contentTypeMapped[key] === otherContentType?.label);
const index = contentModels.findIndex(ct => ct?.uid === data?.uid);


if(! content_type && contentTypeKey){
const contentTypesArr: ContentTypeList[] = contentModels;

if(index != -1) {
contentTypesArr[index] = data;
}
setContentModels(data?.globalFields);
setContentTypeSchema(data?.schema);
}

const contentField = contentModels?.find((item: ContentTypeList)=>item?.title === otherContentType?.label);
const contentFieldKey = Object.keys(contentTypeMapped).find(key => contentTypeMapped[key] === otherContentType?.label);

if(! contentField && contentFieldKey) {
const updatedState = { ...contentTypeMapped };
delete updatedState[contentTypeKey];
delete updatedState[contentFieldKey];

setContentTypeMapped((prevState: ContentTypeMap) => {
const newState = { ...prevState };

delete newState[contentTypeKey]
delete newState[contentFieldKey]

return newState;
});
Expand All @@ -1663,42 +1738,6 @@ const ContentMapper = forwardRef((props, ref: React.ForwardedRef<ContentTypeSave

});
}

if (otherContentType?.label === "Select Content Type") {
Notification({
notificationContent: { text: "Please Select a Content Type to fetch." },
notificationProps: {
position: 'bottom-center',
hideProgressBar: false
},
type: 'error'
});
} else {
const { data , status} = await fetchExistingContentType(projectId, otherContentType?.id ?? '');

const index = contentModels.findIndex(ct => ct?.uid === data?.uid);

const contentTypesArr: ContentTypeList[] = contentModels;

if(index != -1) {
contentTypesArr[index] = data;
}

//setContentTypesList(contentTypesArr);
setContentTypeSchema(data?.schema);
if (status == 201) {
Notification({
notificationContent: { text: 'Content type data fetched successfully' },
notificationProps: {
position: 'bottom-center',
hideProgressBar: false
},
type: 'success'
});
}


}
}

const columns = [
Expand Down
4 changes: 4 additions & 0 deletions ui/src/context/app/app.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export interface IDestinationStack {
stackArray: IDropDown[];
}
export interface IContentMapper {
existingGlobal: ContentTypeList[] | (() => ContentTypeList[]);
existingCT: ContentTypeList[] | (() => ContentTypeList[]);
content_type_mapping: ContentTypeMap;
isDropDownChanged?: boolean;
otherCmsTitle?: string;
Expand Down Expand Up @@ -311,6 +313,8 @@ export const DEFAULT_CONTENT_MAPPER: IContentMapper = {
isDropDownChanged: false,
otherCmsTitle: '',
contentTypeList: [],
existingCT: [],
existingGlobal: []
};

export const DEFAULT_TEST_MIGRATION: ITestMigration = {
Expand Down
Loading