Skip to content

Commit

Permalink
refactor(protected-areas): move pa entity to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Sep 13, 2021
1 parent ed8e612 commit cabbc81
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 114 deletions.
5 changes: 5 additions & 0 deletions api/apps/api/src/modules/projects/project.api.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ export class Project extends TimeUserEntityMetadata {
description: "Display name of Country / Gid1 / Gid2 of project's area",
})
planningAreaName?: string;

@ApiPropertyOptional({
isArray: true,
})
customProtectedAreas?: string[];
}

export class JSONAPIProjectData {
Expand Down
5 changes: 5 additions & 0 deletions api/apps/api/src/modules/projects/projects-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
import { UsersProjectsApiEntity } from './control-level/users-projects.api.entity';
import { Roles } from '@marxan-api/modules/users/role.api.entity';
import { AppInfoDTO } from '@marxan-api/dto/info.dto';
import { DbConnections } from '@marxan-api/ormconfig.connections';
import { ProtectedArea } from '@marxan/protected-areas';

const projectFilterKeyNames = [
'name',
Expand Down Expand Up @@ -67,6 +69,8 @@ export class ProjectsCrudService extends AppBaseService<
private readonly planningAreasService: PlanningAreasService,
@InjectRepository(UsersProjectsApiEntity)
private readonly userProjects: Repository<UsersProjectsApiEntity>,
@InjectRepository(ProtectedArea, DbConnections.geoprocessingDB)
private readonly protectedAreas: Repository<ProtectedArea>,
) {
super(repository, 'project', 'projects', {
logging: { muteAll: AppConfig.get<boolean>('logging.muteAll', false) },
Expand All @@ -90,6 +94,7 @@ export class ProjectsCrudService extends AppBaseService<
'planningAreaId',
'planningAreaName',
'bbox',
'customProtectedAreas',
],
keyForAttribute: 'camelCase',
users: {
Expand Down
6 changes: 6 additions & 0 deletions api/apps/api/src/modules/projects/projects.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { UsersProjectsApiEntity } from './control-level/users-projects.api.entit
import { ProjectsListingController } from './projects-listing.controller';
import { ProjectDetailsController } from './project-details.controller';
import { ShapefilesModule } from '@marxan/shapefile-converter';
import { ProtectedArea } from '@marxan/protected-areas';
import { apiConnections } from '@marxan-api/ormconfig';

@Module({
imports: [
Expand All @@ -36,6 +38,10 @@ import { ShapefilesModule } from '@marxan/shapefile-converter';
ScenarioJobStatus,
UsersProjectsApiEntity,
]),
TypeOrmModule.forFeature(
[ProtectedArea],
apiConnections.geoprocessingDB.name,
),
UsersModule,
PlanningUnitsModule,
ProtectedAreasModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { ProtectedArea } from '../protected-area.geo.entity';
import { IUCNCategory } from '@marxan/iucn';
import { ProtectedArea } from '@marxan/protected-areas';

export class IUCNProtectedAreaCategoryDTO extends PickType(ProtectedArea, [
'iucnCategory',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,84 +1,5 @@
import { IUCNCategory } from '@marxan/iucn';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity('wdpa')
export class ProtectedArea {
@ApiProperty()
@PrimaryGeneratedColumn('uuid')
id!: string;

/**
* WDPA id.
*/
@ApiProperty()
@Column('double precision', { name: 'wdpaid' })
wdpaId?: number;

/**
* Full name of the protected area.
*/
@ApiPropertyOptional()
@Column('character varying', { name: 'full_name' })
fullName?: string;

/**
* IUCN category.
*
* Only applies to IUCN-defined protected areas.
*/
@ApiPropertyOptional()
@Column('character varying', { name: 'iucn_cat' })
iucnCategory?: IUCNCategory;

/**
* Total length of the protected area's shape.
*/
@ApiPropertyOptional()
@Column('double precision', { name: 'shape_leng' })
shapeLength?: number;

/**
* Total area of the protected area's shape.
*/
@ApiPropertyOptional()
@Column('double precision', { name: 'shape_area' })
shapeArea?: number;

/**
* Country where the protected area is located.
*
* This references the admin_regions.gid_0 column.
*/
@ApiPropertyOptional()
@Column('character varying', { name: 'iso3' })
countryId?: string;

/**
* Protection status of the area.
*
* For example: "Inscribed", or "Designated".
*/
@ApiPropertyOptional()
@Column('text')
status?: string;

/**
* Protection designation.
*/
@ApiPropertyOptional()
@Column('text', { name: 'desig' })
designation?: string;

/**
* Geometry for the protected area.
*
* GeoJSON representation when retrieved from db.
*/
@ApiPropertyOptional()
@Column('geometry', { name: 'the_geom' })
theGeom?: Record<string, unknown>;
}
import { ApiProperty } from '@nestjs/swagger';
import { ProtectedArea } from '@marxan/protected-areas';

export class JSONAPIProtectedAreaData {
@ApiProperty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { ProtectedAreasController } from './protected-areas.controller';
import { ProtectedArea } from './protected-area.geo.entity';
import { ProtectedArea } from '@marxan/protected-areas';
import { ProtectedAreasService } from './protected-areas.service';
import { apiConnections } from '../../ormconfig';
import { ProxyService } from '@marxan-api/modules/proxy/proxy.service';
import { DbConnections } from '@marxan-api/ormconfig.connections';

@Module({
imports: [
TypeOrmModule.forFeature(
[ProtectedArea],
apiConnections.geoprocessingDB.name,
),
TypeOrmModule.forFeature([ProtectedArea], DbConnections.geoprocessingDB),
],
providers: [ProtectedAreasService, ProxyService],
controllers: [ProtectedAreasController],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import { AppInfoDTO } from '@marxan-api/dto/info.dto';
import { Repository, SelectQueryBuilder } from 'typeorm';
import { CreateProtectedAreaDTO } from './dto/create.protected-area.dto';
import { UpdateProtectedAreaDTO } from './dto/update.protected-area.dto';
import { ProtectedArea } from './protected-area.geo.entity';
import { ProtectedArea } from '@marxan/protected-areas';
import * as JSONAPISerializer from 'jsonapi-serializer';

import {
AppBaseService,
JSONAPISerializerConfig,
} from '@marxan-api/utils/app-base.service';
import { isNil } from 'lodash';
import { FetchSpecification } from 'nestjs-base-service';
import {
IUCNProtectedAreaCategoryDTO,
Expand All @@ -24,6 +23,7 @@ import { IsBoolean, IsOptional, IsUUID } from 'class-validator';
import { apiConnections } from '../../ormconfig';
import { AppConfig } from '@marxan-api/utils/config.utils';
import { IUCNCategory } from '@marxan/iucn';
import { isDefined } from '@marxan/utils';

const protectedAreaFilterKeyNames = [
'fullName',
Expand Down Expand Up @@ -164,16 +164,13 @@ export class ProtectedAreasService extends AppBaseService<
/**
* List IUCN categories of protected areas.
*/
async listProtectedAreaCategories(): Promise<Array<string | undefined>> {
const results = await this.repository
async listProtectedAreaCategories(): Promise<Array<string>> {
return await this.repository
.createQueryBuilder(this.alias)
.select(`${this.alias}.iucnCategory`, 'iucnCategory')
.distinct(true)
.getRawMany<ProtectedArea>()
.then((results) =>
results.map((i) => i.iucnCategory).filter((i) => !isNil(i)),
);
return results;
.then((results) => results.map((i) => i.iucnCategory).filter(isDefined));
}

/**
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 @@ -66,6 +66,8 @@
"@marxan/iucn/(.*)": "<rootDir>/../../../libs/iucn/src/$1",
"@marxan/iucn": "<rootDir>/../../../libs/iucn/src",
"@marxan/shapefile-converter/(.*)": "<rootDir>/../../../libs/shapefile-converter/src/$1",
"@marxan/shapefile-converter": "<rootDir>/../../../libs/shapefile-converter/src"
"@marxan/shapefile-converter": "<rootDir>/../../../libs/shapefile-converter/src",
"@marxan/protected-areas/(.*)": "<rootDir>/../../../libs/protected-areas/src/$1",
"@marxan/protected-areas": "<rootDir>/../../../libs/protected-areas/src"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CqrsModule } from '@nestjs/cqrs';
import { ProtectedAreasController } from './protected-areas.controller';
import { ProtectedAreasService } from './protected-areas.service';
import { TileModule } from '@marxan-geoprocessing/modules/tile/tile.module';
import { ProtectedArea } from '@marxan-geoprocessing/modules/protected-areas/protected-areas.geo.entity';
import { ProtectedArea } from '@marxan/protected-areas/protected-areas.geo.entity';
import { ProtectedAreaWorkerModule } from './worker/protected-area-worker.module';

@Module({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { IsNumber, IsOptional, IsString } from 'class-validator';

import { ProtectedArea } from '@marxan-geoprocessing/modules/protected-areas/protected-areas.geo.entity';
import { ProtectedArea } from '@marxan/protected-areas/protected-areas.geo.entity';
import { BBox } from 'geojson';
import { Transform } from 'class-transformer';
import { nominatim2bbox } from '@marxan-geoprocessing/utils/bbox.utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ShapefileService } from '@marxan/shapefile-converter';

import { WorkerProcessor } from '../../worker';
import { ProtectedAreasJobInput } from './worker-input';
import { ProtectedArea } from '../protected-areas.geo.entity';
import { ProtectedArea } from '@marxan/protected-areas/protected-areas.geo.entity';

@Injectable()
export class ProtectedAreaProcessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { CqrsModule } from '@nestjs/cqrs';
import { WorkerModule } from '../../worker';
import { ShapefilesModule } from '@marxan/shapefile-converter';
import { ProtectedArea } from '../protected-areas.geo.entity';
import { ProtectedArea } from '@marxan/protected-areas/protected-areas.geo.entity';

import { ProtectedAreaProcessor } from './protected-area-processor';
import { ProtectedAreaWorkerService } from './protected-area-worker.service';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { v4 } from 'uuid';
import { Job } from 'bullmq';

import { ProtectedAreasJobInput } from '../../../../src/modules/protected-areas/worker/worker-input';
import { ProtectedArea } from '../../../../src/modules/protected-areas/protected-areas.geo.entity';
import { ProtectedArea } from '@marxan/protected-areas/protected-areas.geo.entity';

export const createWorld = (
app: INestApplication,
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 @@ -39,6 +39,8 @@
"@marxan/iucn/(.*)": "<rootDir>/../../libs/iucn/src/$1",
"@marxan/iucn": "<rootDir>/../../libs/iucn/src",
"@marxan/shapefile-converter/(.*)": "<rootDir>/../../libs/shapefile-converter/src/$1",
"@marxan/shapefile-converter": "<rootDir>/../../libs/shapefile-converter/src"
"@marxan/shapefile-converter": "<rootDir>/../../libs/shapefile-converter/src",
"@marxan/protected-areas/(.*)": "<rootDir>/../../libs/protected-areas/src/$1",
"@marxan/protected-areas": "<rootDir>/../../libs/protected-areas/src"
}
}
1 change: 1 addition & 0 deletions api/libs/protected-areas/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ProtectedArea } from './protected-areas.geo.entity';
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/**
* @todo We are replicating the same code that we have in the api. If we update something here we should also replicate it in the api side.
*/
import { ApiProperty } from '@nestjs/swagger';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Check, Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
import { MultiPolygon } from 'geojson';
import { defaultSrid } from '@marxan/utils/geo';
import { IUCNCategory } from '@marxan/iucn';
import { TimeUserEntityMetadata } from '../../types/time-user-entity-metadata';
import { TimeUserEntityMetadata } from '@marxan/utils';

@Entity('wdpa')
export class ProtectedArea extends TimeUserEntityMetadata {
Expand All @@ -24,7 +21,7 @@ export class ProtectedArea extends TimeUserEntityMetadata {
/**
* geometry column.
*/
@ApiProperty()
@ApiPropertyOptional()
@Index(`wdpa_geom_idx`, {
spatial: true,
})
Expand All @@ -38,41 +35,49 @@ export class ProtectedArea extends TimeUserEntityMetadata {
})
theGeom?: MultiPolygon | null;

@ApiPropertyOptional()
@Column({
type: 'float8',
name: 'wdpaid',
nullable: true,
})
wdpaId?: number | null;

@ApiPropertyOptional()
@Column({
name: 'full_name',
type: 'varchar',
nullable: true,
})
fullName?: string | null;

@ApiPropertyOptional()
@Column({
type: 'varchar',
name: 'iucn_cat',
nullable: true,
})
iucnCategory?: IUCNCategory | null;

@ApiPropertyOptional()
@Column({
type: 'float8',
name: 'shape_leng',
nullable: true,
})
shapeLength?: number | null;

@ApiPropertyOptional({
description: `Total area of the protected area's shape.`,
})
@Column({
type: 'float8',
name: 'shape_area',
nullable: true,
})
shapeArea?: number | null;

@ApiPropertyOptional()
@Column({
type: 'text',
name: 'desig',
Expand All @@ -85,6 +90,7 @@ export class ProtectedArea extends TimeUserEntityMetadata {
*
* This references the admin_regions.gid_0 column.
*/
@ApiPropertyOptional()
@Column({
type: 'character varying',
name: 'iso3',
Expand Down
9 changes: 9 additions & 0 deletions api/libs/protected-areas/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/protected-areas"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
1 change: 1 addition & 0 deletions api/libs/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { isDefined } from './is-defined';
export { assertDefined } from './assert-defined';
export { FieldsOf } from './fields-of.type';
export { TimeUserEntityMetadata } from './time-user-entity-metadata';
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';

/**
* duplicated in API - except the relations
*/
export abstract class TimeUserEntityMetadata {
@CreateDateColumn({
name: 'created_at',
Expand Down
Loading

0 comments on commit cabbc81

Please sign in to comment.