forked from eclipse-edc/DataDashboard
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: data offer creation process (#773)
Co-authored-by: Richard Treier <[email protected]>
- Loading branch information
1 parent
7da1e07
commit 46e2166
Showing
21 changed files
with
373 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
src/app/component-library/edit-asset-form/edit-asset-form/assets-id-validator-builder.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/app/component-library/edit-asset-form/edit-asset-form/form/edit-asset-form-validators.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import {Injectable} from '@angular/core'; | ||
import { | ||
AbstractControl, | ||
AsyncValidatorFn, | ||
ValidationErrors, | ||
} from '@angular/forms'; | ||
import {Observable, combineLatest, of} from 'rxjs'; | ||
import {catchError, map} from 'rxjs/operators'; | ||
import {IdAvailabilityResponse} from '@sovity.de/edc-client'; | ||
import {EdcApiService} from 'src/app/core/services/api/edc-api.service'; | ||
import {ALWAYS_TRUE_POLICY_ID} from './model/always-true-policy-id'; | ||
import {EditAssetFormValue} from './model/edit-asset-form-model'; | ||
|
||
/** | ||
* Handles AngularForms for Edit Asset Form | ||
*/ | ||
@Injectable({providedIn: 'root'}) | ||
export class EditAssetFormValidators { | ||
constructor(private edcApiService: EdcApiService) {} | ||
|
||
/** | ||
* Use on asset control, reset asset control on publish mode changes, accesses parent form | ||
*/ | ||
isValidId(): AsyncValidatorFn { | ||
return (control: AbstractControl): Observable<ValidationErrors | null> => { | ||
const value = control?.parent?.parent?.value as EditAssetFormValue | null; | ||
if (value?.mode !== 'CREATE') { | ||
return of(null); | ||
} | ||
|
||
const assetId = control.value! as string; | ||
if (value.publishMode === 'PUBLISH_UNRESTRICTED') { | ||
return combineLatest([ | ||
this.assetIdExistsErrorMessage(assetId), | ||
this.contractDefinitionIdErrorMessage(assetId), | ||
this.policyIdExistsErrorMessage(ALWAYS_TRUE_POLICY_ID).pipe( | ||
map((errorMessage) => | ||
// We want to throw an error if always-true was not found | ||
errorMessage ? null : 'Always True Policy does not exist.', | ||
), | ||
), | ||
]).pipe( | ||
map((errorMessages) => this.buildValidationErrors(errorMessages)), | ||
); | ||
} else if (value.publishMode === 'PUBLISH_RESTRICTED') { | ||
return combineLatest([ | ||
this.assetIdExistsErrorMessage(assetId), | ||
this.contractDefinitionIdErrorMessage(assetId), | ||
this.policyIdExistsErrorMessage(assetId), | ||
]).pipe( | ||
map((errorMessages) => this.buildValidationErrors(errorMessages)), | ||
); | ||
} else { | ||
return this.assetIdExistsErrorMessage(assetId).pipe( | ||
map((result) => this.buildValidationErrors([result])), | ||
); | ||
} | ||
|
||
return of(null); | ||
}; | ||
} | ||
|
||
private assetIdExistsErrorMessage(id: string): Observable<string | null> { | ||
return this.edcApiService.isAssetIdAvailable(id).pipe( | ||
catchError(() => of<IdAvailabilityResponse>({id, available: false})), | ||
map((it) => (it.available ? null : 'Asset already exists.')), | ||
); | ||
} | ||
|
||
private contractDefinitionIdErrorMessage( | ||
id: string, | ||
): Observable<string | null> { | ||
return this.edcApiService.isContractDefinitionIdAvailable(id).pipe( | ||
catchError(() => of<IdAvailabilityResponse>({id, available: false})), | ||
map((it) => | ||
it.available ? null : 'Contract Definition already exists.', | ||
), | ||
); | ||
} | ||
|
||
private policyIdExistsErrorMessage(id: string): Observable<string | null> { | ||
return this.edcApiService.isPolicyIdAvailable(id).pipe( | ||
catchError(() => of<IdAvailabilityResponse>({id, available: false})), | ||
map((it) => (it.available ? null : 'Policy already exists.')), | ||
); | ||
} | ||
|
||
private buildValidationErrors( | ||
errorMessages: (string | null)[], | ||
): ValidationErrors | null { | ||
const errors = errorMessages.filter((it) => it); | ||
if (!errors.length) { | ||
return null; | ||
} | ||
|
||
const message = | ||
errors.length === 3 ? 'Data Offer already exists.' : errors.join(' '); | ||
|
||
return {exists: message}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...app/component-library/edit-asset-form/edit-asset-form/form/model/always-true-policy-id.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ALWAYS_TRUE_POLICY_ID = 'always-true'; |
Oops, something went wrong.