@@ -36,8 +36,7 @@ export class ConfigSyncJob extends SyncJob {
3636 //if we get boolean, a problem occurred while getting data from service (most likely 401 or 403 error)
3737 //we dont have all the service objects and stop the job
3838 if ( typeof objectsToSync === "boolean" ) {
39- let message : string = 'Problem occurred while getting primary service objects.' ;
40- this . _jobLog . errors . push ( this . _errorService . createConfigJobError ( message ) ) ;
39+ await this . updateJobLog ( primarySyncedService . errors )
4140 return false ;
4241 }
4342
@@ -51,8 +50,7 @@ export class ConfigSyncJob extends SyncJob {
5150 const allServiceObjects = await syncedService . getAllServiceObjects ( configLastSyncAt ) ;
5251 //same as above, there was a problem communication problem with service. We stop the job.
5352 if ( typeof allServiceObjects === "boolean" ) {
54- let message : string = 'Problem occurred while getting secondary service objects' ;
55- this . _jobLog . errors . push ( this . _errorService . createConfigJobError ( message ) ) ;
53+ await this . updateJobLog ( syncedService . errors )
5654 return false ;
5755 }
5856
@@ -103,11 +101,6 @@ export class ConfigSyncJob extends SyncJob {
103101 }
104102 } catch ( ex ) {
105103 operationsOk = false ;
106- //TODO figure out better way to work with extra context.
107- //temporary console log to test if this is the correct place.
108- console . log ( `User: ${ this . _user . username } experienced error in config job for object: ${ objectToSync . name } , ${ objectToSync . type } , ${ objectToSync . id } ` ) ;
109- let context = this . _sentryService . createExtraContext ( 'Object_to_sync' , JSON . parse ( JSON . stringify ( objectToSync ) ) ) ;
110- this . _sentryService . logError ( ex , context ) ;
111104 }
112105 }
113106
@@ -123,7 +116,7 @@ export class ConfigSyncJob extends SyncJob {
123116 // even if some api operations were not ok, persist changes to the mappings - better than nothing
124117 await databaseService . updateUserMappings ( this . _user ) ;
125118
126- await databaseService . updateJobLog ( this . _jobLog ) ;
119+ await this . updateJobLog ( Array . from ( secondaryServicesWrappersMap . values ( ) ) . flatMap ( wrapper => wrapper . syncedService ?. errors ?? [ ] ) ) ;
127120
128121 return operationsOk ;
129122 }
@@ -152,7 +145,7 @@ export class ConfigSyncJob extends SyncJob {
152145 continue ;
153146 }
154147 // firstly create object in the service, then create serviceObject with newly acquired id
155- const newObject = await this . _createServiceObjectInService ( serviceWrapper , objectToSync ) ;
148+ const newObject = await serviceWrapper . syncedService . createServiceObject ( objectToSync . id , objectToSync . name , objectToSync . type ) ;
156149 mappingsObject = new MappingsObject ( newObject . id , newObject . name , serviceDefinition . name , newObject . type ) ;
157150 }
158151
@@ -189,45 +182,27 @@ export class ConfigSyncJob extends SyncJob {
189182 // mappingObject is missing, create a new one and add to mapping (maybe new service was added)
190183 // create a real object in the service and add mappingObject
191184 // firstly create object in the service, then create serviceObject with newly acquired id
192- const newObject = await this . _createServiceObjectInService ( serviceWrapper , objectToSync ) ;
185+ const newObject = await serviceWrapper . syncedService . createServiceObject ( objectToSync . id , objectToSync . name , objectToSync . type ) ;
193186 const newMappingsObject = new MappingsObject ( newObject . id , newObject . name , serviceDefinition . name , newObject . type ) ;
194187 mapping . mappingsObjects . push ( newMappingsObject ) ;
195188 } else {
196189 // scenario b), c), e)
197190 // check if mapping corresponds with real object in the service
198- const objectBasedOnMapping = await serviceWrapper . allServiceObjects
191+ const objectBasedOnMapping = serviceWrapper . allServiceObjects
199192 . find ( serviceObject => serviceObject . id === mappingsObject . id && serviceObject . type === mappingsObject . type ) ;
200193
201194 if ( ! objectBasedOnMapping ) {
202195 // scenario e), create new object in the service
203- const newObject = await this . _createServiceObjectInService ( serviceWrapper , objectToSync ) ;
196+ const newObject = await serviceWrapper . syncedService . createServiceObject ( objectToSync . id , objectToSync . name , objectToSync . type ) ;
204197 mappingsObject . id = newObject . id ;
205198 mappingsObject . name = newObject . name ;
206199 mappingsObject . lastUpdated = Date . now ( ) ;
207200 } else if ( objectBasedOnMapping . name !== serviceWrapper . syncedService . getFullNameForServiceObject ( objectToSync ) ) {
208201 // scenario b)
209202 // name is incorrect => maybe mapping was outdated or/and real object was outdated
210- let updatedObject = null ;
211- try {
212- updatedObject = await serviceWrapper . syncedService . updateServiceObject (
213- mappingsObject . id , new ServiceObject ( objectToSync . id , objectToSync . name , objectToSync . type )
214- ) ;
215- } catch ( ex : any ) {
216- if ( ex . status !== 400 ) {
217- throw ex ;
218- }
219- // 400 ~ maybe object already exists and cannot be updated (for example object needs to be unique - name)?
220- // => try to find it and use it for the mapping
221- const serviceObjectName = serviceWrapper . syncedService . getFullNameForServiceObject ( new ServiceObject ( objectToSync . id , objectToSync . name , objectToSync . type ) ) ;
222- updatedObject = serviceWrapper . allServiceObjects . find ( serviceObject => serviceObject . name === serviceObjectName ) ;
223- if ( ! updatedObject ) {
224- const context = [
225- this . _sentryService . createExtraContext ( 'Object_to_sync' , { 'id' : objectToSync . id , 'name' : objectToSync . name , 'type' : objectToSync . type } )
226- ]
227- this . _sentryService . logError ( ex , context ) ;
228- throw ex ;
229- }
230- }
203+ const updatedObject = await serviceWrapper . syncedService . updateServiceObject (
204+ mappingsObject . id , new ServiceObject ( objectToSync . id , objectToSync . name , objectToSync . type )
205+ ) ;
231206 // console.log(`ConfigSyncJob: Updated object ${updatedObject.name}`);
232207 mappingsObject . name = updatedObject . name ;
233208 mappingsObject . lastUpdated = Date . now ( ) ;
@@ -240,28 +215,4 @@ export class ConfigSyncJob extends SyncJob {
240215
241216 return true ;
242217 }
243-
244- private async _createServiceObjectInService ( serviceWrapper : SyncedServiceWrapper , objectToSync : ServiceObject ) : Promise < ServiceObject > {
245- let newObject ;
246- try {
247- newObject = await serviceWrapper . syncedService . createServiceObject ( objectToSync . id , objectToSync . name , objectToSync . type ) ;
248- } catch ( ex : any ) {
249- if ( ex . status !== 400 ) {
250- throw ex ;
251- }
252- // 400 ~ maybe object already exists and cannot be created (for example object needs to be unique - name)?
253- // => try to find it and use it for the mapping
254- const serviceObjectName = serviceWrapper . syncedService . getFullNameForServiceObject ( new ServiceObject ( objectToSync . id , objectToSync . name , objectToSync . type ) ) ;
255- newObject = serviceWrapper . allServiceObjects . find ( serviceObject => serviceObject . name === serviceObjectName ) ;
256- if ( ! newObject ) {
257- let context = [
258- this . _sentryService . createExtraContext ( 'Object_to_sync' , { 'id' : objectToSync . id , 'name' : objectToSync . name , 'type' : objectToSync . type } )
259- ]
260- this . _sentryService . logError ( ex , context ) ;
261- throw ex ;
262- }
263- // console.log(`ConfigSyncJob: Creating mapping, but object exists, using real object ${newObject.name}`);
264- }
265- return newObject ;
266- }
267218}
0 commit comments