Skip to content

Commit

Permalink
PIN-5500: added 'caching" tenant within enhanceCatalogEService method (
Browse files Browse the repository at this point in the history
  • Loading branch information
borgesis95 authored Jan 16, 2025
1 parent 5cf8901 commit 1a90fb7
Showing 1 changed file with 73 additions and 52 deletions.
125 changes: 73 additions & 52 deletions packages/backend-for-frontend/src/services/catalogService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
invalidZipStructure,
missingDescriptorInClonedEservice,
noDescriptorInEservice,
tenantNotFound,
} from "../model/errors.js";
import { getLatestActiveDescriptor } from "../model/modelMappingUtils.js";
import {
Expand Down Expand Up @@ -61,51 +62,74 @@ import { assertRequesterIsProducer } from "./validators.js";

export type CatalogService = ReturnType<typeof catalogServiceBuilder>;

const enhanceCatalogEService =
(
tenantProcessClient: TenantProcessClient,
agreementProcessClient: AgreementProcessClient,
headers: Headers,
requesterId: TenantId
): ((eservice: catalogApi.EService) => Promise<bffApi.CatalogEService>) =>
async (eservice: catalogApi.EService): Promise<bffApi.CatalogEService> => {
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<bffApi.CatalogEService[]> => {
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<bffApi.CatalogEService>) =>
async (eservice: catalogApi.EService): Promise<bffApi.CatalogEService> => {
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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1a90fb7

Please sign in to comment.