Skip to content

Commit

Permalink
chore: Migrate Contract Definition Ui
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadEGI committed Aug 29, 2023
1 parent 6d5c5db commit 8d35b85
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 77 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@ng-apimock/core": "^3.11.0",
"@ngxs/store": "^3.8.1",
"@sovity.de/broker-server-client": "0.20230712.71619-main-a4860cff",
"@sovity.de/edc-client": "0.20230822.60338-main-c7ece097",
"@sovity.de/edc-client": "0.20230829.91501-main-0b8664eb",
"clean-deep": "^3.4.0",
"date-fns": "^2.30.0",
"dotenv": "^16.3.1",
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/services/api/edc-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import {AssetCreateRequest} from '@sovity.de/edc-client/dist/generated/models/AssetCreateRequest';
import {ContractDefinitionRequest} from '@sovity.de/edc-client/dist/generated/models/ContractDefinitionRequest';
import {APP_CONFIG, AppConfig} from '../../config/app-config';
import {EDC_FAKE_BACKEND} from './fake-backend/edc-api.fake-backend';
import {EDC_FAKE_BACKEND} from './fake-backend/edc-fake-backend';

@Injectable({providedIn: 'root'})
export class EdcApiService {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
ContractDefinitionEntry,
ContractDefinitionPage,
IdResponseDto,
} from '@sovity.de/edc-client';
import {ContractDefinitionRequest} from '@sovity.de/edc-client/dist/generated/models/ContractDefinitionRequest';
import {AssetProperties} from '../../asset-properties';

export let contractDefinitions: ContractDefinitionEntry[] = [
{
contractDefinitionId: 'test-contract-definition-1',
contractPolicyId: 'test-policy-definition-1',
accessPolicyId: 'test-policy-definition-1',
assetSelector: [
{
operandLeft: AssetProperties.id,
operator: 'EQ',
operandRight: {type: 'VALUE', value: 'test-asset-1'},
},
],
},
];

export const contractDefinitionPage = (): ContractDefinitionPage => {
return {
contractDefinitions,
};
};

export const createContractDefinition = (
request: ContractDefinitionRequest,
): IdResponseDto => {
let newEntry: ContractDefinitionEntry = {
contractDefinitionId: request.contractDefinitionId!,
contractPolicyId: request.contractPolicyId!,
accessPolicyId: request.accessPolicyId!,
assetSelector: request.assetSelector!,
};

contractDefinitions = [newEntry, ...contractDefinitions];

return {
id: newEntry.contractDefinitionId,
lastUpdatedDate: new Date(),
};
};

export const deleteContractDefinition = (id: string): IdResponseDto => {
contractDefinitions = contractDefinitions.filter(
(it) => it.contractDefinitionId !== id,
);
return {id, lastUpdatedDate: new Date()};
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {FetchAPI} from '@sovity.de/edc-client';
import {AssetCreateRequest} from '@sovity.de/edc-client/dist/generated/models/AssetCreateRequest';
import {ContractDefinitionRequest} from '@sovity.de/edc-client/dist/generated/models/ContractDefinitionRequest';
import {assetPage, createAsset, deleteAsset} from './asset-fake-service';
import {
contractDefinitionPage,
createContractDefinition,
deleteContractDefinition,
} from './contract-definition-fake-service';
import {getBody, getMethod, getUrl} from './utils/request-utils';
import {ok} from './utils/response-utils';
import {UrlInterceptor} from './utils/url-interceptor';
Expand All @@ -26,5 +32,18 @@ export const EDC_FAKE_BACKEND: FetchAPI = async (
.url('pages/asset-page/assets/*')
.on('DELETE', (assetId) => ok(deleteAsset(assetId)))

.url('pages/contract-definition-page')
.on('GET', () => ok(contractDefinitionPage()))

.url('pages/contract-definition-page/contract-definitions')
.on('POST', () =>
ok(createContractDefinition(body as ContractDefinitionRequest)),
)

.url('pages/contract-definition-page/contract-definitions/*')
.on('DELETE', (contractDefinitionId) =>
ok(deleteContractDefinition(contractDefinitionId)),
)

.tryMatch();
};
1 change: 1 addition & 0 deletions src/app/core/services/contract-definition-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class ContractDefinitionBuilder {
formValue: ContractDefinitionEditorDialogFormValue,
): ContractDefinitionRequest {
return {
contractDefinitionId: formValue.id ?? '',
accessPolicyId: policyDefinitionId(formValue.accessPolicy!),
contractPolicyId: policyDefinitionId(formValue.contractPolicy!),
assetSelector: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {Injectable} from '@angular/core';
import {
ContractDefinitionDto,
Criterion,
ContractDefinitionEntry,
ContractDefinitionPage,
UiCriterionDto,
} from '@sovity.de/edc-client';
import {
PolicyDefinition,
policyDefinitionId,
} from '../../../../core/services/api/legacy-managent-api-client';
Expand All @@ -22,7 +25,7 @@ import {
@Injectable({providedIn: 'root'})
export class ContractDefinitionCardBuilder {
buildContractDefinitionCards(
contractDefinitions: ContractDefinitionDto[],
contractDefinitionPage: ContractDefinitionPage,
assets: Asset[],
policyDefinitions: PolicyDefinition[],
): ContractDefinitionCard[] {
Expand All @@ -32,22 +35,23 @@ export class ContractDefinitionCardBuilder {
policyDefinitionId,
);

return contractDefinitions.map((contractDefinition) =>
this.buildContractDefinitionCard(
contractDefinition,
assetById,
policyDefinitionById,
),
return contractDefinitionPage.contractDefinitions.map(
(contractDefinition) =>
this.buildContractDefinitionCard(
contractDefinition,
assetById,
policyDefinitionById,
),
);
}

buildContractDefinitionCard(
contractDefinition: ContractDefinitionDto,
contractDefinition: ContractDefinitionEntry,
assetById: Map<string, Asset>,
policyDefinitionById: Map<string, PolicyDefinition>,
): ContractDefinitionCard {
return {
id: contractDefinition.id,
id: contractDefinition.contractDefinitionId,
contractPolicy: this.extractPolicy(
contractDefinition.contractPolicyId,
policyDefinitionById,
Expand All @@ -57,7 +61,7 @@ export class ContractDefinitionCardBuilder {
policyDefinitionById,
),

criteria: contractDefinition.criteria.map((criterion) => ({
criteria: contractDefinition.assetSelector.map((criterion) => ({
label: this.extractCriterionOperation(criterion),
values: this.extractCriterionValues(criterion, assetById),
})),
Expand All @@ -75,59 +79,54 @@ export class ContractDefinitionCardBuilder {
};
}

private extractCriterionOperation(criterion: Criterion): string {
const {operandLeft} = criterion;
private extractCriterionOperation(criterion: UiCriterionDto): string {
const {operandLeft, operator} = criterion;
if (
operandLeft.toLowerCase() === AssetProperties.id &&
(criterion.operator.toUpperCase() === 'EQ' ||
criterion.operator.toUpperCase() === 'IN')
operandLeft.toLowerCase() === AssetProperties.id.toLowerCase() &&
(operator === 'EQ' || operator === 'IN')
) {
return 'Assets';
}

let operator =
OperatorSymbols[criterion.operator as Operator] ?? criterion.operator;
return `${operandLeft} ${operator}`;
let operatorStr = OperatorSymbols[operator as Operator] ?? operator;
return `${operandLeft} ${operatorStr}`;
}

private extractCriterionValues(
criterion: Criterion,
criterion: UiCriterionDto,
assetsById: Map<string, Asset>,
): ContractDefinitionCardCriterionValue[] {
let {operandLeft, operandRight} = criterion;

let values: (object | string)[] = [];
if (Array.isArray(operandRight)) {
values = operandRight as string[];
let values: string[] = [];
if (operandRight.type === 'VALUE_LIST') {
values = operandRight.valueList ?? [];
} else {
values = [operandRight];
values = [operandRight.value!!];
}

return values.map((it) => {
if (typeof it === 'string') {
const stringType: ContractDefinitionCardCriterionValue = {
type: 'string',
value: it,
searchTargets: [it],
};
const stringType: ContractDefinitionCardCriterionValue = {
type: 'string',
value: it,
searchTargets: [it],
};

// Try to find asset
if (operandLeft === AssetProperties.id) {
let asset = assetsById.get(it);
if (asset) {
return {
type: 'asset',
asset,
searchTargets: assetSearchTargets(asset),
};
}
// Try to find asset
if (operandLeft === AssetProperties.id) {
let asset = assetsById.get(it);
if (asset) {
return {
type: 'asset',
asset,
searchTargets: assetSearchTargets(asset),
};
}

return stringType;
}

// fall back to JSON for generic objects
return {type: 'json', json: it, searchTargets: [JSON.stringify(it)]};
return stringType;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
ContractDefinitionDto,
PolicyDefinition,
} from '../../../../core/services/api/legacy-managent-api-client';
import {ContractDefinitionEntry} from '@sovity.de/edc-client';
import {PolicyDefinition} from '../../../../core/services/api/legacy-managent-api-client';
import {Asset} from '../../../../core/services/models/asset';

export interface ContractDefinitionCard {
Expand All @@ -10,7 +8,7 @@ export interface ContractDefinitionCard {
contractPolicy: ContractDefinitionCardPolicy;
accessPolicy: ContractDefinitionCardPolicy;

detailJsonObj: ContractDefinitionDto;
detailJsonObj: ContractDefinitionEntry;
}

export interface ContractDefinitionCardPolicy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Component, OnDestroy, OnInit} from '@angular/core';
import {MatDialogRef} from '@angular/material/dialog';
import {Subject} from 'rxjs';
import {finalize, takeUntil} from 'rxjs/operators';
import {EdcApiService} from '../../../../core/services/api/edc-api.service';
import {
PolicyDefinition,
PolicyService,
Expand Down Expand Up @@ -30,7 +31,7 @@ export class ContractDefinitionEditorDialog implements OnInit, OnDestroy {
public form: ContractDefinitionEditorDialogForm,
private notificationService: NotificationService,
private policyService: PolicyService,
private contractDefinitionService: ContractDefinitionService,
private edcApiService: EdcApiService,
private contractDefinitionBuilder: ContractDefinitionBuilder,
private dialogRef: MatDialogRef<ContractDefinitionEditorDialog>,
public validationMessages: ValidationMessages,
Expand All @@ -55,9 +56,8 @@ export class ContractDefinitionEditorDialog implements OnInit, OnDestroy {
const formValue = this.form.value;
const contractDefinition =
this.contractDefinitionBuilder.buildContractDefinition(formValue);
this.form.group.disable();
this.loading = true;
this.contractDefinitionService
this.edcApiService
.createContractDefinition(contractDefinition)
.pipe(
takeUntil(this.ngOnDestroy$),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import {FormControl} from '@angular/forms';
import {MatDialog} from '@angular/material/dialog';
import {BehaviorSubject, Observable, distinctUntilChanged} from 'rxjs';
import {filter, map} from 'rxjs/operators';
import {ContractDefinitionPage} from '@sovity.de/edc-client';
import {EdcApiService} from '../../../../core/services/api/edc-api.service';
import {Fetched} from '../../../../core/services/models/fetched';
import {value$} from '../../../../core/utils/form-group-utils';
import {ContractDefinitionEditorDialogResult} from '../contract-definition-editor-dialog/contract-definition-editor-dialog-result';
import {ContractDefinitionEditorDialog} from '../contract-definition-editor-dialog/contract-definition-editor-dialog.component';
import {
ContractDefinitionPageData,
FetchResult,
} from './contract-definition-page.data';
import {ContractDefinitionPageData} from './contract-definition-page.data';
import {ContractDefinitionPageService} from './contract-definition-page.service';

@Component({
selector: 'app-contract-definition-page',
Expand All @@ -27,17 +23,13 @@ export class ContractDefinitionPageComponent implements OnInit {
private fetch$ = new BehaviorSubject(null);

constructor(
private edcApiService: EdcApiService,
private contractDefinitionPageService: ContractDefinitionPageService,
private readonly dialog: MatDialog,
) {}

ngOnInit(): void {
this.edcApiService
.getContractDefinitionPage()
.pipe(
map((data): ContractDefinitionPageData => ({})),
Fetched.wrap({failureMessage: 'Failed fetching contract list.'}),
)
this.contractDefinitionPageService
.contractDefinitionPageData$(this.fetch$, this.searchText$())
.subscribe((contractDefinitionList) => {
this.contractDefinitionList = contractDefinitionList;
});
Expand Down
Loading

0 comments on commit 8d35b85

Please sign in to comment.