-
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-308] add new UserDataDeleteOrchestratorV2 (#158)
* [#IP-308] [#IP-308] add new UserDataDeleteOrchestratorV2 * [#IP-308] add new tests for subscription feed * [#IP-308] fix function.json UserDataDeleteOrchestratorV2 * [#IP-308] fix getServicesPreferences bug on retrieve * [#IP-308] apply suggestions from fn-app review
- Loading branch information
Showing
17 changed files
with
2,820 additions
and
212 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"bindings": [ | ||
{ | ||
"name": "name", | ||
"type": "activityTrigger", | ||
"direction": "in" | ||
} | ||
], | ||
"scriptFile": "../dist/GetServicesPreferencesActivity/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,103 @@ | ||
import { Context } from "@azure/functions"; | ||
import { | ||
ServicePreference, | ||
ServicesPreferencesModel | ||
} from "@pagopa/io-functions-commons/dist/src/models/service_preference"; | ||
import { | ||
asyncIteratorToArray, | ||
flattenAsyncIterator | ||
} from "@pagopa/io-functions-commons/dist/src/utils/async"; | ||
import { | ||
CosmosErrors, | ||
toCosmosErrorResponse | ||
} from "@pagopa/io-functions-commons/dist/src/utils/cosmosdb_model"; | ||
import { NonNegativeInteger } from "@pagopa/ts-commons/lib/numbers"; | ||
import { readableReport } from "@pagopa/ts-commons/lib/reporters"; | ||
import { FiscalCode } from "@pagopa/ts-commons/lib/strings"; | ||
import { isRight } from "fp-ts/lib/Either"; | ||
import { fromEither, tryCatch } from "fp-ts/lib/TaskEither"; | ||
import * as t from "io-ts"; | ||
|
||
const ActivityInput = t.interface({ | ||
fiscalCode: FiscalCode, | ||
settingsVersion: NonNegativeInteger | ||
}); | ||
type ActivityInput = t.TypeOf<typeof ActivityInput>; | ||
|
||
// Activity result | ||
export const ActivityResultSuccess = t.interface({ | ||
kind: t.literal("SUCCESS"), | ||
preferences: t.readonlyArray(ServicePreference) | ||
}); | ||
|
||
export type ActivityResultSuccess = t.TypeOf<typeof ActivityResultSuccess>; | ||
|
||
export const InvalidInputFailure = t.interface({ | ||
kind: t.literal("INVALID_INPUT") | ||
}); | ||
export type InvalidInputFailure = t.TypeOf<typeof InvalidInputFailure>; | ||
|
||
export const ActivityResult = t.taggedUnion("kind", [ | ||
ActivityResultSuccess, | ||
InvalidInputFailure | ||
]); | ||
export type ActivityResult = t.TypeOf<typeof ActivityResult>; | ||
|
||
export const GetServicesPreferencesActivityHandler = ( | ||
servicePreferences: ServicesPreferencesModel | ||
) => async (context: Context, input: unknown): Promise<ActivityResult> => | ||
fromEither<t.Errors, ActivityInput>(ActivityInput.decode(input)) | ||
.mapLeft<InvalidInputFailure | CosmosErrors>(_ => | ||
InvalidInputFailure.encode({ kind: "INVALID_INPUT" }) | ||
) | ||
.chain(({ fiscalCode, settingsVersion }) => | ||
tryCatch( | ||
async () => | ||
servicePreferences | ||
.getQueryIterator({ | ||
parameters: [ | ||
{ | ||
name: "@fiscalCode", | ||
value: fiscalCode | ||
}, | ||
{ | ||
name: "@version", | ||
value: settingsVersion | ||
} | ||
], | ||
query: `SELECT * FROM m WHERE m.fiscalCode = @fiscalCode AND m.settingsVersion = @version` | ||
}) | ||
[Symbol.asyncIterator](), | ||
toCosmosErrorResponse | ||
) | ||
) | ||
.map(flattenAsyncIterator) | ||
.map(asyncIteratorToArray) | ||
.chain(i => tryCatch(() => i, toCosmosErrorResponse)) | ||
.map(values => values.filter(isRight).map(_ => _.value)) | ||
.fold<ActivityResult>( | ||
err => { | ||
if (err.kind === "INVALID_INPUT") { | ||
context.log.error( | ||
`GetServicesPreferencesActivityHandler|ERROR|Invalid activity input [${err}]` | ||
); | ||
return err; | ||
} | ||
context.log.error( | ||
`GetServicesPreferencesActivityHandler|ERROR|Cosmos error [${ | ||
err.kind === "COSMOS_DECODING_ERROR" | ||
? readableReport(err.error) | ||
: err.kind === "COSMOS_ERROR_RESPONSE" | ||
? err.error.message | ||
: err.kind | ||
}]` | ||
); | ||
throw new Error(err.kind); | ||
}, | ||
preferences => | ||
ActivityResultSuccess.encode({ | ||
kind: "SUCCESS", | ||
preferences | ||
}) | ||
) | ||
.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,17 @@ | ||
import { | ||
SERVICE_PREFERENCES_COLLECTION_NAME, | ||
ServicesPreferencesModel | ||
} from "@pagopa/io-functions-commons/dist/src/models/service_preference"; | ||
import { cosmosdbInstance } from "../utils/cosmosdb"; | ||
import { GetServicesPreferencesActivityHandler } from "./handler"; | ||
|
||
const servicePreferencesModel = new ServicesPreferencesModel( | ||
cosmosdbInstance.container(SERVICE_PREFERENCES_COLLECTION_NAME), | ||
SERVICE_PREFERENCES_COLLECTION_NAME | ||
); | ||
|
||
const activityFunctionHandler = GetServicesPreferencesActivityHandler( | ||
servicePreferencesModel | ||
); | ||
|
||
export default activityFunctionHandler; |
Oops, something went wrong.