diff --git a/packages/backend-for-frontend/src/services/catalogService.ts b/packages/backend-for-frontend/src/services/catalogService.ts index ab79e56a25..7d848ca186 100644 --- a/packages/backend-for-frontend/src/services/catalogService.ts +++ b/packages/backend-for-frontend/src/services/catalogService.ts @@ -26,6 +26,7 @@ import { invalidZipStructure, missingDescriptorInClonedEservice, noDescriptorInEservice, + tenantNotFound, } from "../model/errors.js"; import { getLatestActiveDescriptor } from "../model/modelMappingUtils.js"; import { @@ -61,51 +62,74 @@ import { assertRequesterIsProducer } from "./validators.js"; export type CatalogService = ReturnType; -const enhanceCatalogEService = - ( - tenantProcessClient: TenantProcessClient, - agreementProcessClient: AgreementProcessClient, - headers: Headers, - requesterId: TenantId - ): ((eservice: catalogApi.EService) => Promise) => - async (eservice: catalogApi.EService): Promise => { - const producerTenant = await tenantProcessClient.tenant.getTenant({ - headers, - params: { - id: eservice.producerId, - }, - }); - - const requesterTenant: tenantApi.Tenant = - requesterId !== eservice.producerId - ? await tenantProcessClient.tenant.getTenant({ +export const enhanceCatalogEservices = async ( + eservices: catalogApi.EService[], + tenantProcessClient: TenantProcessClient, + agreementProcessClient: AgreementProcessClient, + headers: Headers, + requesterId: TenantId +): Promise => { + const tenantsIds = new Set([ + ...eservices.map((e) => e.producerId), + requesterId, + ] as TenantId[]); + + const cachedTenants = new Map( + await Promise.all( + Array.from(tenantsIds).map( + async (tenantId): Promise<[TenantId, tenantApi.Tenant]> => [ + tenantId, + await tenantProcessClient.tenant.getTenant({ headers, - params: { - id: requesterId, - }, - }) - : producerTenant; - - const latestActiveDescriptor = getLatestActiveDescriptor(eservice); - - const latestAgreement = await getLatestAgreement( - agreementProcessClient, - requesterId, - eservice, - headers - ); - - const isRequesterEqProducer = requesterId === eservice.producerId; - - return toBffCatalogApiEService( - eservice, - producerTenant, - requesterTenant, - isRequesterEqProducer, - latestActiveDescriptor, - latestAgreement - ); + params: { id: tenantId }, + }), + ] + ) + ) + ); + + const getCachedTenant = (tenantId: TenantId): tenantApi.Tenant => { + const tenant = cachedTenants.get(tenantId); + if (!tenant) { + throw tenantNotFound(tenantId); + } + return tenant; }; + const enhanceEService = + ( + agreementProcessClient: AgreementProcessClient, + headers: Headers, + requesterId: TenantId + ): ((eservice: catalogApi.EService) => Promise) => + async (eservice: catalogApi.EService): Promise => { + const producerTenant = getCachedTenant(eservice.producerId as TenantId); + const requesterTenant = getCachedTenant(requesterId); + + const latestActiveDescriptor = getLatestActiveDescriptor(eservice); + + const latestAgreement = await getLatestAgreement( + agreementProcessClient, + requesterId, + eservice, + headers + ); + + const isRequesterEqProducer = requesterId === eservice.producerId; + + return toBffCatalogApiEService( + eservice, + producerTenant, + requesterTenant, + isRequesterEqProducer, + latestActiveDescriptor, + latestAgreement + ); + }; + + return await Promise.all( + eservices.map(enhanceEService(agreementProcessClient, headers, requesterId)) + ); +}; const enhanceProducerEService = ( eservice: catalogApi.EService @@ -209,15 +233,12 @@ export function catalogServiceBuilder( queries, }); - const results = await Promise.all( - eservicesResponse.results.map( - enhanceCatalogEService( - tenantProcessClient, - agreementProcessClient, - headers, - requesterId - ) - ) + const results = await enhanceCatalogEservices( + eservicesResponse.results, + tenantProcessClient, + agreementProcessClient, + headers, + requesterId ); const response: bffApi.CatalogEServices = { results,