-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#IP-154] UserDataProcessingDeleteOrchestrator can manage previous fa…
…iled requests if they are resumed (#156)
- Loading branch information
1 parent
bf99991
commit 3636839
Showing
8 changed files
with
1,200 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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
199 changes: 199 additions & 0 deletions
199
IsFailedUserDataProcessingActivity/__tests__/handler.test.ts
This file contains 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,199 @@ | ||
import { FiscalCode, NonEmptyString } from "@pagopa/ts-commons/lib/strings"; | ||
import { TableService } from "azure-storage"; | ||
import { | ||
UserDataProcessingChoice, | ||
UserDataProcessingChoiceEnum | ||
} from "@pagopa/io-functions-commons/dist/generated/definitions/UserDataProcessingChoice"; | ||
import { | ||
ActivityResultFailure, | ||
ActivityResultSuccess, | ||
IsFailedUserDataProcessing | ||
} from "../handler"; | ||
|
||
const findEntry = ( | ||
entries: ReadonlyArray<{ | ||
PartitionKey: UserDataProcessingChoice; | ||
RowKey: FiscalCode; | ||
}> | ||
) => (choice, fiscalCode) => | ||
entries.length > 0 | ||
? entries | ||
.filter(e => e.PartitionKey === choice && e.RowKey === fiscalCode) | ||
.map(e => ({ | ||
RowKey: { _: e.RowKey } | ||
}))[0] | ||
: null; | ||
|
||
const retrieveEntityFailedUserDataProcessingMock = ( | ||
entries: ReadonlyArray<{ | ||
PartitionKey: UserDataProcessingChoice; | ||
RowKey: FiscalCode; | ||
}> | ||
) => | ||
jest.fn((_, choice, fiscalCode, ____, cb) => { | ||
return cb( | ||
findEntry(entries)(choice, fiscalCode) | ||
? null | ||
: new Error("Internal error"), | ||
findEntry(entries)(choice, fiscalCode), | ||
{ | ||
isSuccessful: findEntry(entries)(choice, fiscalCode), | ||
statusCode: findEntry(entries)(choice, fiscalCode) ? 200 : 404 | ||
} | ||
); | ||
}); | ||
|
||
const internalErrorRetrieveEntityFailedUserDataProcessingMock = ( | ||
entries: ReadonlyArray<{ | ||
PartitionKey: UserDataProcessingChoice; | ||
RowKey: FiscalCode; | ||
}> | ||
) => | ||
jest.fn((_, choice, fiscalCode, ____, cb) => { | ||
return cb(new Error("Internal error"), null, { isSuccessful: false }); | ||
}); | ||
|
||
const storageTableMock = "FailedUserDataProcessing" as NonEmptyString; | ||
|
||
const fiscalCode1 = "UEEFON48A55Y758X" as FiscalCode; | ||
const fiscalCode2 = "VEEGON48A55Y758Z" as FiscalCode; | ||
|
||
const noFailedRequests = []; | ||
|
||
const failedRequests = [ | ||
{ | ||
PartitionKey: UserDataProcessingChoiceEnum.DELETE, | ||
RowKey: fiscalCode1 | ||
}, | ||
{ | ||
PartitionKey: UserDataProcessingChoiceEnum.DOWNLOAD, | ||
RowKey: fiscalCode1 | ||
}, | ||
{ | ||
PartitionKey: UserDataProcessingChoiceEnum.DELETE, | ||
RowKey: fiscalCode2 | ||
}, | ||
{ | ||
PartitionKey: UserDataProcessingChoiceEnum.DOWNLOAD, | ||
RowKey: fiscalCode2 | ||
} | ||
]; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("IsFailedUserDataProcessingHandler", () => { | ||
it("should fail if input is not valid", async () => { | ||
const tableServiceMock = ({ | ||
retrieveEntity: retrieveEntityFailedUserDataProcessingMock( | ||
noFailedRequests | ||
) | ||
} as any) as TableService; | ||
|
||
const getFailedUserDataProcessingHandler = IsFailedUserDataProcessing( | ||
tableServiceMock, | ||
storageTableMock | ||
); | ||
|
||
const result = await getFailedUserDataProcessingHandler({} as any, { | ||
a: "a", | ||
b: "b", | ||
c: "c" | ||
}); | ||
|
||
expect(ActivityResultFailure.is(result)).toBe(true); | ||
const decodedResult = ActivityResultFailure.decode(result); | ||
expect(decodedResult.isRight()).toBe(true); | ||
if (decodedResult.isRight()) { | ||
expect(JSON.stringify(decodedResult.value)).toBe( | ||
JSON.stringify({ | ||
kind: "FAILURE", | ||
reason: "Invalid input" | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
it("should fail if any error occurs", async () => { | ||
const tableServiceMock = ({ | ||
retrieveEntity: internalErrorRetrieveEntityFailedUserDataProcessingMock( | ||
failedRequests | ||
) | ||
} as any) as TableService; | ||
|
||
const getFailedUserDataProcessingHandler = IsFailedUserDataProcessing( | ||
tableServiceMock, | ||
storageTableMock | ||
); | ||
|
||
const result = await getFailedUserDataProcessingHandler({} as any, { | ||
choice: UserDataProcessingChoiceEnum.DELETE, | ||
fiscalCode: fiscalCode1 | ||
}); | ||
|
||
expect(ActivityResultFailure.is(result)).toBe(true); | ||
const decodedResult = ActivityResultFailure.decode(result); | ||
expect(decodedResult.isRight()).toBe(true); | ||
if (decodedResult.isRight()) { | ||
expect(JSON.stringify(decodedResult.value)).toBe( | ||
JSON.stringify({ | ||
kind: "FAILURE", | ||
reason: "ERROR|tableService.retrieveEntity|Cannot retrieve entity" | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
it("should succeed with false value if no failed user data processing is present", async () => { | ||
const tableServiceMock = ({ | ||
retrieveEntity: retrieveEntityFailedUserDataProcessingMock( | ||
noFailedRequests | ||
) | ||
} as any) as TableService; | ||
|
||
const getFailedUserDataProcessingHandler = IsFailedUserDataProcessing( | ||
tableServiceMock, | ||
storageTableMock | ||
); | ||
|
||
const result = await getFailedUserDataProcessingHandler({} as any, { | ||
choice: UserDataProcessingChoiceEnum.DELETE, | ||
fiscalCode: fiscalCode1 | ||
}); | ||
|
||
expect(ActivityResultSuccess.is(result)).toBe(true); | ||
const decodedResult = ActivityResultSuccess.decode(result); | ||
expect(decodedResult.isRight()).toBe(true); | ||
if (decodedResult.isRight()) { | ||
expect(JSON.stringify(decodedResult.value)).toBe( | ||
JSON.stringify({ kind: "SUCCESS", value: false }) | ||
); | ||
} | ||
}); | ||
|
||
it("should succeed with true value if failed user data processing is present", async () => { | ||
const tableServiceMock = ({ | ||
retrieveEntity: retrieveEntityFailedUserDataProcessingMock(failedRequests) | ||
} as any) as TableService; | ||
|
||
const getFailedUserDataProcessingHandler = IsFailedUserDataProcessing( | ||
tableServiceMock, | ||
storageTableMock | ||
); | ||
|
||
const result = await getFailedUserDataProcessingHandler({} as any, { | ||
choice: UserDataProcessingChoiceEnum.DELETE, | ||
fiscalCode: fiscalCode1 | ||
}); | ||
|
||
expect(ActivityResultSuccess.is(result)).toBe(true); | ||
const decodedResult = ActivityResultSuccess.decode(result); | ||
expect(decodedResult.isRight()).toBe(true); | ||
if (decodedResult.isRight()) { | ||
expect(JSON.stringify(decodedResult.value)).toBe( | ||
JSON.stringify({ kind: "SUCCESS", value: true }) | ||
); | ||
} | ||
}); | ||
}); |
This file contains 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,10 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"name": "name", | ||
"type": "activityTrigger", | ||
"direction": "in" | ||
} | ||
], | ||
"scriptFile": "../dist/IsFailedUserDataProcessingActivity/index.js" | ||
} |
This file contains 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,96 @@ | ||
import { Context } from "@azure/functions"; | ||
import { ServiceResponse, TableService } from "azure-storage"; | ||
import { fromOption } from "fp-ts/lib/Either"; | ||
import { NonEmptyString, FiscalCode } from "@pagopa/ts-commons/lib/strings"; | ||
import { UserDataProcessingChoice } from "@pagopa/io-functions-commons/dist/generated/definitions/UserDataProcessingChoice"; | ||
import { fromEither, tryCatch } from "fp-ts/lib/TaskEither"; | ||
import { none, Option, some } from "fp-ts/lib/Option"; | ||
import * as t from "io-ts"; | ||
import { identity } from "fp-ts/lib/function"; | ||
|
||
// Activity input | ||
export const ActivityInput = t.interface({ | ||
choice: UserDataProcessingChoice, | ||
fiscalCode: FiscalCode | ||
}); | ||
export type ActivityInput = t.TypeOf<typeof ActivityInput>; | ||
|
||
// Activity result | ||
export type ActivityResultSuccess = t.TypeOf<typeof ActivityResultSuccess>; | ||
export const ActivityResultSuccess = t.interface({ | ||
kind: t.literal("SUCCESS"), | ||
value: t.boolean | ||
}); | ||
|
||
export type ActivityResultFailure = t.TypeOf<typeof ActivityResultFailure>; | ||
export const ActivityResultFailure = t.interface({ | ||
kind: t.literal("FAILURE"), | ||
reason: t.string | ||
}); | ||
|
||
export const ActivityResult = t.taggedUnion("kind", [ | ||
ActivityResultSuccess, | ||
ActivityResultFailure | ||
]); | ||
export type ActivityResult = t.TypeOf<typeof ActivityResult>; | ||
|
||
// Table storage result | ||
type TableEntry = Readonly<{ | ||
readonly RowKey: Readonly<{ | ||
readonly _: FiscalCode; | ||
}>; | ||
}>; | ||
|
||
export const IsFailedUserDataProcessing = ( | ||
tableService: TableService, | ||
failedUserDataProcessingTable: NonEmptyString | ||
) => (context: Context, input: unknown): Promise<ActivityResult> => | ||
fromEither(ActivityInput.decode(input)) | ||
.mapLeft<ActivityResult>(_ => | ||
ActivityResultFailure.encode({ | ||
kind: "FAILURE", | ||
reason: "Invalid input" | ||
}) | ||
) | ||
.chain(i => | ||
tryCatch( | ||
() => | ||
new Promise<Option<TableEntry>>((resolve, reject) => | ||
tableService.retrieveEntity( | ||
failedUserDataProcessingTable, | ||
i.choice, | ||
i.fiscalCode, | ||
null, | ||
(error: Error, result: TableEntry, response: ServiceResponse) => | ||
response.isSuccessful | ||
? resolve(some(result)) | ||
: response.statusCode === 404 | ||
? resolve(none) | ||
: reject(error) | ||
) | ||
), | ||
_ => | ||
ActivityResultFailure.encode({ | ||
kind: "FAILURE", | ||
reason: "ERROR|tableService.retrieveEntity|Cannot retrieve entity" | ||
}) | ||
) | ||
) | ||
.chain(maybeTableEntry => | ||
fromEither( | ||
fromOption( | ||
ActivityResultSuccess.encode({ | ||
kind: "SUCCESS", | ||
value: false | ||
}) | ||
)(maybeTableEntry) | ||
) | ||
) | ||
.map(_ => | ||
ActivityResultSuccess.encode({ | ||
kind: "SUCCESS", | ||
value: true | ||
}) | ||
) | ||
.fold<ActivityResult>(identity, identity) | ||
.run(); |
This file contains 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,19 @@ | ||
import { createTableService } from "azure-storage"; | ||
import { getConfigOrThrow } from "../utils/config"; | ||
import { IsFailedUserDataProcessing } from "./handler"; | ||
|
||
/** | ||
* Table service | ||
*/ | ||
const config = getConfigOrThrow(); | ||
const storageConnectionString = | ||
config.FailedUserDataProcessingStorageConnection; | ||
const failedUserDataProcessingTable = config.FAILED_USER_DATA_PROCESSING_TABLE; | ||
const tableService = createTableService(storageConnectionString); | ||
|
||
const activityFunctionHandler = IsFailedUserDataProcessing( | ||
tableService, | ||
failedUserDataProcessingTable | ||
); | ||
|
||
export default activityFunctionHandler; |
Oops, something went wrong.