Skip to content

Commit 5e1d3c1

Browse files
committed
implement new durable utilities
1 parent 18948b5 commit 5e1d3c1

File tree

10 files changed

+250
-265
lines changed

10 files changed

+250
-265
lines changed

HandleNHCreateOrUpdateInstallationCallActivity/__tests__/handler.test.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// tslint:disable:no-any
22
import { NonEmptyString } from "italia-ts-commons/lib/strings";
33
import { context as contextMock } from "../../__mocks__/durable-functions";
4-
import { getCallNHCreateOrUpdateInstallationActivityHandler } from "../handler";
5-
import { ActivityInput as NHServiceActivityInput } from "../handler";
4+
import { activityBody, ActivityInput, ActivityResultSuccess } from "../handler";
65

76
import * as azure from "azure-sb";
87
import { PlatformEnum } from "../../generated/backend/Platform";
@@ -13,6 +12,9 @@ import { NotificationHubConfig } from "../../utils/notificationhubServicePartiti
1312

1413
import { envConfig } from "../../__mocks__/env-config.mock";
1514
import { Azure, NotificationHubService } from "azure-sb";
15+
import { createActivity } from "../../utils/durable/activities";
16+
17+
const activityName = "any";
1618

1719
const aFiscalCodeHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" as NonEmptyString;
1820
const aPushChannel =
@@ -49,6 +51,13 @@ const buildNHServiceMockBuilder = func =>
4951
} as undefined) as NotificationHubService;
5052
});
5153

54+
const handler = createActivity(
55+
activityName,
56+
ActivityInput, // FIXME: the editor marks it as type error, but tests compile correctly
57+
ActivityResultSuccess,
58+
activityBody
59+
);
60+
5261
describe("HandleNHCreateOrUpdateInstallationCallActivity", () => {
5362
beforeEach(() => {
5463
jest.clearAllMocks();
@@ -63,32 +72,35 @@ describe("HandleNHCreateOrUpdateInstallationCallActivity", () => {
6372
)
6473
);
6574

66-
const handler = getCallNHCreateOrUpdateInstallationActivityHandler();
67-
const input = NHServiceActivityInput.encode({
75+
const input = ActivityInput.encode({
6876
installationId: aCreateOrUpdateInstallationMessage.installationId,
6977
platform: aCreateOrUpdateInstallationMessage.platform,
7078
tags: aCreateOrUpdateInstallationMessage.tags,
7179
pushChannel: aCreateOrUpdateInstallationMessage.pushChannel,
7280
notificationHubConfig: aNHConfig
7381
});
82+
7483
expect.assertions(2);
84+
7585
await handler(contextMock as any, input);
86+
7687
expect(buildNHServiceMock).toHaveBeenCalledTimes(1);
7788
expect(buildNHServiceMock).toBeCalledWith(aNHConfig);
7889
});
7990

8091
it("should trigger a retry if CreateOrUpdateInstallation fails", async () => {
8192
buildNHServiceMockBuilder(createOrUpdateInstallation_WithError_Mock);
8293

83-
const handler = getCallNHCreateOrUpdateInstallationActivityHandler();
84-
const input = NHServiceActivityInput.encode({
94+
const input = ActivityInput.encode({
8595
installationId: aCreateOrUpdateInstallationMessage.installationId,
8696
platform: aCreateOrUpdateInstallationMessage.platform,
8797
tags: aCreateOrUpdateInstallationMessage.tags,
8898
pushChannel: aCreateOrUpdateInstallationMessage.pushChannel,
8999
notificationHubConfig: aNHConfig
90100
});
101+
91102
expect.assertions(2);
103+
92104
try {
93105
await handler(contextMock as any, input);
94106
} catch (e) {
Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
import { identity, toString } from "fp-ts/lib/function";
2-
import { fromEither } from "fp-ts/lib/TaskEither";
1+
import { toString } from "fp-ts/lib/function";
32
import * as t from "io-ts";
43

5-
import { Context } from "@azure/functions";
6-
74
import { InstallationId } from "../generated/notifications/InstallationId";
85
import { Platform } from "../generated/notifications/Platform";
96

10-
import { readableReport } from "italia-ts-commons/lib/reporters";
11-
127
import {
13-
ActivityResult,
8+
ActivityBody,
149
ActivityResultSuccess,
15-
failActivity,
16-
retryActivity,
17-
success
18-
} from "../utils/activity";
10+
retryActivity
11+
} from "../utils/durable/activities";
1912
import { createOrUpdateInstallation } from "../utils/notification";
2013
import {
2114
buildNHService,
2215
NotificationHubConfig
2316
} from "../utils/notificationhubServicePartition";
2417

25-
// Activity Name
26-
export const ActivityName = "HandleNHCreateOrUpdateInstallationCallActivity";
27-
2818
// Activity input
19+
export type ActivityInput = t.TypeOf<typeof ActivityInput>;
2920
export const ActivityInput = t.interface({
3021
installationId: InstallationId,
3122
notificationHubConfig: NotificationHubConfig,
@@ -34,36 +25,24 @@ export const ActivityInput = t.interface({
3425
tags: t.readonlyArray(t.string, "array of string")
3526
});
3627

37-
export type ActivityInput = t.TypeOf<typeof ActivityInput>;
38-
39-
/**
40-
* For each Notification Hub Message of type "Delete" calls related Notification Hub service
41-
*/
42-
export const getCallNHCreateOrUpdateInstallationActivityHandler = (
43-
logPrefix = "NHCreateOrUpdateCallServiceActivity"
44-
) => async (context: Context, input: unknown) => {
45-
const failure = failActivity(context, logPrefix);
46-
return fromEither(ActivityInput.decode(input))
47-
.mapLeft(errs =>
48-
failure("Error decoding activity input", readableReport(errs))
49-
)
50-
.chain<ActivityResultSuccess>(activityInput => {
51-
context.log.info(
52-
`${logPrefix}|INSTALLATION_ID=${activityInput.installationId}`
53-
);
54-
55-
const nhService = buildNHService(activityInput.notificationHubConfig);
56-
57-
return createOrUpdateInstallation(
58-
nhService,
59-
activityInput.installationId,
60-
activityInput.platform,
61-
activityInput.pushChannel,
62-
activityInput.tags
63-
).mapLeft(e =>
64-
retryActivity(context, `${logPrefix}|ERROR=${toString(e)}`)
65-
);
66-
})
67-
.fold<ActivityResult>(identity, success)
68-
.run();
28+
export { ActivityResultSuccess } from "../utils/durable/activities";
29+
30+
export type ActivityBodyImpl = ActivityBody<
31+
ActivityInput,
32+
ActivityResultSuccess
33+
>;
34+
35+
export const activityBody: ActivityBodyImpl = ({ input, logger }) => {
36+
logger.info(`INSTALLATION_ID=${input.installationId}`);
37+
const nhService = buildNHService(input.notificationHubConfig);
38+
return createOrUpdateInstallation(
39+
nhService,
40+
input.installationId,
41+
input.platform,
42+
input.pushChannel,
43+
input.tags
44+
).bimap(
45+
e => retryActivity(logger, `ERROR=${toString(e)}`),
46+
ActivityResultSuccess.encode
47+
);
6948
};
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
import { getCallNHCreateOrUpdateInstallationActivityHandler } from "./handler";
1+
import { createActivity } from "../utils/durable/activities";
2+
import { activityBody, ActivityInput, ActivityResultSuccess } from "./handler";
23

3-
const activityFunctionHandler = getCallNHCreateOrUpdateInstallationActivityHandler();
4+
export {
5+
ActivityBodyImpl,
6+
ActivityInput,
7+
ActivityResultSuccess
8+
} from "./handler";
9+
export const activityName = "HandleNHCreateOrUpdateInstallationCallActivity";
10+
11+
const activityFunctionHandler = createActivity(
12+
activityName,
13+
ActivityInput,
14+
ActivityResultSuccess,
15+
activityBody
16+
);
417

518
export default activityFunctionHandler;

0 commit comments

Comments
 (0)