Skip to content

Commit

Permalink
Merge pull request #1455 from ustaxcourt/test
Browse files Browse the repository at this point in the history
Merge Test into Staging
  • Loading branch information
mmarcotte authored Jul 7, 2021
2 parents 8f3c749 + 8321147 commit 7acf563
Show file tree
Hide file tree
Showing 16 changed files with 658 additions and 121 deletions.
13 changes: 7 additions & 6 deletions shared/src/business/entities/cases/Case.js
Original file line number Diff line number Diff line change
Expand Up @@ -1201,12 +1201,13 @@ const getPractitionersRepresenting = function (rawCase, petitionerContactId) {
Case.prototype.removeRepresentingFromPractitioners = function (
petitionerContactId,
) {
this.privatePractitioners.forEach(practitioner =>
practitioner.representing.splice(
practitioner.representing.indexOf(petitionerContactId),
1,
),
);
this.privatePractitioners.forEach(practitioner => {
const representingArrayIndex =
practitioner.representing.indexOf(petitionerContactId);
if (representingArrayIndex >= 0) {
practitioner.representing.splice(representingArrayIndex, 1);
}
});
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ describe('removeRepresentingFromPractitioners', () => {
{
representing: ['ggg', '123'],
},
{
representing: ['ggg'],
},
],
},
{
Expand All @@ -28,5 +31,8 @@ describe('removeRepresentingFromPractitioners', () => {
expect(caseToVerify.privatePractitioners[1]).toMatchObject({
representing: ['ggg'],
});
expect(caseToVerify.privatePractitioners[2]).toMatchObject({
representing: ['ggg'],
});
});
});
6 changes: 6 additions & 0 deletions shared/src/business/test/createTestApplicationContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ const {
const {
putWorkItemInOutbox,
} = require('../../persistence/dynamo/workitems/putWorkItemInOutbox');
const {
removeCounselFromRemovedPetitioner,
} = require('../useCaseHelper/caseAssociation/removeCounselFromRemovedPetitioner');
const {
saveWorkItem,
} = require('../../persistence/dynamo/workitems/saveWorkItem');
Expand Down Expand Up @@ -355,6 +358,9 @@ const createTestApplicationContext = ({ user } = {}) => {
createCaseAndAssociations: jest
.fn()
.mockImplementation(createCaseAndAssociations),
removeCounselFromRemovedPetitioner: jest
.fn()
.mockImplementation(removeCounselFromRemovedPetitioner),
updateCaseAndAssociations: jest
.fn()
.mockImplementation(updateCaseAndAssociations),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const {
isAuthorized,
ROLE_PERMISSIONS,
} = require('../../../authorization/authorizationClientService');
const { UnauthorizedError } = require('../../../errors/errors');

/**
* removeCounselFromRemovedPetitioner
*
* @param {object} options the options object
* @param {object} options.applicationContext the applicationContext
* @param {object} options.caseEntity the case entity to modify and return
* @param {string} options.petitionerContactId the contactId of the petitioner being removed from the case
* @returns {Case} the updated case entity
*/
exports.removeCounselFromRemovedPetitioner = async ({
applicationContext,
caseEntity,
petitionerContactId,
}) => {
const authorizedUser = applicationContext.getCurrentUser();

if (
!isAuthorized(authorizedUser, ROLE_PERMISSIONS.QC_PETITION) &&
!isAuthorized(authorizedUser, ROLE_PERMISSIONS.REMOVE_PETITIONER)
) {
throw new UnauthorizedError('Unauthorized');
}

const practitioners =
caseEntity.getPractitionersRepresenting(petitionerContactId);

for (const practitioner of practitioners) {
if (!practitioner.isRepresenting(petitionerContactId)) continue;

if (practitioner.representing.length === 1) {
caseEntity.removePrivatePractitioner(practitioner);

await applicationContext.getPersistenceGateway().deleteUserFromCase({
applicationContext,
docketNumber: caseEntity.docketNumber,
userId: practitioner.userId,
});
} else {
caseEntity.removeRepresentingFromPractitioners(petitionerContactId);
}
}

return caseEntity.validate();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const {
applicationContext,
} = require('../../test/createTestApplicationContext');
const {
CONTACT_TYPES,
PARTY_TYPES,
ROLES,
} = require('../../entities/EntityConstants');
const {
MOCK_PRACTITIONER,
petitionsClerkUser,
} = require('../../../test/mockUsers');
const {
removeCounselFromRemovedPetitioner,
} = require('./removeCounselFromRemovedPetitioner');
const { Case } = require('../../entities/cases/Case');
const { MOCK_CASE } = require('../../../test/mockCase');

describe('removeCounselFromRemovedPetitioner', () => {
const mockContactPrimaryId = MOCK_CASE.petitioners[0].contactId;
const mockContactSecondaryId = 'bff888e0-6070-40ac-a7d0-6b4d88ef4b01';
const mockSecondPractitionerUserId = '5dde0389-6e09-4e2f-a7f4-34e4f2a534a8';
const mockThirdPractitionerUserId = '0bd63272-781f-4cbd-8b7d-7cb649ca255d';

beforeEach(() => {
applicationContext.getCurrentUser.mockReturnValue(petitionsClerkUser);
});

it('throws an unauthorized error if user does not have correct permissions', async () => {
applicationContext.getCurrentUser.mockReturnValue({
role: ROLES.petitioner,
});

await expect(
removeCounselFromRemovedPetitioner({
applicationContext,
caseEntity: new Case(MOCK_CASE, { applicationContext }),
petitionerContactId: mockContactPrimaryId,
}),
).rejects.toThrow('Unauthorized');
});

it('should remove the petitioner from privatePractitioner representing array but not remove them completely from the case if another petitioner is also represented', async () => {
const caseEntity = new Case(
{
...MOCK_CASE,
partyType: PARTY_TYPES.petitionerSpouse,
petitioners: [
MOCK_CASE.petitioners[0],
{
...MOCK_CASE.petitioners[0],
contactId: mockContactSecondaryId,
contactType: CONTACT_TYPES.secondary,
},
],
privatePractitioners: [
{
...MOCK_PRACTITIONER,
representing: [mockContactPrimaryId, mockContactSecondaryId],
},
],
},
{ applicationContext },
);

const updatedCase = await removeCounselFromRemovedPetitioner({
applicationContext,
caseEntity,
petitionerContactId: mockContactSecondaryId,
});

expect(updatedCase.privatePractitioners[0].representing).toEqual([
mockContactPrimaryId,
]);
expect(
applicationContext.getPersistenceGateway().deleteUserFromCase,
).not.toHaveBeenCalled();
});

it('should remove the privatePractitioner from the case privatePractitioner if they are only representing the petitioner being removed', async () => {
const caseEntity = new Case(
{
...MOCK_CASE,
partyType: PARTY_TYPES.petitionerSpouse,
petitioners: [
MOCK_CASE.petitioners[0],
{
...MOCK_CASE.petitioners[0],
contactId: mockContactSecondaryId,
contactType: CONTACT_TYPES.secondary,
},
],
privatePractitioners: [
{
...MOCK_PRACTITIONER,
representing: [mockContactSecondaryId],
userId: mockSecondPractitionerUserId,
},
{
...MOCK_PRACTITIONER,
representing: [mockContactPrimaryId],
userId: mockThirdPractitionerUserId,
},
],
},
{ applicationContext },
);

const updatedCase = await removeCounselFromRemovedPetitioner({
applicationContext,
caseEntity,
petitionerContactId: mockContactSecondaryId,
});

expect(
applicationContext.getPersistenceGateway().deleteUserFromCase.mock
.calls[0][0].userId,
).toEqual(mockSecondPractitionerUserId);
expect(updatedCase.privatePractitioners.length).toEqual(1);
expect(updatedCase.privatePractitioners[0].representing).toEqual([
mockContactPrimaryId,
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.removePetitionerAndUpdateCaptionInteractor = async (
.getPersistenceGateway()
.getCaseByDocketNumber({ applicationContext, docketNumber });

const caseEntity = new Case(caseToUpdate, { applicationContext });
let caseEntity = new Case(caseToUpdate, { applicationContext });

if (caseToUpdate.status === CASE_STATUS_TYPES.new) {
throw new Error(
Expand All @@ -48,23 +48,13 @@ exports.removePetitionerAndUpdateCaptionInteractor = async (
);
}

const practitioners =
caseEntity.getPractitionersRepresenting(petitionerContactId);
for (const practitioner of practitioners) {
if (!practitioner.isRepresenting(petitionerContactId)) continue;

if (practitioner.representing.length === 1) {
caseEntity.removePrivatePractitioner(practitioner);

await applicationContext.getPersistenceGateway().deleteUserFromCase({
applicationContext,
docketNumber,
userId: practitioner.userId,
});
} else if (practitioner.representing.length > 1) {
caseEntity.removeRepresentingFromPractitioners(petitionerContactId);
}
}
caseEntity = await applicationContext
.getUseCaseHelpers()
.removeCounselFromRemovedPetitioner({
applicationContext,
caseEntity,
petitionerContactId,
});

caseEntity.removePetitioner(petitionerContactId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ exports.saveCaseDetailInternalEditInteractor = async (
docketNumber,
});

const originalCaseEntity = new Case(caseRecord, { applicationContext });

const editableFields = {
caseCaption: caseToUpdate.caseCaption,
caseType: caseToUpdate.caseType,
Expand Down Expand Up @@ -87,11 +89,7 @@ exports.saveCaseDetailInternalEditInteractor = async (
});

if (!isEmpty(caseToUpdate.contactPrimary)) {
const caseEntityFromCaseRecord = new Case(caseRecord, {
applicationContext,
});
const primaryContactId =
caseEntityFromCaseRecord.getContactPrimary().contactId;
const primaryContactId = originalCaseEntity.getContactPrimary().contactId;

caseEntityWithFormEdits.updatePetitioner({
...caseToUpdate.contactPrimary,
Expand All @@ -109,6 +107,17 @@ exports.saveCaseDetailInternalEditInteractor = async (
contactId: secondaryContactId,
contactType: CONTACT_TYPES.secondary,
});
} else if (originalCaseEntity.getContactSecondary()) {
const originalSecondaryContactId =
originalCaseEntity.getContactSecondary().contactId;

await applicationContext
.getUseCaseHelpers()
.removeCounselFromRemovedPetitioner({
applicationContext,
caseEntity: caseEntityWithFormEdits,
petitionerContactId: originalSecondaryContactId,
});
}

const caseEntity = new Case(caseEntityWithFormEdits, {
Expand Down
Loading

0 comments on commit 7acf563

Please sign in to comment.