Skip to content

Commit

Permalink
feat(service-portal): detailed audit logs for law and order (#16321)
Browse files Browse the repository at this point in the history
* feat: detailed audit logs for law and order

* fix: updates after PR review

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
disaerna and kodiakhq[bot] authored Oct 15, 2024
1 parent 5ce0312 commit 2a7cb5b
Showing 1 changed file with 60 additions and 9 deletions.
69 changes: 60 additions & 9 deletions libs/api/domains/law-and-order/src/lib/law-and-order.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
ScopesGuard,
} from '@island.is/auth-nest-tools'
import { ApiScope } from '@island.is/auth/scopes'
import { Audit } from '@island.is/nest/audit'
import { Audit, AuditService } from '@island.is/nest/audit'
import {
FeatureFlag,
FeatureFlagGuard,
Features,
} from '@island.is/nest/feature-flags'
import type { Locale } from '@island.is/shared/types'
import { UseGuards } from '@nestjs/common'
import { Inject, UseGuards } from '@nestjs/common'
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'
import { GetCourtCaseInput } from '../dto/getCourtCaseInput'
import { GetSubpoenaInput } from '../dto/getSubpoenaInput'
Expand All @@ -24,14 +24,21 @@ import { DefenseChoice } from '../models/defenseChoice.model'
import { Lawyers } from '../models/lawyers.model'
import { Subpoena } from '../models/subpoena.model'
import { LawAndOrderService } from './law-and-order.service'
import { LOGGER_PROVIDER, type Logger } from '@island.is/logging'

const LOG_CATEGORY = 'law-and-order-resolver'

@UseGuards(IdsUserGuard, ScopesGuard, FeatureFlagGuard)
@Resolver()
@Audit({ namespace: '@island.is/api/law-and-order' })
@FeatureFlag(Features.servicePortalLawAndOrderModuleEnabled)
@Scopes(ApiScope.lawAndOrder)
export class LawAndOrderResolver {
constructor(private readonly lawAndOrderService: LawAndOrderService) {}
constructor(
private readonly lawAndOrderService: LawAndOrderService,
private readonly auditService: AuditService,
@Inject(LOGGER_PROVIDER) private readonly logger: Logger,
) {}

@Query(() => CourtCases, {
name: 'lawAndOrderCourtCasesList',
Expand All @@ -51,24 +58,34 @@ export class LawAndOrderResolver {
nullable: true,
})
@Audit()
getCourtCaseDetail(
async getCourtCaseDetail(
@CurrentUser() user: User,
@Args('input') input: GetCourtCaseInput,
@Args('locale', { type: () => String, nullable: true })
locale: Locale = 'is',
) {
return this.lawAndOrderService.getCourtCase(user, input.id, locale)
return this.auditAndHandle(
'getCourtCaseDetail',
input.id,
this.lawAndOrderService.getCourtCase(user, input.id, locale),
user,
)
}

@Query(() => Subpoena, { name: 'lawAndOrderSubpoena', nullable: true })
@Audit()
getSubpoena(
async getSubpoena(
@CurrentUser() user: User,
@Args('input') input: GetSubpoenaInput,
@Args('locale', { type: () => String, nullable: true })
locale: Locale = 'is',
) {
return this.lawAndOrderService.getSubpoena(user, input.id, locale)
return this.auditAndHandle(
'getSubpoena',
input.id,
this.lawAndOrderService.getSubpoena(user, input.id, locale),
user,
)
}

@Query(() => Lawyers, { name: 'lawAndOrderLawyers', nullable: true })
Expand All @@ -86,12 +103,46 @@ export class LawAndOrderResolver {
nullable: true,
})
@Audit()
postDefenseChoice(
async postDefenseChoice(
@Args('input') input: PostDefenseChoiceInput,
@Args('locale', { type: () => String, nullable: true })
locale: Locale = 'is',
@CurrentUser() user: User,
) {
return this.lawAndOrderService.postDefenseChoice(user, input, locale)
return this.auditAndHandle(
'postDefenseChoice',
input.caseId,
this.lawAndOrderService.postDefenseChoice(user, input, locale),
user,
{ defenseChoice: input.choice },
)
}

private async auditAndHandle<T>(
actionName: string,
resources: string,
promise: Promise<T>,
user: User,
meta?: any,
): Promise<T> {
try {
return await this.auditService.auditPromise(
{
auth: user,
namespace: '@island.is/api/law-and-order',
action: actionName,
resources: resources,
meta: meta,
},
promise,
)
} catch (e) {
this.logger.error(`Failed to ${actionName}`, {
category: LOG_CATEGORY,
id: resources,
error: e,
})
throw e
}
}
}

0 comments on commit 2a7cb5b

Please sign in to comment.