Skip to content

Commit

Permalink
[#IP-342] small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gquadrati committed Sep 3, 2021
1 parent 9c42baa commit d9b54db
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
3 changes: 1 addition & 2 deletions HandleNHNotifyMessageCallActivity/handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { pipe } from "fp-ts/lib/function";
import * as TE from "fp-ts/lib/TaskEither";
import { taskEither } from "fp-ts/lib/TaskEither";
import * as t from "io-ts";

import { TelemetryClient } from "applicationinsights";
Expand Down Expand Up @@ -55,7 +54,7 @@ export const getActivityBody = (
const doNotify = fiscalCodeNotificationBlacklist
.map(toSHA256)
.includes(input.message.installationId) // by convention, installationId equals sha256 of user's fiscal code
? taskEither.of<Error, ActivityResultSuccess>(
? TE.of<Error, ActivityResultSuccess>(
ActivityResultSuccess.encode({ kind: "SUCCESS", skipped: true })
)
: notify(nhService, input.message.installationId, input.message.payload);
Expand Down
19 changes: 10 additions & 9 deletions IsUserInActiveSubsetActivity/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { taskEither } from "fp-ts/lib/TaskEither";
import * as TE from "fp-ts/lib/TaskEither";
import { pipe } from "fp-ts/lib/function";
import * as t from "io-ts";

import { errorsToReadableMessages } from "@pagopa/ts-commons/lib/reporters";
Expand Down Expand Up @@ -52,13 +53,13 @@ export const getActivityBody = (param: {
`ENABLED_FF=${param.enabledFeatureFlag}|INSTALLATION_ID=${input.installationId}`
);

return taskEither.of(
successWithValue(
param.isInActiveSubset(
param.enabledFeatureFlag,
input.installationId,
context.bindings.betaTestUser
)
)
return pipe(
param.isInActiveSubset(
param.enabledFeatureFlag,
input.installationId,
context.bindings.betaTestUser
),
successWithValue,
TE.of
);
};
8 changes: 8 additions & 0 deletions utils/__tests__/healthcheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ describe("healthcheck - storage account", () => {
TE.mapLeft(err => {
expect(err[0]).toBe(`AzureStorage|error - ${name}`);
done();
}),
TE.map(_ => {
expect(true).toBeFalsy();
done();
})
)();
}
Expand Down Expand Up @@ -143,6 +147,10 @@ describe("healthcheck - notification hub", () => {
TE.mapLeft(_ => {
expect(true).toBe(true);
done();
}),
TE.map(_ => {
expect(true).toBeFalsy();
done();
})
)();
});
Expand Down
2 changes: 1 addition & 1 deletion utils/durable/orchestrators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const createOrchestrator = <I, TNext = TNextDefault>(
const failure = pipe(
error,
OrchestratorFailure.decode,
E.getOrElseW(() => failureUnhandled(error))
E.getOrElse(() => failureUnhandled(error) as OrchestratorFailure)
);
logger.error(failure);

Expand Down
29 changes: 14 additions & 15 deletions utils/healthcheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import {
createQueueService,
createTableService
} from "azure-storage";

import { sequenceT } from "fp-ts/lib/Apply";
import { array } from "fp-ts/lib/Array";
import { toError } from "fp-ts/lib/Either";
import {
fromEither,
taskEither,
TaskEither,
tryCatch
} from "fp-ts/lib/TaskEither";
import { readableReport } from "@pagopa/ts-commons/lib/reporters";
import * as A from "fp-ts/lib/Array";
import * as TE from "fp-ts/lib/TaskEither";
import { TaskEither } from "fp-ts/lib/TaskEither";
import { toError } from "fp-ts/lib/Either";
import { pipe } from "fp-ts/lib/function";

import { readableReport } from "@pagopa/ts-commons/lib/reporters";

import { getConfig, IConfig } from "./config";
import { NHResultSuccess } from "./notification";
import { buildNHService } from "./notificationhubServicePartition";
Expand Down Expand Up @@ -51,7 +49,8 @@ const toHealthProblems = <S extends ProblemSource>(source: S) => (
*/
export const checkConfigHealth = (): HealthCheck<"Config", IConfig> =>
pipe(
fromEither(getConfig()),
getConfig(),
TE.fromEither,
TE.mapLeft(errors =>
errors.map(e =>
// give each problem its own line
Expand All @@ -77,7 +76,7 @@ export const checkAzureNotificationHub = ({
AZURE_NH_HUB_NAME: p.name
}))
].map(connString =>
tryCatch(
TE.tryCatch(
() =>
new Promise<NHResultSuccess>((resolve, reject) =>
buildNHService({
Expand All @@ -94,7 +93,7 @@ export const checkAzureNotificationHub = ({
toHealthProblems("AzureNotificationHub")
)
),
array.sequence(taskEither),
A.sequence(TE.ApplicativeSeq),
TE.map(_ => true)
);

Expand All @@ -118,7 +117,7 @@ export const checkAzureStorageHealth = (
]
// for each, create a task that wraps getServiceProperties
.map(createService =>
tryCatch(
TE.tryCatch(
() =>
new Promise<
azurestorageCommon.models.ServicePropertiesResult.ServiceProperties
Expand All @@ -133,7 +132,7 @@ export const checkAzureStorageHealth = (
toHealthProblems("AzureStorage")
)
),
array.sequence(taskEither),
A.sequence(TE.ApplicativeSeq),
TE.map(_ => true)
);

Expand All @@ -150,7 +149,7 @@ export const checkApplicationHealth = (): HealthCheck<ProblemSource, true> =>
TE.chain(config =>
// TODO: once we upgrade to fp-ts >= 1.19 we can use Validation to collect all errors, not just the first to happen
pipe(
sequenceT(taskEither)<
sequenceT(TE.ApplicativeSeq)<
ReadonlyArray<HealthProblem<ProblemSource>>,
// eslint-disable-next-line functional/prefer-readonly-type
Array<TaskEither<ReadonlyArray<HealthProblem<ProblemSource>>, true>>
Expand Down

0 comments on commit d9b54db

Please sign in to comment.