Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IOCOM-1366] fix: restricted rc_configuration model to be non versioned #378

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions __integrations__/models/rc_configurations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const aRemoteContentEnvironmentConfiguration = {

const aRemoteContentConfigurationWithNoEnv: RCConfigurationBase = {
userId: "aUserId" as NonEmptyString,
id: "01HMRBX079WA5SGYBQP1A7FSKH" as NonEmptyString,
configurationId: "01HMRBX079WA5SGYBQP1A7FSKH" as Ulid,
name: "aName" as NonEmptyString,
description: "a simple description" as NonEmptyString,
Expand All @@ -43,7 +44,7 @@ describe("findAllLastVersionByConfigurationId", () => {
await context.init();
const model = new RCConfigurationModel(context.container);

const r = await model.findAllLastVersionByConfigurationId([])();
const r = await model.findAllByConfigurationId([])();

expect(E.isRight(r)).toBe(true);
if (E.isRight(r)) expect(r.right).toHaveLength(0);
Expand All @@ -62,22 +63,14 @@ describe("findAllLastVersionByConfigurationId", () => {
configurationId: "01HMRBX079WA5SGYBQP1A7FSKK" as Ulid
})();

const r = await model.findAllLastVersionByConfigurationId([
const r = await model.findAllByConfigurationId([
aRemoteContentConfigurationWithTestEnv.configurationId,
"01HMRBX079WA5SGYBQP1A7FSKK" as Ulid
])();

expect(E.isRight(r)).toBe(true);
if (E.isRight(r)) {
expect(r.right).toHaveLength(2);
expect(
r.right.find(i => i.configurationId === "01HMRBX079WA5SGYBQP1A7FSKH")
?.version
).toBe(1);
expect(
r.right.find(i => i.configurationId === "01HMRBX079WA5SGYBQP1A7FSKK")
?.version
).toBe(0);
}
});
});
9 changes: 2 additions & 7 deletions src/models/__tests__/rc_configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
RCConfigurationBase,
RetrievedRCConfiguration
} from "../rc_configuration";
import { NonNegativeInteger } from "@pagopa/ts-commons/lib/numbers";
import { HasPreconditionEnum } from "../../../generated/definitions/HasPrecondition";

const aRemoteContentEnvironmentConfiguration = {
Expand All @@ -19,6 +18,7 @@ const aRemoteContentEnvironmentConfiguration = {

const aRemoteContentConfigurationWithNoEnv: RCConfigurationBase = {
userId: "aUserId" as NonEmptyString,
id: "01HMRBX079WA5SGYBQP1A7FSKH" as NonEmptyString,
configurationId: "01HMRBX079WA5SGYBQP1A7FSKH" as Ulid,
name: "aName" as NonEmptyString,
description: "a simple description" as NonEmptyString,
Expand All @@ -34,10 +34,7 @@ const aRemoteContentConfigurationWithProdEnv: RCConfiguration = {

const aRetrievedRemoteContentConfigurationWithProdEnv: RetrievedRCConfiguration = {
...aRemoteContentConfigurationWithProdEnv,
id: `${aRemoteContentConfigurationWithProdEnv.configurationId}-${"0".repeat(
16
)}` as NonEmptyString,
version: 0 as NonNegativeInteger,
id: `${aRemoteContentConfigurationWithProdEnv.configurationId}` as NonEmptyString,
_etag: "_etag",
_rid: "_rid",
_self: "_self",
Expand All @@ -57,7 +54,6 @@ const aRetrievedRemoteContentConfigurationWithTestEnv: RetrievedRCConfiguration
id: `${aRemoteContentConfigurationWithTestEnv.configurationId}-${"0".repeat(
16
)}` as NonEmptyString,
version: 0 as NonNegativeInteger,
_etag: "_etag",
_rid: "_rid",
_self: "_self",
Expand All @@ -78,7 +74,6 @@ const aRetrievedRemoteContentConfigurationWithBothEnv: RetrievedRCConfiguration
id: `${aRemoteContentConfigurationWithProdEnv.configurationId}-${"0".repeat(
16
)}` as NonEmptyString,
version: 0 as NonNegativeInteger,
_etag: "_etag",
_rid: "_rid",
_self: "_self",
Expand Down
57 changes: 24 additions & 33 deletions src/models/rc_configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as t from "io-ts";
import * as TE from "fp-ts/lib/TaskEither";
import * as RA from "fp-ts/lib/ReadonlyArray";
import * as O from "fp-ts/lib/Option";

import {
FiscalCode,
Expand All @@ -10,12 +11,13 @@ import {
import { enumType } from "@pagopa/ts-commons/lib/types";
import { Container } from "@azure/cosmos";
import { pipe } from "fp-ts/lib/function";
import {
CosmosdbModelVersioned,
RetrievedVersionedModel
} from "../utils/cosmosdb_model_versioned";
import { HasPreconditionEnum } from "../../generated/definitions/HasPrecondition";
import { CosmosErrors, toCosmosErrorResponse } from "../utils/cosmosdb_model";
import {
AzureCosmosResource,
CosmosErrors,
CosmosdbModel,
toCosmosErrorResponse
} from "../utils/cosmosdb_model";
import { asyncIterableToArray } from "../utils/async";

export const RC_CONFIGURATION_COLLECTION_NAME = "message-configuration";
Expand Down Expand Up @@ -63,6 +65,7 @@ const RCConfigurationR = t.interface({
HasPreconditionEnum,
"hasPrecondition"
),
id: NonEmptyString,
isLollipopEnabled: t.boolean,
name: NonEmptyString,
userId: NonEmptyString
Expand All @@ -85,33 +88,39 @@ export const RCConfiguration = t.intersection([

export const RetrievedRCConfiguration = t.intersection([
RCConfiguration,
RetrievedVersionedModel
AzureCosmosResource
]);
export type RetrievedRCConfiguration = t.TypeOf<
typeof RetrievedRCConfiguration
>;

export class RCConfigurationModel extends CosmosdbModelVersioned<
export class RCConfigurationModel extends CosmosdbModel<
RCConfiguration,
RCConfiguration,
RetrievedRCConfiguration,
typeof RC_CONFIGURATION_MODEL_PK_FIELD
> {
constructor(container: Container) {
super(
container,
RCConfiguration,
RetrievedRCConfiguration,
RC_CONFIGURATION_MODEL_PK_FIELD
);
super(container, RCConfiguration, RetrievedRCConfiguration);
}

/**
* Returns a RCConfiguration identified by configurationId
*
* @param configurationId a configurationId
*/
public findByConfigurationId(
configurationId: Ulid
): TE.TaskEither<CosmosErrors, O.Option<RetrievedRCConfiguration>> {
return this.find([configurationId, configurationId]);
}

/**
* Returns all the RCConfiguration identified by every configurationId
*
* @param configurationIds an array of configurationId
*/
public findAllLastVersionByConfigurationId(
public findAllByConfigurationId(
configurationIds: ReadonlyArray<Ulid>
): TE.TaskEither<CosmosErrors, ReadonlyArray<RetrievedRCConfiguration>> {
if (configurationIds.length === 0) {
Expand All @@ -132,25 +141,7 @@ export class RCConfigurationModel extends CosmosdbModelVersioned<
toCosmosErrorResponse
),
TE.map(RA.flatten),
TE.map(RA.rights),
TE.map(retrievedRCConfigurations =>
this.filterLastVersion(retrievedRCConfigurations, configurationIds)
)
);
}

/**
* Filter the last version of every configuration identified by every configurationId
* */
private filterLastVersion(
retrievedRCConfiguration: ReadonlyArray<RetrievedRCConfiguration>,
configurationIds: ReadonlyArray<Ulid>
): ReadonlyArray<RetrievedRCConfiguration> {
return configurationIds.map(
configurationId =>
retrievedRCConfiguration
.filter(rc => rc.configurationId === configurationId)
.sort((a, b) => b.version - a.version)[0]
TE.map(RA.rights)
);
}
}
167 changes: 0 additions & 167 deletions src/models/rc_configuration_non_versioned_temp.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/rc_configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
RCAuthenticationConfig as RCAuthenticationConfigModel,
RCClientCert as RCClientCertModel,
RCEnvironmentConfig
} from "../models/rc_configuration_non_versioned_temp";
} from "../models/rc_configuration";

const getCert = (cert: RCClientCertModel): RCClientCert => ({
client_cert: cert.clientCert,
Expand Down
Loading