Skip to content

Commit

Permalink
[IOCIT-195] Use messageStatusUpdarter to set the ttl (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garma00 authored Nov 23, 2022
1 parent 83ff579 commit aa70806
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 56 deletions.
13 changes: 10 additions & 3 deletions ProcessMessage/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { ServiceId } from "@pagopa/io-functions-commons/dist/generated/definitio
import { RejectedMessageStatusValueEnum } from "@pagopa/io-functions-commons/dist/generated/definitions/RejectedMessageStatusValue";
import { RejectionReasonEnum } from "@pagopa/io-functions-commons/dist/generated/definitions/RejectionReason";
import { CosmosErrors } from "@pagopa/io-functions-commons/dist/src/utils/cosmosdb_model";
import { Ttl } from "@pagopa/io-functions-commons/dist/src/utils/cosmosdb_model_ttl";

const TTL_FOR_USER_NOT_FOUND = 94670856 as NonNegativeInteger;
const isUserEligibleForNewFeature = (_: FiscalCode) => true;
Expand Down Expand Up @@ -824,11 +825,17 @@ describe("getprocessMessageHandler", () => {
const messageStatusUpdaterMock = getMessageStatusUpdaterMock.mock.results[0]
.value as jest.Mock;
expect(messageStatusUpdaterMock).toHaveBeenCalledTimes(1);
expect(messageStatusUpdaterMock).toHaveBeenCalledWith({
rejection_reason: "USER_NOT_FOUND",
status: "REJECTED",
ttl: 94670856
});
const messageStatusUpdaterParam = messageStatusUpdaterMock.mock.calls[0][0];

expect(messageStatusUpdaterParam).toEqual({
status: RejectedMessageStatusValueEnum.REJECTED,
rejection_reason: RejectionReasonEnum.USER_NOT_FOUND
rejection_reason: RejectionReasonEnum.USER_NOT_FOUND,
ttl: 94670856
});

// check if models are being used only when expected
Expand Down Expand Up @@ -875,7 +882,7 @@ describe("getprocessMessageHandler", () => {

await expect(
processMessageHandler(context, JSON.stringify(aCreatedMessageEvent))
).rejects.toThrow("Error while setting ttl");
).rejects.toThrow("Error while setting the ttl");
});

it("it should throw an error if the set of ttl on message status fails", async () => {
Expand Down Expand Up @@ -908,6 +915,6 @@ describe("getprocessMessageHandler", () => {

await expect(
processMessageHandler(context, JSON.stringify(aCreatedMessageEvent))
).rejects.toThrow("Error while setting ttl");
).rejects.toThrow("Error while setting the ttl");
});
});
118 changes: 70 additions & 48 deletions ProcessMessage/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable max-lines-per-function */

import { Context } from "@azure/functions";
import * as t from "io-ts";
import { BlockedInboxOrChannelEnum } from "@pagopa/io-functions-commons/dist/generated/definitions/BlockedInboxOrChannel";
import { EUCovidCert } from "@pagopa/io-functions-commons/dist/generated/definitions/EUCovidCert";
import { FiscalCode } from "@pagopa/io-functions-commons/dist/generated/definitions/FiscalCode";
Expand Down Expand Up @@ -41,6 +40,7 @@ import * as T from "fp-ts/lib/Task";
import * as TE from "fp-ts/lib/TaskEither";
import { TaskEither } from "fp-ts/lib/TaskEither";
import { RejectionReasonEnum } from "@pagopa/io-functions-commons/dist/generated/definitions/RejectionReason";
import { Ttl } from "@pagopa/io-functions-commons/dist/src/utils/cosmosdb_model_ttl";
import { SpecialServiceCategoryEnum } from "../generated/api-admin/SpecialServiceCategory";
import { LegalData } from "../generated/definitions/LegalData";
import { PaymentData } from "../generated/definitions/PaymentData";
Expand Down Expand Up @@ -351,12 +351,8 @@ const createMessageOrThrow = async (
}
};

// ttl can only be a positive integer or -1
const ttlType = t.union([NonNegativeInteger, t.literal(-1)]);
type TtlType = t.TypeOf<typeof ttlType>;

export interface IProcessMessageHandlerInput {
readonly TTL_FOR_USER_NOT_FOUND: TtlType;
readonly TTL_FOR_USER_NOT_FOUND: Ttl;
readonly isUserEligibleForNewFeature: (fc: FiscalCode) => boolean;
readonly lActivation: ActivationModel;
readonly lProfileModel: ProfileModel;
Expand Down Expand Up @@ -429,61 +425,63 @@ export const getProcessMessageHandler = ({

if (O.isNone(maybeProfile)) {
// the recipient doesn't have any profile yet
await pipe(
messageStatusUpdater({
rejection_reason: RejectionReasonEnum.USER_NOT_FOUND,
status: RejectedMessageStatusValueEnum.REJECTED
}),
TE.getOrElse(e => {
context.log.error(
`${logPrefix}|PROFILE_NOT_FOUND|UPSERT_STATUS=REJECTED|ERROR=${JSON.stringify(
e
)}`
);
throw new Error(
"Error while updating message status to REJECTED|PROFILE_NOT_FOUND"
);
})
)();

// if the user is enabled for feature flag we want to execute the new code
if (
isUserEligibleForNewFeature(newMessageWithoutContent.fiscalCode)
) {
await pipe(
lMessageStatusModel.updateTTLForAllVersions(
[newMessageWithoutContent.id],
TTL_FOR_USER_NOT_FOUND
),
TE.map(updatedCount => {
if (updatedCount === 0) {
telemetryClient.trackEvent({
name: "api.messages.create.update-status-ttl-count-zero",
properties: {
errorKind:
"updateTTLForAllVersions updated zero documents",
fiscalCode: toHash(newMessageWithoutContent.fiscalCode),
messageId: newMessageWithoutContent.id,
senderId: newMessageWithoutContent.senderServiceId
},
tagOverrides: { samplingEnabled: "false" }
});
}
return updatedCount;
messageStatusUpdater({
rejection_reason: RejectionReasonEnum.USER_NOT_FOUND,
status: RejectedMessageStatusValueEnum.REJECTED,
ttl: TTL_FOR_USER_NOT_FOUND
}),
TE.mapLeft((error: CosmosErrors) => {
TE.mapLeft((err: CosmosErrors) => {
telemetryClient.trackEvent({
name: "api.messages.create.fail-status-ttl-set",
name: "api.messages.create.create-status-fail",
properties: {
errorAsJson: JSON.stringify(error),
errorKind: error.kind,
errorAsJson: JSON.stringify(err),
errorKind: "messageStatusUpdater failed",
fiscalCode: toHash(newMessageWithoutContent.fiscalCode),
messageId: newMessageWithoutContent.id,
senderId: newMessageWithoutContent.senderServiceId
},
tagOverrides: { samplingEnabled: "false" }
});
return error;
context.log.error(
`${logPrefix}|PROFILE_NOT_FOUND|UPSERT_STATUS=REJECTED|ERROR=${JSON.stringify(
err
)}`
);
throw new Error(
"Error while updating message status to REJECTED|PROFILE_NOT_FOUND"
);
}),
TE.chain(
flow(
() =>
lMessageStatusModel.updateTTLForAllVersions(
[newMessageWithoutContent.id],
TTL_FOR_USER_NOT_FOUND
),
TE.mapLeft((error: CosmosErrors) => {
telemetryClient.trackEvent({
name: "api.messages.create.fail-status-ttl-set",
properties: {
errorAsJson: JSON.stringify(error),
errorKind: error.kind,
fiscalCode: toHash(
newMessageWithoutContent.fiscalCode
),
messageId: newMessageWithoutContent.id,
senderId: newMessageWithoutContent.senderServiceId
},
tagOverrides: { samplingEnabled: "false" }
});
return error;
})
)
),
TE.chain(() =>
pipe(
lMessageModel.patch(
Expand Down Expand Up @@ -513,8 +511,32 @@ export const getProcessMessageHandler = ({
})
)
),
TE.mapLeft(_ => {
throw new Error("Error while setting ttl");
TE.getOrElse(e => {
context.log.error(
`${logPrefix}|PROFILE_NOT_FOUND|UPSERT_STATUS=REJECTED|ERROR=${JSON.stringify(
e
)}`
);
throw new Error("Error while setting the ttl");
})
)();
// if the user is not enabled for feature flag we just execute the messageStatusUpdater without the ttl
} else {
await pipe(
messageStatusUpdater({
rejection_reason: RejectionReasonEnum.USER_NOT_FOUND,
status: RejectedMessageStatusValueEnum.REJECTED
}),
// eslint-disable-next-line
TE.getOrElse(e => {
context.log.error(
`${logPrefix}|PROFILE_NOT_FOUND|UPSERT_STATUS=REJECTED|ERROR=${JSON.stringify(
e
)}`
);
throw new Error(
"Error while updating message status to REJECTED|PROFILE_NOT_FOUND"
);
})
)();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"dependencies": {
"@azure/cosmos": "^3.17.1",
"@pagopa/express-azure-functions": "^2.0.0",
"@pagopa/io-functions-commons": "^26.1.1",
"@pagopa/io-functions-commons": "^26.2.1",
"@pagopa/ts-commons": "^10.10.0",
"applicationinsights": "^1.7.4",
"azure-storage": "^2.10.4",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,10 @@
resolved "https://registry.yarnpkg.com/@pagopa/express-azure-functions/-/express-azure-functions-2.0.0.tgz#eb52a0b997d931c1509372e2a9bea22a8ca85c17"
integrity sha512-IFZqtk0e2sfkMZIxYqPORzxcKRkbIrVJesR6eMLNwzh1rA4bl2uh9ZHk1m55LNq4ZmaxREDu+1JcGlIaZQgKNQ==

"@pagopa/io-functions-commons@^26.1.1":
version "26.1.1"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-commons/-/io-functions-commons-26.1.1.tgz#3c29868dcb146eeb8f5ba0bee6d8f6b83dc968e5"
integrity sha512-QWR9NP+fF4t0sSsQwUXqD4H4areW6Y65tuJKvC4J7AjmmgpF4p67oP/YorbOa2AR9dP9ct4PV4viXTaQCwBKfw==
"@pagopa/io-functions-commons@^26.2.1":
version "26.2.1"
resolved "https://registry.yarnpkg.com/@pagopa/io-functions-commons/-/io-functions-commons-26.2.1.tgz#82975563db316b12aa539a163dff7a14d8773f3e"
integrity sha512-k3K8Fur7gMOG1+d9ckVOjem8ARRSV8FXII2uWH96SN0gcDJuiQYhwlAUYBJhlPDm/s0TlC9r2RdZRXaiUaZFTQ==
dependencies:
"@azure/cosmos" "^3.17.1"
"@pagopa/ts-commons" "^10.9.0"
Expand Down

0 comments on commit aa70806

Please sign in to comment.