From 6e68aeb203422171007e481d76fc06ea5ee4c795 Mon Sep 17 00:00:00 2001 From: paologaleotti <45665769+paologaleotti@users.noreply.github.com> Date: Wed, 11 Sep 2024 11:59:58 +0200 Subject: [PATCH] IMN-685 IMN-686 IMN-687 IMN-688 - BFF Agreement routes pt.1 (#830) --- packages/bff/src/model/api/apiConverter.ts | 18 ++ packages/bff/src/services/agreementService.ts | 155 ++++++++++++++++++ packages/commons/src/index.ts | 1 - 3 files changed, 173 insertions(+), 1 deletion(-) diff --git a/packages/bff/src/model/api/apiConverter.ts b/packages/bff/src/model/api/apiConverter.ts index 7bdd1902e0..fdf819ba81 100644 --- a/packages/bff/src/model/api/apiConverter.ts +++ b/packages/bff/src/model/api/apiConverter.ts @@ -153,6 +153,24 @@ export function toTenantWithOnlyAttributes( }; } +export function toCompactEserviceLight( + eservice: agreementApi.CompactEService +): bffApi.CompactEServiceLight { + return { + id: eservice.id, + name: eservice.name, + }; +} + +export function toCompactOrganization( + organization: agreementApi.CompactOrganization +): bffApi.CompactOrganization { + return { + id: organization.id, + name: organization.name, + }; +} + export function toCompactEservice( eservice: catalogApi.EService, producer: tenantApi.Tenant diff --git a/packages/bff/src/services/agreementService.ts b/packages/bff/src/services/agreementService.ts index 559d20b07a..187d80f5b5 100644 --- a/packages/bff/src/services/agreementService.ts +++ b/packages/bff/src/services/agreementService.ts @@ -19,6 +19,10 @@ import { } from "../providers/clientProvider.js"; import { BffAppContext, Headers } from "../utilities/context.js"; import { agreementDescriptorNotFound } from "../model/domain/errors.js"; +import { + toCompactEserviceLight, + toCompactOrganization, +} from "../model/api/apiConverter.js"; import { toCompactEservice, toCompactDescriptor, @@ -103,6 +107,148 @@ export function agreementServiceBuilder(clients: PagoPAInteropBeClients) { return enrichAgreement(agreement, clients, ctx); }, + + async getAgreementsEserviceProducers( + { + offset, + limit, + requesterId, + states, + eServiceName, + }: { + offset: number; + limit: number; + requesterId: string; + states: agreementApi.AgreementState[]; + eServiceName?: string; + }, + { headers, logger }: WithLogger + ): Promise { + logger.info( + `Retrieving producer eservices from agreement filtered by eservice name ${eServiceName}, offset ${offset}, limit ${limit}` + ); + + if (eServiceName && eServiceName.length < 3) { + return emptyPagination(offset, limit); + } + + const eservices = await agreementProcessClient.getAgreementEServices({ + queries: { + offset, + limit, + eServiceName, + producersIds: [requesterId], + states, + }, + headers, + }); + + return { + pagination: { + limit, + offset, + totalCount: eservices.totalCount, + }, + results: eservices.results.map((e) => toCompactEserviceLight(e)), + }; + }, + + async getAgreementsEserviceConsumers( + offset: number, + limit: number, + requesterId: string, + eServiceName: string | undefined, + { headers, logger }: WithLogger + ) { + logger.info( + `Retrieving consumer eservices from agreement filtered by eservice name ${eServiceName}, offset ${offset}, limit ${limit}` + ); + + if (eServiceName && eServiceName.length < 3) { + return emptyPagination(offset, limit); + } + + const eservices = await agreementProcessClient.getAgreementEServices({ + queries: { + offset, + limit, + eServiceName, + consumersIds: [requesterId], + }, + headers, + }); + + return { + pagination: { + limit, + offset, + totalCount: eservices.totalCount, + }, + results: eservices.results.map((e) => toCompactEserviceLight(e)), + }; + }, + + async getAgreementProducers( + offset: number, + limit: number, + producerName: string | undefined, + { logger, headers }: WithLogger + ): Promise { + logger.info(`Retrieving agreement producers`); + + if (producerName && producerName.length < 3) { + return emptyPagination(offset, limit); + } + + const producers = await agreementProcessClient.getAgreementProducers({ + queries: { + offset, + limit, + producerName, + }, + headers, + }); + + return { + pagination: { + limit, + offset, + totalCount: producers.totalCount, + }, + results: producers.results.map((p) => toCompactOrganization(p)), + }; + }, + + async getAgreementConsumers( + offset: number, + limit: number, + consumerName: string | undefined, + { logger, headers }: WithLogger + ): Promise { + logger.info(`Retrieving agreement consumers`); + + if (consumerName && consumerName.length < 3) { + return emptyPagination(offset, limit); + } + + const consumers = await agreementProcessClient.getAgreementConsumers({ + queries: { + offset, + limit, + consumerName, + }, + headers, + }); + + return { + pagination: { + limit, + offset, + totalCount: consumers.totalCount, + }, + results: consumers.results.map((c) => toCompactOrganization(c)), + }; + }, }; } @@ -349,3 +495,12 @@ export function getCurrentDescriptor( } return descriptor; } + +const emptyPagination = (offset: number, limit: number) => ({ + pagination: { + limit, + offset, + totalCount: 0, + }, + results: [], +}); diff --git a/packages/commons/src/index.ts b/packages/commons/src/index.ts index 9b22427c9b..e72b686511 100644 --- a/packages/commons/src/index.ts +++ b/packages/commons/src/index.ts @@ -20,7 +20,6 @@ export * from "./types/index.js"; export * from "./utils/arrays.js"; export * from "./utils/getAll.js"; export * from "./utils/date.js"; -export * from "./utils/getAll.js"; export * from "./auth/jwk.js"; export * from "./auth/converters.js"; export * from "./email-manager/index.js";