Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(j-s): Transition Side Effects #16660

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
115 changes: 3 additions & 112 deletions apps/judicial-system/backend/src/app/modules/case/case.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ import {
} from '@island.is/judicial-system/formatters'
import type { User } from '@island.is/judicial-system/types'
import {
CaseAppealDecision,
CaseAppealRulingDecision,
CaseAppealState,
CaseDecision,
CaseState,
CaseTransition,
CaseType,
indictmentCases,
investigationCases,
isIndictmentCase,
isRestrictionCase,
restrictionCases,
UserRole,
Expand Down Expand Up @@ -305,118 +302,12 @@ export class CaseController {
): Promise<Case> {
this.logger.debug(`Transitioning case ${caseId}`)

let update: UpdateCase = transitionCase(
const update: UpdateCase = transitionCase(
transition.transition,
theCase.type,
theCase.state,
theCase.appealState,
theCase,
user,
)

switch (transition.transition) {
case CaseTransition.DELETE:
update.parentCaseId = null
break
case CaseTransition.SUBMIT:
if (isIndictmentCase(theCase.type)) {
update.indictmentDeniedExplanation = null
}
break
case CaseTransition.ACCEPT:
case CaseTransition.REJECT:
case CaseTransition.DISMISS:
case CaseTransition.COMPLETE:
update.rulingDate = isIndictmentCase(theCase.type)
? nowFactory()
: theCase.courtEndTime

// Handle appealed in court
if (
!theCase.appealState && // don't appeal twice
(theCase.prosecutorAppealDecision === CaseAppealDecision.APPEAL ||
theCase.accusedAppealDecision === CaseAppealDecision.APPEAL)
) {
if (theCase.prosecutorAppealDecision === CaseAppealDecision.APPEAL) {
update.prosecutorPostponedAppealDate = nowFactory()
} else {
update.accusedPostponedAppealDate = nowFactory()
}

update = {
...update,
...transitionCase(
CaseTransition.APPEAL,
theCase.type,
update.state ?? theCase.state,
update.appealState ?? theCase.appealState,
),
}
}
break
case CaseTransition.REOPEN:
update.rulingDate = null
update.courtRecordSignatoryId = null
update.courtRecordSignatureDate = null
break
// TODO: Consider changing the names of the postponed appeal date variables
// as they are now also used when the case is appealed in court
case CaseTransition.APPEAL:
// The only roles that can appeal a case here are prosecutor roles
update.prosecutorPostponedAppealDate = nowFactory()
break
case CaseTransition.RECEIVE_APPEAL:
update.appealReceivedByCourtDate = nowFactory()
break
case CaseTransition.COMPLETE_APPEAL:
if (
isRestrictionCase(theCase.type) &&
theCase.state === CaseState.ACCEPTED &&
(theCase.decision === CaseDecision.ACCEPTING ||
theCase.decision === CaseDecision.ACCEPTING_PARTIALLY)
) {
if (
theCase.appealRulingDecision === CaseAppealRulingDecision.CHANGED ||
theCase.appealRulingDecision ===
CaseAppealRulingDecision.CHANGED_SIGNIFICANTLY
) {
// The court of appeals has modified the ruling of a restriction case
update.validToDate = theCase.appealValidToDate
update.isCustodyIsolation = theCase.isAppealCustodyIsolation
update.isolationToDate = theCase.appealIsolationToDate
} else if (
theCase.appealRulingDecision === CaseAppealRulingDecision.REPEAL
) {
// The court of appeals has repealed the ruling of a restriction case
update.validToDate = nowFactory()
}
}
break
case CaseTransition.WITHDRAW_APPEAL:
// We only want to set the appeal ruling decision if the
// case has already been received.
// Otherwise the court of appeals never knew of the appeal in
// the first place so it remains withdrawn without a decision.
if (
!theCase.appealRulingDecision &&
theCase.appealState === CaseAppealState.RECEIVED
) {
update.appealRulingDecision = CaseAppealRulingDecision.DISCONTINUED
}
break
case CaseTransition.ASK_FOR_CONFIRMATION:
update.indictmentReturnedExplanation = null
break
case CaseTransition.RETURN_INDICTMENT:
update.courtCaseNumber = null
update.indictmentHash = null
break
case CaseTransition.ASK_FOR_CANCELLATION:
if (theCase.indictmentDecision) {
throw new ForbiddenException(
`Cannot ask for cancellation of an indictment that is already in progress at the district court`,
)
}
}

const updatedCase = await this.caseService.update(
theCase,
update,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1689,12 +1689,7 @@ export class CaseService {
return this.sequelize
.transaction(async (transaction) => {
if (receivingCase) {
update.state = transitionCase(
CaseTransition.RECEIVE,
theCase.type,
theCase.state,
theCase.appealState,
).state
update = transitionCase(CaseTransition.RECEIVE, theCase, user, update)
}

await this.handleDateUpdates(theCase, update, transaction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ import {
} from '@island.is/judicial-system/auth'
import type { User as TUser } from '@island.is/judicial-system/types'
import {
CaseAppealRulingDecision,
CaseAppealState,
CaseState,
CaseTransition,
CaseType,
indictmentCases,
investigationCases,
Expand Down Expand Up @@ -171,23 +168,10 @@ export class LimitedAccessCaseController {

const update: LimitedAccessUpdateCase = transitionCase(
transition.transition,
theCase.type,
theCase.state,
theCase.appealState,
theCase,
user,
)

if (update.appealState === CaseAppealState.APPEALED) {
update.accusedPostponedAppealDate = nowFactory()
}

if (
transition.transition === CaseTransition.WITHDRAW_APPEAL &&
!theCase.appealRulingDecision &&
theCase.appealState === CaseAppealState.RECEIVED
) {
update.appealRulingDecision = CaseAppealRulingDecision.DISCONTINUED
}

const updatedCase = await this.limitedAccessCaseService.update(
theCase,
update,
Expand Down
Loading
Loading