Skip to content

Commit

Permalink
feat(api): projects: job-status api serializer (#255)
Browse files Browse the repository at this point in the history
* feat(api): projects: job-status api serializer

* Update api/apps/api/src/modules/projects/projects.service.ts

Co-authored-by: Dyostiq <15174395+Dyostiq@users.noreply.github.com>
  • Loading branch information
kgajowy and Dyostiq committed Jun 16, 2021
1 parent 72742e0 commit ee3c2f1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 32 deletions.
25 changes: 25 additions & 0 deletions api/apps/api/src/modules/projects/dto/job-status.serializer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common';
import { Scenario } from '../job-status';
import { ProjectJobsStatusDto } from './project-jobs-status.dto';

@Injectable()
export class JobStatusSerializer {
serialize(
projectId: string,
scenarioWithJobs: Scenario[],
): ProjectJobsStatusDto {
return {
data: {
type: 'project-jobs',
id: projectId,
attributes: {
scenarios: scenarioWithJobs.map((scenario) => ({
status: scenario.status,
id: scenario.scenarioId,
jobs: scenario.jobs,
})),
},
},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class JSONAPIProjectJobStatusData {
@ApiProperty({
type: String,
})
type = 'project-jobs';
type: 'project-jobs' = 'project-jobs';

@ApiProperty()
id!: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { ApiEventsService } from '@marxan-api/modules/api-events/api-events.service';
import { ProjectsService } from '@marxan-api/modules/projects/projects.service';
import { ScenariosService } from '@marxan-api/modules/scenarios/scenarios.service';
import { JobStatus as Status } from '@marxan-api/modules/scenarios/scenario.api.entity';
import { JobType } from './jobs.enum';

export { Status };

export interface Job {
kind: JobType;
status: Job;
status: Status;
}

export interface Scenario {
Expand All @@ -22,21 +22,16 @@ export class JobStatusService {
constructor(
private readonly apiEvents: ApiEventsService,
private readonly scenariosService: ScenariosService,
private readonly projectsService: ProjectsService,
) {}

/**
* @throws NotFoundException
*/
async getJobStatusFor(projectId: string): Promise<Scenario[]> {
await this.projectsService.findOne(projectId);
async getJobStatusFor(_projectId: string): Promise<Scenario[]> {
return [];
// get status of project job(s) ?

// get all scenarios for given project

// for each scenario, find its jobs and relevant statuses

/**
*
* Draft of SQL
Expand Down
27 changes: 4 additions & 23 deletions api/apps/api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Delete,
Get,
NotFoundException,
NotImplementedException,
Param,
Patch,
Post,
Expand Down Expand Up @@ -54,6 +53,7 @@ import { ProjectSerializer } from './dto/project.serializer';
import { ProjectJobsStatusDto } from './dto/project-jobs-status.dto';
import { JobStatus } from '@marxan-api/modules/scenarios/scenario.api.entity';
import { JobType } from './job-status/jobs.enum';
import { JobStatusSerializer } from './dto/job-status.serializer';

@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
Expand All @@ -64,6 +64,7 @@ export class ProjectsController {
private readonly projectsService: ProjectsService,
private readonly geoFeatureSerializer: GeoFeatureSerializer,
private readonly projectSerializer: ProjectSerializer,
private readonly jobsStatusSerizalizer: JobStatusSerializer,
) {}

@ApiOperation({
Expand Down Expand Up @@ -184,28 +185,8 @@ export class ProjectsController {
async getJobsForProjectScenarios(
@Param('id') projectId: string,
): Promise<ProjectJobsStatusDto> {
// TODO add JobStatus DTO
// TODO add serializer
return {
data: {
id: projectId,
type: 'project-jobs',
attributes: {
scenarios: [
{
id: projectId,
status: JobStatus.running,
jobs: [
{
kind: JobType.CostSurface,
status: JobStatus.running,
},
],
},
],
},
},
};
const scenarios = await this.projectsService.getJobStatusFor(projectId);
return this.jobsStatusSerizalizer.serialize(projectId, scenarios);
}

@ApiConsumesShapefile(false)
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 @@ -15,6 +15,7 @@ import { ProtectedAreasModule } from './protected-areas/protected-areas.module';
import { ProjectsService } from './projects.service';
import { GeoFeatureSerializer } from './dto/geo-feature.serializer';
import { ProjectSerializer } from './dto/project.serializer';
import { JobStatusSerializer } from './dto/job-status.serializer';
import { BboxResolver } from './bbox/bbox-resolver';
import { JobStatusService } from './job-status/job-status.service';

Expand All @@ -37,6 +38,7 @@ import { JobStatusService } from './job-status/job-status.service';
ProjectSerializer,
BboxResolver,
JobStatusService,
JobStatusSerializer,
],
controllers: [ProjectsController],
exports: [ProjectsCrudService],
Expand Down
7 changes: 7 additions & 0 deletions api/apps/api/src/modules/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AppInfoDTO } from '@marxan-api/dto/info.dto';
import { GeoFeaturesService } from '@marxan-api/modules/geo-features/geo-features.service';

import { ProjectsCrudService } from './projects-crud.service';
import { JobStatusService } from './job-status';
import { ProtectedAreasFacade } from './protected-areas/protected-areas.facade';
import { Project } from './project.api.entity';
import { CreateProjectDTO } from './dto/create.project.dto';
Expand All @@ -16,6 +17,7 @@ export class ProjectsService {
private readonly geoCrud: GeoFeaturesService,
private readonly projectsCrud: ProjectsCrudService,
private readonly protectedAreaShapefile: ProtectedAreasFacade,
private readonly jobStatusService: JobStatusService,
) {}

async findAllGeoFeatures(
Expand Down Expand Up @@ -68,6 +70,11 @@ export class ProjectsService {
return;
}

async getJobStatusFor(projectId: string) {
await this.projectsCrud.getById(projectId);
return await this.jobStatusService.getJobStatusFor(projectId);
}

async importLegacyProject(_: Express.Multer.File) {
return new Project();
}
Expand Down

0 comments on commit ee3c2f1

Please sign in to comment.