forked from ustaxcourt/ef-cms
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
10007: Refactor and simplify creating practitioner user
- Loading branch information
1 parent
19ef516
commit 3d9a147
Showing
9 changed files
with
84 additions
and
120 deletions.
There are no files selected for viewing
45 changes: 0 additions & 45 deletions
45
shared/src/business/useCases/practitioners/createPractitionerUserInteractor.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 1 addition & 9 deletions
10
shared/src/proxies/practitioners/createPractitionerUserProxy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, { | ||
|
@@ -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]'; | ||
|
||
|
@@ -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( | ||
|
35 changes: 35 additions & 0 deletions
35
web-api/src/business/useCases/practitioner/createPractitionerUserInteractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = { | ||
|
@@ -24,9 +21,6 @@ describe('createPractitionerUserAction', () => { | |
modules: { | ||
presenter, | ||
}, | ||
props: { | ||
computedDate: '2019-03-01T21:40:46.415Z', | ||
}, | ||
state: { | ||
form: { | ||
confirmEmail: '[email protected]', | ||
|
@@ -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', | ||
|
@@ -56,9 +50,7 @@ describe('createPractitionerUserAction', () => { | |
presenter, | ||
}, | ||
state: { | ||
form: { | ||
user: {}, | ||
}, | ||
form: {}, | ||
}, | ||
}); | ||
|
||
|
@@ -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(() => { | ||
|
@@ -83,13 +74,10 @@ describe('createPractitionerUserAction', () => { | |
presenter, | ||
}, | ||
state: { | ||
form: { | ||
user: {}, | ||
}, | ||
form: {}, | ||
}, | ||
}); | ||
|
||
expect(errorMock).toHaveBeenCalled(); | ||
expect(errorMock).toHaveBeenCalledWith({ | ||
alertError: { | ||
message: 'Please try again.', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters