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

chore(j-s): Update subpoena status from digital-mailbox #16645

Merged
merged 12 commits into from
Nov 7, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Controller, Get, Inject, Param, UseGuards } from '@nestjs/common'
import { ApiOkResponse, ApiTags } from '@nestjs/swagger'

import { type Logger, LOGGER_PROVIDER } from '@island.is/logging'

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

import { Case, CaseExistsGuard, CurrentCase } from '../case'
import { Subpoena } from '../subpoena'
import { PoliceService } from './police.service'

@UseGuards(TokenGuard, CaseExistsGuard)
@Controller('api/internal/case/:caseId')
@ApiTags('internal police')
export class InternalPoliceController {
constructor(
private readonly policeService: PoliceService,
@Inject(LOGGER_PROVIDER) private readonly logger: Logger,
) {}

@Get('subpoenaStatus/:subpoenaId')
@ApiOkResponse({
type: Subpoena,
description: 'Gets subpoena status',
})
oddsson marked this conversation as resolved.
Show resolved Hide resolved
getSubpoenaStatus(
@Param('subpoenaId') subpoenaId: string,
@CurrentCase() theCase: Case,
@CurrentHttpUser() user: User,
): Promise<Subpoena> {
this.logger.debug(`Gets subpoena status in case ${theCase.id}`)

return this.policeService.getSubpoenaStatus(subpoenaId, user)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { forwardRef, Module } from '@nestjs/common'

import { AwsS3Module, CaseModule, EventModule, SubpoenaModule } from '../index'
import { InternalPoliceController } from './internalPolice.controller'
import { PoliceController } from './police.controller'
import { PoliceService } from './police.service'

Expand All @@ -13,6 +14,6 @@ import { PoliceService } from './police.service'
],
providers: [PoliceService],
exports: [PoliceService],
controllers: [PoliceController],
controllers: [PoliceController, InternalPoliceController],
})
export class PoliceModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export class PoliceService {
})
}

async getSubpoenaStatus(subpoenaId: string, user: User): Promise<Subpoena> {
async getSubpoenaStatus(subpoenaId: string, user?: User): Promise<Subpoena> {
return this.fetchPoliceDocumentApi(
`${this.xRoadPath}/GetSubpoenaStatus?id=${subpoenaId}`,
)
Expand Down Expand Up @@ -407,8 +407,8 @@ export class PoliceService {
'Failed to get subpoena',
{
subpoenaId,
actor: user.name,
institution: user.institution?.name,
actor: user?.name || 'Digital-mailbox',
institution: user?.institution?.name,
},
reason,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const ApiLocaleQuery = applyDecorators(
@Controller('api')
@ApiTags('cases')
@UseGuards(IdsUserGuard, ScopesGuard)
@Scopes(JudicialSystemScope.lawAndOrder)
// @Scopes(JudicialSystemScope.lawAndOrder)
oddsson marked this conversation as resolved.
Show resolved Hide resolved
export class CaseController {
constructor(
private readonly caseService: CaseService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class CaseService {
lang?: string,
): Promise<CasesResponse[]> {
const response = await this.fetchCases(nationalId)

return CasesResponse.fromInternalCasesResponse(response, lang)
}

Expand All @@ -95,6 +96,13 @@ export class CaseService {
lang?: string,
): Promise<CaseResponse> {
const response = await this.fetchCase(id, nationalId)
const defendant = response.defendants[0]
oddsson marked this conversation as resolved.
Show resolved Hide resolved
const subpoenas = defendant.subpoenas

if (subpoenas && subpoenas[0].subpoenaId) {
await this.fetchServiceStatus(id, subpoenas[0].subpoenaId)
}
oddsson marked this conversation as resolved.
Show resolved Hide resolved

return CaseResponse.fromInternalCaseResponse(response, lang)
}

Expand Down Expand Up @@ -230,6 +238,53 @@ export class CaseService {
}
}

private async fetchServiceStatus(
caseId: string,
subpoenaId: string,
): Promise<InternalCaseResponse> {
try {
console.log()
const res = await fetch(
`${this.config.backendUrl}/api/internal/case/${caseId}/subpoenaStatus/${subpoenaId}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${this.config.secretToken}`,
},
},
)

if (!res.ok) {
if (res.status === 404) {
throw new NotFoundException(`Case ${caseId} not found`)
}

const reason = await res.text()

throw new BadGatewayException(
reason ||
'Unexpected error occurred while fetching serviceStatus by subpoenaID',
)
}

const caseData = await res.json()

return caseData
} catch (reason) {
if (
reason instanceof BadGatewayException ||
reason instanceof NotFoundException
) {
throw reason
}

throw new BadGatewayException(
`Failed to fetch serviceStatus by subpoenaId: ${reason.message}`,
)
}
}
oddsson marked this conversation as resolved.
Show resolved Hide resolved

private async patchDefenseInfo(
defendantNationalId: string,
caseId: string,
Expand Down
Loading