@@ -43,7 +43,9 @@ async function cleanupES (keys) {
4343 id : topResources . user . pipeline . id
4444 } )
4545 } catch ( e ) {
46- if ( e . meta && e . meta . body . error . type !== RESOURCE_NOT_FOUND ) {
46+ if ( e . meta && e . meta . body . error . type === RESOURCE_NOT_FOUND ) {
47+ // Ignore
48+ } else {
4749 throw e
4850 }
4951 }
@@ -53,7 +55,9 @@ async function cleanupES (keys) {
5355 id : topResources . skillprovider . pipeline . id
5456 } )
5557 } catch ( e ) {
56- if ( e . meta && e . meta . body . error . type !== RESOURCE_NOT_FOUND ) {
58+ if ( e . meta && e . meta . body . error . type === RESOURCE_NOT_FOUND ) {
59+ // Ignore
60+ } else {
5761 throw e
5862 }
5963 }
@@ -63,7 +67,9 @@ async function cleanupES (keys) {
6367 id : topResources . attributegroup . pipeline . id
6468 } )
6569 } catch ( e ) {
66- if ( e . meta && e . meta . body . error . type !== RESOURCE_NOT_FOUND ) {
70+ if ( e . meta && e . meta . body . error . type === RESOURCE_NOT_FOUND ) {
71+ // Ignore
72+ } else {
6773 throw e
6874 }
6975 }
@@ -80,7 +86,9 @@ async function cleanupES (keys) {
8086 name : topResources [ esResourceName ] . enrich . policyName
8187 } )
8288 } catch ( e ) {
83- if ( e . meta && e . meta . body . error . type !== RESOURCE_NOT_FOUND ) {
89+ if ( e . meta && e . meta . body . error . type === RESOURCE_NOT_FOUND ) {
90+ // Ignore
91+ } else {
8492 throw e
8593 }
8694 }
@@ -91,7 +99,9 @@ async function cleanupES (keys) {
9199 index : topResources [ esResourceName ] . index
92100 } )
93101 } catch ( e ) {
94- if ( e . meta && e . meta . body . error . type !== INDEX_NOT_FOUND ) {
102+ if ( e . meta && e . meta . body . error . type === INDEX_NOT_FOUND ) {
103+ // Ignore
104+ } else {
95105 throw e
96106 }
97107 }
@@ -101,7 +111,9 @@ async function cleanupES (keys) {
101111 name : organizationResources [ esResourceName ] . enrich . policyName
102112 } )
103113 } catch ( e ) {
104- if ( e . meta && e . meta . body . error . type !== RESOURCE_NOT_FOUND ) {
114+ if ( e . meta && e . meta . body . error . type === RESOURCE_NOT_FOUND ) {
115+ // Ignore
116+ } else {
105117 throw e
106118 }
107119 }
@@ -136,11 +148,27 @@ async function insertIntoES (modelName, body) {
136148 } else if ( _ . includes ( _ . keys ( userResources ) , esResourceName ) ) {
137149 const userResource = userResources [ esResourceName ]
138150
139- const { body : user } = await client . getSource ( {
140- index : topResources . user . index ,
141- type : topResources . user . type ,
142- id : body . userId
143- } )
151+ let user
152+
153+ try {
154+ const res = await client . getSource ( {
155+ index : topResources . user . index ,
156+ type : topResources . user . type ,
157+ id : body . userId
158+ } )
159+
160+ user = res . body
161+ } catch ( e ) {
162+ if ( e . meta && e . meta . body . error . type === RESOURCE_NOT_FOUND ) {
163+ logger . info ( `The ${ modelName } references user with id ${ body . userId } , which does not exist. Deleting the reference...` )
164+ // The user does not exist. Delete the referece records
165+ await models . DBHelper . delete ( models [ modelName ] , body . id )
166+ logger . info ( 'Reference deleted' )
167+ return
168+ } else {
169+ throw e
170+ }
171+ }
144172
145173 if ( userResource . nested === true && userResource . mappingCreated !== true ) {
146174 await client . indices . putMapping ( {
@@ -180,11 +208,27 @@ async function insertIntoES (modelName, body) {
180208 } else if ( _ . includes ( _ . keys ( organizationResources ) , esResourceName ) ) {
181209 const orgResource = organizationResources [ esResourceName ]
182210
183- const { body : organization } = await client . getSource ( {
184- index : topResources . organization . index ,
185- type : topResources . organization . type ,
186- id : body . organizationId
187- } )
211+ let organization
212+
213+ try {
214+ const res = await client . getSource ( {
215+ index : topResources . organization . index ,
216+ type : topResources . organization . type ,
217+ id : body . organizationId
218+ } )
219+
220+ organization = res . body
221+ } catch ( e ) {
222+ if ( e . meta && e . meta . body . error . type === RESOURCE_NOT_FOUND ) {
223+ logger . info ( `The ${ modelName } references org with id ${ body . organizationId } , which does not exist. Deleting the reference...` )
224+ // The user does not exist. Delete the referece records
225+ await models . DBHelper . delete ( models [ modelName ] , body . id )
226+ logger . info ( 'Reference deleted' )
227+ return
228+ } else {
229+ throw e
230+ }
231+ }
188232
189233 const relateId = body [ orgResource . relateKey ]
190234
0 commit comments