Skip to content

Commit

Permalink
[#IP-308] add new UserDataDeleteOrchestratorV2 (#158)
Browse files Browse the repository at this point in the history
* [#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
gquadrati authored Aug 9, 2021
1 parent fc9c788 commit 2339140
Show file tree
Hide file tree
Showing 17 changed files with 2,820 additions and 212 deletions.
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

0 comments on commit 2339140

Please sign in to comment.