Skip to content

Commit

Permalink
IMN-683 IMN-684 IMN-689 IMN-703 - BFF Agreement enhanceAgreement (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
paologaleotti authored Sep 11, 2024
1 parent 0e45e69 commit 3706aea
Show file tree
Hide file tree
Showing 8 changed files with 569 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/bff/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ app.use(genericRouter(zodiosCtx));
app.use(catalogRouter(zodiosCtx, clients, fileManager));
app.use(attributeRouter(zodiosCtx, clients));
app.use(purposeRouter(zodiosCtx, clients));
app.use(agreementRouter(zodiosCtx));
app.use(agreementRouter(zodiosCtx, clients));
app.use(selfcareRouter(zodiosCtx));
app.use(tenantRouter(zodiosCtx, clients));
app.use(clientRouter(zodiosCtx, clients));
Expand Down
36 changes: 31 additions & 5 deletions packages/bff/src/model/api/apiConverter.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
/* eslint-disable max-params */

import {
DescriptorWithOnlyAttributes,
TenantWithOnlyAttributes,
} from "pagopa-interop-agreement-lifecycle";
import {
agreementApi,
authorizationApi,
bffApi,
catalogApi,
selfcareV2ClientApi,
tenantApi,
agreementApi,
} from "pagopa-interop-api-clients";
import { match, P } from "ts-pattern";
import {
AttributeId,
CertifiedTenantAttribute,
DeclaredTenantAttribute,
EServiceAttribute,
unsafeBrandId,
TenantAttribute,
CertifiedTenantAttribute,
AttributeId,
tenantAttributeType,
unsafeBrandId,
VerifiedTenantAttribute,
DeclaredTenantAttribute,
} from "pagopa-interop-models";
import { isAgreementUpgradable } from "../validators.js";

Expand Down Expand Up @@ -152,6 +153,31 @@ export function toTenantWithOnlyAttributes(
};
}

export function toCompactEservice(
eservice: catalogApi.EService,
producer: tenantApi.Tenant
): bffApi.CompactEService {
return {
id: eservice.id,
name: eservice.name,
producer: {
id: producer.id,
name: producer.name,
kind: producer.kind,
},
};
}

export function toCompactDescriptor(
descriptor: catalogApi.EServiceDescriptor
): bffApi.CompactDescriptor {
return {
id: descriptor.id,
audience: descriptor.audience,
state: descriptor.state,
version: descriptor.version,
};
}
export const toBffApiCompactClient = (
input: authorizationApi.ClientWithKeys
): bffApi.CompactClient => ({
Expand Down
11 changes: 11 additions & 0 deletions packages/bff/src/model/domain/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const errorCodes = {
invalidInterfaceFileDetected: "0019",
openapiVersionNotRecognized: "0020",
interfaceExtractingInfoError: "0021",
agreementDescriptorNotFound: "0022",
};

export type ErrorCodes = keyof typeof errorCodes;
Expand Down Expand Up @@ -66,6 +67,16 @@ export function purposeNotFound(purposeId: string): ApiError<ErrorCodes> {
});
}

export function agreementDescriptorNotFound(
agreementId: string
): ApiError<ErrorCodes> {
return new ApiError({
detail: `Descriptor of agreement ${agreementId} not found`,
code: "agreementDescriptorNotFound",
title: "Agreement descriptor not found",
});
}

export function eServiceNotFound(eserviceId: string): ApiError<ErrorCodes> {
return new ApiError({
detail: `EService ${eserviceId} not found`,
Expand Down
99 changes: 91 additions & 8 deletions packages/bff/src/routers/agreementRouter.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,86 @@
import { ZodiosEndpointDefinitions } from "@zodios/core";
import { ZodiosRouter } from "@zodios/express";
import { bffApi } from "pagopa-interop-api-clients";
import {
ExpressContext,
ZodiosContext,
ExpressContext,
zodiosValidationErrorToApiProblem,
} from "pagopa-interop-commons";
import { bffApi } from "pagopa-interop-api-clients";
import { makeApiProblem } from "../model/domain/errors.js";
import { PagoPAInteropBeClients } from "../providers/clientProvider.js";
import { fromBffAppContext } from "../utilities/context.js";
import {
emptyErrorMapper,
getAgreementByIdErrorMapper,
getAgreementsErrorMapper,
} from "../utilities/errorMappers.js";
import { agreementServiceBuilder } from "../services/agreementService.js";

const agreementRouter = (
ctx: ZodiosContext
ctx: ZodiosContext,
clients: PagoPAInteropBeClients
): ZodiosRouter<ZodiosEndpointDefinitions, ExpressContext> => {
const agreementRouter = ctx.router(bffApi.agreementsApi.api, {
validationErrorHandler: zodiosValidationErrorToApiProblem,
});

const agreementService = agreementServiceBuilder(clients);

agreementRouter
.get("/agreements", async (_req, res) => res.status(501).send())
.post("/agreements", async (_req, res) => res.status(501).send())
.get("/agreements", async (req, res) => {
const ctx = fromBffAppContext(req.ctx, req.headers);

try {
const {
consumersIds,
eservicesIds,
limit,
offset,
producersIds,
showOnlyUpgradeable,
states,
} = req.query;

const result = await agreementService.getAgreements(
{
offset,
limit,
producersIds,
eservicesIds,
consumersIds,
states,
showOnlyUpgradeable,
},
ctx
);
return res.status(200).json(result).end();
} catch (error) {
const errorRes = makeApiProblem(
error,
getAgreementsErrorMapper,
ctx.logger,
"Error retrieving agreements"
);
return res.status(errorRes.status).json(errorRes).end();
}
})

.post("/agreements", async (req, res) => {
const ctx = fromBffAppContext(req.ctx, req.headers);

try {
const result = await agreementService.createAgreement(req.body, ctx);
return res.status(200).json(result).end();
} catch (error) {
const errorRes = makeApiProblem(
error,
emptyErrorMapper,
ctx.logger,
`Error creating agreement for EService ${req.body.eserviceId} and Descriptor ${req.body.descriptorId}`
);
return res.status(errorRes.status).json(errorRes).end();
}
})
.get("/producers/agreements/eservices", async (_req, res) =>
res.status(501).send()
)
Expand All @@ -28,9 +93,27 @@ const agreementRouter = (
.get("/agreements/filter/consumers", async (_req, res) =>
res.status(501).send()
)
.get("/agreements/:agreementId", async (_req, res) =>
res.status(501).send()
)

.get("/agreements/:agreementId", async (req, res) => {
const ctx = fromBffAppContext(req.ctx, req.headers);

try {
const result = await agreementService.getAgreementById(
req.params.agreementId,
ctx
);
return res.status(200).json(result).end();
} catch (error) {
const errorRes = makeApiProblem(
error,
getAgreementByIdErrorMapper,
ctx.logger,
`Error retrieving agreement ${req.params.agreementId}`
);
return res.status(errorRes.status).json(errorRes).end();
}
})

.delete("/agreements/:agreementId", async (_req, res) =>
res.status(501).send()
)
Expand Down
Loading

0 comments on commit 3706aea

Please sign in to comment.