Skip to content
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

[#IP-308] add new UserDataDeleteOrchestratorV2 #158

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions GetServicesPreferencesActivity/function.json
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"
}
103 changes: 103 additions & 0 deletions GetServicesPreferencesActivity/handler.ts
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();
17 changes: 17 additions & 0 deletions GetServicesPreferencesActivity/index.ts
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;
Loading