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-145] New table for users with failed user_data_processing #138

Merged
merged 17 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 6 additions & 6 deletions UserDataProcessingTrigger/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import {
} from "../utils/appinsightsEvents";
import { flags } from "../utils/featureFlags";
import { isOrchestratorRunning } from "../utils/orchestrator";
import { InsertTableEntityType, DeleteTableEntityType } from "./utils";
import { DeleteTableEntity, InsertTableEntity } from "../utils/storage";

const eg = TableUtilities.entityGenerator;

// configure log prefix
const logPrefix = "UserDataProcessingTrigger";
const logPrefix = "UserDataProcessingHandler";

// models the subset of UserDataProcessing documents that this orchestrator accepts
export type ProcessableUserDataDownload = t.TypeOf<
Expand Down Expand Up @@ -174,7 +174,7 @@ const raiseAbortEventOnOrchestrator = (
const processFailedUserDataProcessing = async (
context: Context,
processable: FailedUserDataProcessing,
insertEntityFn: InsertTableEntityType
insertEntityFn: InsertTableEntity
): Promise<void> => {
// If a failed user_data_processing has been inserted
// we insert a record into failed_user_data_processing table storage
Expand All @@ -194,7 +194,7 @@ const processFailedUserDataProcessing = async (
const processClosedUserDataProcessing = async (
context: Context,
processable: ClosedUserDataProcessing,
deleteEntityFn: DeleteTableEntityType
deleteEntityFn: DeleteTableEntity
): Promise<void> => {
// If a completed user_data_processing has been inserted
// we delete any record into failed_user_data_processing table storage
Expand All @@ -213,8 +213,8 @@ const processClosedUserDataProcessing = async (

// eslint-disable-next-line prefer-arrow/prefer-arrow-functions, sonarjs/cognitive-complexity
export const triggerHandler = (
addFailure: InsertTableEntityType,
removeFailure: DeleteTableEntityType
addFailure: InsertTableEntity,
removeFailure: DeleteTableEntity
) => (
context: Context,
input: unknown // eslint-disable-next-line sonarjs/cognitive-complexity
Expand Down
36 changes: 27 additions & 9 deletions UserDataProcessingTrigger/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import { Context } from "@azure/functions";
import { triggerHandler } from "./handler";
import { createTableService } from "azure-storage";
import { getConfigOrThrow } from "../utils/config";
import {
addFailedUserDataProcessing,
removeFailedUserDataProcessing,
createFailedUserDataProcessingTableIfNotExists
} from "./utils";
createTableIfNotExists,
deleteTableEntity,
insertTableEntity
} from "../utils/storage";
import { triggerHandler } from "./handler";

createFailedUserDataProcessingTableIfNotExists();
// configure log prefix
const logPrefix = "UserDataProcessingTrigger";

export const index = (
// prepare table storage utils
const config = getConfigOrThrow();
const connectionString = config.FailedUserDataProcessingStorageConnection;
const failedUserDataProcessingTable = config.FAILED_USER_DATA_PROCESSING_TABLE;
const tableService = createTableService(connectionString);

export const index = async (
context: Context,
input: unknown
): Promise<ReadonlyArray<string | void>> => {
(await createTableIfNotExists(
tableService,
failedUserDataProcessingTable
)).mapLeft(_ => {
context.log.verbose(
`${logPrefix}|Failed to create storage table ${failedUserDataProcessingTable}`
);
});
michaeldisaro marked this conversation as resolved.
Show resolved Hide resolved

const handler = triggerHandler(
addFailedUserDataProcessing,
removeFailedUserDataProcessing
insertTableEntity(tableService, failedUserDataProcessingTable),
deleteTableEntity(tableService, failedUserDataProcessingTable)
);
return handler(context, input);
};
27 changes: 0 additions & 27 deletions UserDataProcessingTrigger/utils.ts

This file was deleted.

17 changes: 17 additions & 0 deletions utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import { none, Option, some } from "fp-ts/lib/Option";

import { ITuple2, Tuple2 } from "italia-ts-commons/lib/tuples";

/**
* A promisified version of TableService.createTableIfNotExists
*/
export const createTableIfNotExists = (
tableService: TableService,
table: string
): Promise<Either<Error, TableService.TableResult>> =>
new Promise(resolve =>
tableService.createTableIfNotExists(table, (error, result, response) =>
resolve(response.isSuccessful ? right(result) : left(error))
)
);
michaeldisaro marked this conversation as resolved.
Show resolved Hide resolved

/**
* A promisified version of TableService.insertEntity
*/
Expand Down Expand Up @@ -33,6 +46,8 @@ export const insertTableEntity = (
)
);

export type InsertTableEntity = ReturnType<typeof insertTableEntity>;

/**
* A promisified version of TableService.deleteEntity
*/
Expand All @@ -54,3 +69,5 @@ export const deleteTableEntity = (
)
)
);

export type DeleteTableEntity = ReturnType<typeof deleteTableEntity>;