Skip to content

Commit

Permalink
10007: Refactor and simplify creating practitioner user
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelschneiderman committed Mar 12, 2024
1 parent 19ef516 commit 3d9a147
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 120 deletions.

This file was deleted.

16 changes: 6 additions & 10 deletions shared/src/business/utilities/createPractitionerUser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Practitioner } from '../entities/Practitioner';
import { Practitioner, RawPractitioner } from '../entities/Practitioner';
import { ServerApplicationContext } from '@web-api/applicationContext';

/**
* Create a new practitioner
*
* @param {object} providers the providers object
* @param {object} providers.applicationContext the application context
* @param {object} providers.user the user data
* @returns {object} new practitioner user
*/
export const createPractitionerUser = async ({ applicationContext, user }) => {
export const createPractitionerUser = async (
applicationContext: ServerApplicationContext,
{ user }: { user: RawPractitioner },
): Promise<RawPractitioner> => {
const barNumber =
user.barNumber ||
(await applicationContext.barNumberGenerator.createBarNumber({
Expand Down
10 changes: 1 addition & 9 deletions shared/src/proxies/practitioners/createPractitionerUserProxy.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { post } from '../requests';

/**
* createPractitionerUserInteractor
*
* @param {object} applicationContext the application context
* @param {object} providers the providers object
* @param {string} providers.user the user data
* @returns {Promise<object>} the created user data
*/
export const createPractitionerUserInteractor = (
applicationContext,
{ user },
) => {
): Promise<{ barNumber: string }> => {
return post({
applicationContext,
body: { user },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
import { ROLES, SERVICE_INDICATOR_TYPES } from '../../entities/EntityConstants';
import {
ROLES,
SERVICE_INDICATOR_TYPES,
} from '../../../../../shared/src/business/entities/EntityConstants';
import { RawPractitioner } from '@shared/business/entities/Practitioner';
import { UnauthorizedError } from '@web-api/errors/errors';
import { applicationContext } from '../../test/createTestApplicationContext';
import { admissionsClerkUser, petitionerUser } from '@shared/test/mockUsers';
import { applicationContext } from '../../../../../shared/src/business/test/createTestApplicationContext';
import { createPractitionerUserInteractor } from './createPractitionerUserInteractor';

describe('create practitioner user', () => {
const mockUser = {
describe('createPractitionerUserInteractor', () => {
const mockUser: RawPractitioner = {
admissionsDate: '2019-03-01',
admissionsStatus: 'Active',
barNumber: 'AT5678',
birthYear: 2019,
birthYear: '2019',
employer: 'Private',
entityName: 'Practitioner',
firmName: 'GW Law Offices',
firstName: 'bob',
lastName: 'sagot',
name: 'Test Attorney',
originalBarState: 'IL',
practitionerType: 'Attorney',
role: ROLES.privatePractitioner,
serviceIndicator: SERVICE_INDICATOR_TYPES.SI_PAPER,
userId: '07044afe-641b-4d75-a84f-0698870b7650',
} as any;

let testUser;
};

beforeEach(() => {
testUser = {
role: ROLES.admissionsClerk,
userId: 'admissionsclerk',
};

applicationContext.environment.stage = 'local';
applicationContext.getCurrentUser.mockImplementation(() => testUser);
applicationContext.getCurrentUser.mockReturnValue(admissionsClerkUser);
applicationContext
.getPersistenceGateway()
.createOrUpdatePractitionerUser.mockResolvedValue(mockUser);
});

it('creates the practitioner user', async () => {
const user = await createPractitionerUserInteractor(applicationContext, {
user: mockUser,
});
expect(user).not.toBeUndefined();
.createOrUpdatePractitionerUser.mockResolvedValue(({ user }) => user);
});

it('throws unauthorized for a non-internal user', async () => {
testUser = {
role: ROLES.petitioner,
userId: '6a2a8f95-0223-442e-8e55-5f094c6bca15',
};
it('should throw an error when the user is unauthorized to create a practitioner user', async () => {
applicationContext.getCurrentUser.mockReturnValue(petitionerUser);

await expect(
createPractitionerUserInteractor(applicationContext, {
Expand All @@ -55,6 +44,17 @@ describe('create practitioner user', () => {
).rejects.toThrow(UnauthorizedError);
});

it('should return the practitioner`s bar number', async () => {
const { barNumber } = await createPractitionerUserInteractor(
applicationContext,
{
user: mockUser,
},
);

expect(barNumber).toEqual(mockUser.barNumber);
});

it('should set practitioner.pendingEmail to practitioner.email and set practitioner.email to undefined', async () => {
const mockEmail = '[email protected]';

Expand All @@ -68,7 +68,6 @@ describe('create practitioner user', () => {
const mockUserCall =
applicationContext.getPersistenceGateway().createOrUpdatePractitionerUser
.mock.calls[0][0].user;

expect(mockUserCall.email).toBeUndefined();
expect(mockUserCall.pendingEmail).toEqual(mockEmail);
expect(mockUserCall.serviceIndicator).toEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
ROLE_PERMISSIONS,
isAuthorized,
} from '../../../../../shared/src/authorization/authorizationClientService';
import { RawPractitioner } from '../../../../../shared/src/business/entities/Practitioner';
import { ServerApplicationContext } from '@web-api/applicationContext';
import { UnauthorizedError } from '@web-api/errors/errors';
import { createPractitionerUser } from '../../../../../shared/src/business/utilities/createPractitionerUser';

export const createPractitionerUserInteractor = async (
applicationContext: ServerApplicationContext,
{ user }: { user: RawPractitioner },
): Promise<{ barNumber: string }> => {
const requestUser = applicationContext.getCurrentUser();

if (!isAuthorized(requestUser, ROLE_PERMISSIONS.ADD_EDIT_PRACTITIONER_USER)) {
throw new UnauthorizedError('Unauthorized for creating practitioner user');
}

user.pendingEmail = user.email;
user.email = undefined;

const practitioner = await createPractitionerUser(applicationContext, {
user,
});

const createdUser = await applicationContext
.getPersistenceGateway()
.createOrUpdatePractitionerUser({
applicationContext,
user: practitioner,
});

return { barNumber: createdUser.barNumber };
};
4 changes: 3 additions & 1 deletion web-api/src/business/useCases/user/createUserInteractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export const createUserInteractor = async (
user.role === ROLES.inactivePractitioner
) {
userEntity = new Practitioner(
await createPractitionerUser({ applicationContext, user }),
await createPractitionerUser(applicationContext, {
user: user as RawPractitioner,
}),
);
} else {
if (user.barNumber === '') {
Expand Down
2 changes: 1 addition & 1 deletion web-api/src/getUseCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { createCaseInteractor } from '../../shared/src/business/useCases/createC
import { createCourtIssuedOrderPdfFromHtmlInteractor } from '../../shared/src/business/useCases/courtIssuedOrder/createCourtIssuedOrderPdfFromHtmlInteractor';
import { createMessageInteractor } from '../../shared/src/business/useCases/messages/createMessageInteractor';
import { createPractitionerDocumentInteractor } from '../../shared/src/business/useCases/practitioners/createPractitionerDocumentInteractor';
import { createPractitionerUserInteractor } from '../../shared/src/business/useCases/practitioners/createPractitionerUserInteractor';
import { createPractitionerUserInteractor } from './business/useCases/practitioner/createPractitionerUserInteractor';
import { createTrialSessionInteractor } from '../../shared/src/business/useCases/trialSessions/createTrialSessionInteractor';
import { createUserInteractor } from './business/useCases/user/createUserInteractor';
import { deleteCaseDeadlineInteractor } from '../../shared/src/business/useCases/caseDeadline/deleteCaseDeadlineInteractor';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import { presenter } from '../presenter-mock';
import { runAction } from '@web-client/presenter/test.cerebral';

describe('createPractitionerUserAction', () => {
let successMock;
let errorMock;

beforeAll(() => {
successMock = jest.fn();
errorMock = jest.fn();
const successMock = jest.fn();
const errorMock = jest.fn();

beforeEach(() => {
presenter.providers.applicationContext = applicationContext;

presenter.providers.path = {
Expand All @@ -24,9 +21,6 @@ describe('createPractitionerUserAction', () => {
modules: {
presenter,
},
props: {
computedDate: '2019-03-01T21:40:46.415Z',
},
state: {
form: {
confirmEmail: '[email protected]',
Expand All @@ -42,7 +36,7 @@ describe('createPractitionerUserAction', () => {
});
});

it('should return path.success with a success message and practitioner information when the practitioner user was successfully created', async () => {
it('should return path.success with a success message and practitioner bar number when the practitioner user was successfully created', async () => {
const mockPractitioner = {
barNumber: 'AB1234',
name: 'Donna Harking',
Expand All @@ -56,9 +50,7 @@ describe('createPractitionerUserAction', () => {
presenter,
},
state: {
form: {
user: {},
},
form: {},
},
});

Expand All @@ -67,11 +59,10 @@ describe('createPractitionerUserAction', () => {
message: 'Practitioner added.',
},
barNumber: mockPractitioner.barNumber,
practitionerUser: mockPractitioner,
});
});

it('should return path.error when the practitioner to create is invalid', async () => {
it('should return path.error when an error occurred while creating the practitioner', async () => {
applicationContext
.getUseCases()
.createPractitionerUserInteractor.mockImplementation(() => {
Expand All @@ -83,13 +74,10 @@ describe('createPractitionerUserAction', () => {
presenter,
},
state: {
form: {
user: {},
},
form: {},
},
});

expect(errorMock).toHaveBeenCalled();
expect(errorMock).toHaveBeenCalledWith({
alertError: {
message: 'Please try again.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ export const createPractitionerUserAction = async ({
}: ActionProps) => {
const practitioner = get(state.form);

practitioner.confirmEmail = undefined;

try {
const practitionerUser = await applicationContext
const { barNumber } = await applicationContext
.getUseCases()
.createPractitionerUserInteractor(applicationContext, {
user: practitioner,
user: { ...practitioner, confirmEmail: undefined },
});

return path.success({
alertSuccess: {
message: 'Practitioner added.',
},
barNumber: practitionerUser.barNumber,
practitionerUser,
barNumber,
});
} catch (err) {
return path.error({
Expand Down

0 comments on commit 3d9a147

Please sign in to comment.