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

fix(j-s): DB Queries #16045

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
58 changes: 31 additions & 27 deletions apps/judicial-system/backend/src/app/modules/case/case.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { Logger } from '@island.is/logging'
import { LOGGER_PROVIDER } from '@island.is/logging'
import { type ConfigType } from '@island.is/nest/config'

import { formatNationalId } from '@island.is/judicial-system/formatters'
import { normalizeAndFormatNationalId } from '@island.is/judicial-system/formatters'
import {
Message,
MessageService,
Expand Down Expand Up @@ -1355,42 +1355,46 @@ export class CaseService {
caseId: string,
defendant: Defendant,
): Promise<Case[]> {
let whereClause: WhereOptions = {
type: CaseType.INDICTMENT,
id: { [Op.ne]: caseId },
state: [CaseState.RECEIVED],
}
const whereClause: WhereOptions = [
{ isArchived: false },
{ type: CaseType.INDICTMENT },
{ id: { [Op.ne]: caseId } },
{ state: CaseState.RECEIVED },
]

if (defendant.noNationalId) {
whereClause = {
...whereClause,
[Op.and]: [
{ '$defendants.national_id$': defendant.nationalId },
{ '$defendants.name$': defendant.name },
],
}
whereClause.push({
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM defendant
WHERE national_id = '${defendant.nationalId}'
AND name = '${defendant.name}'
)`),
},
})
gudjong marked this conversation as resolved.
Show resolved Hide resolved
} else {
const formattedNationalId = formatNationalId(defendant.nationalId)
whereClause = {
...whereClause,
[Op.or]: [
{ '$defendants.national_id$': defendant.nationalId },
{ '$defendants.national_id$': formattedNationalId },
],
}
const [normalizedNationalId, formattedNationalId] =
normalizeAndFormatNationalId(defendant.nationalId)
gudjong marked this conversation as resolved.
Show resolved Hide resolved

whereClause.push({
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM defendant
WHERE national_id in ('${normalizedNationalId}', '${formattedNationalId}')
)`),
},
})
gudjong marked this conversation as resolved.
Show resolved Hide resolved
}

return this.caseModel.findAll({
include: [
{ model: Institution, as: 'court' },
{ model: Defendant, as: 'defendants' },
{
gudjong marked this conversation as resolved.
Show resolved Hide resolved
model: DateLog,
as: 'dateLogs',
},
],
order: [[{ model: DateLog, as: 'dateLogs' }, 'created', 'DESC']],
attributes: ['id', 'courtCaseNumber', 'type', 'state'],
where: whereClause,
where: { [Op.and]: whereClause },
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatNationalId } from '@island.is/judicial-system/formatters'
import { normalizeAndFormatNationalId } from '@island.is/judicial-system/formatters'
import type { User } from '@island.is/judicial-system/types'
import {
CaseAppealState,
Expand Down Expand Up @@ -330,22 +330,27 @@ const canDefenceUserAccessCase = (theCase: Case, user: User): boolean => {
}
}

const formattedNationalId = formatNationalId(user.nationalId)
const normalizedAndFormattedNationalId = normalizeAndFormatNationalId(
user.nationalId,
)

// Check case defender access
if (isIndictmentCase(theCase.type)) {
if (
!theCase.defendants?.some(
(defendant) =>
defendant.defenderNationalId === user.nationalId ||
defendant.defenderNationalId === formattedNationalId,
defendant.defenderNationalId &&
normalizedAndFormattedNationalId.includes(
defendant.defenderNationalId,
),
)
) {
return false
}
} else {
if (
theCase.defenderNationalId !== user.nationalId &&
theCase.defenderNationalId !== formattedNationalId
!theCase.defenderNationalId ||
!normalizedAndFormattedNationalId.includes(theCase.defenderNationalId)
) {
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Op, Sequelize, WhereOptions } from 'sequelize'

import { ForbiddenException } from '@nestjs/common'

import { formatNationalId } from '@island.is/judicial-system/formatters'
import { normalizeAndFormatNationalId } from '@island.is/judicial-system/formatters'
import type { User } from '@island.is/judicial-system/types'
import {
CaseAppealState,
Expand Down Expand Up @@ -216,7 +216,9 @@ const getPrisonAdminUserCasesQueryFilter = (): WhereOptions => {
}

const getDefenceUserCasesQueryFilter = (user: User): WhereOptions => {
const formattedNationalId = formatNationalId(user.nationalId)
const [normalizedNationalId, formattedNationalId] =
normalizeAndFormatNationalId(user.nationalId)

const options: WhereOptions = [
{ is_archived: false },
{
Expand All @@ -238,6 +240,8 @@ const getDefenceUserCasesQueryFilter = (user: User): WhereOptions => {
{
[Op.and]: [
{ state: CaseState.RECEIVED },
// The following condition will filter out all date logs that are not of type ARRAIGNMENT_DATE
// but that should be ok for request cases since they only have one date log
{ '$dateLogs.date_type$': DateType.ARRAIGNMENT_DATE },
],
},
Expand All @@ -251,7 +255,7 @@ const getDefenceUserCasesQueryFilter = (user: User): WhereOptions => {
],
},
{
defender_national_id: [user.nationalId, formattedNationalId],
defender_national_id: [normalizedNationalId, formattedNationalId],
},
],
},
Expand All @@ -266,10 +270,13 @@ const getDefenceUserCasesQueryFilter = (user: User): WhereOptions => {
],
},
{
'$defendants.defender_national_id$': [
user.nationalId,
formattedNationalId,
],
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM defendant
WHERE defender_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 @@ -452,10 +452,13 @@ describe('getCasesQueryFilter', () => {
],
},
{
'$defendants.defender_national_id$': [
user.nationalId,
user.nationalId,
],
id: {
[Op.in]: Sequelize.literal(`
(SELECT case_id
FROM defendant
WHERE defender_national_id in ('${user.nationalId}', '${user.nationalId}'))
`),
},
gudjong marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type { ConfigType } from '@island.is/nest/config'

import {
formatCaseType,
formatNationalId,
normalizeAndFormatNationalId,
} from '@island.is/judicial-system/formatters'
import {
CaseFileCategory,
Expand Down Expand Up @@ -1202,8 +1202,6 @@ export class InternalCaseService {
// As this is only currently used by the digital mailbox API
// we will only return indictment cases that have a court date
async getIndictmentCases(nationalId: string): Promise<Case[]> {
const formattedNationalId = formatNationalId(nationalId)

return this.caseModel.findAll({
include: [
{ model: Defendant, as: 'defendants' },
Expand All @@ -1220,19 +1218,14 @@ export class InternalCaseService {
attributes: ['id', 'courtCaseNumber', 'type', 'state'],
where: {
type: CaseType.INDICTMENT,
[Op.or]: [
{ '$defendants.national_id$': nationalId },
{ '$defendants.national_id$': formattedNationalId },
],
// The national id could be without a hyphen or with a hyphen so we need to
// search for both
'$defendants.national_id$': normalizeAndFormatNationalId(nationalId),
gudjong marked this conversation as resolved.
Show resolved Hide resolved
},
})
}

async getIndictmentCase(caseId: string, nationalId: string): Promise<Case> {
// The national id could be without a hyphen or with a hyphen so we need to
// search for both
const formattedNationalId = formatNationalId(nationalId)

const caseById = await this.caseModel.findOne({
include: [
{ model: Defendant, as: 'defendants' },
Expand All @@ -1246,10 +1239,9 @@ export class InternalCaseService {
where: {
type: CaseType.INDICTMENT,
id: caseId,
[Op.or]: [
{ '$defendants.national_id$': nationalId },
{ '$defendants.national_id$': formattedNationalId },
],
// The national id could be without a hyphen or with a hyphen so we need to
// search for both
'$defendants.national_id$': normalizeAndFormatNationalId(nationalId),
gudjong marked this conversation as resolved.
Show resolved Hide resolved
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { InjectModel } from '@nestjs/sequelize'
import type { Logger } from '@island.is/logging'
import { LOGGER_PROVIDER } from '@island.is/logging'

import { formatNationalId } from '@island.is/judicial-system/formatters'
import { normalizeAndFormatNationalId } from '@island.is/judicial-system/formatters'
import { MessageService, MessageType } from '@island.is/judicial-system/message'
import type { User as TUser } from '@island.is/judicial-system/types'
import {
Expand Down Expand Up @@ -363,14 +363,10 @@ export class LimitedAccessCaseService {
}

async findDefenderByNationalId(nationalId: string): Promise<User> {
const formattedNationalId = formatNationalId(nationalId)
return this.caseModel
.findOne({
where: {
[Op.or]: [
{ defenderNationalId: formattedNationalId },
{ defenderNationalId: nationalId },
],
defenderNationalId: normalizeAndFormatNationalId(nationalId),
gudjong marked this conversation as resolved.
Show resolved Hide resolved
state: { [Op.not]: CaseState.DELETED },
isArchived: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ describe('LimitedAccessCaseController - Find defender by national id', () => {
it('should look for defender', () => {
expect(mockCaseModel.findOne).toHaveBeenCalledWith({
where: {
[Op.or]: [
{ defenderNationalId: formattedDefenderNationalId },
{ defenderNationalId: defenderNationalId },
],
defenderNationalId: [defenderNationalId, formattedDefenderNationalId],
state: { [Op.not]: CaseState.DELETED },
isArchived: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { InjectModel } from '@nestjs/sequelize'
import type { Logger } from '@island.is/logging'
import { LOGGER_PROVIDER } from '@island.is/logging'

import { formatNationalId } from '@island.is/judicial-system/formatters'
import { normalizeAndFormatNationalId } from '@island.is/judicial-system/formatters'
import {
Message,
MessageService,
Expand Down Expand Up @@ -201,17 +201,12 @@ export class DefendantService {
defendantNationalId: string,
update: UpdateDefendantDto,
): Promise<Defendant> {
const formattedNationalId = formatNationalId(defendantNationalId)

const [numberOfAffectedRows, defendants] = await this.defendantModel.update(
update,
{
where: {
caseId,
[Op.or]: [
{ national_id: formattedNationalId },
{ national_id: defendantNationalId },
],
national_id: normalizeAndFormatNationalId(defendantNationalId),
gudjong marked this conversation as resolved.
Show resolved Hide resolved
},
returning: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ApiProperty } from '@nestjs/swagger'

import {
formatDate,
formatNationalId,
normalizeAndFormatNationalId,
} from '@island.is/judicial-system/formatters'
import { DateType, DefenderChoice } from '@island.is/judicial-system/types'

Expand Down Expand Up @@ -47,13 +47,14 @@ export class SubpoenaResponse {
defendantNationalId: string,
lang?: string,
): SubpoenaResponse {
const formattedNationalId = formatNationalId(defendantNationalId)
const t = getTranslations(lang)

const defendantInfo = internalCase.defendants.find(
(defendant) =>
defendant.nationalId === formattedNationalId ||
defendant.nationalId === defendantNationalId,
defendant.nationalId &&
normalizeAndFormatNationalId(defendantNationalId).includes(
defendant.nationalId,
),
gudjong marked this conversation as resolved.
Show resolved Hide resolved
)

const waivedRight = defendantInfo?.defenderChoice === DefenderChoice.WAIVE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const useCourtArrangements = (

setCourtDate((previous) => ({
...previous,
date: date ? formatDateForServer(date) : null,
date: date ? date.toISOString() : null,
}))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ const SelectConnectedCase: FC<Props> = ({ workingCase, setWorkingCase }) => {
}))
}

const connectedCases = connectedCasesData?.connectedCases?.map((aCase) => ({
label: `${aCase.courtCaseNumber}`,
value: aCase.id,
})) as ConnectedCaseOption[]
const connectedCases = connectedCasesData?.connectedCases
?.filter(
// The filtering is done here rather than on the server side
// as we may allow more relaxed merging later on
(connectedCase) =>
connectedCase.defendants?.length === 1 &&
connectedCase.court?.id === workingCase.court?.id,
)
?.map((connectedCase) => ({
label: `${connectedCase.courtCaseNumber}`,
value: connectedCase.id,
})) as ConnectedCaseOption[]

// For now we only want to allow cases with a single defendant to be able to merge
// in to another case
Expand Down
Loading
Loading