Skip to content

Commit

Permalink
feat(project): upload user feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Aug 26, 2021
1 parent 83a512d commit 0d35639
Show file tree
Hide file tree
Showing 29 changed files with 145 additions and 22 deletions.
2 changes: 0 additions & 2 deletions api/apps/api/src/modules/geo-features/geo-features.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ export class GeoFeaturesService extends AppBaseService<
'An error occurred creating features for shapefile (changes have been rolled back)',
String(err),
);

console.log(err);
} finally {
// you need to release a queryRunner which was manually instantiated
await apiQueryRunner.release();
Expand Down
4 changes: 2 additions & 2 deletions api/apps/api/src/modules/projects/projects.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ import { JobStatusSerializer } from './dto/job-status.serializer';
import { PlanningAreaResponseDto } from './dto/planning-area-response.dto';
import { isLeft } from 'fp-ts/Either';
import { ShapefileUploadResponse } from './dto/project-upload-shapefile.dto';
import { ShapefileService } from '@marxan-geoprocessing/modules/shapefiles/shapefiles.service';
import { UploadShapefileDTO } from './dto/upload-shapefile.dto';
import { GeoFeaturesService } from '../geo-features/geo-features.service';
import { AppConfig } from '@marxan-api/utils/config.utils';
import { ShapefileService } from '@marxan/shapefile-converter';

@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
Expand Down Expand Up @@ -216,7 +216,7 @@ export class ProjectsController {
@ApiOkResponse({ type: ShapefileUploadResponse })
@Post(`:id/features/shapefile`)
@UseInterceptors(
FileInterceptor('shapefile', {
FileInterceptor('file', {
...uploadOptions,
limits: {
fileSize: (() =>
Expand Down
2 changes: 1 addition & 1 deletion api/apps/api/src/modules/projects/projects.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { CountriesModule } from '@marxan-api/modules/countries/countries.module'
import { PlanningUnitsModule } from '@marxan-api/modules/planning-units/planning-units.module';
import { GeoFeaturesModule } from '@marxan-api/modules/geo-features/geo-features.module';
import { ApiEventsModule } from '@marxan-api/modules/api-events/api-events.module';
import { ShapefilesModule } from '@marxan-geoprocessing/modules/shapefiles/shapefiles.module';
import { ProtectedAreasModule } from './protected-areas/protected-areas.module';
import { ProjectsService } from './projects.service';
import { GeoFeatureSerializer } from './dto/geo-feature.serializer';
Expand All @@ -23,6 +22,7 @@ import { PlanningAreasModule } from './planning-areas';
import { UsersProjectsApiEntity } from './control-level/users-projects.api.entity';
import { ProjectsListingController } from './projects-listing.controller';
import { ProjectDetailsController } from './project-details.controller';
import { ShapefilesModule } from '@marxan/shapefile-converter';

@Module({
imports: [
Expand Down
4 changes: 3 additions & 1 deletion api/apps/api/test/jest-e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"@marxan/geofeature-calculations/(.*)": "<rootDir>/../../../libs/geofeature-calculations/src/$1",
"@marxan/geofeature-calculations": "<rootDir>/../../../libs/geofeature-calculations/src",
"@marxan/iucn/(.*)": "<rootDir>/../../../libs/iucn/src/$1",
"@marxan/iucn": "<rootDir>/../../../libs/iucn/src"
"@marxan/iucn": "<rootDir>/../../../libs/iucn/src",
"@marxan/shapefile-converter/(.*)": "<rootDir>/../../../libs/shapefile-converter/src/$1",
"@marxan/shapefile-converter": "<rootDir>/../../../libs/shapefile-converter/src"
}
}
17 changes: 17 additions & 0 deletions api/apps/api/test/upload-feature/upload-feature.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { FixtureType } from '@marxan/utils/tests/fixture-type';
import { getFixtures } from './upload-feature.fixtures';

let fixtures: FixtureType<typeof getFixtures>;

beforeEach(async () => {
fixtures = await getFixtures();
});

afterEach(async () => {
await fixtures?.cleanup();
});

test(`custom feature upload`, async () => {
const result = await fixtures.WhenUploadingCustomFeature();
await fixtures.ThenGeoFeaturesAreCreated(result);
});
75 changes: 75 additions & 0 deletions api/apps/api/test/upload-feature/upload-feature.fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { bootstrapApplication } from '../utils/api-application';
import { GivenUserIsLoggedIn } from '../steps/given-user-is-logged-in';
import { GivenProjectExists } from '../steps/given-project';

import { GeoFeature } from '@marxan-api/modules/geo-features/geo-feature.api.entity';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import * as request from 'supertest';

export const getFixtures = async () => {
const app = await bootstrapApplication();
const token = await GivenUserIsLoggedIn(app);
const { projectId, cleanup } = await GivenProjectExists(
app,
token,
{
name: `Project ${Date.now()}`,
countryCode: undefined!,
},
{
name: `Organization ${Date.now()}`,
},
);
const customFeatureName = `User custom feature ${Date.now()}`;
const customFeatureTag = `bioregional`;
const customFeatureDesc = `User custom feature desc`;

const geoFeaturesApiRepo: Repository<GeoFeature> = app.get(
getRepositoryToken(GeoFeature),
);

return {
cleanup: async () => {
await geoFeaturesApiRepo.delete({
projectId,
});
await cleanup();
await app.close();
},
WhenUploadingCustomFeature: async () =>
request(app.getHttpServer())
.post(`/api/v1/projects/${projectId}/features/shapefile`)
.set('Authorization', `Bearer ${token}`)
.attach(`file`, __dirname + `/wetlands.zip`)
.field({
name: customFeatureName,
type: customFeatureTag,
description: customFeatureDesc,
}),
ThenGeoFeaturesAreCreated: async (result: request.Response) => {
expect(result.body).toEqual({
success: true,
});
expect(
await geoFeaturesApiRepo.find({
where: {
projectId,
},
}),
).toEqual([
{
id: expect.any(String),
featureClassName: customFeatureName,
description: customFeatureDesc,
alias: null,
propertyName: null,
intersection: null,
tag: customFeatureTag,
creationStatus: `done`,
projectId,
},
]);
},
};
};
Binary file added api/apps/api/test/upload-feature/wetlands.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ApiOkResponse,
ApiOperation,
} from '@nestjs/swagger';
import { ShapefileGeoJSONResponseDTO } from '../modules/shapefiles/dto/shapefile.geojson.response.dto';
import { ShapefileGeoJSONResponseDTO } from '../types/shapefile.geojson.response.dto';

export function ApiConsumesShapefile() {
return applyDecorators(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { PlanningAreaRepositoryModule } from '@marxan/planning-area-repository';
import { ShapefilesModule } from '@marxan-geoprocessing/modules/shapefiles/shapefiles.module';
import { ShapefilesModule } from '@marxan/shapefile-converter';
import { geoprocessingConnections } from '@marxan-geoprocessing/ormconfig';
import { PlanningAreaService } from './planning-area.service';
import { PlanningAreaController } from './planning-area.controller';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { GeoJSON } from 'geojson';
import * as uuid from 'uuid';
import { CustomPlanningAreaRepository } from '@marxan/planning-area-repository';
import { ShapefileService } from '@marxan-geoprocessing/modules/shapefiles/shapefiles.service';
import { ShapefileService } from '@marxan/shapefile-converter';
import { GarbageCollectorConfig } from '@marxan-geoprocessing/modules/planning-area/garbage-collector-config';
import { SaveGeoJsonResult } from '@marxan/planning-area-repository/custom-planning-area.repository';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import {
tileSpecification,
} from './planning-units.service';
import { apiGlobalPrefixes } from '@marxan-geoprocessing/api.config';
import { ShapefileService } from '../shapefiles/shapefiles.service';
import { ShapefileService } from '@marxan/shapefile-converter';
import { ApiConsumesShapefile } from '../../decoratos/shapefile.decorator';
import { ShapefileGeoJSONResponseDTO } from '../shapefiles/dto/shapefile.geojson.response.dto';
import { ShapefileGeoJSONResponseDTO } from '../../types/shapefile.geojson.response.dto';

import {
ApiOperation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Logger, Module } from '@nestjs/common';
import { TileModule } from '@marxan-geoprocessing/modules/tile/tile.module';
import { PlanningUnitsProcessor } from './planning-units.worker';
import { PlanningUnitsController } from './planning-units.controller';
import { ShapefileService } from '../shapefiles/shapefiles.service';
import { ShapefileService, FileService } from '@marxan/shapefile-converter';
import { PlanningUnitsService } from './planning-units.service';
import { FileService } from '../files/files.service';
import { WorkerModule } from '../worker';

@Module({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Job } from 'bullmq';
import { FakeGeometryExtractor } from './__mocks__/geometry-extractor';
import { createJob } from './__mocks__/job';

import { ShapefileService } from '../../shapefiles/shapefiles.service';
import { ShapefileService } from '@marxan/shapefile-converter';
import { ProtectedAreaProcessor } from './protected-area-processor';
import { GeometryExtractor } from './geometry-extractor';
import { ProtectedAreasJobInput } from './worker-input';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Job } from 'bullmq';

import { WorkerProcessor } from '../../worker';
import { ProtectedAreasJobInput } from './worker-input';
import { ShapefileService } from '../../shapefiles/shapefiles.service';
import { ShapefileService } from '@marxan/shapefile-converter';
import { ProtectedArea } from '../protected-areas.geo.entity';
import { GeoJSON } from 'geojson';
import { GeometryExtractor } from './geometry-extractor';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CqrsModule } from '@nestjs/cqrs';
import { WorkerModule } from '../../worker';
import { ShapefilesModule } from '../../shapefiles/shapefiles.module';
import { ShapefilesModule } from '@marxan/shapefile-converter';
import { ProtectedArea } from '../protected-areas.geo.entity';

import { ProtectedAreaProcessor } from './protected-area-processor';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { GeoJSON } from 'geojson';
import { ShapefileService } from '../../shapefiles/shapefiles.service';
import { ShapefileService } from '@marxan/shapefile-converter';

import { CostSurfaceJobInput } from '../cost-surface-job-input';
import { ShapefileConverterPort } from '../ports/shapefile-converter/shapefile-converter.port';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { ScenariosPlanningUnitGeoEntity } from '@marxan/scenarios-planning-unit';
import { CqrsModule } from '@nestjs/cqrs';
import { WorkerModule } from '@marxan-geoprocessing/modules/worker';
import { ShapefilesModule } from '@marxan-geoprocessing/modules/shapefiles/shapefiles.module';
import { ShapefilesModule } from '@marxan/shapefile-converter';

import { SurfaceCostProcessor } from './application/surface-cost-processor';
import { SurfaceCostWorker } from './application/surface-cost-worker';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { ShapefileService } from '@marxan-geoprocessing/modules/shapefiles/shapefiles.service';
import { ShapefileService } from '@marxan/shapefile-converter';

@Injectable()
export class FakeShapefileService
Expand Down
4 changes: 3 additions & 1 deletion api/apps/geoprocessing/test/jest-e2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"@marxan/geofeature-calculations/(.*)": "<rootDir>/../../libs/geofeature-calculations/src/$1",
"@marxan/geofeature-calculations": "<rootDir>/../../libs/geofeature-calculations/src",
"@marxan/iucn/(.*)": "<rootDir>/../../libs/iucn/src/$1",
"@marxan/iucn": "<rootDir>/../../libs/iucn/src"
"@marxan/iucn": "<rootDir>/../../libs/iucn/src",
"@marxan/shapefile-converter/(.*)": "<rootDir>/../../libs/shapefile-converter/src/$1",
"@marxan/shapefile-converter": "<rootDir>/../../libs/shapefile-converter/src"
}
}
4 changes: 4 additions & 0 deletions api/libs/shapefile-converter/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { FilesModule } from './files/files.module';
export { FileService } from './files/files.service';
export { ShapefilesModule } from './shapefiles/shapefiles.module';
export { ShapefileService } from './shapefiles/shapefiles.service';
9 changes: 9 additions & 0 deletions api/libs/shapefile-converter/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "../../dist/libs/shapefile-converter"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
9 changes: 9 additions & 0 deletions api/nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@
"compilerOptions": {
"tsConfigPath": "libs/iucn/tsconfig.lib.json"
}
},
"shapefile-converter": {
"type": "library",
"root": "libs/shapefile-converter",
"entryFile": "index",
"sourceRoot": "libs/shapefile-converter/src",
"compilerOptions": {
"tsConfigPath": "libs/shapefile-converter/tsconfig.lib.json"
}
}
}
}
6 changes: 4 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@
"@marxan/geofeature-calculations/(.*)": "<rootDir>/libs/geofeature-calculations/src/$1",
"@marxan/geofeature-calculations": "<rootDir>/libs/geofeature-calculations/src",
"@marxan/iucn/(.*)": "<rootDir>/libs/iucn/src/$1",
"@marxan/iucn": "<rootDir>/libs/iucn/src"
"@marxan/iucn": "<rootDir>/libs/iucn/src",
"@marxan/shapefile-converter/(.*)": "<rootDir>/libs/shapefile-converter/src/$1",
"@marxan/shapefile-converter": "<rootDir>/libs/shapefile-converter/src"
}
}
}
}
6 changes: 6 additions & 0 deletions api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@
],
"@marxan/iucn/*": [
"libs/iucn/src/*"
],
"@marxan/shapefile-converter": [
"libs/shapefile-converter/src"
],
"@marxan/shapefile-converter/*": [
"libs/shapefile-converter/src/*"
]
}
},
Expand Down

0 comments on commit 0d35639

Please sign in to comment.