Skip to content

Commit

Permalink
[IOCOM-1364] feat: track a not sampled event for rc configuration upd…
Browse files Browse the repository at this point in the history
…ate (#97)
  • Loading branch information
michaeldisaro authored May 15, 2024
1 parent 8da6566 commit 34fbd30
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
15 changes: 12 additions & 3 deletions UpdateRCConfiguration/__tests__/updateRCConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ import {
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import { redisClientMock } from "../../__mocks__/redis.mock";
import { RC_CONFIGURATION_REDIS_PREFIX } from "../../GetRCConfiguration/handler";
import { TelemetryClient } from "applicationinsights";

const setWithExpirationTaskMock = jest.fn();
jest
.spyOn(redis_storage, "setWithExpirationTask")
.mockImplementation(setWithExpirationTaskMock);

const trackEventMock = jest.fn();
const telemetryClientMock = ({
trackEvent: trackEventMock
} as unknown) as TelemetryClient;

describe("isUserAllowedToUpdateConfiguration", () => {
test("should return a left if the userId is not equal to the userId of the configuration", async () => {
const r = await isUserAllowedToUpdateConfiguration(
Expand Down Expand Up @@ -99,7 +105,8 @@ describe("handleUpsert", () => {
const r = await handleUpsert({
rccModel: rccModelMock,
config: envConfig,
redisClientFactory: redisClientMock
redisClientFactory: redisClientMock,
telemetryClient: telemetryClientMock
})(aRemoteContentConfiguration)();
expect(E.isLeft(r)).toBe(true);
expect(setWithExpirationTaskMock).not.toHaveBeenCalled();
Expand All @@ -118,7 +125,8 @@ describe("handleUpsert", () => {
const r = await handleUpsert({
rccModel: rccModelMock,
config: envConfig,
redisClientFactory: redisClientMock
redisClientFactory: redisClientMock,
telemetryClient: telemetryClientMock
})(aRemoteContentConfiguration)();
expect(E.isRight(r)).toBe(true);
expect(setWithExpirationTaskMock).toHaveBeenCalledWith(
Expand All @@ -141,7 +149,8 @@ describe("handleUpsert", () => {
const r = await handleUpsert({
rccModel: rccModelMock,
config: envConfig,
redisClientFactory: redisClientMock
redisClientFactory: redisClientMock,
telemetryClient: telemetryClientMock
})(aRemoteContentConfiguration)();
expect(E.isRight(r)).toBe(true);
expect(setWithExpirationTaskMock).toHaveBeenCalledWith(
Expand Down
37 changes: 31 additions & 6 deletions UpdateRCConfiguration/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
RCConfigurationModel
} from "@pagopa/io-functions-commons/dist/src/models/rc_configuration";
import { NonEmptyString, Ulid } from "@pagopa/ts-commons/lib/strings";
import { TelemetryClient } from "applicationinsights";
import { NewRCConfigurationPublic } from "../generated/definitions/NewRCConfigurationPublic";
import { RequiredUserIdMiddleware } from "../middlewares/required_headers_middleware";
import { makeNewRCConfigurationWithConfigurationId } from "../utils/mappers";
Expand All @@ -33,6 +34,8 @@ import { RedisClientFactory } from "../utils/redis";
import { RC_CONFIGURATION_REDIS_PREFIX } from "../GetRCConfiguration/handler";
import { IConfig } from "../utils/config";

export const eventName = "remote.content.configuration.updated";

export const isUserAllowedToUpdateConfiguration = (
userId: NonEmptyString
): ((
Expand All @@ -47,12 +50,14 @@ interface IHandleUpsertParameter {
readonly rccModel: RCConfigurationModel;
readonly redisClientFactory: RedisClientFactory;
readonly config: IConfig;
readonly telemetryClient: TelemetryClient;
}

export const handleUpsert = ({
rccModel,
redisClientFactory,
config
config,
telemetryClient
}: IHandleUpsertParameter) => (
newRCConfiguration: RCConfiguration
): TE.TaskEither<IResponseErrorInternal, IResponseSuccessNoContent> =>
Expand All @@ -74,7 +79,20 @@ export const handleUpsert = ({
TE.orElseW(() => TE.of(rcConfiguration))
)
),
TE.map(ResponseSuccessNoContent)
TE.map(rcConfiguration =>
pipe(
telemetryClient.trackEvent({
name: eventName,
properties: {
configurationId: rcConfiguration.configurationId,
configurationName: rcConfiguration.name,
userId: rcConfiguration.userId
},
tagOverrides: { samplingEnabled: "false" }
}),
ResponseSuccessNoContent
)
)
);

export const handleEmptyConfiguration = (
Expand Down Expand Up @@ -113,6 +131,7 @@ interface IUpdateRCConfigurationHandlerParameter {
readonly rccModel: RCConfigurationModel;
readonly redisClientFactory: RedisClientFactory;
readonly config: IConfig;
readonly telemetryClient: TelemetryClient;
}

type UpdateRCConfigurationHandlerReturnType = (
Expand All @@ -131,7 +150,8 @@ type UpdateRCConfigurationHandler = (
export const updateRCConfigurationHandler: UpdateRCConfigurationHandler = ({
rccModel,
redisClientFactory,
config
config,
telemetryClient
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
}) => ({ newRCConfiguration, configurationId, userId }) =>
pipe(
Expand All @@ -145,14 +165,17 @@ export const updateRCConfigurationHandler: UpdateRCConfigurationHandler = ({
newRCConfiguration
)
),
TE.chainW(handleUpsert({ config, rccModel, redisClientFactory })),
TE.chainW(
handleUpsert({ config, rccModel, redisClientFactory, telemetryClient })
),
TE.toUnion
)();

interface IGetUpdateRCConfigurationHandlerParameter {
readonly rccModel: RCConfigurationModel;
readonly redisClientFactory: RedisClientFactory;
readonly config: IConfig;
readonly telemetryClient: TelemetryClient;
}

type GetUpdateRCConfigurationHandlerReturnType = express.RequestHandler;
Expand All @@ -164,12 +187,14 @@ type GetUpdateRCConfigurationHandler = (
export const getUpdateRCConfigurationExpressHandler: GetUpdateRCConfigurationHandler = ({
rccModel,
redisClientFactory,
config
config,
telemetryClient
}) => {
const handler = updateRCConfigurationHandler({
config,
rccModel,
redisClientFactory
redisClientFactory,
telemetryClient
});

const middlewaresWrap = withRequestMiddlewares(
Expand Down
6 changes: 5 additions & 1 deletion UpdateRCConfiguration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { remoteContentCosmosDbInstance } from "../utils/cosmosdb";
import { getConfigOrThrow } from "../utils/config";
import { RedisClientFactory } from "../utils/redis";
import { initTelemetryClient } from "../utils/appinsights";
import { getUpdateRCConfigurationExpressHandler } from "./handler";

const rccModel = new RCConfigurationModel(
Expand All @@ -26,13 +27,16 @@ const redisClientFactory = new RedisClientFactory(config);
const app = express();
secureExpressApp(app);

const telemetryClient = initTelemetryClient();

// Add express route
app.put(
"/api/v1/remote-contents/configurations/:configurationId",
getUpdateRCConfigurationExpressHandler({
config,
rccModel,
redisClientFactory
redisClientFactory,
telemetryClient
})
);

Expand Down

0 comments on commit 34fbd30

Please sign in to comment.