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

Add a backfill script for previousRevisions, submissionCount #11516

Merged
merged 2 commits into from
Nov 14, 2023
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
79 changes: 79 additions & 0 deletions services/app-api/handlers/etl/addRevisionsToMcpar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { addRevisionsHandler } from "./addRevisionsToMcpar";
import dynamodbLib from "../../utils/dynamo/dynamodb-lib";
import { ReportStatus } from "../../utils/types";

jest.mock("../../utils/constants/constants", () => ({
...jest.requireActual("../../utils/constants/constants"),
reportTables: {
MCPAR: "local-mcpar-reports",
},
}));

jest.mock("../../utils/dynamo/dynamodb-lib", () => ({
scanIterator: jest.fn(),
put: jest.fn(),
}));

describe("addRevisionsHandler", () => {
beforeEach(() => {
jest.clearAllMocks();
});

test("should add previousRevisions to reports without them", async () => {
(dynamodbLib.scanIterator as jest.Mock).mockReturnValue([
{
reportId: "mock-id-1",
status: ReportStatus.SUBMITTED,
},
{
reportId: "mock-id-2",
status: ReportStatus.IN_PROGRESS,
},
{
reportId: "mock-id-3",
status: ReportStatus.NOT_STARTED,
previousRevisions: [],
submissionCount: 0,
locked: false,
},
]);

const result = await addRevisionsHandler();

expect(result.statusCode).toBe(200);
const mockedPut = dynamodbLib.put as jest.Mock;
expect(mockedPut).toBeCalledTimes(2);
expect(mockedPut.mock.calls[0][0]).toEqual({
TableName: "local-mcpar-reports",
Item: {
reportId: "mock-id-1",
status: ReportStatus.SUBMITTED,
previousRevisions: [],
submissionCount: 1,
locked: true,
},
});
expect(mockedPut.mock.calls[1][0]).toEqual({
TableName: "local-mcpar-reports",
Item: {
reportId: "mock-id-2",
status: ReportStatus.IN_PROGRESS,
previousRevisions: [],
submissionCount: 0,
locked: false,
},
});
});

test("should return 500 if dynamo errors", async () => {
jest.spyOn(console, "error").mockImplementationOnce(() => undefined);

(dynamodbLib.scanIterator as jest.Mock).mockImplementationOnce(() => {
throw new Error("no DB for you");
});

const result = await addRevisionsHandler();

expect(result.statusCode).toBe(500);
});
});
46 changes: 46 additions & 0 deletions services/app-api/handlers/etl/addRevisionsToMcpar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable no-console */
import { APIGatewayProxyResult } from "aws-lambda";
import { ReportStatus, ReportType } from "../../utils/types";
import { reportTables } from "../../utils/constants/constants";
import dynamodbLib from "../../utils/dynamo/dynamodb-lib";

export const addRevisionsHandler = async (): Promise<APIGatewayProxyResult> => {
try {
await addRevisionsToExistingMcparReports();

return {
statusCode: 200,
body: JSON.stringify({
message: "Update complete",
}),
};
} catch (err: any) {
console.error(err);
return {
statusCode: 500,
body: JSON.stringify({
message: err.message,
}),
};
}
};

const addRevisionsToExistingMcparReports = async () => {
const TableName = reportTables[ReportType.MCPAR];
for await (const reportMetadata of dynamodbLib.scanIterator({ TableName })) {
if (reportMetadata.previousRevisions !== undefined) {
continue;
}

const wasSubmitted = reportMetadata.status === ReportStatus.SUBMITTED;

reportMetadata.previousRevisions = [];
reportMetadata.submissionCount = wasSubmitted ? 1 : 0;
reportMetadata.locked = wasSubmitted ? true : false;

await dynamodbLib.put({
benmartin-coforma marked this conversation as resolved.
Show resolved Hide resolved
TableName,
Item: reportMetadata,
});
}
};
7 changes: 7 additions & 0 deletions services/app-api/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ provider:
PRINCE_API_PATH: ${self:custom.princeApiPath}

functions:
addRevisionsToMcpar:
handler: handlers/etl/addRevisionsToMcpar.addRevisionsHandler
timeout: 300
warmup:
default:
enabled: false
maximumRetryAttempts: 0
numberFieldDataToFile:
handler: handlers/etl/numberFieldDataToFile.exportNumericData
timeout: 300
Expand Down