From c4e46a2e65aac8d680245bbc6c6191fe61b02069 Mon Sep 17 00:00:00 2001 From: Simone Camito <32327779+MalpenZibo@users.noreply.github.com> Date: Thu, 12 Sep 2024 09:55:56 +0200 Subject: [PATCH] IMN-659 IMN-673 IMN-674 eservice (#885) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sandro Tajè --- packages/bff/src/model/api/apiConverter.ts | 16 +++++++ packages/bff/src/routers/catalogRouter.ts | 50 ++++++++++++++++++--- packages/bff/src/services/catalogService.ts | 39 ++++++++++++++++ 3 files changed, 100 insertions(+), 5 deletions(-) diff --git a/packages/bff/src/model/api/apiConverter.ts b/packages/bff/src/model/api/apiConverter.ts index fdf819ba81..219cadfd61 100644 --- a/packages/bff/src/model/api/apiConverter.ts +++ b/packages/bff/src/model/api/apiConverter.ts @@ -153,6 +153,22 @@ export function toTenantWithOnlyAttributes( }; } +export function toCatalogCreateEServiceSeed( + eServiceSeed: bffApi.EServiceSeed +): catalogApi.EServiceSeed { + return { + ...eServiceSeed, + descriptor: { + audience: [], + voucherLifespan: 60, + dailyCallsPerConsumer: 1, + dailyCallsTotal: 1, + agreementApprovalPolicy: + catalogApi.AgreementApprovalPolicy.Values.AUTOMATIC, + }, + }; +} + export function toCompactEserviceLight( eservice: agreementApi.CompactEService ): bffApi.CompactEServiceLight { diff --git a/packages/bff/src/routers/catalogRouter.ts b/packages/bff/src/routers/catalogRouter.ts index 2090551601..67dba5b017 100644 --- a/packages/bff/src/routers/catalogRouter.ts +++ b/packages/bff/src/routers/catalogRouter.ts @@ -151,7 +151,6 @@ const catalogRouter = ( } } ) - .post("/eservices", async (_req, res) => res.status(501).send()) .get("/eservices/:eServiceId/consumers", async (req, res) => { const ctx = fromBffAppContext(req.ctx, req.headers); try { @@ -404,10 +403,51 @@ const catalogRouter = ( } } ) - .delete("/eservices/:eServiceId", async (_req, res) => - res.status(501).send() - ) - .put("/eservices/:eServiceId", async (_req, res) => res.status(501).send()) + .post("/eservices", async (req, res) => { + const ctx = fromBffAppContext(req.ctx, req.headers); + try { + const createdResource = await catalogService.createEService( + req.body, + ctx + ); + return res.status(200).send(createdResource); + } catch (error) { + const errorRes = makeApiProblem( + error, + emptyErrorMapper, + ctx.logger, + `Error creating eservice with seed: ${req.body}` + ); + return res.status(errorRes.status).json(errorRes).end(); + } + }) + .delete("/eservices/:eServiceId", async (req, res) => { + const ctx = fromBffAppContext(req.ctx, req.headers); + try { + await catalogService.deleteEService( + unsafeBrandId(req.params.eServiceId), + ctx + ); + return res.status(204).send(); + } catch (error) { + const errorRes = makeApiProblem(error, emptyErrorMapper, ctx.logger); + return res.status(errorRes.status).json(errorRes).end(); + } + }) + .put("/eservices/:eServiceId", async (req, res) => { + const ctx = fromBffAppContext(req.ctx, req.headers); + try { + const createdResource = await catalogService.updateEServiceById( + unsafeBrandId(req.params.eServiceId), + req.body, + ctx + ); + return res.status(200).send(createdResource); + } catch (error) { + const errorRes = makeApiProblem(error, emptyErrorMapper, ctx.logger); + return res.status(errorRes.status).json(errorRes).end(); + } + }) .post("/eservices/:eServiceId/riskAnalysis", async (req, res) => { const ctx = fromBffAppContext(req.ctx, req.headers); try { diff --git a/packages/bff/src/services/catalogService.ts b/packages/bff/src/services/catalogService.ts index 7b8f4a9e46..d95f9d5717 100644 --- a/packages/bff/src/services/catalogService.ts +++ b/packages/bff/src/services/catalogService.ts @@ -17,6 +17,7 @@ import { } from "pagopa-interop-models"; import { CreatedResource } from "../../../api-clients/dist/bffApi.js"; import { config } from "../config/config.js"; +import { toCatalogCreateEServiceSeed } from "../model/api/apiConverter.js"; import { catalogApiDescriptorState } from "../model/api/apiTypes.js"; import { toBffCatalogApiDescriptorAttributes, @@ -330,6 +331,44 @@ export function catalogServiceBuilder( id: updatedEservice.id, }; }, + createEService: async ( + eServiceSeed: bffApi.EServiceSeed, + { headers }: WithLogger + ): Promise => { + const { id, descriptors } = await catalogProcessClient.createEService( + toCatalogCreateEServiceSeed(eServiceSeed), + { + headers, + } + ); + return { id, descriptorId: descriptors[0].id }; + }, + updateEServiceById: async ( + eServiceId: EServiceId, + updateEServiceSeed: bffApi.UpdateEServiceSeed, + { headers }: WithLogger + ): Promise => { + const { id } = await catalogProcessClient.updateEServiceById( + updateEServiceSeed, + { + headers, + params: { + eServiceId, + }, + } + ); + return { id }; + }, + deleteEService: async ( + eServiceId: EServiceId, + { headers }: WithLogger + ): Promise => + await catalogProcessClient.deleteEService(undefined, { + headers, + params: { + eServiceId, + }, + }), createEServiceDocument: async ( eServiceId: string, descriptorId: string,