Skip to content

Commit

Permalink
explore: service info
Browse files Browse the repository at this point in the history
  • Loading branch information
jxhnx committed Oct 31, 2024
1 parent a34b068 commit 2367da9
Showing 1 changed file with 70 additions and 21 deletions.
91 changes: 70 additions & 21 deletions src/app/components/marketplace/marketplace.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { MatCardModule } from '@angular/material/card';
import { MatSelectModule } from '@angular/material/select';
import { QueryService } from '../../services/query.service';
import { HttpErrorResponse } from '@angular/common/http';
import { QueryResponse, NodeQueryResult } from '../../types/dtos';
import { EMPTY_RESULTS } from '../../types/dtos';
// import { QueryResponse, NodeQueryResult } from '../../types/dtos';
// import { EMPTY_RESULTS } from '../../types/dtos';
import { catchError, forkJoin, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';

Expand Down Expand Up @@ -37,6 +37,41 @@ WHERE 'LegalParticipant' IN labels(lp)
RETURN DISTINCT lp.legalName AS legalName
`;

// TODO:
// Offers names not unique (see Materna)
// 1. Get all LegalParticipants 2. Map offers to LegalParticipants 3. RUn OfferInfoQuery for Node ID instead

interface QueryResponse<T> {
totalCount: number;
items: T[];
}

interface NodeQueryResult {
id: number;
value: Record<string, unknown>;
labels: string[];
}

const EMPTY_RESULTS: QueryResponse<NodeQueryResult> = {
items: [],
totalCount: 0,
};

interface ServiceCard {
legalParticipant: Resource;
serviceOffering: Resource;
}

interface Resource {
name: string;
description?: string;
}

interface ServiceOfferingNodes {
data: QueryResponse<NodeQueryResult>;
offer: string;
}

@Component({
selector: 'app-marketplace',
standalone: true,
Expand All @@ -50,6 +85,7 @@ export class MarketplaceComponent implements OnInit {
legalName: string | null = null;
legalNames: string[] = [];
serviceOfferingNames: string[] = [];
services: ServiceCard[] = [];

constructor(private _queryService: QueryService) {}

Expand Down Expand Up @@ -77,9 +113,8 @@ export class MarketplaceComponent implements OnInit {
.queryData<{ serviceOfferingName: string }>(GET_OFFER_NAMES_QUERY, { participant: legalName })
.pipe(
switchMap((result) => {
this.serviceOfferingNames = result.items.map((item) => item.serviceOfferingName);

const offerInfoQueries = this.serviceOfferingNames.map((offer) => {
const offers = result.items.map((item) => item.serviceOfferingName);
const offerInfoQueries = offers.map((offer) => {
console.log(`Querying data for offer: ${offer}`);

return this._queryService
Expand All @@ -89,6 +124,7 @@ export class MarketplaceComponent implements OnInit {
console.error(`Error fetching data for offer "${offer}":`, err);
return of(EMPTY_RESULTS);
}),
switchMap((data) => of({ data, offer })),
);
});

Expand All @@ -97,28 +133,41 @@ export class MarketplaceComponent implements OnInit {
catchError((err: HttpErrorResponse) => {
this.error = err;
console.error('Error occurred during query:', err);
return of([EMPTY_RESULTS]);
return of([{ data: EMPTY_RESULTS, offer: '' }] as ServiceOfferingNodes[]);
}),
)
.subscribe((results) => {
this.data = {
totalCount: results.reduce((acc, result) => acc + result.totalCount, 0),
items: [],
};

results.forEach((result, index) => {
const serviceOfferingName = this.serviceOfferingNames[index];
console.log(`Service Offering: ${serviceOfferingName}`);

result.items.forEach((item) => {
console.log(item);
});

this.data.items.push(...result.items);
.subscribe((results: ServiceOfferingNodes[]) => {
this.data = { totalCount: 0, items: [] };
this.services = results.map(({ data, offer }) => {
this.data.totalCount += data.totalCount;
this.data.items.push(...data.items);
return this.fillServiceCard(offer, data.items);
});

console.log('card:', this.services);
});
}

private fillServiceCard(serviceOfferingName: string, items: NodeQueryResult[]): ServiceCard {
const serviceCard: ServiceCard = {
legalParticipant: { name: 'Default Participant' },
serviceOffering: { name: serviceOfferingName },
};

console.log(items);
items.forEach((item) => {
if (item.labels.includes('LegalParticipant') && item.value['legalName']) {
serviceCard.legalParticipant = { name: item.value['legalName'] as string };
}

if (item.labels.includes('ServiceOffering') && item.value['name']) {
serviceCard.serviceOffering = { name: item.value['name'] as string };
}
});

return serviceCard;
}

onLegalNameChange(newLegalName: string | null): void {
this.legalName = newLegalName;
this.fetchData(this.legalName);
Expand Down

0 comments on commit 2367da9

Please sign in to comment.