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): Send Indictment Case Defender Info to Robot #16927

Merged
merged 2 commits into from
Nov 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
Original file line number Diff line number Diff line change
Expand Up @@ -178,31 +178,6 @@ export class InternalCaseController {
)
}

@UseGuards(CaseExistsGuard, new CaseTypeGuard(indictmentCases))
@Post(
`case/:caseId/${
messageEndpoint[MessageType.DELIVERY_TO_COURT_INDICTMENT_DEFENDER]
}`,
)
@ApiOkResponse({
type: DeliverResponse,
description: 'Delivers indictment case defender info to court',
})
deliverIndictmentDefenderInfoToCourt(
@Param('caseId') caseId: string,
@CurrentCase() theCase: Case,
@Body() deliverDto: DeliverDto,
): Promise<DeliverResponse> {
this.logger.debug(
`Delivering the indictment defender info for case ${caseId} to court`,
)

return this.internalCaseService.deliverIndictmentDefenderInfoToCourt(
theCase,
deliverDto.user,
)
}

@UseGuards(CaseExistsGuard, new CaseTypeGuard(indictmentCases))
@Post(
`case/:caseId/${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,29 +617,6 @@ export class InternalCaseService {
})
}

async deliverIndictmentDefenderInfoToCourt(
theCase: Case,
user: TUser,
): Promise<DeliverResponse> {
return this.courtService
.updateIndictmentCaseWithDefenderInfo(
user,
theCase.id,
theCase.court?.name,
theCase.courtCaseNumber,
theCase.defendants,
)
.then(() => ({ delivered: true }))
.catch((reason) => {
this.logger.error(
`Failed to update indictment case ${theCase.id} with defender info`,
{ reason },
)

return { delivered: false }
})
}

async deliverIndictmentAssignedRolesToCourt(
theCase: Case,
user: TUser,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
isIndictmentCase,
} from '@island.is/judicial-system/types'

import { Defendant } from '../defendant'
import { EventService } from '../event'
import { RobotLog } from './models/robotLog.model'
import { courtModuleConfig } from './court.config'
Expand Down Expand Up @@ -615,17 +614,17 @@ export class CourtService {
caseId: string,
courtName?: string,
courtCaseNumber?: string,
defendants?: Defendant[],
defendantNationalId?: string,
defenderName?: string,
defenderEmail?: string,
gudjong marked this conversation as resolved.
Show resolved Hide resolved
): Promise<unknown> {
try {
const defendantInfo = defendants?.map((defendant) => ({
nationalId: defendant.nationalId,
defenderName: defendant.defenderName,
defenderEmail: defendant.defenderEmail,
}))

const subject = `${courtName} - ${courtCaseNumber} - verjanda upplýsingar`
const content = JSON.stringify({ defendants: defendantInfo, courtName })
const subject = `${courtName} - ${courtCaseNumber} - verjandi varnaraðila`
const content = JSON.stringify({
nationalId: defendantNationalId,
defenderName,
defenderEmail,
})

return this.sendToRobot(
subject,
Expand All @@ -635,7 +634,7 @@ export class CourtService {
)
} catch (error) {
this.eventService.postErrorEvent(
'Failed to update indictment with defender info',
'Failed to update indictment case with defender info',
{
caseId,
actor: user.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,30 +133,42 @@ export class DefendantService {
theCase: Case,
updatedDefendant: Defendant,
oldDefendant: Defendant,
user: User,
): Promise<void> {
if (!theCase.courtCaseNumber) {
return
}

if (updatedDefendant.isDefenderChoiceConfirmed) {
if (
updatedDefendant.isDefenderChoiceConfirmed &&
!oldDefendant.isDefenderChoiceConfirmed
) {
// Defender choice was just confirmed by the court
const messages: Message[] = [
{
type: MessageType.DELIVERY_TO_COURT_INDICTMENT_DEFENDER,
user,
caseId: theCase.id,
elementId: updatedDefendant.id,
},
]

if (
updatedDefendant.defenderChoice === DefenderChoice.CHOOSE ||
updatedDefendant.defenderChoice === DefenderChoice.DELEGATE
) {
// TODO: Update defender with robot if needed

// Defender was just confirmed by judge
if (!oldDefendant.isDefenderChoiceConfirmed) {
await this.messageService.sendMessagesToQueue([
{
type: MessageType.DEFENDANT_NOTIFICATION,
caseId: theCase.id,
body: { type: DefendantNotificationType.DEFENDER_ASSIGNED },
elementId: updatedDefendant.id,
},
])
messages.push({
type: MessageType.DEFENDANT_NOTIFICATION,
caseId: theCase.id,
body: { type: DefendantNotificationType.DEFENDER_ASSIGNED },
elementId: updatedDefendant.id,
})
}
}

return this.messageService.sendMessagesToQueue(messages)
}
}

Expand Down Expand Up @@ -212,7 +224,7 @@ export class DefendantService {
)
}

async updateRequestCase(
async updateRequestCaseDefendant(
theCase: Case,
defendant: Defendant,
update: UpdateDefendantDto,
Expand All @@ -234,10 +246,11 @@ export class DefendantService {
return updatedDefendant
}

async updateIndictmentCase(
async updateIndictmentCaseDefendant(
theCase: Case,
defendant: Defendant,
update: UpdateDefendantDto,
user: User,
): Promise<Defendant> {
const updatedDefendant = await this.updateDatabaseDefendant(
theCase.id,
Expand All @@ -249,6 +262,7 @@ export class DefendantService {
theCase,
updatedDefendant,
defendant,
user,
)

return updatedDefendant
Expand All @@ -261,9 +275,14 @@ export class DefendantService {
user: User,
): Promise<Defendant> {
if (isIndictmentCase(theCase.type)) {
return this.updateIndictmentCase(theCase, defendant, update)
return this.updateIndictmentCaseDefendant(
theCase,
defendant,
update,
user,
)
} else {
return this.updateRequestCase(theCase, defendant, update, user)
return this.updateRequestCaseDefendant(theCase, defendant, update, user)
}
}

Expand Down Expand Up @@ -429,4 +448,30 @@ export class DefendantService {
return { delivered: false }
})
}

async deliverIndictmentDefenderToCourt(
theCase: Case,
defendant: Defendant,
user: User,
): Promise<DeliverResponse> {
return this.courtService
.updateIndictmentCaseWithDefenderInfo(
user,
theCase.id,
theCase.court?.name,
theCase.courtCaseNumber,
defendant.nationalId,
defendant.defenderName,
defendant.defenderEmail,
)
.then(() => ({ delivered: true }))
.catch((reason) => {
this.logger.error(
`Failed to update defender info for defendant ${defendant.id} of indictment case ${theCase.id}`,
{ reason },
)

return { delivered: false }
})
}
gudjong marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ApiProperty } from '@nestjs/swagger'

import type { User } from '@island.is/judicial-system/types'

export class DeliverDefendantToCourtDto {
export class DeliverDto {
@IsNotEmpty()
@IsObject()
@ApiProperty({ type: Object })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ import {
messageEndpoint,
MessageType,
} from '@island.is/judicial-system/message'
import { indictmentCases } from '@island.is/judicial-system/types'
import {
indictmentCases,
investigationCases,
restrictionCases,
} from '@island.is/judicial-system/types'

import { Case, CaseExistsGuard, CaseTypeGuard, CurrentCase } from '../case'
import { DeliverDefendantToCourtDto } from './dto/deliverDefendantToCourt.dto'
import { DeliverDto } from './dto/deliver.dto'
import { InternalUpdateDefendantDto } from './dto/internalUpdateDefendant.dto'
import { CurrentDefendant } from './guards/defendant.decorator'
import { DefendantExistsGuard } from './guards/defendantExists.guard'
Expand All @@ -38,7 +42,10 @@ export class InternalDefendantController {
@Inject(LOGGER_PROVIDER) private readonly logger: Logger,
) {}

@UseGuards(DefendantExistsGuard)
@UseGuards(
new CaseTypeGuard([...restrictionCases, ...investigationCases]),
DefendantExistsGuard,
)
@Post(
`${messageEndpoint[MessageType.DELIVERY_TO_COURT_DEFENDANT]}/:defendantId`,
)
Expand All @@ -51,7 +58,7 @@ export class InternalDefendantController {
@Param('defendantId') defendantId: string,
@CurrentCase() theCase: Case,
@CurrentDefendant() defendant: Defendant,
@Body() deliverDefendantToCourtDto: DeliverDefendantToCourtDto,
@Body() deliverDefendantToCourtDto: DeliverDto,
): Promise<DeliverResponse> {
this.logger.debug(
`Delivering defendant ${defendantId} of case ${caseId} to court`,
Expand Down Expand Up @@ -85,4 +92,32 @@ export class InternalDefendantController {
updatedDefendantChoice,
)
}

@UseGuards(new CaseTypeGuard(indictmentCases), DefendantExistsGuard)
@Post(
`${
messageEndpoint[MessageType.DELIVERY_TO_COURT_INDICTMENT_DEFENDER]
}/:defendantId`,
)
@ApiOkResponse({
type: DeliverResponse,
description: 'Delivers indictment case defender info to court',
})
deliverIndictmentDefenderToCourt(
@Param('caseId') caseId: string,
@Param('defendantId') defendantId: string,
@CurrentCase() theCase: Case,
@CurrentDefendant() defendant: Defendant,
@Body() deliverDto: DeliverDto,
): Promise<DeliverResponse> {
this.logger.debug(
`Delivering defender info for defendant ${defendantId} of case ${caseId} to court`,
)

return this.defendantService.deliverIndictmentDefenderToCourt(
theCase,
defendant,
deliverDto.user,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,14 @@ describe('DefendantController - Update', () => {
})

if (shouldSendEmail) {
it('should queue message if defender has been confirmed', () => {
it('should queue messages if defender has been confirmed', () => {
expect(mockMessageService.sendMessagesToQueue).toHaveBeenCalledWith([
{
type: MessageType.DELIVERY_TO_COURT_INDICTMENT_DEFENDER,
user,
caseId,
elementId: defendantId,
},
{
type: MessageType.DEFENDANT_NOTIFICATION,
caseId,
Expand Down
Loading
Loading