Skip to content

Commit

Permalink
Permits data integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignas-rgb committed May 22, 2024
1 parent e02dffa commit 4efd174
Show file tree
Hide file tree
Showing 21 changed files with 529 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TerminusModule } from '@nestjs/terminus';
import { CommonModule } from './common/common.module';
import { SentryInterceptor, SentryModule } from '@ntegral/nestjs-sentry';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { PermitModule } from './permit/permit.module';

@Module({
imports: [
Expand All @@ -24,6 +25,7 @@ import { APP_INTERCEPTOR } from '@nestjs/core';
AdminModule,
DumpModule,
ReportModule,
PermitModule,
MongooseModule.forRoot(process.env['DATABASE_URL']!),
AuthModule,
CloudinaryModule,
Expand Down
1 change: 1 addition & 0 deletions src/common/dto/report-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum ReportCategory {
trash = 'trash',
forest = 'forest',
beetle = 'beetle',
permits = 'permits',
}
14 changes: 14 additions & 0 deletions src/permit/dto/geometry-coordinates.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiProperty } from '@nestjs/swagger';

export class GeometryCoordinatesDto {
@ApiProperty({ format: 'double' })
latitude: number;

@ApiProperty({ format: 'double' })
longitude: number;

constructor(latitude: number, longitude: number) {
this.latitude = latitude;
this.longitude = longitude;
}
}
1 change: 1 addition & 0 deletions src/permit/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './permit.dto';
15 changes: 15 additions & 0 deletions src/permit/dto/permit-geometry.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';
import { GeometryCoordinatesDto } from './geometry-coordinates.dto';

export class PermitGeometryDto {
@ApiProperty()
type: string;

@ApiProperty({ type: [GeometryCoordinatesDto] })
coordinates: GeometryCoordinatesDto[];

constructor(type: string, coordinates: GeometryCoordinatesDto[]) {
this.type = type;
this.coordinates = coordinates;
}
}
67 changes: 67 additions & 0 deletions src/permit/dto/permit-properties.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ApiProperty } from '@nestjs/swagger';

export class PermitPropertiesDto {
@ApiProperty()
type: string;

@ApiProperty()
issuedFrom: string;

@ApiProperty()
issuedTo: string;

@ApiProperty()
cadastralNumber: string;

@ApiProperty({ nullable: true })
subdivision?: string;

@ApiProperty({ nullable: true })
forestryDistrict?: string;

@ApiProperty({ format: 'double', nullable: true })
block?: number;

@ApiProperty({ nullable: true })
plot?: string;

@ApiProperty({ format: 'double', nullable: true })
cuttableArea?: number;

@ApiProperty({ nullable: true })
dominantTree?: string;

@ApiProperty({ nullable: true })
cuttingType?: string;

@ApiProperty({ nullable: true })
reinstatementType?: string;

constructor(
type: string,
issuedFrom: string,
issuedTo: string,
cadastralNumber: string,
subdivision?: string,
forestryDistrict?: string,
block?: number,
plot?: string,
cuttableArea?: number,
dominantTree?: string,
cuttingType?: string,
reinstatementType?: string,
) {
this.type = type;
this.issuedFrom = issuedFrom;
this.issuedTo = issuedTo;
this.cadastralNumber = cadastralNumber;
this.subdivision = subdivision;
this.forestryDistrict = forestryDistrict;
this.block = block;
this.plot = plot;
this.cuttableArea = cuttableArea;
this.dominantTree = dominantTree;
this.cuttingType = cuttingType;
this.reinstatementType = reinstatementType;
}
}
19 changes: 19 additions & 0 deletions src/permit/dto/permit.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ApiProperty } from '@nestjs/swagger';
import { PermitPropertiesDto } from './permit-properties.dto';
import { PermitGeometryDto } from './permit-geometry.dto';

export class PermitDto {
@ApiProperty({ type: PermitPropertiesDto })
permitProperties: PermitPropertiesDto;

@ApiProperty({ type: PermitGeometryDto })
permitGeometry: PermitGeometryDto;

constructor(
permitProperties: PermitPropertiesDto,
permitGeometry: PermitGeometryDto,
) {
this.permitProperties = permitProperties;
this.permitGeometry = permitGeometry;
}
}
40 changes: 40 additions & 0 deletions src/permit/permit.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Body, Controller, Get, ParseArrayPipe, Post } from '@nestjs/common';
import { PermitService } from './permit.service';
import { ApiCreatedResponse, ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { PermitDto } from './dto';

@Controller('permits')
@ApiTags('permits')
export class PermitController {
constructor(private readonly permitService: PermitService) {}

@ApiCreatedResponse({
description: 'New Permits have been successfully created',
type: [PermitDto],
})
@Post('/multiple')
createMultiplePermits(
@Body(new ParseArrayPipe({ items: PermitDto }))
createPermits: PermitDto[],
): Promise<PermitDto[]> {
return this.permitService.createMultiplePermits(createPermits);
}

@ApiCreatedResponse({
description: 'New Permit has been successfully created',
type: PermitDto,
})
@Post()
createNewPermit(@Body() createPermit: PermitDto): Promise<PermitDto> {
return this.permitService.createPermit(createPermit);
}

@ApiOkResponse({
description: 'All permits have been successfully found',
type: [PermitDto],
})
@Get()
getAllPermits(): Promise<PermitDto[]> {
return this.permitService.getAllPermits();
}
}
15 changes: 15 additions & 0 deletions src/permit/permit.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Module } from '@nestjs/common';
import { PermitController } from './permit.controller';
import { PermitService } from './permit.service';
import { MongooseModule } from '@nestjs/mongoose';
import { Permit, PermitSchema } from '../repositories/permit/schemas';
import { PermitRepository } from '../repositories/permit/permit.repository';

@Module({
imports: [
MongooseModule.forFeature([{ name: Permit.name, schema: PermitSchema }]),
],
controllers: [PermitController],
providers: [PermitService, PermitRepository],
})
export class PermitModule {}
73 changes: 73 additions & 0 deletions src/permit/permit.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Injectable } from '@nestjs/common';
import { PermitDto } from './dto';
import { PermitRepository } from '../repositories/permit/permit.repository';
import {
GeometryCoordinates,
Permit,
PermitGeometry,
PermitProperties,
} from '../repositories/permit/schemas';
import { PermitPropertiesDto } from './dto/permit-properties.dto';
import { PermitGeometryDto } from './dto/permit-geometry.dto';
import { GeometryCoordinatesDto } from './dto/geometry-coordinates.dto';

@Injectable()
export class PermitService {
constructor(private readonly permitRepository: PermitRepository) {}

async getAllPermits(): Promise<PermitDto[]> {
const permits = await this.permitRepository.getAllPermits();
return permits.map(PermitService.docToPermit);
}

async createPermit(permit: PermitDto): Promise<PermitDto> {
const newPermit = await this.permitRepository.createPermit(permit);
return PermitService.docToPermit(newPermit);
}

async createMultiplePermits(permits: PermitDto[]): Promise<PermitDto[]> {
const convertedPermits = [];
for (let i = 0; i < permits.length; i++) {
const newPermit = await this.permitRepository.createPermit(permits[i]);
convertedPermits[i] = PermitService.docToPermit(newPermit);
}
return convertedPermits;
}

private static docToPermit(e: Permit): PermitDto {
return new PermitDto(
PermitService.docToProperties(e.permitProperties),
PermitService.docToGeometry(e.permitGeometry),
);
}

private static docToProperties(e: PermitProperties): PermitPropertiesDto {
return new PermitPropertiesDto(
e.type,
e.issuedFrom,
e.issuedTo,
e.cadastralNumber,
e.subdivision,
e.forestryDistrict,
e.block,
e.plot,
e.cuttableArea,
e.dominantTree,
e.cuttingType,
e.reinstatementType,
);
}

private static docToGeometry(e: PermitGeometry): PermitGeometryDto {
return new PermitGeometryDto(
e.type,
e.coordinates.map(PermitService.coordinatesToGeometry),
);
}

private static coordinatesToGeometry(
e: GeometryCoordinates,
): GeometryCoordinatesDto {
return new GeometryCoordinatesDto(e.latitude, e.longitude);
}
}
14 changes: 14 additions & 0 deletions src/report/dto/geometry-coordinates.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiProperty } from '@nestjs/swagger';

export class GeometryCoordinatesDto {
@ApiProperty({ format: 'double' })
latitude: number;

@ApiProperty({ format: 'double' })
longitude: number;

constructor(latitude: number, longitude: number) {
this.latitude = latitude;
this.longitude = longitude;
}
}
1 change: 1 addition & 0 deletions src/report/dto/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './create-report.dto';
export * from './public-report.dto';
export * from './status-records.dto';
export * from './permit.dto';
15 changes: 15 additions & 0 deletions src/report/dto/permit-geometry.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';
import { GeometryCoordinatesDto } from './geometry-coordinates.dto';

export class PermitGeometryDto {
@ApiProperty()
type: string;

@ApiProperty({ type: [GeometryCoordinatesDto] })
coordinates: GeometryCoordinatesDto[];

constructor(type: string, coordinates: GeometryCoordinatesDto[]) {
this.type = type;
this.coordinates = coordinates;
}
}
67 changes: 67 additions & 0 deletions src/report/dto/permit-properties.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ApiProperty } from '@nestjs/swagger';

export class PermitPropertiesDto {
@ApiProperty()
type: string;

@ApiProperty()
issuedFrom: string;

@ApiProperty()
issuedTo: string;

@ApiProperty()
cadastralNumber: string;

@ApiProperty({ nullable: true })
subdivision?: string;

@ApiProperty({ nullable: true })
forestryDistrict?: string;

@ApiProperty({ format: 'double', nullable: true })
block?: number;

@ApiProperty({ nullable: true })
plot?: string;

@ApiProperty({ format: 'double', nullable: true })
cuttableArea?: number;

@ApiProperty({ nullable: true })
dominantTree?: string;

@ApiProperty({ nullable: true })
cuttingType?: string;

@ApiProperty({ nullable: true })
reinstatementType?: string;

constructor(
type: string,
issuedFrom: string,
issuedTo: string,
cadastralNumber: string,
subdivision?: string,
forestryDistrict?: string,
block?: number,
plot?: string,
cuttableArea?: number,
dominantTree?: string,
cuttingType?: string,
reinstatementType?: string,
) {
this.type = type;
this.issuedFrom = issuedFrom;
this.issuedTo = issuedTo;
this.cadastralNumber = cadastralNumber;
this.subdivision = subdivision;
this.forestryDistrict = forestryDistrict;
this.block = block;
this.plot = plot;
this.cuttableArea = cuttableArea;
this.dominantTree = dominantTree;
this.cuttingType = cuttingType;
this.reinstatementType = reinstatementType;
}
}
19 changes: 19 additions & 0 deletions src/report/dto/permit.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ApiProperty } from '@nestjs/swagger';
import { PermitPropertiesDto } from './permit-properties.dto';
import { PermitGeometryDto } from './permit-geometry.dto';

export class PermitDto {
@ApiProperty({ type: PermitPropertiesDto })
permitProperties: PermitPropertiesDto;

@ApiProperty({ type: PermitGeometryDto })
permitGeometry: PermitGeometryDto;

constructor(
permitProperties: PermitPropertiesDto,
permitGeometry: PermitGeometryDto,
) {
this.permitProperties = permitProperties;
this.permitGeometry = permitGeometry;
}
}
Loading

0 comments on commit 4efd174

Please sign in to comment.