-
Notifications
You must be signed in to change notification settings - Fork 0
feats/activation sync to legacy #1359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leopoldo94
wants to merge
5
commits into
master
Choose a base branch
from
feats/activation-sync-to-legacy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
443a50c
add env that fixes the cosmos db emulator problem inside a docker net…
leopoldo94 d030144
implement the activations sync to legacy function
leopoldo94 e125aa1
add unit tests
leopoldo94 d561b5d
add activation sync to legacy function to infra
leopoldo94 0243bdf
changeset
leopoldo94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"io-services-cms-webapp": minor | ||
--- | ||
|
||
implementation of activations sync to legacy function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
apps/io-services-cms-webapp/ActivationSyncToLegacy/function.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"name": "activation", | ||
"type": "blobTrigger", | ||
"direction": "in", | ||
"path": "%ACTIVATIONS_CONTAINER_NAME%/{name}", | ||
"connection": "INTERNAL_STORAGE_CONNECTION_STRING" | ||
} | ||
], | ||
"scriptFile": "../dist/main.js", | ||
"entryPoint": "activationsSyncToLegacyEntryPoint" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
apps/io-services-cms-webapp/src/watchers/__tests__/on-activation-change.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import * as E from "fp-ts/lib/Either"; | ||
import * as TE from "fp-ts/lib/TaskEither"; | ||
import { makeHandler } from "../on-activations-change"; | ||
import { CosmosErrors } from "@pagopa/io-functions-commons/dist/src/utils/cosmosdb_model"; | ||
|
||
const mock = vi.hoisted(() => ({ | ||
legacyActivationModel: { upsert: vi.fn() }, | ||
})); | ||
|
||
beforeEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
describe("makeHandler", () => { | ||
const deps = { | ||
legacyActivationModel: mock.legacyActivationModel, | ||
} as unknown as Parameters<typeof makeHandler>[0]; | ||
|
||
const validActivation = { | ||
fiscalCode: "RSSMRA80A01H501U", | ||
serviceId: "serviceId", | ||
status: "ACTIVE" as const, | ||
modifiedAt: 1617187200000, | ||
}; | ||
|
||
it("should fail when the buffer is not valid JSON", async () => { | ||
// given | ||
const invalidBuffer = Buffer.from("invalid json"); | ||
|
||
// when | ||
const result = await makeHandler(deps)({ inputs: [invalidBuffer] })(); | ||
|
||
// then | ||
expect(E.isLeft(result)).toBeTruthy(); | ||
if (E.isLeft(result)) { | ||
expect(result.left).toBeInstanceOf(Error); | ||
expect(result.left.message).toContain("Unexpected token"); | ||
} | ||
expect(mock.legacyActivationModel.upsert).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should fail when the buffer is not a valid Activation", async () => { | ||
// given | ||
const invalidActivation = { ...validActivation, status: "WRONG_STATUS" }; | ||
const invalidBuffer = Buffer.from(JSON.stringify(invalidActivation)); | ||
|
||
// when | ||
const result = await makeHandler(deps)({ inputs: [invalidBuffer] })(); | ||
// then | ||
expect(E.isLeft(result)).toBeTruthy(); | ||
if (E.isLeft(result)) { | ||
expect(result.left).toBeInstanceOf(Error); | ||
expect(result.left.message).toContain( | ||
"at [root.status.0] is not a valid", | ||
); | ||
} | ||
expect(mock.legacyActivationModel.upsert).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should fail when upsert fails", async () => { | ||
// given | ||
const validBuffer = Buffer.from(JSON.stringify(validActivation)); | ||
const error = new Error("upsert error"); | ||
mock.legacyActivationModel.upsert.mockReturnValueOnce(TE.left(error)); | ||
|
||
// when | ||
const result = await makeHandler(deps)({ inputs: [validBuffer] })(); | ||
// then | ||
expect(E.isLeft(result)).toBeTruthy(); | ||
if (E.isLeft(result)) { | ||
expect(result.left).toBeInstanceOf(Error); | ||
expect(result.left).toStrictEqual(error); | ||
} | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledOnce(); | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledWith({ | ||
fiscalCode: validActivation.fiscalCode, | ||
kind: "INewActivation", | ||
serviceId: validActivation.serviceId, | ||
status: "ACTIVE", | ||
}); | ||
}); | ||
|
||
it("should fail when upsert fails for a cosmos empty response", async () => { | ||
// given | ||
const validBuffer = Buffer.from(JSON.stringify(validActivation)); | ||
const error: CosmosErrors = { kind: "COSMOS_CONFLICT_RESPONSE" }; | ||
mock.legacyActivationModel.upsert.mockReturnValueOnce(TE.left(error)); | ||
|
||
// when | ||
const result = await makeHandler(deps)({ inputs: [validBuffer] })(); | ||
// then | ||
expect(E.isLeft(result)).toBeTruthy(); | ||
if (E.isLeft(result)) { | ||
expect(result.left).toBeInstanceOf(Error); | ||
expect(result.left.message).toStrictEqual(error.kind); | ||
} | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledOnce(); | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledWith({ | ||
fiscalCode: validActivation.fiscalCode, | ||
kind: "INewActivation", | ||
serviceId: validActivation.serviceId, | ||
status: "ACTIVE", | ||
}); | ||
}); | ||
|
||
it("should fail when upsert fails for a cosmos decoding error", async () => { | ||
// given | ||
const validBuffer = Buffer.from(JSON.stringify(validActivation)); | ||
const error: CosmosErrors = { | ||
kind: "COSMOS_DECODING_ERROR", | ||
error: [{ context: [], value: "a", message: "b" }], | ||
}; | ||
mock.legacyActivationModel.upsert.mockReturnValueOnce(TE.left(error)); | ||
|
||
// when | ||
const result = await makeHandler(deps)({ inputs: [validBuffer] })(); | ||
// then | ||
expect(E.isLeft(result)).toBeTruthy(); | ||
if (E.isLeft(result)) { | ||
expect(result.left).toBeInstanceOf(Error); | ||
expect(result.left.message).toStrictEqual(JSON.stringify(error.error)); | ||
} | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledOnce(); | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledWith({ | ||
fiscalCode: validActivation.fiscalCode, | ||
kind: "INewActivation", | ||
serviceId: validActivation.serviceId, | ||
status: "ACTIVE", | ||
}); | ||
}); | ||
|
||
it("should fail when upsert fails for a cosmos error", async () => { | ||
// given | ||
const validBuffer = Buffer.from(JSON.stringify(validActivation)); | ||
const error: CosmosErrors = { | ||
kind: "COSMOS_ERROR_RESPONSE", | ||
error: { | ||
code: 500, | ||
message: "internal server error", | ||
name: "Error Response", | ||
}, | ||
}; | ||
mock.legacyActivationModel.upsert.mockReturnValueOnce(TE.left(error)); | ||
|
||
// when | ||
const result = await makeHandler(deps)({ inputs: [validBuffer] })(); | ||
// then | ||
expect(E.isLeft(result)).toBeTruthy(); | ||
if (E.isLeft(result)) { | ||
expect(result.left).toBeInstanceOf(Error); | ||
expect(result.left.message).toStrictEqual(error.error.message); | ||
} | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledOnce(); | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledWith({ | ||
fiscalCode: validActivation.fiscalCode, | ||
kind: "INewActivation", | ||
serviceId: validActivation.serviceId, | ||
status: "ACTIVE", | ||
}); | ||
}); | ||
|
||
it("should complete successfully when upsert do not fail", async () => { | ||
// given | ||
const validBuffer = Buffer.from(JSON.stringify(validActivation)); | ||
mock.legacyActivationModel.upsert.mockReturnValueOnce(TE.right({})); | ||
|
||
// when | ||
const result = await makeHandler(deps)({ inputs: [validBuffer] })(); | ||
// then | ||
expect(E.isRight(result)).toBeTruthy(); | ||
if (E.isRight(result)) { | ||
expect(result.right).toBeUndefined(); | ||
} | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledOnce(); | ||
expect(mock.legacyActivationModel.upsert).toHaveBeenCalledWith({ | ||
fiscalCode: validActivation.fiscalCode, | ||
kind: "INewActivation", | ||
serviceId: validActivation.serviceId, | ||
status: "ACTIVE", | ||
}); | ||
}); | ||
}); |
86 changes: 86 additions & 0 deletions
86
apps/io-services-cms-webapp/src/watchers/on-activations-change.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Activations } from "@io-services-cms/models"; | ||
import { ActivationStatusEnum } from "@pagopa/io-functions-commons/dist/generated/definitions/ActivationStatus"; | ||
import { | ||
ActivationModel, | ||
NewActivation, | ||
} from "@pagopa/io-functions-commons/dist/src/models/activation"; | ||
import { readableReport } from "@pagopa/ts-commons/lib/reporters"; | ||
import * as E from "fp-ts/lib/Either"; | ||
import * as RTE from "fp-ts/lib/ReaderTaskEither"; | ||
import * as TE from "fp-ts/lib/TaskEither"; | ||
import { flow, pipe } from "fp-ts/lib/function"; | ||
|
||
interface HandlerDependencies { | ||
readonly legacyActivationModel: ActivationModel; | ||
} | ||
|
||
const cmsToLegacy = (activation: Activations.Activation): NewActivation => ({ | ||
fiscalCode: activation.fiscalCode, | ||
kind: "INewActivation", | ||
serviceId: activation.serviceId, | ||
status: toLegacyStatus(activation.status), | ||
}); | ||
|
||
const toLegacyStatus = ( | ||
status: Activations.Activation["status"], | ||
): Activations.LegacyCosmosResource["status"] => { | ||
switch (status) { | ||
case "ACTIVE": | ||
return ActivationStatusEnum.ACTIVE; | ||
case "INACTIVE": | ||
return ActivationStatusEnum.INACTIVE; | ||
case "PENDING": | ||
return ActivationStatusEnum.PENDING; | ||
default: | ||
// Should never happen if validation is correct | ||
return ActivationStatusEnum.INACTIVE; | ||
} | ||
}; | ||
|
||
export const makeHandler = | ||
({ | ||
legacyActivationModel, | ||
}: HandlerDependencies): RTE.ReaderTaskEither< | ||
{ inputs: unknown[] }, | ||
Error, | ||
void | ||
> => | ||
({ inputs }) => | ||
pipe( | ||
inputs[0] as Buffer, | ||
(blob) => blob.toString("utf-8"), | ||
E.tryCatchK(JSON.parse, E.toError), | ||
E.chain( | ||
flow( | ||
Activations.Activation.decode, | ||
E.mapLeft(flow(readableReport, (e) => new Error(e))), | ||
), | ||
), | ||
TE.fromEither, | ||
TE.chainW((activation) => | ||
pipe( | ||
cmsToLegacy(activation), | ||
(newActivation) => legacyActivationModel.upsert(newActivation), | ||
TE.mapLeft((err) => { | ||
if (err instanceof Error) { | ||
return err; | ||
} else { | ||
switch (err.kind) { | ||
case "COSMOS_EMPTY_RESPONSE": | ||
case "COSMOS_CONFLICT_RESPONSE": | ||
return new Error(err.kind); | ||
case "COSMOS_DECODING_ERROR": | ||
return E.toError(JSON.stringify(err.error)); | ||
case "COSMOS_ERROR_RESPONSE": | ||
return E.toError(err.error.message); | ||
default: | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars, no-case-declarations | ||
const _: never = err; | ||
return new Error(`should not have executed this with ${err}`); | ||
} | ||
} | ||
}), | ||
), | ||
), | ||
TE.map(() => void 0), | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (non-blocking): use typechecker to be sure you have handled all cases.
Furthermore, consider whether in this “impossible” case it is correct to return a default value (are you sure
INACTIVE
is the correct default value?) or to throw an error. (throw new Error("Invalid status");
)