Skip to content
Merged

Dev #290

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
19 changes: 18 additions & 1 deletion api/src/controllers/projects.contentMapper.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ const getExistingContentTypes = async (
const resp = await contentMapperService.getExistingContentTypes(req);
res.status(201).json(resp);
};

/**
* Retrieves the existing global fields.
*
* @param {Request} req - The request object.
* @param {Response} res - The response object.
* @returns {Promise<void>} - A promise that resolves when the operation is complete.
*/
const getExistingGlobalFields = async (
req: Request,
res: Response
): Promise<void> => {
const resp = await contentMapperService.getExistingGlobalFields(req);
res.status(201).json(resp);
};

/**
* Updates the content type fields.
*
Expand Down Expand Up @@ -131,5 +147,6 @@ export const contentMapperController = {
// removeMapping,
getSingleContentTypes,
removeContentMapper,
updateContentMapper
updateContentMapper,
getExistingGlobalFields
};
9 changes: 9 additions & 0 deletions api/src/routes/contentMapper.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ router.get(
asyncRouter(contentMapperController.getExistingContentTypes)
);

/**
* Get Existing GlobalFields List
* @route GET /:projectId
*/
router.get(
"/globalFields/:projectId",
asyncRouter(contentMapperController.getExistingGlobalFields)
);

/**
* Update FieldMapping or contentType
* @route PUT /contentTypes/:orgId/:projectId/:contentTypeId
Expand Down
58 changes: 53 additions & 5 deletions api/src/services/contentMapper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ const getExistingContentTypes = async (req: Request) => {
data: err.response.data,
status: err.response.status,
};

const contentTypes = res.data.content_types.map((singleCT: any) => {
return {
title: singleCT.title,
Expand All @@ -280,6 +279,57 @@ const getExistingContentTypes = async (req: Request) => {
//Add logic to get Project from DB
return { contentTypes };
};

/**
* Retrieves existing global fields for a given project.
* @param req - The request object containing the project ID and token payload.
* @returns An object containing the retrieved content types.
*/
const getExistingGlobalFields = async (req: Request) => {
const projectId = req?.params?.projectId;

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`,
headers: {
api_key: stackId,
authtoken: authtoken,
},
})
);

if (err)
return {
data: err.response.data,
status: err.response.status,
};
const globalFields = res.data.global_fields.map((global: any) => {
return {
title: global.title,
uid: global.uid,
schema: global.schema,
};
});

//Add logic to get Project from DB
return { globalFields };
};

/**
* Updates the content type based on the provided request.
* @param req - The request object containing the necessary parameters and data.
Expand Down Expand Up @@ -923,8 +973,6 @@ const removeContentMapper = async (req: Request) => {
* @throws ExceptionFunction if an error occurs during the update.
*/
const updateContentMapper = async (req: Request) => {
console.info("updateContentMapper", req.params, req.body);

const { orgId, projectId } = req.params;
const { token_payload, content_mapper } = req.body;
const srcFunc = "updateContentMapper";
Expand All @@ -944,7 +992,6 @@ const updateContentMapper = async (req: Request) => {

try {
ProjectModelLowdb.update((data: any) => {
// console.info("data ===============", data, content_mapper)
data.projects[projectIndex].mapperKeys = content_mapper;
data.projects[projectIndex].updated_at = new Date().toISOString();
});
Expand Down Expand Up @@ -989,5 +1036,6 @@ export const contentMapperService = {
removeContentMapper,
removeMapping,
getSingleContentTypes,
updateContentMapper
updateContentMapper,
getExistingGlobalFields
};
Loading