Skip to content

Commit

Permalink
group project Marxan summaries-related endpoints within their own con…
Browse files Browse the repository at this point in the history
…troller and OpenAPI category
  • Loading branch information
hotzevzl committed Oct 9, 2023
1 parent 83e1212 commit 7a7ba14
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 46 deletions.
49 changes: 3 additions & 46 deletions api/apps/api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@ import {
Controller,
Delete,
ForbiddenException,
Get,
Header,
InternalServerErrorException, Param, ParseUUIDPipe,
Get, InternalServerErrorException, Param, ParseUUIDPipe,
Patch,
Post,
Req,
Res, UseGuards
Req, UseGuards
} from '@nestjs/common';

import { projectResource, ProjectResultSingular } from './project.api.entity';
import {
ApiBearerAuth, ApiOkResponse,
ApiOperation,
ApiParam,
ApiProduces,
ApiTags
ApiOperation, ApiTags
} from '@nestjs/swagger';
import { apiGlobalPrefixes } from '@marxan-api/api.config';
import { JwtAuthGuard } from '@marxan-api/guards/jwt-auth.guard';
Expand All @@ -34,15 +28,13 @@ import { ProjectJobsStatusDto } from './dto/project-jobs-status.dto';
import { JobStatusSerializer } from './dto/job-status.serializer';
import { isLeft } from 'fp-ts/Either';
import { asyncJobTag } from '@marxan-api/dto/async-job-tag';
import { Response } from 'express';
import {
ImplementsAcl,
IsMissingAclImplementation,
} from '@marxan-api/decorators/acl.decorator';
import { ScenarioLockResultPlural } from '@marxan-api/modules/access-control/scenarios-acl/locks/dto/scenario.lock.dto';
import { mapAclDomainToHttpError } from '@marxan-api/utils/acl.utils';
import { deleteProjectFailed } from './delete-project/delete-project.command';
import { outputProjectSummaryResource } from './output-project-summaries/output-project-summary.api.entity';

@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
Expand Down Expand Up @@ -144,41 +136,6 @@ export class ProjectsController {
return this.jobsStatusSerializer.serialize(projectId, projectWithScenarios);
}

@ImplementsAcl()
@ApiOperation({
description:
"Returns a zip file containing CSVs with summary information for the execution of all the Project's Scenarios",
})
@ApiParam({ name: 'projectId', description: 'Id of the Project' })
@ApiProduces('application/zip')
@Header('Content-Type', 'application/zip')
@Get(':projectId/output-summary')
async getOutputSummary(
@Param('projectId') projectId: string,
@Req() req: RequestWithAuthenticatedUser,
@Res() response: Response,
) {
const result = await this.projectsService.getOutputSummary(
req.user.id,
projectId,
);

if (isLeft(result)) {
throw mapAclDomainToHttpError(result.left, {
resourceType: outputProjectSummaryResource.name.singular,
projectId,
userId: req.user.id,
});
}

response.set(
'Content-Disposition',
`attachment; filename="output-summary-${projectId}"`,
);

response.send(result.right); // @debt should refactored to use StreameableFile or at least use streams, but doesn't seem to work right away
}

@ImplementsAcl()
@Get(':projectId/editing-locks')
@ApiOperation({ summary: 'Get all locks by project' })
Expand Down
2 changes: 2 additions & 0 deletions api/apps/api/src/modules/projects/projects.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import { ProjectFeaturesController } from './projects.project-features.controlle
import { ProjectCloningController } from './projects.cloning.controller';
import { ProjectPlanningAreaAndGridController } from './projects.planning-area-and-grid.controller';
import { ProjectBLMController } from './projects.blm.controller';
import { ProjectSummariesController } from './projects.summaries.controller';

@Module({
imports: [
Expand Down Expand Up @@ -134,6 +135,7 @@ import { ProjectBLMController } from './projects.blm.controller';
ProjectPlanningAreaAndGridController,
ProjectProtectedAreasController,
ProjectScenarioComparisonController,
ProjectSummariesController,
],
// @ToDo Remove TypeOrmModule after project publish will stop use the ProjectRepository
exports: [ProjectsCrudService, TypeOrmModule, ProjectsService],
Expand Down
71 changes: 71 additions & 0 deletions api/apps/api/src/modules/projects/projects.summaries.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
Controller, Get,
Header, Param, Req,
Res, UseGuards
} from '@nestjs/common';

import {
ApiBearerAuth, ApiOperation,
ApiParam,
ApiProduces,
ApiTags
} from '@nestjs/swagger';
import { apiGlobalPrefixes } from '@marxan-api/api.config';
import { JwtAuthGuard } from '@marxan-api/guards/jwt-auth.guard';

import { RequestWithAuthenticatedUser } from '@marxan-api/app.controller';
import {
ProjectsService
} from './projects.service';
import { isLeft } from 'fp-ts/Either';
import { Response } from 'express';
import {
ImplementsAcl
} from '@marxan-api/decorators/acl.decorator';
import { mapAclDomainToHttpError } from '@marxan-api/utils/acl.utils';
import { outputProjectSummaryResource } from './output-project-summaries/output-project-summary.api.entity';

@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiTags('Project - Marxan summaries')
@Controller(`${apiGlobalPrefixes.v1}/projects`)
export class ProjectSummariesController {
constructor(
private readonly projectsService: ProjectsService,
) {}

@ImplementsAcl()
@ApiOperation({
description:
"Returns a zip file containing CSVs with summary information for the execution of all the Project's Scenarios",
})
@ApiParam({ name: 'projectId', description: 'Id of the Project' })
@ApiProduces('application/zip')
@Header('Content-Type', 'application/zip')
@Get(':projectId/output-summary')
async getOutputSummary(
@Param('projectId') projectId: string,
@Req() req: RequestWithAuthenticatedUser,
@Res() response: Response,
) {
const result = await this.projectsService.getOutputSummary(
req.user.id,
projectId,
);

if (isLeft(result)) {
throw mapAclDomainToHttpError(result.left, {
resourceType: outputProjectSummaryResource.name.singular,
projectId,
userId: req.user.id,
});
}

response.set(
'Content-Disposition',
`attachment; filename="output-summary-${projectId}"`,
);

response.send(result.right); // @debt should refactored to use StreameableFile or at least use streams, but doesn't seem to work right away
}
}

0 comments on commit 7a7ba14

Please sign in to comment.