Skip to content

Commit

Permalink
IMN-693 IMN-694 IMN-695 IMN-696 - Agreement routes pt.4 (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
paologaleotti authored Sep 12, 2024
1 parent deeee18 commit 1cf21d5
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 23 deletions.
6 changes: 6 additions & 0 deletions packages/api-clients/template-bff.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export const {{@key}}Endpoints = makeApi([
response: z.instanceof(Buffer),
{{else if (and (eq method "get") (eq path "/eservices/:eServiceId/consumers"))}}
response: z.instanceof(Buffer),
{{else if (and (eq method "post") (eq path "/agreements/:agreementId/consumer-documents"))}}
response: z.instanceof(Buffer),
{{else if (and (eq method "get") (eq path "/agreements/:agreementId/consumer-documents"))}}
response: z.instanceof(Buffer),
{{else if (and (eq method "get") (eq path "/agreements/:agreementId/contract"))}}
response: z.instanceof(Buffer),
{{else if (and (eq method "get") (eq path "/eservices/:eServiceId/descriptors/:descriptorId/documents/:documentId"))}}
response: z.instanceof(Buffer),
{{else}}
Expand Down
3 changes: 3 additions & 0 deletions packages/bff/.env
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ RISK_ANALYSIS_DOCUMENTS_PATH="risk-analysis/docs"

ESERVICE_DOCUMENTS_PATH="interop-eservice-documents"

CONSUMER_DOCUMENTS_PATH="interop-consumer-documents"
CONSUMER_DOCUMENTS_CONTAINER="interop-local-bucket"

AWS_CONFIG_FILE=aws.config.local

SELFCARE_V2_URL=localhost
Expand Down
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(rateLimiterMiddleware(redisRateLimiter));
app.use(catalogRouter(zodiosCtx, clients, fileManager));
app.use(attributeRouter(zodiosCtx, clients));
app.use(purposeRouter(zodiosCtx, clients));
app.use(agreementRouter(zodiosCtx, clients));
app.use(agreementRouter(zodiosCtx, clients, fileManager));
app.use(selfcareRouter(zodiosCtx));
app.use(supportRouter(zodiosCtx, clients, redisRateLimiter));
app.use(toolRouter(zodiosCtx));
Expand Down
4 changes: 4 additions & 0 deletions packages/bff/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ export type TenantProcessServerConfig = z.infer<
export const AgreementProcessServerConfig = z
.object({
AGREEMENT_PROCESS_URL: APIEndpoint,
CONSUMER_DOCUMENTS_PATH: z.string(),
CONSUMER_DOCUMENTS_CONTAINER: z.string(),
})
.transform((c) => ({
agreementProcessUrl: c.AGREEMENT_PROCESS_URL,
consumerDocumentsPath: c.CONSUMER_DOCUMENTS_PATH,
consumerDocumentsContainer: c.CONSUMER_DOCUMENTS_CONTAINER,
}));
export type AgreementProcessServerConfig = z.infer<
typeof AgreementProcessServerConfig
Expand Down
31 changes: 31 additions & 0 deletions packages/bff/src/model/domain/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export const errorCodes = {
invalidJwtClaim: "0026",
samlNotValid: "0027",
missingSelfcareId: "0028",
invalidContentType: "0029",
contractNotFound: "0030",
contractException: "0031",
};

export type ErrorCodes = keyof typeof errorCodes;
Expand Down Expand Up @@ -283,3 +286,31 @@ export function interfaceExtractingInfoError(): ApiError<ErrorCodes> {
title: "Error extracting info from interface file",
});
}

export function contractNotFound(agreementId: string): ApiError<ErrorCodes> {
return new ApiError({
detail: `Contract not found for agreement ${agreementId}`,
code: "contractNotFound",
title: "Contract not found",
});
}

export function contractException(agreementId: string): ApiError<ErrorCodes> {
return new ApiError({
detail: `Contract exception for agreement ${agreementId}`,
code: "contractException",
title: "Contract exception",
});
}

export function invalidContentType(
contentType: string,
agreementId: string,
documentId: string
): ApiError<ErrorCodes> {
return new ApiError({
detail: `Invalid contentType ${contentType} for document ${documentId} from agreement ${agreementId}`,
code: "invalidContentType",
title: "Invalid content type",
});
}
108 changes: 101 additions & 7 deletions packages/bff/src/routers/agreementRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ZodiosContext,
ExpressContext,
zodiosValidationErrorToApiProblem,
FileManager,
} from "pagopa-interop-commons";
import { makeApiProblem } from "../model/domain/errors.js";
import { PagoPAInteropBeClients } from "../providers/clientProvider.js";
Expand All @@ -13,19 +14,22 @@ import {
activateAgreementErrorMapper,
emptyErrorMapper,
getAgreementByIdErrorMapper,
getAgreementConsumerDocumentErrorMapper,
getAgreementContractErrorMapper,
getAgreementsErrorMapper,
} from "../utilities/errorMappers.js";
import { agreementServiceBuilder } from "../services/agreementService.js";

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

const agreementService = agreementServiceBuilder(clients);
const agreementService = agreementServiceBuilder(clients, fileManager);

agreementRouter
.get("/agreements", async (req, res) => {
Expand Down Expand Up @@ -132,6 +136,35 @@ const agreementRouter = (
}
})

.post("/agreements/:agreementId/activate", async (_req, res) =>
res.status(501).send()
)
.post("/agreements/:agreementId/clone", async (_req, res) =>
res.status(501).send()
)

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

try {
const result = await agreementService.addAgreementConsumerDocument(
req.params.agreementId,
req.body,
ctx
);

return res.status(200).send(result).end();
} catch (error) {
const errorRes = makeApiProblem(
error,
emptyErrorMapper,
ctx.logger,
`Error adding consumer document to agreement ${req.params.agreementId}`
);
return res.status(errorRes.status).json(errorRes).end();
}
})

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

Expand Down Expand Up @@ -175,17 +208,77 @@ const agreementRouter = (
.post("/agreements/:agreementId/consumer-documents", async (_req, res) =>
res.status(501).send()
)

.get(
"/agreements/:agreementId/consumer-documents/:documentId",
async (_req, res) => res.status(501).send()
async (req, res) => {
const ctx = fromBffAppContext(req.ctx, req.headers);

try {
const result = await agreementService.getAgreementConsumerDocument(
req.params.agreementId,
req.params.documentId,
ctx
);

return res.status(200).send(result).end();
} catch (error) {
const errorRes = makeApiProblem(
error,
getAgreementConsumerDocumentErrorMapper,
ctx.logger,
`Error downloading contract for agreement ${req.params.agreementId}`
);
return res.status(errorRes.status).json(errorRes).end();
}
}
)

.delete(
"/agreements/:agreementId/consumer-documents/:documentId",
async (_req, res) => res.status(501).send()
)
.get("/agreements/:agreementId/contract", async (_req, res) =>
res.status(501).send()
async (req, res) => {
const ctx = fromBffAppContext(req.ctx, req.headers);

try {
await agreementService.removeConsumerDocument(
req.params.agreementId,
req.params.documentId,
ctx
);

return res.status(204).end();
} catch (error) {
const errorRes = makeApiProblem(
error,
emptyErrorMapper,
ctx.logger,
`Error deleting consumer document ${req.params.documentId} for agreement ${req.params.agreementId}`
);
return res.status(errorRes.status).json(errorRes).end();
}
}
)
.get("/agreements/:agreementId/contract", async (req, res) => {
const ctx = fromBffAppContext(req.ctx, req.headers);

try {
const result = await agreementService.getAgreementContract(
req.params.agreementId,
ctx
);

return res.status(200).send(result).end();
} catch (error) {
const errorRes = makeApiProblem(
error,
getAgreementContractErrorMapper,
ctx.logger,
`Error downloading contract for agreement ${req.params.agreementId}`
);
return res.status(errorRes.status).json(errorRes).end();
}
})

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

Expand All @@ -206,6 +299,7 @@ const agreementRouter = (
return res.status(errorRes.status).json(errorRes).end();
}
})

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

Expand Down
Loading

0 comments on commit 1cf21d5

Please sign in to comment.