Skip to content

Commit

Permalink
Merge pull request #5469 from flexion/devex-custom-case-no-appcontext
Browse files Browse the repository at this point in the history
No App Context Experiment
  • Loading branch information
jimlerza authored Nov 13, 2024
2 parents 39a503b + 751f98b commit d95d05f
Show file tree
Hide file tree
Showing 30 changed files with 362 additions and 334 deletions.
1 change: 0 additions & 1 deletion shared/src/business/test/createTestApplicationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ export const createTestApplicationContext = () => {
getCaseDeadlinesByDocketNumber: jest
.fn()
.mockImplementation(getCaseDeadlinesByDocketNumber),
getCasesByFilters: jest.fn(),
getConfigurationItemValue: jest
.fn()
.mockImplementation(getConfigurationItemValue),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CustomCaseReportCsvRequest } from '@web-api/business/useCases/customCaseReport/createCsvCustomCaseReportFileInteractor';
import { applicationContext } from '@web-client/applicationContext';
import { post } from '../requests';

export const createCsvCustomCaseReportFileInteractor = (
applicationContext,
data: CustomCaseReportCsvRequest,
) => {
): Promise<void> => {
return post({
applicationContext,
body: data,
Expand Down
2 changes: 1 addition & 1 deletion shared/src/proxies/reports/getCustomCaseReportProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {
GetCustomCaseReportRequest,
GetCustomCaseReportResponse,
} from '@web-api/business/useCases/caseInventoryReport/getCustomCaseReportInteractor';
import { applicationContext } from '@web-client/applicationContext';
import { get } from '../requests';

export const getCustomCaseReportInteractor = (
applicationContext,
filters: GetCustomCaseReportRequest,
): Promise<GetCustomCaseReportResponse> => {
return get({
Expand Down
25 changes: 24 additions & 1 deletion shared/src/test/mockFactory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
export const mockFactory = method => {
export const mockFactory = (method: string) => {
return {
[method]: jest.fn(() =>
console.debug(`${method} was not implemented, using default mock`),
),
};
};

export const mockEntireFile = ({
keepImplementation = false,
module,
}: {
module: string;
keepImplementation?: boolean;
}) => {
const originalModule = jest.requireActual(module);

Object.keys(originalModule).forEach(method => {
if (typeof originalModule[method] === 'function') {
const dummyImplementation = () =>
console.debug(`${method} was not implemented, using default mock`);

originalModule[method] = keepImplementation
? jest.fn(originalModule[method])
: jest.fn(dummyImplementation);
}
});

return originalModule;
};
43 changes: 7 additions & 36 deletions web-api/src/applicationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as barNumberGenerator from './persistence/dynamo/users/barNumberGenerat
import * as docketNumberGenerator from './persistence/dynamo/cases/docketNumberGenerator';
import * as pdfLib from 'pdf-lib';
import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf';
import { AwsSigv4Signer } from '@opensearch-project/opensearch/aws-v3';
import {
CASE_STATUS_TYPES,
CLERK_OF_THE_COURT_CONFIGURATION,
Expand All @@ -17,7 +16,6 @@ import {
} from '../../shared/src/business/entities/EntityConstants';
import { Case } from '../../shared/src/business/entities/cases/Case';
import { CaseDeadline } from '../../shared/src/business/entities/CaseDeadline';
import { Client } from '@opensearch-project/opensearch';
import { CognitoIdentityProvider } from '@aws-sdk/client-cognito-identity-provider';
import { Correspondence } from '../../shared/src/business/entities/Correspondence';
import { DocketEntry } from '../../shared/src/business/entities/DocketEntry';
Expand All @@ -34,7 +32,6 @@ import { UserCase } from '../../shared/src/business/entities/UserCase';
import { UserCaseNote } from '../../shared/src/business/entities/notes/UserCaseNote';
import { WorkItem } from '../../shared/src/business/entities/WorkItem';
import { WorkerMessage } from '@web-api/gateways/worker/workerRouter';
import { defaultProvider } from '@aws-sdk/credential-provider-node';
import { environment } from '@web-api/environment';
import { getBatchClient } from '@web-api/persistence/batch/getBatchClient';
import {
Expand All @@ -51,24 +48,22 @@ import { getDynamoClient } from '@web-api/persistence/dynamo/getDynamoClient';
import { getEmailClient } from './persistence/messages/getEmailClient';
import { getEnvironment, getUniqueId } from '../../shared/src/sharedAppContext';
import { getLogger } from '@web-api/utilities/logger/getLogger';
import { getNotificationClient } from '@web-api/notifications/getNotificationClient';
import { getNotificationClient } from '@web-api/notifications/notificationClient/getNotificationClient';
import { getNotificationGateway } from '@web-api/notifications/notificationClient/getNotificationGateway';
import { getNotificationService } from '@web-api/notifications/getNotificationService';
import { getPersistenceGateway } from './getPersistenceGateway';
import { getSearchClient } from '@web-api/persistence/elasticsearch/searchClient/getSearchClient';
import { getStorageClient } from '@web-api/persistence/s3/getStorageClient';
import { getUseCaseHelpers } from './getUseCaseHelpers';
import { getUseCases } from './getUseCases';
import { getUserGateway } from '@web-api/getUserGateway';
import { getUtilities } from './getUtilities';
import { isAuthorized } from '../../shared/src/authorization/authorizationClientService';
import { isCurrentColorActive } from './persistence/dynamo/helpers/isCurrentColorActive';
import { retrySendNotificationToConnections } from './notifications/retrySendNotificationToConnections';
import { saveRequestResponse } from '@web-api/persistence/dynamo/polling/saveRequestResponse';
import { sendBulkTemplatedEmail } from './dispatchers/ses/sendBulkTemplatedEmail';
import { sendEmailEventToQueue } from './persistence/messages/sendEmailEventToQueue';
import { sendEmailToUser } from '@web-api/persistence/messages/sendEmailToUser';
import { sendNotificationOfSealing } from './dispatchers/sns/sendNotificationOfSealing';
import { sendNotificationToConnection } from './notifications/sendNotificationToConnection';
import { sendNotificationToUser } from './notifications/sendNotificationToUser';
import { sendSetTrialSessionCalendarEvent } from './persistence/messages/sendSetTrialSessionCalendarEvent';
import { sendSlackNotification } from './dispatchers/slack/sendSlackNotification';
import { sendZipperBatchJob } from '@web-api/dispatchers/batch/sendZipperBatchJob';
Expand All @@ -79,7 +74,6 @@ import pug from 'pug';
import sass from 'sass';

let sqsCache: SQSClient;
let searchClientCache: Client;

const entitiesByName = {
Case,
Expand Down Expand Up @@ -208,12 +202,7 @@ export const createApplicationContext = (appContextUser = {}) => {
return sass;
},
getNotificationClient,
getNotificationGateway: () => ({
retrySendNotificationToConnections,
saveRequestResponse,
sendNotificationToConnection,
sendNotificationToUser,
}),
getNotificationGateway,
getNotificationService,
getPdfJs: () => {
pdfjsLib.GlobalWorkerOptions.workerSrc = './pdf.worker.js';
Expand All @@ -232,27 +221,7 @@ export const createApplicationContext = (appContextUser = {}) => {
process.env.SCANNER_RESOURCE_URI || 'http://localhost:10000/Resources'
);
},
getSearchClient: () => {
if (!searchClientCache) {
if (environment.stage === 'local') {
searchClientCache = new Client({
node: environment.elasticsearchEndpoint,
});
} else {
searchClientCache = new Client({
...AwsSigv4Signer({
getCredentials: () => {
const credentialsProvider = defaultProvider();
return credentialsProvider();
},
region: 'us-east-1',
}),
node: `https://${environment.elasticsearchEndpoint}:443`,
});
}
}
return searchClientCache;
},
getSearchClient,
getSlackWebhookUrl: () => process.env.SLACK_WEBHOOK_URL,
getStorageClient,
getUniqueId,
Expand All @@ -278,6 +247,8 @@ export const createApplicationContext = (appContextUser = {}) => {
};
};

export const applicationContext = createApplicationContext();

export type ServerApplicationContext = ReturnType<
typeof createApplicationContext
>;
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
jest.mock('@web-api/persistence/elasticsearch/getCasesByFilters');
import {
GetCustomCaseReportRequest,
getCustomCaseReportInteractor,
} from './getCustomCaseReportInteractor';
import { applicationContext } from '../../../../../shared/src/business/test/createTestApplicationContext';
import { getCasesByFilters as getCasesByFiltersMock } from '@web-api/persistence/elasticsearch/getCasesByFilters';
import {
mockDocketClerkUser,
mockPetitionerUser,
} from '@shared/test/mockAuthUsers';

describe('getCustomCaseReportInteractor', () => {
const getCasesByFilters = jest.mocked(getCasesByFiltersMock);
let params: GetCustomCaseReportRequest;
beforeEach(() => {
params = {
Expand All @@ -28,18 +30,13 @@ describe('getCustomCaseReportInteractor', () => {
describe('Validation', () => {
it('throws an error if user is not authorized for case inventory report', async () => {
await expect(
getCustomCaseReportInteractor(
applicationContext,
params,
mockPetitionerUser,
),
getCustomCaseReportInteractor(params, mockPetitionerUser),
).rejects.toThrow('Unauthorized for case inventory report');
});

it('should be valid when no arguments are passed in for getCustomCaseReportInteractor', async () => {
await expect(
getCustomCaseReportInteractor(
applicationContext,
{
caseStatuses: undefined,
caseTypes: undefined,
Expand All @@ -64,34 +61,22 @@ describe('getCustomCaseReportInteractor', () => {
delete params[testCase.missingField];

await expect(
getCustomCaseReportInteractor(
applicationContext,
params,
mockDocketClerkUser,
),
getCustomCaseReportInteractor(params, mockDocketClerkUser),
).rejects.toThrow();
});
});
});

it('should fetch cases from persistence with the user selected filters', async () => {
applicationContext
.getPersistenceGateway()
.getCasesByFilters.mockResolvedValue({
foundCases: [],
totalCount: 0,
});
getCasesByFilters.mockResolvedValue({
foundCases: [],
lastCaseId: { pk: 'case|102-20', receivedAt: 0 },
totalCount: 0,
});

await getCustomCaseReportInteractor(
applicationContext,
params,
mockDocketClerkUser,
);
await getCustomCaseReportInteractor(params, mockDocketClerkUser);

expect(
applicationContext.getPersistenceGateway().getCasesByFilters,
).toHaveBeenCalledWith({
applicationContext: expect.anything(),
expect(getCasesByFilters).toHaveBeenCalledWith({
params,
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
ROLE_PERMISSIONS,
isAuthorized,
} from '@shared/authorization/authorizationClientService';
import { ServerApplicationContext } from '@web-api/applicationContext';
import { UnauthorizedError } from '@web-api/errors/errors';
import { UnknownAuthUser } from '@shared/business/entities/authUser/AuthUser';
import { getCasesByFilters } from '@web-api/persistence/elasticsearch/getCasesByFilters';

export type CustomCaseReportFilters = {
caseStatuses: CaseStatus[];
Expand Down Expand Up @@ -59,7 +59,6 @@ export type CustomCaseReportSearchAfter = {
};

export const getCustomCaseReportInteractor = async (
applicationContext: ServerApplicationContext,
params: GetCustomCaseReportRequest,
authorizedUser: UnknownAuthUser,
): Promise<GetCustomCaseReportResponse> => {
Expand All @@ -78,8 +77,7 @@ export const getCustomCaseReportInteractor = async (

new CustomCaseReportSearch(params).validate();

return await applicationContext.getPersistenceGateway().getCasesByFilters({
applicationContext,
return await getCasesByFilters({
params,
});
};
Loading

0 comments on commit d95d05f

Please sign in to comment.