Skip to content

Commit

Permalink
chore(api): scenario cost surfaces shell
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed May 13, 2021
1 parent 45333c4 commit 8aa1df6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
20 changes: 20 additions & 0 deletions api/src/decorators/shapefile.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ import {

import { ShapefileGeoJSONResponseDTO } from 'modules/scenarios/dto/shapefile.geojson.response.dto';

export function ApiConsumesShape() {
return applyDecorators(
ApiOperation({
description: 'Upload Zip file containing .shp, .dbj, .prj and .shx files',
}),
ApiConsumes('multipart/form-data'),
ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'Zip file containing .shp, .dbj, .prj and .shx files',
format: 'binary',
},
},
},
}),
);
}

export function ApiConsumesShapefile() {
return applyDecorators(
ApiOperation({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export class UpdateCostSurfaceService implements AdjustCostSurface {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
constraints: CostSurfaceInputDto,
): Promise<Success> {
// TODO 1 validate geoJson formally
// TODO 2 extract pu ids from geoJson
// TODO 3 validate if pu ids are allowed (probably use existing code from planning-units)
// TODO 4 update geoDb

return true;
}
}
40 changes: 40 additions & 0 deletions api/src/modules/scenarios/cost-surface/cost-surface.facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Injectable } from '@nestjs/common';
import { Request, Response } from 'express';
import { AdjustCostSurface } from '../../analysis/entry-points/adjust-cost-surface';
import { ProxyService } from '../../proxy/proxy.service';

@Injectable()
export class CostSurfaceFacade {
constructor(
private readonly adjustCostSurfaceService: AdjustCostSurface,
private readonly proxyService: ProxyService,
) {}

/**
* non blocking - will do job in "background"
*
* @param scenarioId
* @param request
*/
convert(scenarioId: string, request: Request) {
// TODO #0 Generate & Dispatch Api Event (some wrapping service for /dummy/"terminating" if already running)

// TODO #1 Call Proxy Service to get GeoJSON
// this.proxyService.... - modify this to send back data, not act on Response

// TODO #2 Call Analysis-module with scenario id & GeoJson from above
this.adjustCostSurfaceService
.update(scenarioId, {
geo: {
features: [],
type: 'FeatureCollection',
},
})
.then(() => {
// dispatch ApiEvent - Done
})
.catch(() => {
// dispatch ApiEvent - Failed
});
}
}
26 changes: 25 additions & 1 deletion api/src/modules/scenarios/scenarios.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ApiBody,
ApiConsumes,
ApiCreatedResponse,
ApiNoContentResponse,
ApiOkResponse,
ApiOperation,
ApiResponse,
Expand All @@ -55,7 +56,11 @@ import { UpdateScenarioPlanningUnitLockStatusDto } from './dto/update-scenario-p
import { uploadOptions } from 'utils/file-uploads.utils';
import { ProxyService } from 'modules/proxy/proxy.service';
import { ShapefileGeoJSONResponseDTO } from './dto/shapefile.geojson.response.dto';
import { ApiConsumesShapefile } from 'decorators/shapefile.decorator';
import {
ApiConsumesShape,
ApiConsumesShapefile,
} from 'decorators/shapefile.decorator';
import { CostSurfaceFacade } from './cost-surface/cost-surface.facade';

@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
Expand All @@ -66,6 +71,7 @@ export class ScenariosController {
public readonly service: ScenariosService,
private readonly proxyService: ProxyService,
private readonly scenarioFeatures: ScenarioFeaturesService,
private readonly costSurface: CostSurfaceFacade,
) {}

@ApiOperation({
Expand Down Expand Up @@ -118,6 +124,24 @@ export class ScenariosController {
);
}

@ApiConsumesShape()
@ApiNoContentResponse()
@Post(`:id/cost-surface/shapefile`)
async processCostSurfaceShapefile(
@Param('id') scenarioId: string,
@Req() request: Request,
): Promise<void> {
// TODO #1 pre-validate scenarioId
/**
* Could be via interceptor
* (would require to not include @Res() and force-ignore http-proxy needs)
* or just ...BaseService
*/

this.costSurface.convert(scenarioId, request);
return;
}

// TODO add Validations
@ApiConsumesShapefile()
@Post(':id/planning-unit-shapefile')
Expand Down
10 changes: 9 additions & 1 deletion api/src/modules/scenarios/scenarios.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@ import { ProjectsModule } from 'modules/projects/projects.module';
import { ScenarioFeaturesModule } from '../scenarios-features';
import { ProxyService } from 'modules/proxy/proxy.service';
import { WdpaAreaCalculationService } from './wdpa-area-calculation.service';
import { CostSurfaceFacade } from './cost-surface/cost-surface.facade';
import { AnalysisModule } from '../analysis/analysis.module';

@Module({
imports: [
AnalysisModule,
ProtectedAreasModule,
forwardRef(() => ProjectsModule),
TypeOrmModule.forFeature([Project, Scenario]),
UsersModule,
ScenarioFeaturesModule,
],
providers: [ScenariosService, ProxyService, WdpaAreaCalculationService],
providers: [
ScenariosService,
ProxyService,
WdpaAreaCalculationService,
CostSurfaceFacade,
],
controllers: [ScenariosController],
exports: [ScenariosService],
})
Expand Down

0 comments on commit 8aa1df6

Please sign in to comment.