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

feat(j-s): Civil Claimant Spokesperson Access #16389

Merged
merged 20 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions apps/judicial-system/api/src/app/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,16 @@ export class AuthController {
? PRISON_CASES_ROUTE
: CASES_ROUTE,
}
} else {
const defender = await this.authService.findDefender(authUser.nationalId)

if (defender) {
return {
userId: defender.id,
userNationalId: defender.nationalId,
jwtToken: this.sharedAuthService.signJwt(defender, csrfToken),
redirectRoute: requestedRedirectRoute ?? DEFENDER_CASES_ROUTE,
}
}

const defender = await this.authService.findDefender(authUser.nationalId)

if (defender) {
return {
userId: defender.id,
userNationalId: defender.nationalId,
jwtToken: this.sharedAuthService.signJwt(defender, csrfToken),
redirectRoute: requestedRedirectRoute ?? DEFENDER_CASES_ROUTE,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { normalizeAndFormatNationalId } from '@island.is/judicial-system/formatters'
import type { User } from '@island.is/judicial-system/types'
import {
CaseAppealState,
CaseDecision,
Expand All @@ -20,16 +19,18 @@ import {
isRequestCase,
isRestrictionCase,
RequestSharedWithDefender,
type User,
UserRole,
} from '@island.is/judicial-system/types'

import { CivilClaimant, Defendant } from '../../defendant'
import { Case } from '../models/case.model'
import { DateLog } from '../models/dateLog.model'

const canProsecutionUserAccessCase = (
theCase: Case,
user: User,
forUpdate = true,
forUpdate: boolean,
): boolean => {
// Check case type access
if (user.role !== UserRole.PROSECUTOR && !isIndictmentCase(theCase.type)) {
Expand Down Expand Up @@ -196,7 +197,7 @@ const canAppealsCourtUserAccessCase = (theCase: Case): boolean => {

const canPrisonStaffUserAccessCase = (
theCase: Case,
forUpdate = true,
forUpdate: boolean,
): boolean => {
// Prison staff users cannot update cases
if (forUpdate) {
Expand Down Expand Up @@ -234,7 +235,7 @@ const canPrisonStaffUserAccessCase = (

const canPrisonAdminUserAccessCase = (
theCase: Case,
forUpdate = true,
forUpdate: boolean,
): boolean => {
// Prison admin users cannot update cases
if (forUpdate) {
Expand Down Expand Up @@ -306,29 +307,27 @@ const canPrisonAdminUserAccessCase = (
return true
}

const canDefenceUserAccessCase = (theCase: Case, user: User): boolean => {
const canDefenceUserAccessRequestCase = (
theCase: Case,
user: User,
): boolean => {
// Check case state access
if (
![
CaseState.SUBMITTED,
CaseState.WAITING_FOR_CANCELLATION,
CaseState.RECEIVED,
CaseState.ACCEPTED,
CaseState.REJECTED,
CaseState.DISMISSED,
CaseState.COMPLETED,
].includes(theCase.state)
) {
return false
}

const arraignmentDate = DateLog.arraignmentDate(theCase.dateLogs)

// Check submitted case access
const canDefenderAccessSubmittedCase =
isRequestCase(theCase.type) &&
theCase.requestSharedWithDefender ===
RequestSharedWithDefender.READY_FOR_COURT
RequestSharedWithDefender.READY_FOR_COURT

if (
theCase.state === CaseState.SUBMITTED &&
Expand All @@ -338,50 +337,94 @@ const canDefenceUserAccessCase = (theCase: Case, user: User): boolean => {
}

// Check received case access
if (theCase.state === CaseState.RECEIVED) {
const canDefenderAccessReceivedCase =
isIndictmentCase(theCase.type) ||
canDefenderAccessSubmittedCase ||
Boolean(arraignmentDate)
const canDefenderAccessReceivedCase =
canDefenderAccessSubmittedCase ||
Boolean(DateLog.arraignmentDate(theCase.dateLogs))

if (!canDefenderAccessReceivedCase) {
return false
}
if (theCase.state === CaseState.RECEIVED && !canDefenderAccessReceivedCase) {
return false
}

const normalizedAndFormattedNationalId = normalizeAndFormatNationalId(
user.nationalId,
)

// Check case defender access
// Check case defender assignment
if (
theCase.defenderNationalId &&
normalizedAndFormattedNationalId.includes(theCase.defenderNationalId)
) {
return true
}

return false
}

const canDefenceUserAccessIndictmentCase = (
theCase: Case,
user: User,
forUpdate: boolean,
): boolean => {
// Check case state access
if (
![
CaseState.WAITING_FOR_CANCELLATION,
CaseState.RECEIVED,
CaseState.COMPLETED,
].includes(theCase.state)
) {
return false
}

// Check received case access
const canDefenderAccessReceivedCase = Boolean(
DateLog.arraignmentDate(theCase.dateLogs),
)

if (theCase.state === CaseState.RECEIVED && !canDefenderAccessReceivedCase) {
return false
}

// Check case defender assignment
if (Defendant.isDefenderOfDefendant(user.nationalId, theCase.defendants)) {
return true
}

// Check case spokesperson assignment
if (
CivilClaimant.isSpokespersonOfCivilClaimant(
user.nationalId,
theCase.civilClaimants,
) &&
!forUpdate
) {
return true
}

return false
}

const canDefenceUserAccessCase = (
theCase: Case,
user: User,
forUpdate: boolean,
): boolean => {
if (isRequestCase(theCase.type)) {
gudjong marked this conversation as resolved.
Show resolved Hide resolved
return canDefenceUserAccessRequestCase(theCase, user)
}

if (isIndictmentCase(theCase.type)) {
if (
!theCase.defendants?.some(
(defendant) =>
defendant.defenderNationalId &&
normalizedAndFormattedNationalId.includes(
defendant.defenderNationalId,
),
)
) {
return false
}
} else {
if (
!theCase.defenderNationalId ||
!normalizedAndFormattedNationalId.includes(theCase.defenderNationalId)
) {
return false
}
return canDefenceUserAccessIndictmentCase(theCase, user, forUpdate)
}

return true
// Other cases are not accessible to defence users
return false
gudjong marked this conversation as resolved.
Show resolved Hide resolved
}

export const canUserAccessCase = (
theCase: Case,
user: User,
forUpdate = true,
forUpdate: boolean,
): boolean => {
if (isProsecutionUser(user)) {
return canProsecutionUserAccessCase(theCase, user, forUpdate)
Expand All @@ -404,7 +447,7 @@ export const canUserAccessCase = (
}

if (isDefenceUser(user)) {
return canDefenceUserAccessCase(theCase, user)
return canDefenceUserAccessCase(theCase, user, forUpdate)
}

if (isPublicProsecutorUser(user)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,26 @@ const getDefenceUserCasesQueryFilter = (user: User): WhereOptions => {
],
},
{
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM defendant
WHERE defender_national_id in ('${normalizedNationalId}', '${formattedNationalId}'))
`),
},
[Op.or]: [
{
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM defendant
WHERE defender_national_id in ('${normalizedNationalId}', '${formattedNationalId}'))
`),
},
},
{
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM civil_claimant
WHERE has_spokesperson = true AND spokesperson_national_id in ('${normalizedNationalId}', '${formattedNationalId}'))
`),
},
},
],
gudjong marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,26 @@ describe('getCasesQueryFilter', () => {
],
},
{
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM defendant
WHERE defender_national_id in ('${user.nationalId}', '${user.nationalId}'))
`),
},
[Op.or]: [
{
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM defendant
WHERE defender_national_id in ('${user.nationalId}', '${user.nationalId}'))
`),
},
},
{
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM civil_claimant
WHERE has_spokesperson = true AND spokesperson_national_id in ('${user.nationalId}', '${user.nationalId}'))
`),
},
},
],
},
],
},
Expand Down
Loading
Loading