1
+ import { ApplicationError } from "@js-soft/ts-utils" ;
1
2
import { LocalRequestStatus } from "@nmshd/consumption" ;
2
- import { RequestItemGroupJSON , RequestItemJSONDerivations } from "@nmshd/content" ;
3
+ import { isRequestItemDerivation , RequestItemGroupJSON , RequestItemJSONDerivations } from "@nmshd/content" ;
3
4
import { CoreDate } from "@nmshd/core-types" ;
4
5
import {
5
6
IncomingRequestStatusChangedEvent ,
@@ -103,12 +104,19 @@ export class DeciderModule extends RuntimeModule<DeciderModuleConfiguration> {
103
104
const requestConfigElement = automationConfigElement . requestConfig ;
104
105
const responseConfigElement = automationConfigElement . responseConfig ;
105
106
106
- const generalRequestIsCompatible = checkGeneralRequestCompatibility ( requestConfigElement , request ) ;
107
+ const services = await this . runtime . getServices ( event . eventTargetAddress ) ;
108
+ const generalRequestIsCompatible = await checkGeneralRequestCompatibility ( requestConfigElement , request , services ) ;
109
+
110
+ if ( generalRequestIsCompatible instanceof Error ) {
111
+ this . logger . error ( generalRequestIsCompatible ) ;
112
+ break ;
113
+ }
114
+
107
115
if ( ! generalRequestIsCompatible ) {
108
116
continue ;
109
117
}
110
118
111
- const updatedRequestItemParameters = checkRequestItemCompatibilityAndApplyResponseConfig (
119
+ const updatedRequestItemParameters = await checkRequestItemCompatibilityAndApplyResponseConfig (
112
120
itemsOfRequest ,
113
121
decideRequestItemParameters ,
114
122
requestConfigElement ,
@@ -260,14 +268,14 @@ function createEmptyDecideRequestItemParameters(array: any[]): { items: any[] }
260
268
} ;
261
269
}
262
270
263
- function checkGeneralRequestCompatibility ( requestConfigElement : RequestConfig , request : LocalRequestDTO ) : boolean {
271
+ async function checkGeneralRequestCompatibility ( requestConfigElement : RequestConfig , request : LocalRequestDTO , services : RuntimeServices ) : Promise < boolean | Error > {
264
272
let generalRequestPartOfConfigElement = requestConfigElement ;
265
273
266
274
if ( isRequestItemDerivationConfig ( requestConfigElement ) ) {
267
275
generalRequestPartOfConfigElement = filterConfigElementByPrefix ( requestConfigElement , false ) ;
268
276
}
269
277
270
- return checkCompatibility ( generalRequestPartOfConfigElement , request ) ;
278
+ return await checkCompatibility ( generalRequestPartOfConfigElement , request , services ) ;
271
279
}
272
280
273
281
function filterConfigElementByPrefix ( requestItemConfigElement : RequestItemDerivationConfig , includePrefix : boolean ) : Record < string , any > {
@@ -287,15 +295,35 @@ function filterConfigElementByPrefix(requestItemConfigElement: RequestItemDeriva
287
295
return filteredRequestItemConfigElement ;
288
296
}
289
297
290
- function checkCompatibility ( requestConfigElement : RequestConfig , requestOrRequestItem : LocalRequestDTO | RequestItemJSONDerivations ) : boolean {
298
+ async function checkCompatibility ( requestConfigElement : RequestConfig , requestOrRequestItem : LocalRequestDTO , services : RuntimeServices ) : Promise < boolean | Error > ;
299
+ async function checkCompatibility ( requestConfigElement : RequestConfig , requestOrRequestItem : RequestItemJSONDerivations ) : Promise < boolean > ;
300
+ async function checkCompatibility (
301
+ requestConfigElement : RequestConfig ,
302
+ requestOrRequestItem : LocalRequestDTO | RequestItemJSONDerivations ,
303
+ services ?: RuntimeServices
304
+ ) : Promise < boolean | Error > {
291
305
let compatible = true ;
306
+
292
307
for ( const property in requestConfigElement ) {
293
308
const unformattedRequestConfigProperty = requestConfigElement [ property as keyof RequestConfig ] ;
294
309
if ( typeof unformattedRequestConfigProperty === "undefined" ) {
295
310
continue ;
296
311
}
297
312
const requestConfigProperty = makeObjectsToStrings ( unformattedRequestConfigProperty ) ;
298
313
314
+ if ( property === "relationshipAlreadyExists" ) {
315
+ if ( isRequestItemDerivation ( requestOrRequestItem ) ) {
316
+ return Error ( "The RelationshipRequestConfig 'relationshipAlreadyExists' is compared to a RequestItem, but should be compared to a Request." ) ;
317
+ }
318
+
319
+ const relationshipCompatibility = await checkRelationshipCompatibility ( requestConfigProperty , requestOrRequestItem as LocalRequestDTO , services ! ) ;
320
+ if ( relationshipCompatibility instanceof ApplicationError ) return relationshipCompatibility ;
321
+
322
+ compatible &&= relationshipCompatibility ;
323
+ if ( ! compatible ) break ;
324
+ continue ;
325
+ }
326
+
299
327
const unformattedRequestProperty = getNestedProperty ( requestOrRequestItem , property ) ;
300
328
if ( typeof unformattedRequestProperty === "undefined" ) {
301
329
compatible = false ;
@@ -338,6 +366,20 @@ function getNestedProperty(object: any, path: string): any {
338
366
return nestedProperty ;
339
367
}
340
368
369
+ async function checkRelationshipCompatibility ( relationshipRequestConfig : boolean , request : LocalRequestDTO , services : RuntimeServices ) : Promise < boolean | ApplicationError > {
370
+ let relationshipExists = false ;
371
+
372
+ const relationshipResult = await services . transportServices . relationships . getRelationshipByAddress ( { address : request . peer } ) ;
373
+ if ( relationshipResult . isError && relationshipResult . error . code !== "error.runtime.recordNotFound" ) return relationshipResult . error ;
374
+
375
+ if ( relationshipResult . isSuccess ) {
376
+ relationshipExists = true ;
377
+ }
378
+
379
+ const compatible = relationshipExists === relationshipRequestConfig ;
380
+ return compatible ;
381
+ }
382
+
341
383
function checkTagCompatibility ( requestConfigTags : string [ ] , requestTags : string [ ] ) : boolean {
342
384
const atLeastOneMatchingTag = requestConfigTags . some ( ( tag ) => requestTags . includes ( tag ) ) ;
343
385
return atLeastOneMatchingTag ;
@@ -354,16 +396,16 @@ function checkDateCompatibility(requestConfigDate: string, requestDate: string):
354
396
return CoreDate . from ( requestDate ) . equals ( CoreDate . from ( requestConfigDate ) ) ;
355
397
}
356
398
357
- function checkRequestItemCompatibilityAndApplyResponseConfig (
399
+ async function checkRequestItemCompatibilityAndApplyResponseConfig (
358
400
itemsOfRequest : ( RequestItemJSONDerivations | RequestItemGroupJSON ) [ ] ,
359
401
parametersToDecideRequest : any ,
360
402
requestConfigElement : RequestItemDerivationConfig ,
361
403
responseConfigElement : ResponseConfig
362
- ) : { items : any [ ] } {
404
+ ) : Promise < { items : any [ ] } > {
363
405
for ( let i = 0 ; i < itemsOfRequest . length ; i ++ ) {
364
406
const item = itemsOfRequest [ i ] ;
365
407
if ( item [ "@type" ] === "RequestItemGroup" ) {
366
- checkRequestItemCompatibilityAndApplyResponseConfig (
408
+ await checkRequestItemCompatibilityAndApplyResponseConfig (
367
409
( item as RequestItemGroupJSON ) . items ,
368
410
parametersToDecideRequest . items [ i ] ,
369
411
requestConfigElement ,
@@ -374,7 +416,7 @@ function checkRequestItemCompatibilityAndApplyResponseConfig(
374
416
if ( alreadyDecidedByOtherConfig ) continue ;
375
417
376
418
if ( isRequestItemDerivationConfig ( requestConfigElement ) ) {
377
- const requestItemIsCompatible = checkRequestItemCompatibility ( requestConfigElement , item as RequestItemJSONDerivations ) ;
419
+ const requestItemIsCompatible = await checkRequestItemCompatibility ( requestConfigElement , item as RequestItemJSONDerivations ) ;
378
420
if ( ! requestItemIsCompatible ) continue ;
379
421
}
380
422
@@ -395,7 +437,7 @@ function checkRequestItemCompatibilityAndApplyResponseConfig(
395
437
return parametersToDecideRequest ;
396
438
}
397
439
398
- function checkRequestItemCompatibility ( requestConfigElement : RequestItemDerivationConfig , requestItem : RequestItemJSONDerivations ) : boolean {
440
+ async function checkRequestItemCompatibility ( requestConfigElement : RequestItemDerivationConfig , requestItem : RequestItemJSONDerivations ) : Promise < boolean > {
399
441
const requestItemPartOfConfigElement = filterConfigElementByPrefix ( requestConfigElement , true ) ;
400
- return checkCompatibility ( requestItemPartOfConfigElement , requestItem ) ;
442
+ return await checkCompatibility ( requestItemPartOfConfigElement , requestItem ) ;
401
443
}
0 commit comments