Skip to content

Commit

Permalink
Replace if/else with switch/case
Browse files Browse the repository at this point in the history
  • Loading branch information
shuyec committed Sep 4, 2024
1 parent 1f10e9a commit 18c0569
Showing 1 changed file with 112 additions and 104 deletions.
216 changes: 112 additions & 104 deletions packages/catalog-platformstate-writer/src/consumerServiceV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EServiceId>(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({
Expand All @@ -138,36 +171,11 @@ export async function handleMessageV1(
descriptor.state,
dynamoDBClient
);
break;
}
} else if (descriptor.state === descriptorState.archived) {
const eserviceId = unsafeBrandId<EServiceId>(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(
Expand Down

0 comments on commit 18c0569

Please sign in to comment.