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

10492 dxox #5518

Draft
wants to merge 16 commits into
base: staging
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions run-local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

if [[ -z "$CI" ]]; then
echo "Stopping postgres in case it's already running"
docker compose -f web-api/src/persistence/postgres/docker-compose.yml down || true
podman compose -f web-api/src/persistence/postgres/docker-compose.yml down || true

echo "Starting postgres"
docker compose -f web-api/src/persistence/postgres/docker-compose.yml up -d || { echo "Failed to start Postgres containers"; exit 1; }
podman compose -f web-api/src/persistence/postgres/docker-compose.yml up -d || { echo "Failed to start Postgres containers"; exit 1; }

echo "Stopping dynamodb in case it's already running"
pkill -f DynamoDBLocal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
BatchWriteCommand,
DynamoDBDocumentClient,
} from '@aws-sdk/lib-dynamodb';

export async function batchDeleteDynamoItems(
itemsToDelete: { DeleteRequest: { Key: { pk: string; sk: string } } }[],
client: DynamoDBDocumentClient,
tableNameInput: string,
): Promise<number> {
const BATCH_SIZE = 25;
const RETRY_DELAY_MS = 5000; // Set the delay between retries (in milliseconds)
let totalItemsDeleted = 0;

for (let i = 0; i < itemsToDelete.length; i += BATCH_SIZE) {
const batch = itemsToDelete.slice(i, i + BATCH_SIZE);

const batchWriteParams = {
RequestItems: {
[tableNameInput]: batch,
},
};

try {
let unprocessedItems: any[] = batch;
let retryCount = 0;
const MAX_RETRIES = 5;

// Retry logic for unprocessed items
while (unprocessedItems.length > 0 && retryCount < MAX_RETRIES) {
const response = await client.send(
new BatchWriteCommand(batchWriteParams),
);

totalItemsDeleted +=
unprocessedItems.length -
(response.UnprocessedItems?.[tableNameInput]?.length || 0);

unprocessedItems = response.UnprocessedItems?.[tableNameInput] ?? [];

if (unprocessedItems.length > 0) {
console.log(
`Retrying unprocessed items: ${unprocessedItems.length}, attempt ${retryCount + 1}`,
);
batchWriteParams.RequestItems[tableNameInput] = unprocessedItems;
retryCount++;

// Add delay before the next retry
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY_MS));
}
}

if (unprocessedItems.length > 0) {
console.error(
`Failed to delete ${unprocessedItems.length} items after ${MAX_RETRIES} retries.`,
);
}
} catch (error) {
console.error('Error in batch delete:', error);
}
}
return totalItemsDeleted;
}
62 changes: 62 additions & 0 deletions scripts/run-once-scripts/postgres-migration/delete-case-notes
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* HOW TO RUN
*
* TABLE_NAME=testing npx ts-node --transpileOnly scripts/run-once-scripts/postgres-migration/delete-case-notes.ts
*/

import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { requireEnvVars } from '../../../shared/admin-tools/util';
import { getDbReader } from '../../../web-api/src/database';
import { isEmpty } from 'lodash';
import { batchDeleteDynamoItems } from './batch-delete-dynamo-items';

const caseUserNotesPageSize = 10000;
const dynamoDbClient = new DynamoDBClient({ region: 'us-east-1' });
const dynamoDbDocClient = DynamoDBDocumentClient.from(dynamoDbClient);

requireEnvVars(['TABLE_NAME']);

const tableNameInput = process.env.TABLE_NAME!;

const getCaseNotesToDelete = async (offset: number) => {
const caseNotes = await getDbReader(reader =>
reader
.selectFrom('dwUserCaseNote')
.select(['docketNumber', 'userId'])
.orderBy('userId')
.limit(caseUserNotesPageSize)
.offset(offset)
.execute(),
);
return caseNotes;
};

let totalItemsDeleted = 0;

async function main() {
let offset = 0;
let caseNotesToDelete = await getCaseNotesToDelete(offset);

while (!isEmpty(caseNotesToDelete)) {
const dynamoItemsToDelete = caseNotesToDelete.map(c => ({
DeleteRequest: {
Key: {
pk: `user-case-note|${c.docketNumber}`,
sk: `user|${c.userId}`,
},
},
}));
totalItemsDeleted += await batchDeleteDynamoItems(
dynamoItemsToDelete,
dynamoDbDocClient,
tableNameInput,
);
console.log(`Total case notes deleted so far: ${totalItemsDeleted}`);
offset += caseUserNotesPageSize;
caseNotesToDelete = await getCaseNotesToDelete(offset);
}
console.log('Done deleting case notes from Dynamo');
}

main().catch(console.error);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ScanCommand,
} from '@aws-sdk/lib-dynamodb';
import { DynamoDBClient, ScanCommandInput } from '@aws-sdk/client-dynamodb';
import { requireEnvVars } from '../../shared/admin-tools/util';
import { requireEnvVars } from '../../../shared/admin-tools/util';

requireEnvVars(['TABLE_NAME']);

Expand Down
6 changes: 0 additions & 6 deletions types/TEntity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ type TPetitioner = {
hasConsentedToEService?: boolean;
};

type TCaseNote = {
userId: string;
docketNumber: string;
notes: string;
};

interface IValidateRawCollection<I> {
(collection: I[], options: { applicationContext: IApplicationContext }): I[];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import '@web-api/persistence/postgres/userCaseNotes/mocks.jest';
import { ROLES } from '../../../../../shared/src/business/entities/EntityConstants';
import { UnauthorizedError } from '@web-api/errors/errors';
import { UnknownAuthUser } from '@shared/business/entities/authUser/AuthUser';
import { User } from '../../../../../shared/src/business/entities/User';
import { applicationContext } from '../../../../../shared/src/business/test/createTestApplicationContext';
import { deleteUserCaseNoteInteractor } from './deleteUserCaseNoteInteractor';
import { deleteUserCaseNote as deleteUserCaseNoteMock } from '@web-api/persistence/postgres/userCaseNotes/deleteUserCaseNote';
import { mockJudgeUser } from '@shared/test/mockAuthUsers';
import { omit } from 'lodash';

describe('deleteUserCaseNoteInteractor', () => {
const deleteUserCaseNote = deleteUserCaseNoteMock as jest.Mock;

it('throws an error if the user is not valid or authorized', async () => {
let user = {} as UnknownAuthUser;

Expand All @@ -33,7 +37,7 @@ describe('deleteUserCaseNoteInteractor', () => {
applicationContext
.getPersistenceGateway()
.getUserById.mockReturnValue(mockUser);
applicationContext.getPersistenceGateway().deleteUserCaseNote = v => v;
deleteUserCaseNote.mockImplementation(v => v);
applicationContext
.getUseCaseHelpers()
.getJudgeInSectionHelper.mockReturnValue({
Expand All @@ -60,7 +64,6 @@ describe('deleteUserCaseNoteInteractor', () => {
applicationContext
.getPersistenceGateway()
.getUserById.mockReturnValue(mockUser);
applicationContext.getPersistenceGateway().deleteUserCaseNote = jest.fn();
applicationContext
.getUseCaseHelpers()
.getJudgeInSectionHelper.mockReturnValue(null);
Expand All @@ -72,9 +75,8 @@ describe('deleteUserCaseNoteInteractor', () => {
omit(mockUser, 'section'),
);

expect(
applicationContext.getPersistenceGateway().deleteUserCaseNote.mock
.calls[0][0].userId,
).toEqual(mockJudgeUser.userId);
expect(deleteUserCaseNote.mock.calls[0][0].userId).toEqual(
mockJudgeUser.userId,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { ServerApplicationContext } from '@web-api/applicationContext';
import { UnauthorizedError } from '@web-api/errors/errors';
import { UnknownAuthUser } from '@shared/business/entities/authUser/AuthUser';
import { deleteUserCaseNote } from '@web-api/persistence/postgres/userCaseNotes/deleteUserCaseNote';

/**
* deleteUserCaseNoteInteractor
Expand All @@ -31,8 +32,7 @@ export const deleteUserCaseNoteInteractor = async (
userIdMakingRequest: authorizedUser.userId,
});

return await applicationContext.getPersistenceGateway().deleteUserCaseNote({
applicationContext,
return await deleteUserCaseNote({
docketNumber,
userId,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import '@web-api/persistence/postgres/userCaseNotes/mocks.jest';
import { MOCK_CASE } from '../../../../../shared/src/test/mockCase';
import { UnauthorizedError } from '@web-api/errors/errors';
import { UnknownAuthUser } from '@shared/business/entities/authUser/AuthUser';
import { UserCaseNote } from '@shared/business/entities/notes/UserCaseNote';
import { applicationContext } from '../../../../../shared/src/business/test/createTestApplicationContext';
import { getUserCaseNoteForCasesInteractor } from './getUserCaseNoteForCasesInteractor';
import { getUserCaseNoteForCases as getUserCaseNoteForCasesMock } from '@web-api/persistence/postgres/userCaseNotes/getUserCaseNoteForCases';
import { mockJudgeUser } from '@shared/test/mockAuthUsers';
import { omit } from 'lodash';

Expand All @@ -21,15 +24,15 @@ describe('getUserCaseNoteForCasesInteractor', () => {
section: 'colvinChambers',
} as UnknownAuthUser;

const getUserCaseNoteForCases = getUserCaseNoteForCasesMock as jest.Mock;

beforeEach(() => {
mockCurrentUser = mockJudge;
mockNote = MOCK_NOTE;
applicationContext
.getPersistenceGateway()
.getUserById.mockImplementation(() => mockCurrentUser);
applicationContext
.getPersistenceGateway()
.getUserCaseNoteForCases.mockResolvedValue([mockNote]);
getUserCaseNoteForCases.mockResolvedValue([new UserCaseNote(mockNote)]);
applicationContext
.getUseCaseHelpers()
.getJudgeInSectionHelper.mockReturnValue(mockJudge);
Expand All @@ -52,9 +55,9 @@ describe('getUserCaseNoteForCasesInteractor', () => {
});

it('throws an error if the entity returned from persistence is invalid', async () => {
applicationContext
.getPersistenceGateway()
.getUserCaseNoteForCases.mockResolvedValue([omit(MOCK_NOTE, 'userId')]);
getUserCaseNoteForCases.mockResolvedValue([
new UserCaseNote([omit(MOCK_NOTE, 'userId')]),
]);

await expect(
getUserCaseNoteForCasesInteractor(
Expand Down Expand Up @@ -100,9 +103,8 @@ describe('getUserCaseNoteForCasesInteractor', () => {
omit(mockUser, 'section'),
);

expect(
applicationContext.getPersistenceGateway().getUserCaseNoteForCases.mock
.calls[0][0].userId,
).toEqual(userIdToExpect);
expect(getUserCaseNoteForCases.mock.calls[0][0].userId).toEqual(
userIdToExpect,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '../../../../../shared/src/authorization/authorizationClientService';
import { UnauthorizedError } from '@web-api/errors/errors';
import { UnknownAuthUser } from '@shared/business/entities/authUser/AuthUser';
import { UserCaseNote } from '../../../../../shared/src/business/entities/notes/UserCaseNote';
import { getUserCaseNoteForCases } from '@web-api/persistence/postgres/userCaseNotes/getUserCaseNoteForCases';

export const getUserCaseNoteForCasesInteractor = async (
applicationContext,
Expand All @@ -21,13 +21,10 @@ export const getUserCaseNoteForCasesInteractor = async (
userIdMakingRequest: authorizedUser.userId,
});

const caseNotes = await applicationContext
.getPersistenceGateway()
.getUserCaseNoteForCases({
applicationContext,
docketNumbers,
userId,
});
const caseNotes = await getUserCaseNoteForCases({
docketNumbers,
userId,
});

return caseNotes.map(note => new UserCaseNote(note).validate().toRawObject());
return caseNotes.map(note => note.validate().toRawObject());
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import '@web-api/persistence/postgres/userCaseNotes/mocks.jest';
import { MOCK_CASE } from '../../../../../shared/src/test/mockCase';
import { UnauthorizedError } from '@web-api/errors/errors';
import { UnknownAuthUser } from '@shared/business/entities/authUser/AuthUser';
import { User } from '../../../../../shared/src/business/entities/User';
import { applicationContext } from '../../../../../shared/src/business/test/createTestApplicationContext';
import { getUserCaseNoteInteractor } from './getUserCaseNoteInteractor';
import { getUserCaseNote as getUserCaseNoteMock } from '@web-api/persistence/postgres/userCaseNotes/getUserCaseNote';
import { mockJudgeUser } from '@shared/test/mockAuthUsers';
import { omit } from 'lodash';

Expand All @@ -19,13 +21,13 @@ describe('Get case note', () => {
userId: 'unauthorizedUser',
} as unknown as UnknownAuthUser;

const getUserCaseNote = getUserCaseNoteMock as jest.Mock;

it('throws error if user is unauthorized', async () => {
applicationContext
.getPersistenceGateway()
.getUserById.mockImplementation(() => new User(mockUnauthorizedUser));
applicationContext
.getPersistenceGateway()
.getUserCaseNote.mockReturnValue({});
getUserCaseNote.mockReturnValue({});
applicationContext
.getUseCaseHelpers()
.getJudgeInSectionHelper.mockReturnValue(null);
Expand All @@ -45,9 +47,7 @@ describe('Get case note', () => {
applicationContext
.getPersistenceGateway()
.getUserById.mockImplementation(() => new User(mockJudgeUser));
applicationContext
.getPersistenceGateway()
.getUserCaseNote.mockResolvedValue(omit(MOCK_NOTE, 'userId'));
getUserCaseNote.mockResolvedValue(omit(MOCK_NOTE, 'userId'));
applicationContext
.getUseCaseHelpers()
.getJudgeInSectionHelper.mockReturnValue(mockJudgeUser);
Expand All @@ -67,9 +67,7 @@ describe('Get case note', () => {
applicationContext
.getPersistenceGateway()
.getUserById.mockImplementation(() => new User(mockJudgeUser));
applicationContext
.getPersistenceGateway()
.getUserCaseNote.mockResolvedValue(MOCK_NOTE);
getUserCaseNote.mockResolvedValue(MOCK_NOTE);
applicationContext
.getUseCaseHelpers()
.getJudgeInSectionHelper.mockReturnValue(mockJudgeUser);
Expand All @@ -89,9 +87,7 @@ describe('Get case note', () => {
applicationContext
.getPersistenceGateway()
.getUserById.mockImplementation(() => new User(mockJudgeUser));
applicationContext
.getPersistenceGateway()
.getUserCaseNote.mockResolvedValue(MOCK_NOTE);
getUserCaseNote.mockResolvedValue(MOCK_NOTE);
applicationContext
.getUseCaseHelpers()
.getJudgeInSectionHelper.mockReturnValue(null);
Expand All @@ -114,9 +110,7 @@ describe('Get case note', () => {
applicationContext
.getPersistenceGateway()
.getUserById.mockImplementation(() => new User(mockJudgeUser));
applicationContext
.getPersistenceGateway()
.getUserCaseNote.mockReturnValue(null);
getUserCaseNote.mockReturnValue(null);
applicationContext
.getUseCaseHelpers()
.getJudgeInSectionHelper.mockReturnValue(mockJudgeUser);
Expand Down
Loading
Loading