Skip to content

Commit

Permalink
IMN-685 IMN-686 IMN-687 IMN-688 - BFF Agreement routes pt.1 (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
paologaleotti authored Sep 11, 2024
1 parent 47861c8 commit 6e68aeb
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/bff/src/model/api/apiConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
155 changes: 155 additions & 0 deletions packages/bff/src/services/agreementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<BffAppContext>
): Promise<bffApi.CompactEServicesLight> {
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<BffAppContext>
) {
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<BffAppContext>
): Promise<bffApi.CompactOrganizations> {
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<BffAppContext>
): Promise<bffApi.CompactOrganizations> {
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)),
};
},
};
}

Expand Down Expand Up @@ -349,3 +495,12 @@ export function getCurrentDescriptor(
}
return descriptor;
}

const emptyPagination = (offset: number, limit: number) => ({
pagination: {
limit,
offset,
totalCount: 0,
},
results: [],
});
1 change: 0 additions & 1 deletion packages/commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 6e68aeb

Please sign in to comment.