-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #720 from ministryofjustice/BICAWS7-3068-extract-o…
…ut-functions Refactor out functions for the OffenceMatching component
- Loading branch information
Showing
16 changed files
with
279 additions
and
146 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { AnnotatedHearingOutcome } from "@moj-bichard7-developers/bichard7-next-core/core/types/AnnotatedHearingOutcome" | ||
import type { DisplayFullCourtCase } from "types/display/CourtCases" | ||
import getExceptionMessage from "./getExceptionMessage" | ||
|
||
describe("getExceptionMessage", () => { | ||
let courtCase: DisplayFullCourtCase | ||
|
||
beforeEach(() => { | ||
courtCase = { | ||
asn: "", | ||
courtName: "", | ||
errorId: 0, | ||
errorStatus: "Unresolved", | ||
errorReport: "", | ||
isUrgent: false, | ||
ptiurn: "", | ||
triggerCount: 0, | ||
defendantName: "", | ||
orgForPoliceFilter: null, | ||
courtCode: null, | ||
courtReference: "", | ||
notes: [], | ||
canUserEditExceptions: false, | ||
triggers: [], | ||
resolutionTimestamp: null, | ||
aho: {} as AnnotatedHearingOutcome, | ||
updatedHearingOutcome: {} as AnnotatedHearingOutcome | ||
} | ||
}) | ||
|
||
it("returns undefined if there's no exceptions in the aho", () => { | ||
const result = getExceptionMessage(courtCase, 0) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it("returns the exception message if there is exceptions in the aho and it's not present ErrorMessages file", () => { | ||
courtCase.aho = { | ||
Exceptions: [{ code: "HO100310", path: ["path", "to", 0, "offence"] }] | ||
} as AnnotatedHearingOutcome | ||
|
||
const result = getExceptionMessage(courtCase, 0) | ||
expect(result).toBe("Multiple court Offences with different Results match a PNC offence") | ||
}) | ||
|
||
it("returns the exception message if there is exceptions in the aho and it's present ErrorMessages file", () => { | ||
courtCase.aho = { | ||
Exceptions: [{ code: "HO100507", path: ["path", "to", 0, "offence"] }] | ||
} as AnnotatedHearingOutcome | ||
|
||
const result = getExceptionMessage(courtCase, 0) | ||
expect(result).toBe( | ||
"Offences have been added in court to a Penalty case. This needs to be manually resolved on the PNC to deal with error, and then manually resolved in Bichard." | ||
) | ||
}) | ||
|
||
it("returns undefined if the exception is resolved", () => { | ||
courtCase.errorStatus = "Resolved" | ||
courtCase.aho = { | ||
Exceptions: [{ code: "HO100310", path: ["path", "to", 0, "offence"] }] | ||
} as AnnotatedHearingOutcome | ||
|
||
const result = getExceptionMessage(courtCase, 0) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it("returns undefined if the exception is not found", () => { | ||
courtCase.aho = { | ||
Exceptions: [{ code: "HO100110", path: ["path", "to", 0, "offence"] }] | ||
} as AnnotatedHearingOutcome | ||
|
||
const result = getExceptionMessage(courtCase, 0) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it("returns undefined if the exception is not found with the offenceIndex begin changed", () => { | ||
courtCase.aho = { | ||
Exceptions: [{ code: "HO100110", path: ["path", "to", 0, "offence"] }] | ||
} as AnnotatedHearingOutcome | ||
|
||
const result = getExceptionMessage(courtCase, 1) | ||
expect(result).toBeUndefined() | ||
}) | ||
}) |
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 type ExceptionCode from "@moj-bichard7-developers/bichard7-next-data/dist/types/ExceptionCode" | ||
import type { DisplayFullCourtCase } from "types/display/CourtCases" | ||
import { findExceptions } from "types/ErrorMessages" | ||
import getExceptionDefinition from "utils/getExceptionDefinition" | ||
import offenceMatchingExceptions from "./offenceMatchingExceptions" | ||
|
||
type ExceptionCodeAndPath = { | ||
code: ExceptionCode | ||
path: (string | number)[] | ||
} | ||
|
||
const findExceptionByOffenceNumber = (courtCase: DisplayFullCourtCase, offenceIndex: number): ExceptionCodeAndPath[] => | ||
courtCase.aho.Exceptions?.filter((exception) => exception.path.includes(offenceIndex)) | ||
|
||
const getExceptionMessage = (courtCase: DisplayFullCourtCase, offenceIndex: number): string | undefined => { | ||
const exceptionByOffenceNumber: ExceptionCodeAndPath[] = findExceptionByOffenceNumber(courtCase, offenceIndex) | ||
|
||
if (exceptionByOffenceNumber === undefined) { | ||
return undefined | ||
} | ||
|
||
if (courtCase.errorStatus === "Resolved") { | ||
return undefined | ||
} | ||
|
||
return ( | ||
findExceptions( | ||
courtCase, | ||
exceptionByOffenceNumber.length > 0 ? exceptionByOffenceNumber : courtCase.aho.Exceptions, | ||
...offenceMatchingExceptions.noOffencesMatched | ||
) || getExceptionDefinition(exceptionByOffenceNumber[0]?.code)?.shortDescription | ||
) | ||
} | ||
|
||
export default getExceptionMessage |
37 changes: 3 additions & 34 deletions
37
src/utils/offenceMatcher/getOffenceMatchingException.test.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
31 changes: 31 additions & 0 deletions
31
src/utils/offenceMatcher/getOffenceMatchingExceptions.test.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,31 @@ | ||
import ExceptionCode from "@moj-bichard7-developers/bichard7-next-data/dist/types/ExceptionCode" | ||
import type { Exception } from "types/exceptions" | ||
import getOffenceMatchingExceptions from "./getOffenceMatchingExceptions" | ||
|
||
describe("getOffenceMatchingExceptions", () => { | ||
it("returns an empty array when given no exceptions", () => { | ||
const result = getOffenceMatchingExceptions([]) | ||
expect(result).toEqual([]) | ||
}) | ||
|
||
it("returns an empty array when given exception that is not in array of valid values", () => { | ||
const exception: Exception = { code: ExceptionCode.HO100312, path: ["test"] } | ||
const result = getOffenceMatchingExceptions([exception]) | ||
expect(result).toEqual([]) | ||
}) | ||
|
||
it("returns exception when given exception that is in array of valid values", () => { | ||
const exception: Exception = { code: ExceptionCode.HO100310, path: ["test"] } | ||
const result = getOffenceMatchingExceptions([exception]) | ||
expect(result).toEqual([exception]) | ||
}) | ||
|
||
it("returns exception when given multiple exceptions and exception is in array of valid values", () => { | ||
const exceptions: Exception[] = [ | ||
{ code: ExceptionCode.HO100310, path: ["test"] }, | ||
{ code: ExceptionCode.HO100312, path: ["test"] } | ||
] | ||
const result = getOffenceMatchingExceptions(exceptions) | ||
expect(result).toEqual([exceptions[0]]) | ||
}) | ||
}) |
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,7 @@ | ||
import type { Exception } from "types/exceptions" | ||
import offenceMatchingExceptions from "./offenceMatchingExceptions" | ||
|
||
const getOffenceMatchingExceptions = (exceptions: Exception[]): Exception[] => | ||
exceptions.filter((exception) => offenceMatchingExceptions.offenceNotMatched.includes(exception.code)) | ||
|
||
export default getOffenceMatchingExceptions |
Oops, something went wrong.