From 0968cae52fa1c9119a1208dbf6a146e1a7410adf Mon Sep 17 00:00:00 2001 From: Roberto Taglioni Date: Wed, 6 Mar 2024 09:46:18 +0100 Subject: [PATCH] IMN-243 Refactor logic functions (part3) in catalog-process (#243) --- .../open-api/catalog-service-spec.yml | 4 + .../src/routers/EServiceRouter.ts | 7 +- .../src/services/catalogService.ts | 450 ++++++++---------- 3 files changed, 197 insertions(+), 264 deletions(-) diff --git a/packages/catalog-process/open-api/catalog-service-spec.yml b/packages/catalog-process/open-api/catalog-service-spec.yml index 2b7bc653dc..da8e92240e 100644 --- a/packages/catalog-process/open-api/catalog-service-spec.yml +++ b/packages/catalog-process/open-api/catalog-service-spec.yml @@ -419,6 +419,10 @@ paths: responses: "200": description: EService with draft descriptor updated. + content: + application/json: + schema: + $ref: "#/components/schemas/EService" "400": description: Invalid input content: diff --git a/packages/catalog-process/src/routers/EServiceRouter.ts b/packages/catalog-process/src/routers/EServiceRouter.ts index 8340832399..dfe5c780a9 100644 --- a/packages/catalog-process/src/routers/EServiceRouter.ts +++ b/packages/catalog-process/src/routers/EServiceRouter.ts @@ -389,13 +389,16 @@ const eservicesRouter = ( authorizationMiddleware([ADMIN_ROLE, API_ROLE]), async (req, res) => { try { - await catalogService.updateDraftDescriptor( + const updatedEService = await catalogService.updateDraftDescriptor( unsafeBrandId(req.params.eServiceId), unsafeBrandId(req.params.descriptorId), req.body, req.ctx.authData ); - return res.status(200).end(); + return res + .status(200) + .json(eServiceToApiEService(updatedEService)) + .end(); } catch (error) { const errorRes = makeApiProblem(error, updateDescriptorErrorMapper); return res.status(errorRes.status).json(errorRes).end(); diff --git a/packages/catalog-process/src/services/catalogService.ts b/packages/catalog-process/src/services/catalogService.ts index 9da0d1bf2a..1fe35592a8 100644 --- a/packages/catalog-process/src/services/catalogService.ts +++ b/packages/catalog-process/src/services/catalogService.ts @@ -307,91 +307,6 @@ export function catalogServiceBuilder( return document; }, - async updateDraftDescriptor( - eserviceId: EServiceId, - descriptorId: DescriptorId, - seed: UpdateEServiceDescriptorSeed, - authData: AuthData - ): Promise { - logger.info( - `Updating draft Descriptor ${descriptorId} for EService ${eserviceId}` - ); - const eservice = await readModelService.getEServiceById(eserviceId); - - await repository.createEvent( - updateDraftDescriptorLogic({ - eserviceId, - descriptorId, - seed, - authData, - eservice, - }) - ); - }, - - async publishDescriptor( - eserviceId: EServiceId, - descriptorId: DescriptorId, - authData: AuthData - ): Promise { - logger.info( - `Publishing Descriptor ${descriptorId} for EService ${eserviceId}` - ); - - const eservice = await readModelService.getEServiceById(eserviceId); - - await repository.createEvent( - publishDescriptorLogic({ - eserviceId, - descriptorId, - authData, - eservice, - }) - ); - }, - - async suspendDescriptor( - eserviceId: EServiceId, - descriptorId: DescriptorId, - authData: AuthData - ): Promise { - logger.info( - `Suspending Descriptor ${descriptorId} for EService ${eserviceId}` - ); - - const eservice = await readModelService.getEServiceById(eserviceId); - - await repository.createEvent( - suspendDescriptorLogic({ - eserviceId, - descriptorId, - authData, - eservice, - }) - ); - }, - - async activateDescriptor( - eserviceId: EServiceId, - descriptorId: DescriptorId, - authData: AuthData - ): Promise { - logger.info( - `Activating descriptor ${descriptorId} for EService ${eserviceId}` - ); - - const eservice = await readModelService.getEServiceById(eserviceId); - - await repository.createEvent( - activateDescriptorLogic({ - eserviceId, - descriptorId, - authData, - eservice, - }) - ); - }, - async cloneDescriptor( eserviceId: EServiceId, descriptorId: DescriptorId, @@ -971,209 +886,220 @@ export function catalogServiceBuilder( await repository.createEvent(event); }, - }; -} -export function updateDraftDescriptorLogic({ - eserviceId, - descriptorId, - seed, - authData, - eservice, -}: { - eserviceId: EServiceId; - descriptorId: DescriptorId; - seed: UpdateEServiceDescriptorSeed; - authData: AuthData; - eservice: WithMetadata | undefined; -}): CreateEvent { - assertEServiceExist(eserviceId, eservice); - assertRequesterAllowed(eservice.data.producerId, authData.organizationId); + async updateDraftDescriptor( + eserviceId: EServiceId, + descriptorId: DescriptorId, + seed: UpdateEServiceDescriptorSeed, + authData: AuthData + ): Promise { + logger.info( + `Updating draft Descriptor ${descriptorId} for EService ${eserviceId}` + ); - const descriptor = retrieveDescriptor(descriptorId, eservice); + const eservice = await retrieveEService(eserviceId, readModelService); + assertRequesterAllowed(eservice.data.producerId, authData.organizationId); - if (descriptor.state !== descriptorState.draft) { - throw notValidDescriptor(descriptorId, descriptor.state.toString()); - } + const descriptor = retrieveDescriptor(descriptorId, eservice); - if (seed.dailyCallsPerConsumer > seed.dailyCallsTotal) { - throw inconsistentDailyCalls(); - } + if (descriptor.state !== descriptorState.draft) { + throw notValidDescriptor(descriptorId, descriptor.state.toString()); + } - const updatedDescriptor: Descriptor = { - ...descriptor, - description: seed.description, - audience: seed.audience, - voucherLifespan: seed.voucherLifespan, - dailyCallsPerConsumer: seed.dailyCallsPerConsumer, - state: "Draft", - dailyCallsTotal: seed.dailyCallsTotal, - agreementApprovalPolicy: - apiAgreementApprovalPolicyToAgreementApprovalPolicy( - seed.agreementApprovalPolicy - ), - }; + if (seed.dailyCallsPerConsumer > seed.dailyCallsTotal) { + throw inconsistentDailyCalls(); + } - const updatedEService = replaceDescriptor(eservice.data, updatedDescriptor); + const updatedDescriptor: Descriptor = { + ...descriptor, + description: seed.description, + audience: seed.audience, + voucherLifespan: seed.voucherLifespan, + dailyCallsPerConsumer: seed.dailyCallsPerConsumer, + state: "Draft", + dailyCallsTotal: seed.dailyCallsTotal, + agreementApprovalPolicy: + apiAgreementApprovalPolicyToAgreementApprovalPolicy( + seed.agreementApprovalPolicy + ), + }; - return toCreateEventEServiceUpdated( - eserviceId, - eservice.metadata.version, - updatedEService - ); -} + const updatedEService = replaceDescriptor( + eservice.data, + updatedDescriptor + ); -export function publishDescriptorLogic({ - eserviceId, - descriptorId, - authData, - eservice, -}: { - eserviceId: EServiceId; - descriptorId: DescriptorId; - authData: AuthData; - eservice: WithMetadata | undefined; -}): CreateEvent { - assertEServiceExist(eserviceId, eservice); - assertRequesterAllowed(eservice.data.producerId, authData.organizationId); + const event = toCreateEventEServiceUpdated( + eserviceId, + eservice.metadata.version, + updatedEService + ); + await repository.createEvent(event); - const descriptor = retrieveDescriptor(descriptorId, eservice); - if (descriptor.state !== descriptorState.draft) { - throw notValidDescriptor(descriptor.id, descriptor.state.toString()); - } + return updatedEService; + }, - if (descriptor.interface === undefined) { - throw eServiceDescriptorWithoutInterface(descriptor.id); - } + async publishDescriptor( + eserviceId: EServiceId, + descriptorId: DescriptorId, + authData: AuthData + ): Promise { + logger.info( + `Publishing Descriptor ${descriptorId} for EService ${eserviceId}` + ); - const currentActiveDescriptor = eservice.data.descriptors.find( - (d: Descriptor) => d.state === descriptorState.published - ); + const eservice = await retrieveEService(eserviceId, readModelService); + assertRequesterAllowed(eservice.data.producerId, authData.organizationId); - const updatedDescriptor = updateDescriptorState( - descriptor, - descriptorState.published - ); + const descriptor = retrieveDescriptor(descriptorId, eservice); + if (descriptor.state !== descriptorState.draft) { + throw notValidDescriptor(descriptor.id, descriptor.state.toString()); + } - const newEservice = updateDescriptor(eservice.data, updatedDescriptor); + if (descriptor.interface === undefined) { + throw eServiceDescriptorWithoutInterface(descriptor.id); + } - if (currentActiveDescriptor !== undefined) { - const newEserviceWithDeprecation = updateDescriptor( - eservice.data, - deprecateDescriptor(eserviceId, currentActiveDescriptor) - ); + const currentActiveDescriptor = eservice.data.descriptors.find( + (d: Descriptor) => d.state === descriptorState.published + ); - return toCreateEventEServiceDescriptorPublished( - eserviceId, - eservice.metadata.version + 1, - descriptorId, - newEserviceWithDeprecation - ); - } else { - return toCreateEventEServiceDescriptorPublished( - eserviceId, - eservice.metadata.version, - descriptorId, - newEservice - ); - } -} + const updatedDescriptor = updateDescriptorState( + descriptor, + descriptorState.published + ); -export function suspendDescriptorLogic({ - eserviceId, - descriptorId, - authData, - eservice, -}: { - eserviceId: EServiceId; - descriptorId: DescriptorId; - authData: AuthData; - eservice: WithMetadata | undefined; -}): CreateEvent { - assertEServiceExist(eserviceId, eservice); - assertRequesterAllowed(eservice.data.producerId, authData.organizationId); + const newEservice = updateDescriptor(eservice.data, updatedDescriptor); - const descriptor = retrieveDescriptor(descriptorId, eservice); - if ( - descriptor.state !== descriptorState.deprecated && - descriptor.state !== descriptorState.published - ) { - throw notValidDescriptor(descriptorId, descriptor.state.toString()); - } + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type + const event = () => { + if (currentActiveDescriptor !== undefined) { + const newEserviceWithDeprecation = updateDescriptor( + eservice.data, + deprecateDescriptor(eserviceId, currentActiveDescriptor) + ); - const updatedDescriptor = updateDescriptorState( - descriptor, - descriptorState.suspended - ); + return toCreateEventEServiceDescriptorPublished( + eserviceId, + eservice.metadata.version + 1, + descriptorId, + newEserviceWithDeprecation + ); + } else { + return toCreateEventEServiceDescriptorPublished( + eserviceId, + eservice.metadata.version, + descriptorId, + newEservice + ); + } + }; - const newEservice = updateDescriptor(eservice.data, updatedDescriptor); + await repository.createEvent(event()); + }, - return toCreateEventEServiceDescriptorSuspended( - eserviceId, - eservice.metadata.version, - descriptorId, - newEservice - ); -} + async suspendDescriptor( + eserviceId: EServiceId, + descriptorId: DescriptorId, + authData: AuthData + ): Promise { + logger.info( + `Suspending Descriptor ${descriptorId} for EService ${eserviceId}` + ); -export function activateDescriptorLogic({ - eserviceId, - descriptorId, - authData, - eservice, -}: { - eserviceId: EServiceId; - descriptorId: DescriptorId; - authData: AuthData; - eservice: WithMetadata | undefined; -}): CreateEvent { - assertEServiceExist(eserviceId, eservice); - assertRequesterAllowed(eservice.data.producerId, authData.organizationId); + const eservice = await retrieveEService(eserviceId, readModelService); + assertRequesterAllowed(eservice.data.producerId, authData.organizationId); - const descriptor = retrieveDescriptor(descriptorId, eservice); - if (descriptor.state !== descriptorState.suspended) { - throw notValidDescriptor(descriptorId, descriptor.state.toString()); - } + const descriptor = retrieveDescriptor(descriptorId, eservice); + if ( + descriptor.state !== descriptorState.deprecated && + descriptor.state !== descriptorState.published + ) { + throw notValidDescriptor(descriptorId, descriptor.state.toString()); + } - const updatedDescriptor = updateDescriptorState( - descriptor, - descriptorState.published - ); - const descriptorVersions: number[] = eservice.data.descriptors - .filter( - (d: Descriptor) => - d.state === descriptorState.suspended || - d.state === descriptorState.deprecated || - d.state === descriptorState.published - ) - .map((d: Descriptor) => parseInt(d.version, 10)); - const recentDescriptorVersion = Math.max(...descriptorVersions); + const updatedDescriptor = updateDescriptorState( + descriptor, + descriptorState.suspended + ); - if ( - recentDescriptorVersion !== null && - parseInt(descriptor.version, 10) === recentDescriptorVersion - ) { - const newEservice = updateDescriptor(eservice.data, updatedDescriptor); + const newEservice = updateDescriptor(eservice.data, updatedDescriptor); - return toCreateEventEServiceDescriptorActivated( - eserviceId, - eservice.metadata.version, - descriptorId, - newEservice - ); - } else { - const newEservice = updateDescriptor( - eservice.data, - deprecateDescriptor(eserviceId, descriptor) - ); + const event = toCreateEventEServiceDescriptorSuspended( + eserviceId, + eservice.metadata.version, + descriptorId, + newEservice + ); + await repository.createEvent(event); + }, - return toCreateEventEServiceDescriptorActivated( - eserviceId, - eservice.metadata.version, - descriptorId, - newEservice - ); - } + async activateDescriptor( + eserviceId: EServiceId, + descriptorId: DescriptorId, + authData: AuthData + ): Promise { + logger.info( + `Activating descriptor ${descriptorId} for EService ${eserviceId}` + ); + + const eservice = await retrieveEService(eserviceId, readModelService); + assertRequesterAllowed(eservice.data.producerId, authData.organizationId); + + const descriptor = retrieveDescriptor(descriptorId, eservice); + if (descriptor.state !== descriptorState.suspended) { + throw notValidDescriptor(descriptorId, descriptor.state.toString()); + } + + const updatedDescriptor = updateDescriptorState( + descriptor, + descriptorState.published + ); + const descriptorVersions: number[] = eservice.data.descriptors + .filter( + (d: Descriptor) => + d.state === descriptorState.suspended || + d.state === descriptorState.deprecated || + d.state === descriptorState.published + ) + .map((d: Descriptor) => parseInt(d.version, 10)); + const recentDescriptorVersion = Math.max(...descriptorVersions); + + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type + const event = () => { + if ( + recentDescriptorVersion !== null && + parseInt(descriptor.version, 10) === recentDescriptorVersion + ) { + const newEservice = updateDescriptor( + eservice.data, + updatedDescriptor + ); + + return toCreateEventEServiceDescriptorActivated( + eserviceId, + eservice.metadata.version, + descriptorId, + newEservice + ); + } else { + const newEservice = updateDescriptor( + eservice.data, + deprecateDescriptor(eserviceId, descriptor) + ); + + return toCreateEventEServiceDescriptorActivated( + eserviceId, + eservice.metadata.version, + descriptorId, + newEservice + ); + } + }; + + await repository.createEvent(event()); + }, + }; } export async function cloneDescriptorLogic({