Skip to content

Commit

Permalink
WIP: replace sb with notification hub library and make tsc compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Garma00 committed Jun 4, 2024
1 parent 6ec21be commit 1b59d1d
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 397 deletions.
21 changes: 14 additions & 7 deletions HandleNHCreateOrUpdateInstallationCallActivity/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import NotificationHubService = require("azure-sb/lib/notificationhubservice");
import { pipe } from "fp-ts/lib/function";
import * as TE from "fp-ts/lib/TaskEither";
import * as t from "io-ts";
import {
createAppleInstallation,
NotificationHubsClient
} from "@azure/notification-hubs";
import { toString } from "../utils/conversions";

import { InstallationId } from "../generated/notifications/InstallationId";
Expand Down Expand Up @@ -33,22 +36,26 @@ export type ActivityBodyImpl = ActivityBody<
>;

export const getActivityBody = (
buildNHService: (nhConfig: NotificationHubConfig) => NotificationHubService
buildNHService: (nhConfig: NotificationHubConfig) => NotificationHubsClient
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
): ActivityBodyImpl => ({ input, logger }) => {
logger.info(`INSTALLATION_ID=${input.installationId}`);
const nhService = buildNHService(input.notificationHubConfig);
return pipe(
createOrUpdateInstallation(
nhService,
input.installationId,
input.platform,
input.pushChannel,
input.tags
createAppleInstallation({
installationId: input.installationId,
pushChannel: input.pushChannel,
// FIX: this map is here as a workaround to the typescript error due to
// the readonly property
tags: input.tags.map(x => x)
})
),
TE.bimap(
e => retryActivity(logger, toString(e)),
ActivityResultSuccess.encode
installation =>
ActivityResultSuccess.encode({ kind: "SUCCESS", ...installation })
)
);
};
6 changes: 3 additions & 3 deletions HandleNHDeleteInstallationCallActivity/handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { pipe } from "fp-ts/lib/function";
import * as TE from "fp-ts/lib/TaskEither";
import { NotificationHubService } from "azure-sb";
import * as t from "io-ts";

import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import { NotificationHubsClient } from "@azure/notification-hubs";
import { toString } from "../utils/conversions";

import {
Expand Down Expand Up @@ -32,7 +32,7 @@ export { ActivityResultSuccess } from "../utils/durable/activities";
*/

export const getActivityBody = (
buildNHService: (nhConfig: NotificationHubConfig) => NotificationHubService
buildNHService: (nhConfig: NotificationHubConfig) => NotificationHubsClient
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
): ActivityBody<ActivityInput, ActivityResultSuccess> => ({
input,
Expand All @@ -45,7 +45,7 @@ export const getActivityBody = (
deleteInstallation(nhService, input.installationId),
TE.bimap(
e => failActivity(logger)(`ERROR=${toString(e)}`),
ActivityResultSuccess.encode
response => ActivityResultSuccess.encode({ kind: "SUCCESS", ...response })
)
);
};
21 changes: 18 additions & 3 deletions HandleNHNotifyMessageCallActivity/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import * as TE from "fp-ts/lib/TaskEither";
import * as t from "io-ts";

import { TelemetryClient } from "applicationinsights";
import { NotificationHubService } from "azure-sb";

import { FiscalCode } from "@pagopa/ts-commons/lib/strings";
import {
createAppleNotification,
NotificationHubsClient
} from "@azure/notification-hubs";
import { toString } from "../utils/conversions";
import {
ActivityBody,
Expand Down Expand Up @@ -39,7 +42,7 @@ export const ActivityResultSuccess = t.intersection([

export const getActivityBody = (
telemetryClient: TelemetryClient,
buildNHService: (nhConfig: NotificationHubConfig) => NotificationHubService,
buildNHService: (nhConfig: NotificationHubConfig) => NotificationHubsClient,
fiscalCodeNotificationBlacklist: ReadonlyArray<FiscalCode>
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
): ActivityBody<ActivityInput, ActivityResultSuccess> => ({
Expand All @@ -57,7 +60,19 @@ export const getActivityBody = (
? TE.of<Error, ActivityResultSuccess>(
ActivityResultSuccess.encode({ kind: "SUCCESS", skipped: true })
)
: notify(nhService, input.message.installationId, input.message.payload);
: // FIX: use the right platform
pipe(
notify(
nhService,
createAppleNotification({ body: input.message.payload })
),
TE.map(notificationMessage =>
ActivityResultSuccess.encode({
kind: "SUCCESS",
...notificationMessage
})
)
);

return pipe(
doNotify,
Expand Down
5 changes: 3 additions & 2 deletions HandleNHNotifyMessageCallActivityQueue/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as TE from "fp-ts/TaskEither";
import * as t from "io-ts";
import { FiscalCode } from "@pagopa/ts-commons/lib/strings";
import { TelemetryClient } from "applicationinsights";
import { createAppleNotification } from "@azure/notification-hubs";
import { errorsToError } from "../IsUserInActiveSubsetActivity/handler";
import {
buildNHService,
Expand Down Expand Up @@ -72,8 +73,8 @@ export const handle = (
pipe(
notify(
buildNHService(nhConfig),
message.installationId,
message.payload
// FIX: use the right platform
createAppleNotification({ body: message.payload })
),
TE.map(() => ({ kind: "SUCCESS", skipped: false }))
)
Expand Down
6 changes: 5 additions & 1 deletion Info/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export function Info(): express.RequestHandler {
healthcheck.checkAzureStorageHealth(
c.NOTIFICATIONS_STORAGE_CONNECTION_STRING
),
c => checkAzureNotificationHub(c.AZURE_NH_ENDPOINT, c.AZURE_NH_HUB_NAME),
c =>
checkAzureNotificationHub(
c.AZURE_NH_CONNECTION_STRING,
c.AZURE_NH_HUB_NAME
),
...[0, 1, 2, 3].map(i => (c: t.TypeOf<typeof IConfig>) =>
checkAzureNotificationHub(
c.AZURE_NOTIFICATION_HUB_PARTITIONS[i].endpoint,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
"version": "auto-changelog -p --config .auto-changelog.json --unreleased && git add CHANGELOG.md"
},
"dependencies": {
"@azure/notification-hubs": "^1.2.3",
"@azure/service-bus": "^7.9.4",
"@pagopa/express-azure-functions": "^2.0.0",
"@pagopa/io-functions-commons": "^28.0.0",
"@pagopa/ts-commons": "^11.0.0",
"applicationinsights": "^1.8.10",
"azure-sb": "^0.11.1",
"azure-storage": "^2.10.3",
"durable-functions": "^1.4.4",
"express": "^4.15.3",
Expand All @@ -43,7 +44,6 @@
"@azure/functions": "^1.2.2",
"@pagopa/eslint-config": "^1.3.1",
"@pagopa/openapi-codegen-ts": "^10.0.5",
"@types/azure-sb": "^0.0.38",
"@types/express": "^4.17.9",
"@types/jest": "^24.0.15",
"@types/node-fetch": "^2.5.7",
Expand Down
2 changes: 1 addition & 1 deletion utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const BaseConfig = t.intersection([

// Legacy Notification Hub configuration
t.interface({
AZURE_NH_ENDPOINT: NonEmptyString,
AZURE_NH_CONNECTION_STRING: NonEmptyString,
AZURE_NH_HUB_NAME: NonEmptyString
}),

Expand Down
19 changes: 5 additions & 14 deletions utils/healthcheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { pipe } from "fp-ts/lib/function";

import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import * as healthcheck from "@pagopa/io-functions-commons/dist/src/utils/healthcheck";
import { NHResultSuccess } from "./notification";
import { buildNHService } from "./notificationhubServicePartition";

/**
Expand All @@ -12,24 +11,16 @@ import { buildNHService } from "./notificationhubServicePartition";
* @returns either true or an array of error messages
*/
export const checkAzureNotificationHub = (
AZURE_NH_ENDPOINT: NonEmptyString,
AZURE_NH_CONNECTION_STRING: NonEmptyString,
AZURE_NH_HUB_NAME: NonEmptyString
): healthcheck.HealthCheck<"AzureNotificationHub"> =>
pipe(
TE.tryCatch(
() =>
new Promise<NHResultSuccess>((resolve, reject) =>
buildNHService({
AZURE_NH_ENDPOINT,
AZURE_NH_HUB_NAME
}).deleteInstallation(
"aFakeInstallation",
(err, _) =>
err == null
? resolve({ kind: "SUCCESS" })
: reject(err.message.replace(/\n/gim, " ")) // avoid newlines
)
),
buildNHService({
AZURE_NH_CONNECTION_STRING,
AZURE_NH_HUB_NAME
}).deleteInstallation("aFakeInstallation"),
healthcheck.toHealthProblems("AzureNotificationHub")
),
TE.map(_ => true)
Expand Down
Loading

0 comments on commit 1b59d1d

Please sign in to comment.