@@ -60,11 +60,26 @@ const putTestData = async (req: Request) => {
6060 if ( item ?. advanced ) {
6161 item . advanced . initial = structuredClone ( item ?. advanced ) ;
6262 }
63+ if ( item ?. refrenceTo ) {
64+ item . initialRefrenceTo = item ?. refrenceTo ;
65+ }
6366 } ) ;
6467 } ) ;
6568
6669
6770
71+ const sanitizeObject = ( obj : Record < string , any > ) => {
72+ const blockedKeys = [ '__proto__' , 'prototype' , 'constructor' ] ;
73+ const safeObj : Record < string , any > = { } ;
74+
75+ for ( const key in obj ) {
76+ if ( ! blockedKeys . includes ( key ) ) {
77+ safeObj [ key ] = obj [ key ] ;
78+ }
79+ }
80+ return safeObj ;
81+ } ;
82+
6883 /*
6984 this code snippet iterates over an array of contentTypes and performs
7085 some operations on each element.
@@ -75,18 +90,38 @@ const putTestData = async (req: Request) => {
7590 Finally, it updates the fieldMapping property of each type in the contentTypes array with the fieldIds array.
7691 */
7792 await FieldMapperModel . read ( ) ;
78- contentTypes . map ( ( type : any , index : any ) => {
93+ contentTypes . forEach ( ( type : any , index : number ) => {
7994 const fieldIds : string [ ] = [ ] ;
80- const fields = Array ?. isArray ?.( type ?. fieldMapping ) ? type ?. fieldMapping ?. filter ( ( field : any ) => field ) ?. map ?.( ( field : any ) => {
81- const id = field ?. id ? field ?. id ?. replace ( / [ { } ] / g, "" ) ?. toLowerCase ( ) : uuidv4 ( ) ;
82- field . id = id ;
83- fieldIds . push ( id ) ;
84- return { id, projectId, contentTypeId : type ?. id , isDeleted : false , ...field } ;
85- } ) : [ ] ;
86-
95+
96+ const fields = Array . isArray ( type ?. fieldMapping ) ?
97+ type . fieldMapping
98+ . filter ( Boolean )
99+ . map ( ( field : any ) => {
100+ const safeField = sanitizeObject ( field ) ;
101+
102+ const id =
103+ safeField ?. id ?
104+ safeField . id . replace ( / [ { } ] / g, '' ) . toLowerCase ( )
105+ : uuidv4 ( ) ;
106+
107+ fieldIds . push ( id ) ;
108+
109+ return {
110+ ...safeField ,
111+ id,
112+ projectId,
113+ contentTypeId : type ?. id ,
114+ isDeleted : false ,
115+ } ;
116+ } )
117+ : [ ] ;
118+
87119 FieldMapperModel . update ( ( data : any ) => {
88- data . field_mapper = [ ...( data ?. field_mapper ?? [ ] ) , ...( fields ?? [ ] ) ] ;
89- } ) ;
120+ data . field_mapper = [
121+ ...( Array . isArray ( data ?. field_mapper ) ? data . field_mapper : [ ] ) ,
122+ ...fields ,
123+ ] ;
124+ } ) ;
90125 if (
91126 Array ?. isArray ?.( contentType ) &&
92127 Number ?. isInteger ?.( index ) &&
@@ -277,8 +312,7 @@ const getFieldMapping = async (req: Request) => {
277312
278313 const fieldMapping : any = fieldData ?. map ( ( field : any ) => {
279314 if ( field ?. advanced ?. initial ) {
280- const { initial, ...restAdvanced } = field ?. advanced ;
281- return { ...field , advanced : restAdvanced } ;
315+ return { ...field , advanced : field ?. advanced } ;
282316 }
283317 return field ;
284318 } ) ;
@@ -775,7 +809,6 @@ const resetToInitialMapping = async (req: Request) => {
775809 ) ;
776810 if ( fieldIndex > - 1 ) {
777811 FieldMapperModel . update ( ( data : any ) => {
778-
779812 data . field_mapper [ fieldIndex ] = {
780813 ...field ,
781814 contentstackField : field ?. otherCmsField ,
@@ -784,7 +817,11 @@ const resetToInitialMapping = async (req: Request) => {
784817 advanced : {
785818 ...field ?. advanced ?. initial ,
786819 initial : field ?. advanced ?. initial ,
787- }
820+ } ,
821+ ...( field ?. referenceTo && {
822+ referenceTo : field ?. initialRefrenceTo
823+ } ) ,
824+ isDeleted : false ,
788825 }
789826 } ) ;
790827 }
0 commit comments