@@ -43,7 +43,9 @@ async function cleanupES (keys) {
43
43
id : topResources . user . pipeline . id
44
44
} )
45
45
} 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 {
47
49
throw e
48
50
}
49
51
}
@@ -53,7 +55,9 @@ async function cleanupES (keys) {
53
55
id : topResources . skillprovider . pipeline . id
54
56
} )
55
57
} 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 {
57
61
throw e
58
62
}
59
63
}
@@ -63,7 +67,9 @@ async function cleanupES (keys) {
63
67
id : topResources . attributegroup . pipeline . id
64
68
} )
65
69
} 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 {
67
73
throw e
68
74
}
69
75
}
@@ -80,7 +86,9 @@ async function cleanupES (keys) {
80
86
name : topResources [ esResourceName ] . enrich . policyName
81
87
} )
82
88
} 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 {
84
92
throw e
85
93
}
86
94
}
@@ -91,7 +99,9 @@ async function cleanupES (keys) {
91
99
index : topResources [ esResourceName ] . index
92
100
} )
93
101
} 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 {
95
105
throw e
96
106
}
97
107
}
@@ -101,7 +111,9 @@ async function cleanupES (keys) {
101
111
name : organizationResources [ esResourceName ] . enrich . policyName
102
112
} )
103
113
} 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 {
105
117
throw e
106
118
}
107
119
}
@@ -136,11 +148,27 @@ async function insertIntoES (modelName, body) {
136
148
} else if ( _ . includes ( _ . keys ( userResources ) , esResourceName ) ) {
137
149
const userResource = userResources [ esResourceName ]
138
150
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
+ }
144
172
145
173
if ( userResource . nested === true && userResource . mappingCreated !== true ) {
146
174
await client . indices . putMapping ( {
@@ -180,11 +208,27 @@ async function insertIntoES (modelName, body) {
180
208
} else if ( _ . includes ( _ . keys ( organizationResources ) , esResourceName ) ) {
181
209
const orgResource = organizationResources [ esResourceName ]
182
210
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
+ }
188
232
189
233
const relateId = body [ orgResource . relateKey ]
190
234
0 commit comments