From 18c0569e1d21fa32a24cbad39315935682f1edc8 Mon Sep 17 00:00:00 2001 From: Stefano Hu <76391491+shuyec@users.noreply.github.com> Date: Wed, 4 Sep 2024 23:41:48 +0200 Subject: [PATCH] Replace if/else with switch/case --- .../src/consumerServiceV1.ts | 216 +++++++++--------- 1 file changed, 112 insertions(+), 104 deletions(-) diff --git a/packages/catalog-platformstate-writer/src/consumerServiceV1.ts b/packages/catalog-platformstate-writer/src/consumerServiceV1.ts index 1d4b1a4c8f..387cabedf6 100644 --- a/packages/catalog-platformstate-writer/src/consumerServiceV1.ts +++ b/packages/catalog-platformstate-writer/src/consumerServiceV1.ts @@ -41,92 +41,125 @@ export async function handleMessageV1( eserviceId, descriptorId: descriptor.id, }); - if (descriptor.state === descriptorState.published) { - // steps: - // capire se siamo in (draft -> published) o (suspened -> published) - // fare query su platform states e vedere se c'è - // se non c'è sono in draft, e continuo questa esecuzione - // se c'è (presumibilmente come inactive) allora era suspended e sono nel caso sotto (sospensione e riattivazione hanno stesso handler) - const existingCatalogEntry = await readCatalogEntry( - eserviceDescriptorPK, - dynamoDBClient - ); - - if ( - existingCatalogEntry && - existingCatalogEntry.version > msg.version - ) { - // Stops processing if the message is older than the catalog entry - return Promise.resolve(); - } else if ( - existingCatalogEntry && - existingCatalogEntry.version <= msg.version - ) { - // suspended->published - await updateDescriptorStateInPlatformStatesEntry( - dynamoDBClient, + switch (descriptor.state) { + case descriptorState.published: { + // steps: + // capire se siamo in (draft -> published) o (suspened -> published) + // fare query su platform states e vedere se c'è + // se non c'è sono in draft, e continuo questa esecuzione + // se c'è (presumibilmente come inactive) allora era suspended e sono nel caso sotto (sospensione e riattivazione hanno stesso handler) + const existingCatalogEntry = await readCatalogEntry( eserviceDescriptorPK, - descriptorStateToClientState(descriptor.state), - msg.version - ); - - // token-generation-states - const eserviceId_descriptorId = makeGSIPKEServiceIdDescriptorId({ - eserviceId, - descriptorId: descriptor.id, - }); - await updateDescriptorStateInTokenGenerationStatesTable( - eserviceId_descriptorId, - descriptor.state, dynamoDBClient ); - } else { - const catalogEntry: PlatformStatesCatalogEntry = { - PK: eserviceDescriptorPK, - state: descriptorStateToClientState(descriptor.state), - descriptorAudience: descriptor.audience[0], - version: msg.version, - updatedAt: new Date().toISOString(), - }; - await writeCatalogEntry(catalogEntry, dynamoDBClient); + if ( + existingCatalogEntry && + existingCatalogEntry.version > msg.version + ) { + // Stops processing if the message is older than the catalog entry + return Promise.resolve(); + } else if ( + existingCatalogEntry && + existingCatalogEntry.version <= msg.version + ) { + // suspended->published + await updateDescriptorStateInPlatformStatesEntry( + dynamoDBClient, + eserviceDescriptorPK, + descriptorStateToClientState(descriptor.state), + msg.version + ); - // token-generation-states - const eserviceId_descriptorId = makeGSIPKEServiceIdDescriptorId({ - eserviceId, - descriptorId: descriptor.id, - }); - await updateDescriptorStateInTokenGenerationStatesTable( - eserviceId_descriptorId, - descriptor.state, + // token-generation-states + const eserviceId_descriptorId = makeGSIPKEServiceIdDescriptorId({ + eserviceId, + descriptorId: descriptor.id, + }); + await updateDescriptorStateInTokenGenerationStatesTable( + eserviceId_descriptorId, + descriptor.state, + dynamoDBClient + ); + } else { + const catalogEntry: PlatformStatesCatalogEntry = { + PK: eserviceDescriptorPK, + state: descriptorStateToClientState(descriptor.state), + descriptorAudience: descriptor.audience[0], + version: msg.version, + updatedAt: new Date().toISOString(), + }; + + await writeCatalogEntry(catalogEntry, dynamoDBClient); + + // token-generation-states + const eserviceId_descriptorId = makeGSIPKEServiceIdDescriptorId({ + eserviceId, + descriptorId: descriptor.id, + }); + await updateDescriptorStateInTokenGenerationStatesTable( + eserviceId_descriptorId, + descriptor.state, + dynamoDBClient + ); + } + break; + } + case descriptorState.suspended: { + // TODO: add version check + const existingCatalogEntry = await readCatalogEntry( + eserviceDescriptorPK, dynamoDBClient ); + + if (!existingCatalogEntry) { + throw genericInternalError( + `EServiceDescriptor not found in catalog for event ${msg.type}` + ); + } else if ( + existingCatalogEntry && + existingCatalogEntry.version > msg.version + ) { + // Stops processing if the message is older than the catalog entry + return Promise.resolve(); + } else { + // platform-states + await updateDescriptorStateInPlatformStatesEntry( + dynamoDBClient, + eserviceDescriptorPK, + descriptorStateToClientState(descriptor.state), + msg.version + ); + + // token-generation-states + const eserviceId_descriptorId = makeGSIPKEServiceIdDescriptorId({ + eserviceId, + descriptorId: descriptor.id, + }); + await updateDescriptorStateInTokenGenerationStatesTable( + eserviceId_descriptorId, + descriptor.state, + dynamoDBClient + ); + } + break; } - } else if (descriptor.state === descriptorState.suspended) { - // TODO: add version check - const existingCatalogEntry = await readCatalogEntry( - eserviceDescriptorPK, - dynamoDBClient - ); + case descriptorState.archived: { + const eserviceId = unsafeBrandId(msg.data.eserviceId); + const descriptorV1 = msg.data.eserviceDescriptor; + if (!descriptorV1) { + throw genericInternalError( + `EServiceDescriptor not found in message data for event ${msg.type}` + ); + } + const descriptor = fromDescriptorV1(descriptorV1); - if (!existingCatalogEntry) { - throw genericInternalError( - `EServiceDescriptor not found in catalog for event ${msg.type}` - ); - } else if ( - existingCatalogEntry && - existingCatalogEntry.version > msg.version - ) { - // Stops processing if the message is older than the catalog entry - return Promise.resolve(); - } else { // platform-states - await updateDescriptorStateInPlatformStatesEntry( - dynamoDBClient, - eserviceDescriptorPK, - descriptorStateToClientState(descriptor.state), - msg.version - ); + const primaryKey = makePlatformStatesEServiceDescriptorPK({ + eserviceId, + descriptorId: descriptor.id, + }); + await deleteCatalogEntry(primaryKey, dynamoDBClient); // token-generation-states const eserviceId_descriptorId = makeGSIPKEServiceIdDescriptorId({ @@ -138,36 +171,11 @@ export async function handleMessageV1( descriptor.state, dynamoDBClient ); + break; } - } else if (descriptor.state === descriptorState.archived) { - const eserviceId = unsafeBrandId(msg.data.eserviceId); - const descriptorV1 = msg.data.eserviceDescriptor; - if (!descriptorV1) { - throw genericInternalError( - `EServiceDescriptor not found in message data for event ${msg.type}` - ); + default: { + return Promise.resolve(); } - const descriptor = fromDescriptorV1(descriptorV1); - - // platform-states - const primaryKey = makePlatformStatesEServiceDescriptorPK({ - eserviceId, - descriptorId: descriptor.id, - }); - await deleteCatalogEntry(primaryKey, dynamoDBClient); - - // token-generation-states - const eserviceId_descriptorId = makeGSIPKEServiceIdDescriptorId({ - eserviceId, - descriptorId: descriptor.id, - }); - await updateDescriptorStateInTokenGenerationStatesTable( - eserviceId_descriptorId, - descriptor.state, - dynamoDBClient - ); - } else { - return Promise.resolve(); } }) .with(