Skip to content

Commit

Permalink
[#IP368] Enable strictNullCheck on tsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnedMarshal committed Aug 19, 2021
1 parent 7fffcd4 commit bb341f8
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 53 deletions.
8 changes: 5 additions & 3 deletions MigrateServicePreferenceFromLegacy/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ export const createServicePreference = (
});

export const blockedsToServicesPreferences = (
blocked: {
[x: string]: readonly BlockedInboxOrChannelEnum[];
},
blocked:
| {
[x: string]: readonly BlockedInboxOrChannelEnum[];
}
| undefined,
fiscalCode: FiscalCode,
version: NonNegativeInteger
) =>
Expand Down
33 changes: 31 additions & 2 deletions StartEmailValidationProcess/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ describe("StartEmailValidationProcessHandler", () => {
it("should start the orchestrator with the right input and return an accepted response", async () => {
const profileModelMock = {
findLastVersionByModelId: jest.fn(() =>
taskEither.of(some({ ...aRetrievedProfile, isEmailValidated: false }))
taskEither.of(
some({
...aRetrievedProfile,
isEmailValidated: false,
email: "email@example.com"
})
)
)
};
mockStartNew.mockImplementationOnce(() => Promise.resolve("start"));
Expand All @@ -46,7 +52,13 @@ describe("StartEmailValidationProcessHandler", () => {
it("should not start a new orchestrator if there is an already running orchestrator and return an accepted response", async () => {
const profileModelMock = {
findLastVersionByModelId: jest.fn(() =>
taskEither.of(some({ ...aRetrievedProfile, isEmailValidated: false }))
taskEither.of(
some({
...aRetrievedProfile,
isEmailValidated: false,
email: "email@example.com"
})
)
)
};

Expand Down Expand Up @@ -82,4 +94,21 @@ describe("StartEmailValidationProcessHandler", () => {
);
expect(result.kind).toBe("IResponseErrorValidation");
});

it("should not start the orchestrator if the email is missing", async () => {
const profileModelMock = {
findLastVersionByModelId: jest.fn(() =>
taskEither.of(some({ ...aRetrievedProfile, isEmailValidated: false }))
)
};

const handler = StartEmailValidationProcessHandler(profileModelMock as any);

await handler(contextMock as any, aRetrievedProfile.fiscalCode);
const result = await handler(
contextMock as any,
aRetrievedProfile.fiscalCode
);
expect(result.kind).toBe("IResponseErrorValidation");
});
});
4 changes: 2 additions & 2 deletions StartEmailValidationProcess/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ export function StartEmailValidationProcessHandler(

const existingProfile = maybeExistingProfile.value;

if (existingProfile.isEmailValidated === true) {
if (existingProfile.isEmailValidated === true || !existingProfile.email) {
return ResponseErrorValidation(
"Validation error",
"The email is already validated"
"The email is missing or already validated"
);
}

Expand Down
5 changes: 3 additions & 2 deletions UpdateProfile/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ describe("UpdateProfileHandler", () => {
expect(result.value).toEqual(
expect.objectContaining({
service_preferences_settings: {
mode: autoApiProfileServicePreferencesSettings.mode,
mode: (autoApiProfileServicePreferencesSettings as any).mode,
version: expectedServicePreferencesSettingsVersion
}
})
Expand Down Expand Up @@ -604,7 +604,8 @@ describe("UpdateProfileHandler", () => {
isWebhookEnabled,
expectedIsInboxEnabled,
expectedIsWebHookEnabled,
acceptedTosVersion
acceptedTosVersion,
_
) => {
const profileModelMock = {
findLastVersionByModelId: jest.fn(() =>
Expand Down
2 changes: 1 addition & 1 deletion UpdateProfile/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function UpdateProfileHandler(
const profile = apiProfileToProfile(
profilePayload,
fiscalCode,
emailChanged ? false : existingProfile.isEmailValidated,
emailChanged ? false : !!existingProfile.isEmailValidated,
servicePreferencesSettingsVersion
);

Expand Down
4 changes: 2 additions & 2 deletions UpsertServicePreferences/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const getFeedOperation = (
* Return a type safe GetServicePreferences handler.
*/
export const GetUpsertServicePreferencesHandler = (
telemetryClient: ReturnType<typeof initAppInsights>,
telemetryClient: ReturnType<typeof initAppInsights> | undefined,
profileModels: ProfileModel,
serviceModels: ServiceModel,
servicePreferencesModel: ServicesPreferencesModel,
Expand Down Expand Up @@ -315,7 +315,7 @@ export const GetUpsertServicePreferencesHandler = (
* Wraps a UpsertServicePreferences handler inside an Express request handler.
*/
export function UpsertServicePreferences(
telemetryClient: ReturnType<typeof initAppInsights>,
telemetryClient: ReturnType<typeof initAppInsights> | undefined,
profileModels: ProfileModel,
serviceModels: ServiceModel,
servicePreferencesModel: ServicesPreferencesModel,
Expand Down
15 changes: 8 additions & 7 deletions UpsertedProfileOrchestrator/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { BlockedInboxOrChannelEnum } from "@pagopa/io-functions-commons/dist/generated/definitions/BlockedInboxOrChannel";
import { readableReport } from "@pagopa/ts-commons/lib/reporters";
import { consumeGenerator } from "../../utils/durable";
import { identity } from "fp-ts/lib/function";

const someRetryOptions = new df.RetryOptions(5000, 10);
// tslint:disable-next-line: no-object-mutation
Expand Down Expand Up @@ -203,7 +204,7 @@ describe("UpsertedProfileOrchestrator", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -310,7 +311,7 @@ describe("UpsertedProfileOrchestrator", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -421,7 +422,7 @@ describe("UpsertedProfileOrchestrator", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -532,7 +533,7 @@ describe("UpsertedProfileOrchestrator", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -650,7 +651,7 @@ describe("UpsertedProfileOrchestrator", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -768,7 +769,7 @@ describe("UpsertedProfileOrchestrator", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -891,7 +892,7 @@ describe("UpsertedProfileOrchestrator", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down
15 changes: 10 additions & 5 deletions UpsertedProfileOrchestrator/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ export const getUpsertedProfileOrchestratorHandler = (params: {
updatedAt
} = upsertedProfileOrchestratorInput;

const profileOperation = oldProfile !== undefined ? "UPDATED" : "CREATED";
const existsOldProfile = (
p: RetrievedProfile | undefined
): p is RetrievedProfile => p !== undefined;
const profileOperation = existsOldProfile(oldProfile)
? "UPDATED"
: "CREATED";

// Check if the profile email is changed
const isProfileEmailChanged =
profileOperation === "UPDATED" && newProfile.email !== oldProfile.email;
if (isProfileEmailChanged) {
existsOldProfile(oldProfile) && newProfile.email !== oldProfile.email;
if (isProfileEmailChanged && newProfile.email) {
try {
const { fiscalCode, email } = newProfile;

Expand Down Expand Up @@ -151,7 +156,7 @@ export const getUpsertedProfileOrchestratorHandler = (params: {
// Send welcome messages to the user
const isInboxEnabled = newProfile.isInboxEnabled === true;
const hasOldProfileWithInboxDisabled =
profileOperation === "UPDATED" && oldProfile.isInboxEnabled === false;
existsOldProfile(oldProfile) && oldProfile.isInboxEnabled === false;

const hasJustEnabledInbox =
isInboxEnabled &&
Expand Down Expand Up @@ -191,7 +196,7 @@ export const getUpsertedProfileOrchestratorHandler = (params: {
}

// Update subscriptions feed
if (profileOperation === "CREATED") {
if (!existsOldProfile(oldProfile)) {
// When a profile get created we add an entry to the profile subscriptions
context.log.verbose(
`${logPrefix}|Calling UpdateSubscriptionsFeedActivity|OPERATION=SUBSCRIBED`
Expand Down
3 changes: 2 additions & 1 deletion UpsertedProfileOrchestrator/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as df from "durable-functions";
import { identity } from "fp-ts/lib/function";
import * as NonEmptyArray from "fp-ts/lib/NonEmptyArray";
import { getConfigOrThrow } from "../utils/config";

Expand All @@ -11,7 +12,7 @@ const orchestrator = df.orchestrator(
notifyOn: config.FF_NEW_USERS_EUCOVIDCERT_ENABLED
? NonEmptyArray.fromArray([
config.EUCOVIDCERT_PROFILE_CREATED_QUEUE_NAME
]).getOrElse(undefined)
]).fold(undefined, identity)
: undefined,
sendCashbackMessage: config.IS_CASHBACK_ENABLED
})
Expand Down
23 changes: 12 additions & 11 deletions UpsertedProfileOrchestratorV2/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { BlockedInboxOrChannelEnum } from "@pagopa/io-functions-commons/dist/generated/definitions/BlockedInboxOrChannel";
import { readableReport } from "@pagopa/ts-commons/lib/reporters";
import { consumeGenerator } from "../../utils/durable";
import { identity } from "fp-ts/lib/function";

const someRetryOptions = new df.RetryOptions(5000, 10);
// tslint:disable-next-line: no-object-mutation
Expand Down Expand Up @@ -203,7 +204,7 @@ describe("UpsertedProfileOrchestratorV2", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -310,7 +311,7 @@ describe("UpsertedProfileOrchestratorV2", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -421,7 +422,7 @@ describe("UpsertedProfileOrchestratorV2", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -532,7 +533,7 @@ describe("UpsertedProfileOrchestratorV2", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -656,7 +657,7 @@ describe("UpsertedProfileOrchestratorV2", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -711,8 +712,8 @@ describe("UpsertedProfileOrchestratorV2", () => {
{
fiscalCode: aFiscalCode,
settingsVersion:
upsertedProfileOrchestratorInput.oldProfile.servicePreferencesSettings
.version
upsertedProfileOrchestratorInput.oldProfile
?.servicePreferencesSettings.version
}
);

Expand Down Expand Up @@ -802,7 +803,7 @@ describe("UpsertedProfileOrchestratorV2", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down Expand Up @@ -855,8 +856,8 @@ describe("UpsertedProfileOrchestratorV2", () => {
{
fiscalCode: aFiscalCode,
settingsVersion:
upsertedProfileOrchestratorInput.oldProfile.servicePreferencesSettings
.version
upsertedProfileOrchestratorInput.oldProfile
?.servicePreferencesSettings.version
}
);

Expand Down Expand Up @@ -945,7 +946,7 @@ describe("UpsertedProfileOrchestratorV2", () => {
};

const orchestratorHandler = getUpsertedProfileOrchestratorHandler({
notifyOn: fromArray([expectedQueueName]).getOrElseL(undefined),
notifyOn: fromArray([expectedQueueName]).fold(undefined, identity),
sendCashbackMessage: true
})(contextMockWithDf as any);

Expand Down
17 changes: 11 additions & 6 deletions UpsertedProfileOrchestratorV2/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,17 @@ export const getUpsertedProfileOrchestratorHandler = (params: {
updatedAt
} = upsertedProfileOrchestratorInput;

const profileOperation = oldProfile !== undefined ? "UPDATED" : "CREATED";
const existsOldProfile = (
p: RetrievedProfile | undefined
): p is RetrievedProfile => p !== undefined;
const profileOperation = existsOldProfile(oldProfile)
? "UPDATED"
: "CREATED";

// Check if the profile email is changed
const isProfileEmailChanged =
profileOperation === "UPDATED" && newProfile.email !== oldProfile.email;
if (isProfileEmailChanged) {
existsOldProfile(oldProfile) && newProfile.email !== oldProfile.email;
if (isProfileEmailChanged && newProfile.email) {
try {
const { fiscalCode, email } = newProfile;

Expand Down Expand Up @@ -150,11 +155,11 @@ export const getUpsertedProfileOrchestratorHandler = (params: {
// Send welcome messages to the user
const isInboxEnabled = newProfile.isInboxEnabled === true;
const hasOldProfileWithInboxDisabled =
profileOperation === "UPDATED" && oldProfile.isInboxEnabled === false;
existsOldProfile(oldProfile) && oldProfile.isInboxEnabled === false;

const hasJustEnabledInbox =
isInboxEnabled &&
(profileOperation === "CREATED" || hasOldProfileWithInboxDisabled);
(!existsOldProfile(oldProfile) || hasOldProfileWithInboxDisabled);

context.log.verbose(
`${logPrefix}|OPERATION=${profileOperation}|INBOX_ENABLED=${isInboxEnabled}|INBOX_JUST_ENABLED=${hasJustEnabledInbox}`
Expand Down Expand Up @@ -190,7 +195,7 @@ export const getUpsertedProfileOrchestratorHandler = (params: {
}

// Update subscriptions feed
if (profileOperation === "CREATED") {
if (!existsOldProfile(oldProfile)) {
// When a profile get created we add an entry to the profile subscriptions
context.log.verbose(
`${logPrefix}|Calling UpdateSubscriptionsFeedActivity|OPERATION=SUBSCRIBED`
Expand Down
Loading

0 comments on commit bb341f8

Please sign in to comment.