Skip to content

Commit 36f799d

Browse files
Merge pull request #585 from contentstack/feature/dropdown-field-choices
Feature/dropdown field choices
2 parents 83db088 + 9f73ebe commit 36f799d

File tree

6 files changed

+172
-87
lines changed

6 files changed

+172
-87
lines changed

api/src/controllers/projects.contentMapper.controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { contentMapperService } from "../services/contentMapper.service.js";
99
*/
1010
const putTestData = async (req: Request, res: Response): Promise<void> => {
1111
const resp = await contentMapperService.putTestData(req);
12-
res.status(200).json(resp);
12+
res.status(resp?.status).json(resp);
1313
};
1414

1515
/**
@@ -21,7 +21,7 @@ const putTestData = async (req: Request, res: Response): Promise<void> => {
2121
*/
2222
const getContentTypes = async (req: Request, res: Response): Promise<void> => {
2323
const resp = await contentMapperService.getContentTypes(req);
24-
res.status(200).json(resp);
24+
res.status(resp?.status).json(resp);
2525
};
2626
/**
2727
* Retrieves the field mapping for a given request and sends the response as JSON.
@@ -32,7 +32,7 @@ const getContentTypes = async (req: Request, res: Response): Promise<void> => {
3232
*/
3333
const getFieldMapping = async (req: Request, res: Response): Promise<void> => {
3434
const resp = await contentMapperService.getFieldMapping(req);
35-
res.status(200).json(resp);
35+
res.status(resp?.status).json(resp);
3636
};
3737
/**
3838
* Retrieves the existing content types.
@@ -76,7 +76,7 @@ const putContentTypeFields = async (
7676
res: Response
7777
): Promise<void> => {
7878
const resp = await contentMapperService.updateContentType(req);
79-
res.status(200).json(resp);
79+
res.status(resp?.status).json(resp);
8080
};
8181
/**
8282
* Resets the content type to its initial mapping.
@@ -87,7 +87,7 @@ const putContentTypeFields = async (
8787
*/
8888
const resetContentType = async (req: Request, res: Response): Promise<void> => {
8989
const resp = await contentMapperService.resetToInitialMapping(req);
90-
res.status(200).json(resp);
90+
res.status(resp?.status).json(resp);
9191
};
9292
// TODO Will remove if not required
9393
// const removeMapping = async (req: Request, res: Response): Promise<void> => {

api/src/services/contentMapper.service.ts

Lines changed: 138 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const putTestData = async (req: Request) => {
3535
const projectId = req.params.projectId;
3636
const contentTypes = req.body.contentTypes;
3737

38-
39-
await FieldMapperModel.read();
38+
try {
39+
await FieldMapperModel.read();
4040

4141
/*
4242
this code snippet iterates over an array of contentTypes and performs
@@ -93,7 +93,7 @@ const putTestData = async (req: Request) => {
9393
if (index > -1 && contentIds?.length) {
9494
ProjectModelLowdb.data.projects[index].content_mapper = contentIds;
9595
ProjectModelLowdb.data.projects[index].extract_path = req?.body?.extractPath;
96-
ProjectModelLowdb.write();
96+
await ProjectModelLowdb.write();
9797
} else {
9898
throw new BadRequestError(HTTP_TEXTS.CONTENT_TYPE_NOT_FOUND);
9999
}
@@ -102,8 +102,21 @@ const putTestData = async (req: Request) => {
102102
.get("projects")
103103
.find({ id: projectId })
104104
.value();
105+
106+
return {
107+
status: HTTP_CODES?.OK,
108+
data: pData
109+
}
110+
111+
} catch (error:any) {
105112

106-
return pData;
113+
throw new ExceptionFunction(
114+
error?.message || HTTP_TEXTS.INTERNAL_ERROR,
115+
error?.statusCode || error?.status || HTTP_CODES.SERVER_ERROR
116+
);
117+
118+
}
119+
107120
};
108121

109122
/**
@@ -120,57 +133,80 @@ const getContentTypes = async (req: Request) => {
120133

121134
let result: any = [];
122135
let totalCount = 0;
123-
124-
await ProjectModelLowdb.read();
125-
const projectDetails = ProjectModelLowdb.chain
136+
try {
137+
await ProjectModelLowdb.read();
138+
const projectDetails = ProjectModelLowdb.chain
126139
.get("projects")
127140
.find({ id: projectId })
128141
.value();
129142

130-
if (isEmpty(projectDetails)) {
131-
logger.error(
143+
if (isEmpty(projectDetails)) {
144+
logger.error(
145+
getLogMessage(
146+
sourceFn,
147+
`${HTTP_TEXTS.PROJECT_NOT_FOUND} projectId: ${projectId}`
148+
)
149+
);
150+
throw new BadRequestError(HTTP_TEXTS.PROJECT_NOT_FOUND);
151+
}
152+
const contentMapperId = projectDetails.content_mapper;
153+
await ContentTypesMapperModelLowdb.read();
154+
await FieldMapperModel.read();
155+
156+
const content_mapper: any = [];
157+
contentMapperId.map((data: any) => {
158+
const contentMapperData = ContentTypesMapperModelLowdb.chain
159+
.get("ContentTypesMappers")
160+
.find({ id: data, projectId: projectId })
161+
.value();
162+
content_mapper.push(contentMapperData);
163+
});
164+
165+
if (!isEmpty(content_mapper)) {
166+
if (search) {
167+
const filteredResult = content_mapper
168+
.filter((item: any) =>
169+
item?.otherCmsTitle?.toLowerCase().includes(search)
170+
)
171+
?.sort((a: any, b: any) =>
172+
a.otherCmsTitle.localeCompare(b.otherCmsTitle)
173+
);
174+
totalCount = filteredResult.length;
175+
result = filteredResult.slice(skip, Number(skip) + Number(limit));
176+
} else {
177+
totalCount = content_mapper.length;
178+
result = content_mapper
179+
?.sort((a: any, b: any) =>
180+
a.otherCmsTitle.localeCompare(b.otherCmsTitle)
181+
)
182+
?.slice(skip, Number(skip) + Number(limit));
183+
}
184+
}
185+
186+
return {
187+
status: HTTP_CODES?.OK,
188+
count: totalCount,
189+
contentTypes: result
190+
};
191+
192+
} catch (error:any) {
193+
// Log error message
194+
logger.error(
132195
getLogMessage(
133196
sourceFn,
134-
`${HTTP_TEXTS.PROJECT_NOT_FOUND} projectId: ${projectId}`
197+
"Error occurred while while getting contentTypes of projects",
198+
error
135199
)
136200
);
137-
throw new BadRequestError(HTTP_TEXTS.PROJECT_NOT_FOUND);
138-
}
139-
const contentMapperId = projectDetails.content_mapper;
140-
await ContentTypesMapperModelLowdb.read();
141-
await FieldMapperModel.read();
142201

143-
const content_mapper: any = [];
144-
contentMapperId.map((data: any) => {
145-
const contentMapperData = ContentTypesMapperModelLowdb.chain
146-
.get("ContentTypesMappers")
147-
.find({ id: data, projectId: projectId })
148-
.value();
149-
content_mapper.push(contentMapperData);
150-
});
151-
152-
if (!isEmpty(content_mapper)) {
153-
if (search) {
154-
const filteredResult = content_mapper
155-
.filter((item: any) =>
156-
item?.otherCmsTitle?.toLowerCase().includes(search)
157-
)
158-
?.sort((a: any, b: any) =>
159-
a.otherCmsTitle.localeCompare(b.otherCmsTitle)
160-
);
161-
totalCount = filteredResult.length;
162-
result = filteredResult.slice(skip, Number(skip) + Number(limit));
163-
} else {
164-
totalCount = content_mapper.length;
165-
result = content_mapper
166-
?.sort((a: any, b: any) =>
167-
a.otherCmsTitle.localeCompare(b.otherCmsTitle)
168-
)
169-
?.slice(skip, Number(skip) + Number(limit));
170-
}
202+
throw new ExceptionFunction(
203+
error?.message || HTTP_TEXTS.INTERNAL_ERROR,
204+
error?.statusCode || error?.status || HTTP_CODES.SERVER_ERROR
205+
);
206+
171207
}
172208

173-
return { count: totalCount, contentTypes: result };
209+
174210
};
175211

176212
/**
@@ -191,46 +227,68 @@ const getFieldMapping = async (req: Request) => {
191227
let filteredResult = [];
192228
let totalCount = 0;
193229

194-
await ContentTypesMapperModelLowdb.read();
230+
try {
231+
await ContentTypesMapperModelLowdb.read();
195232

196-
const contentType = ContentTypesMapperModelLowdb.chain
197-
.get("ContentTypesMappers")
198-
.find({ id: contentTypeId, projectId: projectId })
199-
.value();
233+
const contentType = ContentTypesMapperModelLowdb.chain
234+
.get("ContentTypesMappers")
235+
.find({ id: contentTypeId, projectId: projectId })
236+
.value();
237+
238+
if (isEmpty(contentType)) {
239+
logger.error(
240+
getLogMessage(
241+
srcFunc,
242+
`${HTTP_TEXTS.CONTENT_TYPE_NOT_FOUND} Id: ${contentTypeId}`
243+
)
244+
);
245+
throw new BadRequestError(HTTP_TEXTS.CONTENT_TYPE_NOT_FOUND);
246+
}
247+
await FieldMapperModel.read();
248+
const fieldData = contentType?.fieldMapping?.map?.((fields: any) => {
249+
const fieldMapper = FieldMapperModel.chain
250+
.get("field_mapper")
251+
.find({ id: fields, projectId: projectId })
252+
.value();
253+
254+
return fieldMapper;
255+
});
256+
const fieldMapping: any = fieldData;
257+
if (!isEmpty(fieldMapping)) {
258+
if (search) {
259+
filteredResult = fieldMapping?.filter?.((item: any) =>
260+
item?.otherCmsField?.toLowerCase().includes(search)
261+
);
262+
totalCount = filteredResult.length;
263+
result = filteredResult.slice(skip, Number(skip) + Number(limit));
264+
} else {
265+
totalCount = fieldMapping.length;
266+
result = fieldMapping.slice(skip, Number(skip) + Number(limit));
267+
}
268+
}
200269

201-
if (isEmpty(contentType)) {
270+
return {
271+
status: HTTP_CODES?.OK,
272+
count: totalCount,
273+
fieldMapping: result };
274+
275+
} catch (error: any) {
276+
// Log error message
202277
logger.error(
203278
getLogMessage(
204279
srcFunc,
205-
`${HTTP_TEXTS.CONTENT_TYPE_NOT_FOUND} Id: ${contentTypeId}`
280+
"Error occurred while getting field mapping of projects",
281+
error
206282
)
207283
);
208-
throw new BadRequestError(HTTP_TEXTS.CONTENT_TYPE_NOT_FOUND);
209-
}
210-
await FieldMapperModel.read();
211-
const fieldData = contentType?.fieldMapping?.map?.((fields: any) => {
212-
const fieldMapper = FieldMapperModel.chain
213-
.get("field_mapper")
214-
.find({ id: fields, projectId: projectId })
215-
.value();
216284

217-
return fieldMapper;
218-
});
219-
const fieldMapping: any = fieldData;
220-
if (!isEmpty(fieldMapping)) {
221-
if (search) {
222-
filteredResult = fieldMapping?.filter?.((item: any) =>
223-
item?.otherCmsField?.toLowerCase().includes(search)
224-
);
225-
totalCount = filteredResult.length;
226-
result = filteredResult.slice(skip, Number(skip) + Number(limit));
227-
} else {
228-
totalCount = fieldMapping.length;
229-
result = fieldMapping.slice(skip, Number(skip) + Number(limit));
230-
}
285+
throw new ExceptionFunction(
286+
error?.message || HTTP_TEXTS.INTERNAL_ERROR,
287+
error?.statusCode || error?.status || HTTP_CODES.SERVER_ERROR
288+
);
289+
231290
}
232-
233-
return { count: totalCount, fieldMapping: result };
291+
234292
};
235293

236294
/**
@@ -705,7 +763,11 @@ const resetToInitialMapping = async (req: Request) => {
705763
await ContentTypesMapperModelLowdb.update((data: any) => {
706764
data.ContentTypesMappers[contentIndex].status = CONTENT_TYPE_STATUS[1];
707765
});
708-
return { message: HTTP_TEXTS.RESET_CONTENT_MAPPING, data: contentTypeData };
766+
return {
767+
status: HTTP_CODES?.OK,
768+
message: HTTP_TEXTS.RESET_CONTENT_MAPPING,
769+
data: contentTypeData
770+
};
709771

710772
} catch (error: any) {
711773
logger.error(

ui/src/components/DestinationStack/Actions/LoadLanguageMapper.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ const Mapper = ({
217217

218218
const updatedOptions = { ...prevOptions }; // Create a shallow copy
219219
csLocale = updatedOptions[index]?.label;
220+
setselectedCsOption((prevSelected) => {
221+
const newSelectedOptions: string[] = prevSelected?.filter(
222+
(item) => item !== csLocale // Remove the item equal to csLocale
223+
);
224+
return newSelectedOptions;
225+
});
226+
220227
delete updatedOptions[index]; // Remove the key
221228

222229
return updatedOptions;
@@ -226,7 +233,14 @@ const Mapper = ({
226233
// Remove item at index from existingLocale
227234
setexistingLocale((prevLocales: ExistingFieldType) => {
228235
if (!prevLocales) return {};
229-
const updatedOptions = { ...prevLocales }; // Create a shallow copy
236+
const updatedOptions = { ...prevLocales }; // Create a shallow copy;
237+
const locale = updatedOptions[index]?.label
238+
setselectedSourceOption((prevSelected) => {
239+
const newSelectedOptions: string[] = prevSelected?.filter(
240+
(item) => item !== locale // Remove the item equal to locale
241+
);
242+
return newSelectedOptions;
243+
});
230244
delete updatedOptions[index]; // Remove the key
231245
return updatedOptions;
232246
});

upload-api/src/controllers/sitecore/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ const createSitecoreMapper = async (filePath: string = "", projectId: string | s
7777
},
7878
data: JSON.stringify(fieldMapping),
7979
};
80-
const response = await axios?.request?.(config)
81-
if (response?.data?.content_mapper?.length) {
80+
81+
const {data, status} = await axios.request(config);
82+
83+
if (data?.data?.content_mapper?.length) {
8284
deleteFolderSync(infoMap?.path);
8385
logger.info('Validation success:', {
8486
status: HTTP_CODES?.OK,

upload-api/src/controllers/wordpress/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ const createWordpressMapper = async (filePath: string = "", projectId: string |
3131
},
3232
data: JSON.stringify(fieldMapping),
3333
};
34-
const response = await axios.request(config)
34+
const {data, status} = await axios.request(config);
35+
if (data?.data?.content_mapper?.length) {
36+
logger.info('Validation success:', {
37+
status: HTTP_CODES?.OK,
38+
message: HTTP_TEXTS?.MAPPER_SAVED,
39+
});
40+
}
41+
3542

3643
const mapperConfig = {
3744
method: 'post',

upload-api/src/services/contentful/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ const createContentfulMapper = async (
2929
},
3030
data: JSON.stringify(initialMapper)
3131
};
32-
const response = await axios.request(req);
33-
if (response?.data?.content_mapper?.length) {
32+
const {data, status} = await axios.request(req);
33+
if (data?.data?.content_mapper?.length) {
3434
logger.info('Validation success:', {
3535
status: HTTP_CODES?.OK,
3636
message: HTTP_TEXTS?.MAPPER_SAVED

0 commit comments

Comments
 (0)