Skip to content

Commit

Permalink
IMN-690 IMN-691 IMN-692 - Agreement routes pt.2 (#831)
Browse files Browse the repository at this point in the history
  • Loading branch information
paologaleotti authored Sep 11, 2024
1 parent 6e68aeb commit 6c3bd7c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 9 deletions.
67 changes: 58 additions & 9 deletions packages/bff/src/routers/agreementRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { makeApiProblem } from "../model/domain/errors.js";
import { PagoPAInteropBeClients } from "../providers/clientProvider.js";
import { fromBffAppContext } from "../utilities/context.js";
import {
activateAgreementErrorMapper,
emptyErrorMapper,
getAgreementByIdErrorMapper,
getAgreementsErrorMapper,
Expand Down Expand Up @@ -114,15 +115,63 @@ const agreementRouter = (
}
})

.delete("/agreements/:agreementId", async (_req, res) =>
res.status(501).send()
)
.post("/agreements/:agreementId/activate", async (_req, res) =>
res.status(501).send()
)
.post("/agreements/:agreementId/clone", async (_req, res) =>
res.status(501).send()
)
.delete("/agreements/:agreementId", async (req, res) => {
const ctx = fromBffAppContext(req.ctx, req.headers);

try {
await agreementService.deleteAgreement(req.params.agreementId, ctx);
return res.status(204).end();
} catch (error) {
const errorRes = makeApiProblem(
error,
emptyErrorMapper,
ctx.logger,
`Error deleting 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);

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

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

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

.post("/agreements/:agreementId/consumer-documents", async (_req, res) =>
res.status(501).send()
)
Expand Down
35 changes: 35 additions & 0 deletions packages/bff/src/services/agreementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,41 @@ export function agreementServiceBuilder(clients: PagoPAInteropBeClients) {
return enrichAgreement(agreement, clients, ctx);
},

async deleteAgreement(
agreementId: string,
{ headers }: WithLogger<BffAppContext>
): Promise<void> {
return await agreementProcessClient.deleteAgreement(undefined, {
params: { agreementId },
headers,
});
},

async activateAgreement(
agreementId: string,
ctx: WithLogger<BffAppContext>
): Promise<bffApi.Agreement> {
const agreement = await agreementProcessClient.activateAgreement(
undefined,
{
params: { agreementId },
headers: ctx.headers,
}
);
return enrichAgreement(agreement, clients, ctx);
},

async cloneAgreement(
agreementId: string,
{ headers }: WithLogger<BffAppContext>
): Promise<bffApi.CreatedResource> {
const agreement = await agreementProcessClient.cloneAgreement(undefined, {
params: { agreementId },
headers,
});
return { id: agreement.id };
},

async getAgreementsEserviceProducers(
{
offset,
Expand Down
7 changes: 7 additions & 0 deletions packages/bff/src/utilities/errorMappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ export const getAgreementByIdErrorMapper = (
)
.otherwise(() => HTTP_STATUS_INTERNAL_SERVER_ERROR);

export const activateAgreementErrorMapper = (
error: ApiError<ErrorCodes>
): number =>
match(error.code)
.with("agreementDescriptorNotFound", () => HTTP_STATUS_NOT_FOUND)
.otherwise(() => HTTP_STATUS_INTERNAL_SERVER_ERROR);

export const getClientUsersErrorMapper = (
error: ApiError<ErrorCodes>
): number =>
Expand Down

0 comments on commit 6c3bd7c

Please sign in to comment.