From 7bd92e8983f8e6cd9c143674c31962dd23030168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0j=C3=B3n=20Gu=C3=B0j=C3=B3nsson?= Date: Fri, 29 Nov 2024 16:44:08 +0000 Subject: [PATCH] Fixes backend endpoints --- .../src/app/modules/file/file.controller.ts | 18 ++---- .../file/guards/writeCaseFile.guard.ts | 55 ------------------- .../createCaseFileGuards.spec.ts | 15 +---- 3 files changed, 6 insertions(+), 82 deletions(-) delete mode 100644 apps/judicial-system/backend/src/app/modules/file/guards/writeCaseFile.guard.ts diff --git a/apps/judicial-system/backend/src/app/modules/file/file.controller.ts b/apps/judicial-system/backend/src/app/modules/file/file.controller.ts index bbd4846faa97..6bca69f1b476 100644 --- a/apps/judicial-system/backend/src/app/modules/file/file.controller.ts +++ b/apps/judicial-system/backend/src/app/modules/file/file.controller.ts @@ -63,7 +63,6 @@ import { UpdateFilesDto } from './dto/updateFile.dto' import { CurrentCaseFile } from './guards/caseFile.decorator' import { CaseFileExistsGuard } from './guards/caseFileExists.guard' import { ViewCaseFileGuard } from './guards/viewCaseFile.guard' -import { WriteCaseFileGuard } from './guards/writeCaseFile.guard' import { DeleteFileResponse } from './models/deleteFile.response' import { CaseFile } from './models/file.model' import { PresignedPost } from './models/presignedPost.model' @@ -107,7 +106,7 @@ export class FileController { return this.fileService.createPresignedPost(theCase, createPresignedPost) } - @UseGuards(RolesGuard, CaseExistsGuard, CaseWriteGuard, WriteCaseFileGuard) + @UseGuards(RolesGuard, CaseExistsGuard, CaseWriteGuard) @RolesRules( prosecutorRule, prosecutorRepresentativeRule, @@ -135,13 +134,7 @@ export class FileController { return this.fileService.createCaseFile(theCase, createFile, user) } - @UseGuards( - RolesGuard, - CaseExistsGuard, - DefendantExistsGuard, - CaseWriteGuard, - WriteCaseFileGuard, - ) + @UseGuards(RolesGuard, CaseExistsGuard, DefendantExistsGuard, CaseWriteGuard) @RolesRules(publicProsecutorStaffRule) @Post('defendant/:defendantId/file') @ApiCreatedResponse({ @@ -168,13 +161,12 @@ export class FileController { CaseExistsGuard, CivilClaimantExistsGuard, CaseWriteGuard, - WriteCaseFileGuard, ) - @RolesRules(publicProsecutorStaffRule) - @Post('defendant/:defendantId/file') + @RolesRules() // This endpoint is not used by any role at the moment + @Post('civilClaimant/:civilClaimantId/file') @ApiCreatedResponse({ type: CaseFile, - description: 'Creates a new case file connected to a defendant', + description: 'Creates a new case file connected to a civil claimant', }) async createCivilClaimantCaseFile( @Param('caseId') caseId: string, diff --git a/apps/judicial-system/backend/src/app/modules/file/guards/writeCaseFile.guard.ts b/apps/judicial-system/backend/src/app/modules/file/guards/writeCaseFile.guard.ts deleted file mode 100644 index da409be6a1fa..000000000000 --- a/apps/judicial-system/backend/src/app/modules/file/guards/writeCaseFile.guard.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { - BadRequestException, - CanActivate, - ExecutionContext, - Injectable, - InternalServerErrorException, -} from '@nestjs/common' - -import { CaseFileCategory, User } from '@island.is/judicial-system/types' - -import { Case } from '../../case' - -@Injectable() -export class WriteCaseFileGuard implements CanActivate { - canActivate(context: ExecutionContext): boolean { - const request = context.switchToHttp().getRequest() - - const user: User = request.user - const theCase: Case = request.case - const defendant = request.defendant - const civilClaimant = request.civilClaimant - - if (!theCase) { - throw new InternalServerErrorException('Missing case') - } - - if (!user) { - throw new InternalServerErrorException('Missing user') - } - - // The case file category is either in the request body (creating case file) - // or in the case file (deleting case file) - const caseFileCategory: CaseFileCategory = - request.body?.category ?? request.caseFile?.category - - if (!caseFileCategory) { - throw new InternalServerErrorException('Missing case file category') - } - - if ( - caseFileCategory === CaseFileCategory.SENT_TO_PRISON_ADMIN_FILE && - !defendant - ) { - throw new BadRequestException('Missing defendant for case file category') - } - - if (caseFileCategory === CaseFileCategory.CIVIL_CLAIM && !civilClaimant) { - throw new BadRequestException( - 'Missing civil claimant for case file category', - ) - } - - return true - } -} diff --git a/apps/judicial-system/backend/src/app/modules/file/test/fileController/createCaseFileGuards.spec.ts b/apps/judicial-system/backend/src/app/modules/file/test/fileController/createCaseFileGuards.spec.ts index 46e9f341c6c5..1823dd2f2884 100644 --- a/apps/judicial-system/backend/src/app/modules/file/test/fileController/createCaseFileGuards.spec.ts +++ b/apps/judicial-system/backend/src/app/modules/file/test/fileController/createCaseFileGuards.spec.ts @@ -4,7 +4,6 @@ import { RolesGuard } from '@island.is/judicial-system/auth' import { CaseExistsGuard, CaseWriteGuard } from '../../../case' import { FileController } from '../../file.controller' -import { WriteCaseFileGuard } from '../../guards/writeCaseFile.guard' describe('FileController - Create case file guards', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -18,7 +17,7 @@ describe('FileController - Create case file guards', () => { }) it('should have three guards', () => { - expect(guards).toHaveLength(4) + expect(guards).toHaveLength(3) }) describe('RolesGuard', () => { @@ -56,16 +55,4 @@ describe('FileController - Create case file guards', () => { expect(guard).toBeInstanceOf(CaseWriteGuard) }) }) - - describe('CaseWriteGuard', () => { - let guard: CanActivate - - beforeEach(() => { - guard = new guards[3]() - }) - - it('should have CaseWriteGuard as guard 4', () => { - expect(guard).toBeInstanceOf(WriteCaseFileGuard) - }) - }) })