@@ -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 (
0 commit comments