Skip to content

Commit

Permalink
Refactor getExceptionMessage out of OffenceMatching component and add…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
richrace committed Sep 4, 2024
1 parent cdb8d49 commit 05feb83
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import ErrorPromptMessage from "components/ErrorPromptMessage"
import ExceptionFieldTableRow from "components/ExceptionFieldTableRow"
import { useCourtCase } from "context/CourtCaseContext"
import { useCurrentUser } from "context/CurrentUserContext"
import { findExceptions } from "types/ErrorMessages"
import { Exception } from "types/exceptions"
import getExceptionDefinition from "utils/getExceptionDefinition"
import getExceptionMessage from "utils/offenceMatcher/getExceptionMessage"
import { getOffenceMatchingException } from "utils/offenceMatcher/getOffenceMatchingException"
import isEnabled from "utils/offenceMatcher/isEnabled"
import offenceMatchingExceptions from "utils/offenceMatcher/offenceMatchingExceptions"
Expand All @@ -34,16 +33,7 @@ export const OffenceMatching = ({
const currentUser = useCurrentUser()

const offenceMatchingException = isCaseUnresolved && getOffenceMatchingException(exceptions, offenceIndex)

const findExceptionByOffenceNumber = courtCase.aho.Exceptions.filter((exception) =>
exception.path.includes(offenceIndex)
)
const offenceMatchingExceptionMessage =
findExceptions(
courtCase,
findExceptionByOffenceNumber.length > 0 ? findExceptionByOffenceNumber : courtCase.aho.Exceptions,
...offenceMatchingExceptions.noOffencesMatched
) || getExceptionDefinition(findExceptionByOffenceNumber[0]?.code)?.shortDescription
const offenceMatchingExceptionMessage = getExceptionMessage(courtCase, offenceIndex)

const displayOffenceMatcher =
isEnabled(currentUser) && exceptions.some((e) => offenceMatchingExceptions.offenceNotMatched.includes(e.code))
Expand Down
83 changes: 83 additions & 0 deletions src/utils/offenceMatcher/getExceptionMessage.test.ts
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()
})
})
35 changes: 35 additions & 0 deletions src/utils/offenceMatcher/getExceptionMessage.ts
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

0 comments on commit 05feb83

Please sign in to comment.