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

MARXAN-1494-exportId-public-project #1016

Merged
merged 3 commits into from
Apr 27, 2022
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
@@ -0,0 +1,18 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddsExportIdToPublicProject1650984916463
implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE published_projects
ADD COLUMN export_id uuid;
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE published_projects
DROP COLUMN export_id;
`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {
accessDenied,
alreadyPublished,
exportError,
internalError,
notFound,
notPublished,
Expand Down Expand Up @@ -118,6 +119,10 @@ export class PublishProjectController {
throw new BadRequestException('This project was already published.');
case notFound:
throw new NotFoundException();
case exportError:
throw new InternalServerErrorException(
`The export could not be generated.`,
);
case internalError:
throw new InternalServerErrorException();
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export interface CreatePublishedProjectDto {
resources?: Resource[];
company?: Company;
pngData?: string;
exportId?: string;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WebshotConfig } from '@marxan/webshot';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsOptional, IsString, ValidateNested } from 'class-validator';
import { IsArray, IsOptional, IsString, IsUUID, ValidateNested } from 'class-validator';
import { Company, Creator, Resource } from './create-published-project.dto';

export class PublishProjectDto {
Expand Down Expand Up @@ -41,4 +41,11 @@ export class CreatePublishProjectDto extends PublishProjectDto {
@ValidateNested({ each: true })
@Type(() => WebshotConfig)
config!: WebshotConfig;

@ApiProperty()
@IsOptional()
@IsArray()
@IsString({ each: true })
@IsUUID('4', { each: true })
scenarioIds?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export class PublishedProject {
@Column('character varying', { name: 'png_data' })
pngData?: string;

@Column('uuid', { name: 'export_id' })
exportId?: string;

@OneToOne(() => Project)
@JoinColumn({ name: 'id' })
originalProject?: Project;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import { PublishedProjectSerializer } from '@marxan-api/modules/published-projec
import { AccessControlModule } from '@marxan-api/modules/access-control';
import { UsersModule } from '@marxan-api/modules/users/users.module';
import { WebshotModule } from '@marxan/webshot';
import { ScenariosModule } from '../scenarios/scenarios.module';
import { Scenario } from '../scenarios/scenario.api.entity';

@Module({
imports: [
AccessControlModule,
ProjectsModule,
TypeOrmModule.forFeature([PublishedProject]),
TypeOrmModule.forFeature([PublishedProject, Scenario]),
UsersModule,
forwardRef(() => WebshotModule),
ScenariosModule,
],
controllers: [PublishProjectController, PublishedProjectReadController],
providers: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { Either, left, right } from 'fp-ts/Either';
import { PublishedProjectCrudService } from '@marxan-api/modules/published-project/published-project-crud.service';
import { Repository } from 'typeorm';
Expand All @@ -14,6 +14,8 @@ import { WebshotService } from '@marxan/webshot';
import { AppConfig } from '@marxan-api/utils/config.utils';
import { assertDefined } from '@marxan/utils';
import { isLeft, isRight } from 'fp-ts/lib/Either';
import { ProjectsService } from '../projects/projects.service';
import { Scenario } from '../scenarios/scenario.api.entity';

export const notFound = Symbol(`project not found`);
export const accessDenied = Symbol(`not allowed`);
Expand All @@ -24,6 +26,7 @@ export const sameUnderModerationStatus = Symbol(
export const alreadyPublished = Symbol(`this project is public`);
export const notPublished = Symbol(`this project is not public yet`);
export const internalError = Symbol(`internal error`);
export const exportError = Symbol(`error while exporting project`);

export type errors =
| typeof notFound
Expand All @@ -36,7 +39,10 @@ export class PublishedProjectService {
@InjectRepository(Project) private projectRepository: Repository<Project>,
@InjectRepository(PublishedProject)
private publicProjectsRepo: Repository<PublishedProject>,
@InjectRepository(Scenario)
private scenarioRepo: Repository<Scenario>,
private crudService: PublishedProjectCrudService,
private projectService: ProjectsService,
private webshotService: WebshotService,
private readonly acl: ProjectAccessControl,
private readonly usersService: UsersService,
Expand All @@ -46,9 +52,10 @@ export class PublishedProjectService {
id: string,
requestingUserId: string,
projectToPublish: CreatePublishProjectDto,
): Promise<Either<errors | typeof alreadyPublished, true>> {
): Promise<
Either<errors | typeof alreadyPublished | typeof exportError, true>
> {
const project = await this.projectRepository.findOne(id);

if (!project) {
return left(notFound);
}
Expand All @@ -64,6 +71,26 @@ export class PublishedProjectService {
return left(alreadyPublished);
}

const allScenariosInProject = await this.scenarioRepo.find({
select: ['id'],
where: { projectId: id },
});
const scenarioIdsForExport =
projectToPublish.scenarioIds ||
allScenariosInProject.map((scenario: any) => {
return scenario?.id;
});
const exportResult = await this.projectService.requestExport(
hotzevzl marked this conversation as resolved.
Show resolved Hide resolved
id,
requestingUserId,
scenarioIdsForExport,
false,
);

if (isLeft(exportResult)) {
return left(exportError);
}

// @debt If we end up moving the scenario map thumbnail generation
// to the end of the Marxan run process, this part here regarding
// the webshot should be removed and/or adapted to it. It looks
Expand Down Expand Up @@ -103,6 +130,7 @@ export class PublishedProjectService {
id,
...projectWithoutScenario,
pngData: isRight(pngData) ? pngData.right : '',
exportId: exportResult.right.exportId.value,
});

return right(true);
Expand Down